Skip to content

Commit

Permalink
Merge pull request #92 from drakkan/errcheck
Browse files Browse the repository at this point in the history
Encode: check for errors while setting claim fields
  • Loading branch information
VojtechVitek committed Jun 18, 2024
2 parents 1ff6081 + c856efe commit b5d850b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 3 additions & 1 deletion jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func VerifyToken(ja *JWTAuth, tokenString string) (jwt.Token, error) {
func (ja *JWTAuth) Encode(claims map[string]interface{}) (t jwt.Token, tokenString string, err error) {
t = jwt.New()
for k, v := range claims {
t.Set(k, v)
if err := t.Set(k, v); err != nil {
return nil, "", err
}
}
payload, err := ja.sign(t)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions jwtauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,23 @@ func TestMore(t *testing.T) {
}
}

func TestEncodeClaims(t *testing.T) {
claims := map[string]interface{}{
"key1": "val1",
"key2": 2,
"key3": time.Now(),
"key4": []string{"1", "2"},
}
claims[jwt.JwtIDKey] = 1
if _, _, err := TokenAuthHS256.Encode(claims); err == nil {
t.Fatal("encoding invalid claims succeeded")
}
claims[jwt.JwtIDKey] = "123"
if _, _, err := TokenAuthHS256.Encode(claims); err != nil {
t.Fatalf("unexpected error encoding valid claims: %v", err)
}
}

//
// Test helper functions
//
Expand Down

0 comments on commit b5d850b

Please sign in to comment.