64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package cnjglobals
|
|
|
|
import (
|
|
"log"
|
|
"regexp"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
const (
|
|
RequestHeaderXAuthorization = "X-Authorization"
|
|
|
|
LenUserId = 36
|
|
LenUserIdStr = "36" // same as LenUserId but as string
|
|
LenXAuthorizationHeader = 36 // used for api calls
|
|
LenSessionEncryptionKey = "32"
|
|
|
|
MinUsername = "2"
|
|
MaxUsername = "24"
|
|
MinAccountName = "4"
|
|
MaxAccountName = "24"
|
|
MinEmail = "3"
|
|
MaxEmail = "48"
|
|
MinPassword = "6"
|
|
MaxPassword = "64"
|
|
MinFcmToken = "116"
|
|
MaxFcmToken = "164"
|
|
|
|
AccountNameRegex = "^[a-zA-Z0-9_.]+$"
|
|
)
|
|
|
|
func init() {
|
|
log.Println("init test")
|
|
}
|
|
|
|
var (
|
|
GeneralRules = map[string]string{
|
|
"Username": "required,min=" + MinUsername + ",max=" + MaxUsername,
|
|
"AccountName": "required,min=" + MinAccountName + ",max=" + MaxAccountName + ",regexp=" + AccountNameRegex,
|
|
"Email": "required,email,min=" + MinEmail + ",max=" + MaxEmail,
|
|
"Password": "required", // password length is checked later because it is sent in base64 format
|
|
"UserId": "required,len=" + LenUserIdStr,
|
|
"TargetUserId": "required,len=" + LenUserIdStr,
|
|
"SessionEncryptionKey": "required,len=" + LenSessionEncryptionKey,
|
|
"Token": "required,min=" + MinFcmToken + ",max=" + MaxFcmToken, // fcm token
|
|
}
|
|
)
|
|
|
|
func RegexTag(fl validator.FieldLevel) bool {
|
|
field := fl.Field().String()
|
|
if field == "" {
|
|
return true
|
|
}
|
|
regexString := fl.Param()
|
|
regex := regexp.MustCompile(regexString)
|
|
match := regex.MatchString(field)
|
|
return match
|
|
}
|
|
|
|
func GetAuhorizationToken(c *fiber.Ctx) string {
|
|
return c.GetReqHeaders()[RequestHeaderXAuthorization]
|
|
}
|