Skip to content
Merged
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
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ import (
)

var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)

claims, ok := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
if !ok {
http.Error(w, "failed to get validated claims", http.StatusInternalServerError)
return
}

payload, err := json.Marshal(claims)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down
6 changes: 5 additions & 1 deletion examples/http-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func (c *CustomClaimsExample) Validate(ctx context.Context) error {
}

var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
claims, ok := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
if !ok {
http.Error(w, "failed to get validated claims", http.StatusInternalServerError)
return
}

payload, err := json.Marshal(claims)
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion examples/http-jwks-example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ import (
)

var handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
claims := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
claims, ok := r.Context().Value(jwtmiddleware.ContextKey{}).(*validator.ValidatedClaims)
if !ok {
http.Error(w, "failed to get validated claims", http.StatusInternalServerError)
return
}

payload, err := json.Marshal(claims)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (v *Validator) ValidateToken(ctx context.Context, tokenString string) (inte
}

claimDest := []interface{}{&jwt.Claims{}}
if v.customClaims != nil {
if v.customClaims != nil && v.customClaims() != nil {
claimDest = append(claimDest, v.customClaims())
}

Expand Down Expand Up @@ -149,7 +149,7 @@ func (v *Validator) ValidateToken(ctx context.Context, tokenString string) (inte
validatedClaims.RegisteredClaims.IssuedAt = registeredClaims.IssuedAt.Time().Unix()
}

if v.customClaims != nil {
if v.customClaims != nil && v.customClaims() != nil {
validatedClaims.CustomClaims = claimDest[1].(CustomClaims)
if err = validatedClaims.CustomClaims.Validate(ctx); err != nil {
return nil, fmt.Errorf("custom claims not validated: %w", err)
Expand Down
37 changes: 37 additions & 0 deletions validator/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ func TestValidator_ValidateToken(t *testing.T) {
},
expectedError: errors.New("custom claims not validated: custom claims error message"),
},
{
name: "it successfully validates a token even if customClaims() returns nil",
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2dvLWp3dC1taWRkbGV3YXJlLmV1LmF1dGgwLmNvbS8iLCJzdWIiOiIxMjM0NTY3ODkwIiwiYXVkIjpbImh0dHBzOi8vZ28tand0LW1pZGRsZXdhcmUtYXBpLyJdLCJzY29wZSI6InJlYWQ6bWVzc2FnZXMifQ.oqtUZQ-Q8un4CPduUBdGVq5gXpQVIFT_QSQjkOXFT5I",
keyFunc: func(context.Context) (interface{}, error) {
return []byte("secret"), nil
},
algorithm: HS256,
customClaims: func() CustomClaims {
return nil
},
expectedClaims: &ValidatedClaims{
RegisteredClaims: RegisteredClaims{
Issuer: issuer,
Subject: subject,
Audience: []string{audience},
},
CustomClaims: nil,
},
},
{
name: "it successfully validates a token with exp, nbf and iat",
token: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJodHRwczovL2dvLWp3dC1taWRkbGV3YXJlLmV1LmF1dGgwLmNvbS8iLCJzdWIiOiIxMjM0NTY3ODkwIiwiYXVkIjpbImh0dHBzOi8vZ28tand0LW1pZGRsZXdhcmUtYXBpLyJdLCJpYXQiOjE2NjY5Mzc2ODYsIm5iZiI6MTY2NjkzOTAwMCwiZXhwIjoxNjY3OTM3Njg2fQ.36iSr7w8Q6b9iJoJo-swmfgAfm23w8SlX92NHIHGX2s",
keyFunc: func(context.Context) (interface{}, error) {
return []byte("secret"), nil
},
algorithm: HS256,
expectedClaims: &ValidatedClaims{
RegisteredClaims: RegisteredClaims{
Issuer: issuer,
Subject: subject,
Audience: []string{audience},
Expiry: 1667937686,
NotBefore: 1666939000,
IssuedAt: 1666937686,
},
},
},
}

for _, testCase := range testCases {
Expand Down