2021-07-12 18:22:16 +00:00
|
|
|
package utils
|
|
|
|
|
2021-08-04 06:48:57 +00:00
|
|
|
import (
|
|
|
|
"net/mail"
|
2021-12-21 13:16:54 +00:00
|
|
|
"regexp"
|
2021-08-04 06:48:57 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-08-04 06:48:57 +00:00
|
|
|
)
|
2021-07-12 18:22:16 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// IsValidEmail validates email
|
2021-07-12 18:22:16 +00:00
|
|
|
func IsValidEmail(email string) bool {
|
|
|
|
_, err := mail.ParseAddress(email)
|
|
|
|
return err == nil
|
|
|
|
}
|
2021-08-04 06:48:57 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// IsValidOrigin validates origin based on ALLOWED_ORIGINS
|
2021-12-21 13:16:54 +00:00
|
|
|
func IsValidOrigin(url string) bool {
|
2022-02-28 02:25:01 +00:00
|
|
|
allowedOrigins := envstore.EnvStoreObj.GetSliceStoreEnvVariable(constants.EnvKeyAllowedOrigins)
|
2022-01-20 11:22:37 +00:00
|
|
|
if len(allowedOrigins) == 1 && allowedOrigins[0] == "*" {
|
2021-08-04 06:48:57 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
hasValidURL := false
|
2021-12-21 13:16:54 +00:00
|
|
|
hostName, port := GetHostParts(url)
|
|
|
|
currentOrigin := hostName + ":" + port
|
2021-08-04 06:48:57 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
for _, origin := range allowedOrigins {
|
2022-01-20 11:22:37 +00:00
|
|
|
replacedString := origin
|
2021-12-21 13:16:54 +00:00
|
|
|
// if has regex whitelisted domains
|
2022-01-20 11:22:37 +00:00
|
|
|
if strings.Contains(origin, "*") {
|
|
|
|
replacedString = strings.Replace(origin, ".", "\\.", -1)
|
2021-12-21 13:16:54 +00:00
|
|
|
replacedString = strings.Replace(replacedString, "*", ".*", -1)
|
|
|
|
|
|
|
|
if strings.HasPrefix(replacedString, ".*") {
|
|
|
|
replacedString += "\\b"
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(replacedString, ".*") {
|
|
|
|
replacedString = "\\b" + replacedString
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matched, _ := regexp.MatchString(replacedString, currentOrigin); matched {
|
2021-08-04 06:48:57 +00:00
|
|
|
hasValidURL = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hasValidURL
|
|
|
|
}
|
2021-09-20 05:06:26 +00:00
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// IsValidRoles validates roles
|
2021-10-13 16:41:41 +00:00
|
|
|
func IsValidRoles(userRoles []string, roles []string) bool {
|
2021-09-20 05:06:26 +00:00
|
|
|
valid := true
|
2021-10-13 16:41:41 +00:00
|
|
|
for _, role := range roles {
|
2021-10-19 07:27:59 +00:00
|
|
|
if !StringSliceContains(userRoles, role) {
|
2021-09-20 05:06:26 +00:00
|
|
|
valid = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return valid
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// IsValidVerificationIdentifier validates verification identifier that is used to identify
|
|
|
|
// the type of verification request
|
2021-12-23 05:01:52 +00:00
|
|
|
func IsValidVerificationIdentifier(identifier string) bool {
|
2022-01-17 06:02:13 +00:00
|
|
|
if identifier != constants.VerificationTypeBasicAuthSignup && identifier != constants.VerificationTypeForgotPassword && identifier != constants.VerificationTypeUpdateEmail {
|
2021-12-23 05:01:52 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
// IsStringArrayEqual validates if string array are equal.
|
|
|
|
// This does check if the order is same
|
2021-09-20 05:06:26 +00:00
|
|
|
func IsStringArrayEqual(a, b []string) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, v := range a {
|
|
|
|
if v != b[i] {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|