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

Example using generic type parameter #403

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
}

switch have := got.(type) {
case VerificationKeySet:
case VerificationKeySet[VerificationKey]:
if len(have.Keys) == 0 {
return token, newError("keyfunc returned empty verification key set", ErrTokenUnverifiable)
}
Expand Down
14 changes: 9 additions & 5 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,26 @@ var (
nilKeyFunc jwt.Keyfunc = nil
multipleZeroKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) { return []interface{}{}, nil }
multipleEmptyKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) {
return jwt.VerificationKeySet{Keys: []jwt.VerificationKey{nil, nil}}, nil
keys := []jwt.VerificationKey{nil, nil}
return jwt.VerificationKeySet[jwt.VerificationKey]{Keys: keys}, nil
}
multipleVerificationKeysFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) {
return []jwt.VerificationKey{jwtTestDefaultKey, jwtTestEC256PublicKey}, nil
}
multipleLastKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) {
return jwt.VerificationKeySet{Keys: []jwt.VerificationKey{jwtTestEC256PublicKey, jwtTestDefaultKey}}, nil
keys := []jwt.VerificationKey{jwtTestEC256PublicKey, jwtTestDefaultKey}
return jwt.VerificationKeySet[jwt.VerificationKey]{Keys: keys}, nil
}
multipleFirstKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) {
return jwt.VerificationKeySet{Keys: []jwt.VerificationKey{jwtTestDefaultKey, jwtTestEC256PublicKey}}, nil
keys := []jwt.VerificationKey{jwtTestDefaultKey, jwtTestEC256PublicKey}
return jwt.VerificationKeySet[jwt.VerificationKey]{Keys: keys}, nil
}
multipleAltTypedKeyFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) {
return jwt.VerificationKeySet{Keys: []jwt.VerificationKey{jwtTestDefaultKey, jwtTestDefaultKey}}, nil
keys := []jwt.VerificationKey{jwtTestDefaultKey, jwtTestDefaultKey}
return jwt.VerificationKeySet[jwt.VerificationKey]{Keys: keys}, nil
}
emptyVerificationKeySetFunc jwt.Keyfunc = func(t *jwt.Token) (interface{}, error) {
return jwt.VerificationKeySet{}, nil
return jwt.VerificationKeySet[jwt.VerificationKey]{}, nil
}
)

Expand Down
4 changes: 2 additions & 2 deletions token.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type VerificationKey interface {
}

// VerificationKeySet is a set of public or secret keys. It is used by the parser to verify a token.
type VerificationKeySet struct {
Keys []VerificationKey
type VerificationKeySet[T VerificationKey] struct {
Keys []T
}

// Token represents a JWT Token. Different fields will be used depending on
Expand Down