Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Argon2 validator #1193

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ validate := validator.New(validator.WithRequiredStructEnabled())
| sha256 | SHA256 hash |
| sha384 | SHA384 hash |
| sha512 | SHA512 hash |
| argon2 | Argon2 hash |
| ripemd128 | RIPEMD-128 hash |
| ripemd128 | RIPEMD-160 hash |
| tiger128 | TIGER128 hash |
Expand Down
21 changes: 21 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ var (
"mongodb": isMongoDB,
"cron": isCron,
"spicedb": isSpiceDB,
"argon2": isArgon2,
}
)

Expand Down Expand Up @@ -2953,3 +2954,23 @@ func isCron(fl FieldLevel) bool {
cronString := fl.Field().String()
return cronRegex.MatchString(cronString)
}

// isArgon2 is the validation function for validating if the field's value is a valid Argon2.
func isArgon2(fl FieldLevel) bool {
val := fl.Field().String()
res := argon2Regex.MatchString(val)
if !res {
return false
}

sp := strings.Split(val, "$")
if strings.Count(sp[4], "+") > 1 || strings.Count(sp[4], "/") > 1 {
return false
}

if strings.Count(sp[5], "+") > 1 || strings.Count(sp[5], "/") > 1 {
return false
}

return true
}
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const (
spicedbIDRegexString = `^(([a-zA-Z0-9/_|\-=+]{1,})|\*)$`
spicedbPermissionRegexString = "^([a-z][a-z0-9_]{1,62}[a-z0-9])?$"
spicedbTypeRegexString = "^([a-z][a-z0-9_]{1,61}[a-z0-9]/)?[a-z][a-z0-9_]{1,62}[a-z0-9]$"
argon2RegexString = "^\\$argon2(?:i|d|id)\\$v=(?:16|19)\\$m=(?:[\\d]+),t=(?:[\\d]+),p=(?:[\\d]+)\\$(?:[A-Za-z0-9+/]+)\\$(?:[A-Za-z0-9+/]+)$"
)

var (
Expand Down Expand Up @@ -142,4 +143,5 @@ var (
spicedbIDRegex = regexp.MustCompile(spicedbIDRegexString)
spicedbPermissionRegex = regexp.MustCompile(spicedbPermissionRegexString)
spicedbTypeRegex = regexp.MustCompile(spicedbTypeRegexString)
argon2Regex = regexp.MustCompile(argon2RegexString)
)
37 changes: 37 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13692,3 +13692,40 @@ func TestOmitNilAndRequired(t *testing.T) {
AssertError(t, err2, "OmitNil.Inner.Str", "OmitNil.Inner.Str", "Str", "Str", "required")
})
}

func TestArgon2Validation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"", false},
{"f16e171ea258bb62c7aa202ff77f9f10", false},
{"argon2id$v=19$m=16,t=2,p=1$YXNnYXNkZmdzZGZnc2Q$aMawMnEl0RKs+CJHr8Vd0Q", false},
{"$argon2$v=19$m=16,t=2,p=1$YXNnYXNkZmdzZGZnc2Q$aMawMnEl0RKs+CJHr8Vd0Q", false},
{"$argon2$v=19$m=8,t=2,p=1$YXNzZGZmZGdzZGZnc2Rhcw$8W4XHqJYu2LHqiAv93++fEA+", false},
{"$argon2d$v=19$m=16,t=2,p=1$YXNnYXNkZmdzZGZnc2Q$WcFGx+WZ5sKl//W0NOissxQ", false},
{"$argon2d$v=19$m=16,t=2,p=1$YXNnYXNkZmdzZGZnc2Q$WcFGx+WZ5sKl/W0NOissxQ", true},
{"$argon2i$v=19$m=16,t=2,p=1$YXNnYXNkZmdzZGZnc2Q$+ieRkqJkcSa6vRuzajIKsA", true},
{"$argon2id$v=19$m=16,t=2,p=1$YXNnYXNkZmdzZGZnc2Q$aMawMnEl0RKs+CJHr8Vd0Q", true},
}

validate := New()

for i, test := range tests {
errs := validate.Var(test.param, "argon2")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d Argon2 failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d Argon2 failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "argon2" {
t.Fatalf("Index: %d Argon2 failed Error: %s", i, errs)
}
}
}
}
}