go-cnj-helper/cnjglobals/cnjglobals.go

73 lines
2.4 KiB
Go

package cnjglobals
import (
"regexp"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
)
const (
RequestHeaderXAuthorization = "X-Authorization"
LenUserId = 36
lenUserId = "36" // Same value as the one above
LenXAuthorizationHeader = 36 // used for api calls
lenSessionEncryptionKey = "32"
LenRoomId = 36
lenRoomId = "36" // Same value as the one above
minUsername = "2"
maxUsername = "24"
minAccountName = "4"
MinAccountName = 4 // Same value as the one above
maxAccountName = "24"
MaxAccountName = 24 // Same value as the one above
minEmail = "3"
maxEmail = "48"
minPassword = "6"
MinPassword = 6 // Same value as the one above
maxPassword = "64"
MaxPassword = 64 // Same value as the one above
minFcmToken = "116"
maxFcmToken = "164"
maxAccountStatus = "4" // Indicates the maximum compatible account status. Look for reference in the API
maxDescription = "128"
maxRoomState = "2" // Look for reference in the API
maxRoomUserRole = "2" // Look for reference in the API
accountNameRegex = "^[a-zA-Z0-9_.]+$"
)
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=" + lenUserId,
"SessionEncryptionKey": "required,len=" + lenSessionEncryptionKey,
"Token": "required,min=" + minFcmToken + ",max=" + maxFcmToken, // fcm token
"AccountStatus": "number,max=" + maxAccountStatus,
"Description": "max=" + maxDescription,
"RoomState": "number,max=" + maxRoomState,
"Role": "number,max=" + maxRoomUserRole,
"RoomId": "required,len=" + lenRoomId,
}
)
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]
}