Files
relay-server/helpers/filter.go
2025-01-25 15:32:52 +01:00

62 lines
1.0 KiB
Go

package helpers
import (
"regexp"
)
const MIN_USERNAME_LENGTH = 4
const MAX_USERNAME_LENGTH = 20
const MIN_PASSWORD_LENGTH = 8
const MAX_PASSWORD_LENGTH = 128
const PASSWORD_REGEX = `^[A-Za-z0-9!@#$%^&*(),.?":{}|<>]+$`
const USERNAME_REGEX = `^[a-zA-Z0-9_]+$`
func IsValidUsername(username interface{}) bool {
// Checks if username is type of string
strUsername, ok := username.(string)
if !ok {
return false
}
match, _ := regexp.MatchString(USERNAME_REGEX, strUsername)
if !match {
return false
}
// Checks if username length is valid
if len(strUsername) < MIN_USERNAME_LENGTH {
return false
}
if len(strUsername) > MAX_USERNAME_LENGTH {
return false
}
return true
}
func IsValidPassword(password interface{}) bool {
strPassword, ok := password.(string)
if !ok {
return false
}
if len(strPassword) < MIN_PASSWORD_LENGTH {
return false
}
if len(strPassword) > MAX_PASSWORD_LENGTH {
return false
}
match, _ := regexp.MatchString(PASSWORD_REGEX, strPassword)
if !match {
return false
}
return true
}