Skip to content

Commit

Permalink
Add support for jwt validation (#783)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveorourke authored Jul 26, 2021
1 parent 8cfa1e9 commit e40bece
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ Baked-in Validations
| isbn10 | International Standard Book Number 10 |
| isbn13 | International Standard Book Number 13 |
| json | JSON |
| jwt | JSON Web Token (JWT) |
| latitude | Latitude |
| longitude | Longitude |
| rgb | RGB String |
Expand Down
6 changes: 6 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ var (
"url_encoded": isURLEncoded,
"dir": isDir,
"json": isJSON,
"jwt": isJWT,
"hostname_port": isHostnamePort,
"lowercase": isLowercase,
"uppercase": isUppercase,
Expand Down Expand Up @@ -2235,6 +2236,11 @@ func isJSON(fl FieldLevel) bool {
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}

// isJWT is the validation function for validating if the current field's value is a valid JWT string.
func isJWT(fl FieldLevel) bool {
return jWTRegex.MatchString(fl.Field().String())
}

// isHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
func isHostnamePort(fl FieldLevel) bool {
val := fl.Field().String()
Expand Down
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,12 @@ This validates that a string value is valid JSON
Usage: json
JWT String
This validates that a string value is a valid JWT
Usage: jwt
File path
This validates that a string value contains a valid file path and that
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const (
uRLEncodedRegexString = `^(?:[^%]|%[0-9A-Fa-f]{2})*$`
hTMLEncodedRegexString = `&#[x]?([0-9a-fA-F]{2})|(&gt)|(&lt)|(&quot)|(&amp)+[;]?`
hTMLRegexString = `<[/]?([a-zA-Z]+).*?>`
jWTRegexString = "^[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]+\\.[A-Za-z0-9-_]*$"
splitParamsRegexString = `'[^']*'|\S+`
bicRegexString = `^[A-Za-z]{6}[A-Za-z0-9]{2}([A-Za-z0-9]{3})?$`
)
Expand Down Expand Up @@ -98,6 +99,7 @@ var (
uRLEncodedRegex = regexp.MustCompile(uRLEncodedRegexString)
hTMLEncodedRegex = regexp.MustCompile(hTMLEncodedRegexString)
hTMLRegex = regexp.MustCompile(hTMLRegexString)
jWTRegex = regexp.MustCompile(jWTRegexString)
splitParamsRegex = regexp.MustCompile(splitParamsRegexString)
bicRegex = regexp.MustCompile(bicRegexString)
)
5 changes: 5 additions & 0 deletions translations/en/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -1284,6 +1284,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} must be a valid json string",
override: false,
},
{
tag: "jwt",
translation: "{0} must be a valid jwt string",
override: false,
},
{
tag: "lowercase",
translation: "{0} must be a lowercase string",
Expand Down
5 changes: 5 additions & 0 deletions translations/en/en_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ func TestTranslations(t *testing.T) {
UniqueArray [3]string `validate:"unique"`
UniqueMap map[string]string `validate:"unique"`
JSONString string `validate:"json"`
JWTString string `validate:"jwt"`
LowercaseString string `validate:"lowercase"`
UppercaseString string `validate:"uppercase"`
Datetime string `validate:"datetime=2006-01-02"`
Expand Down Expand Up @@ -646,6 +647,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.JSONString",
expected: "JSONString must be a valid json string",
},
{
ns: "Test.JWTString",
expected: "JWTString must be a valid jwt string",
},
{
ns: "Test.LowercaseString",
expected: "LowercaseString must be a lowercase string",
Expand Down
36 changes: 36 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10787,6 +10787,42 @@ func TestJSONValidation(t *testing.T) {
}, "Bad field type int")
}

func TestJWTValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"eyJhbGciOiJIUzI1NiJ9.eyJuYW1lIjoiZ29waGVyIn0.O_bROM_szPq9qBql-XDHMranHwP48ODdoLICWzqBr_U", true},
{"acb123-_.def456-_.ghi789-_", true},
{"eyJhbGciOiJOT05FIn0.e30.", true},
{"eyJhbGciOiJOT05FIn0.e30.\n", false},
{"\x00.\x00.\x00", false},
{"", false},
}

validate := New()

for i, test := range tests {

errs := validate.Var(test.param, "jwt")

if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d jwt failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d jwt failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "jwt" {
t.Fatalf("Index: %d jwt failed Error: %s", i, errs)
}
}
}
}
}

func Test_hostnameport_validator(t *testing.T) {
type Host struct {
Addr string `validate:"hostname_port"`
Expand Down

0 comments on commit e40bece

Please sign in to comment.