-
Notifications
You must be signed in to change notification settings - Fork 349
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 support for type parameters in the ParseXXX
functions
#271
Conversation
Following the discussion in #269, this PR serves as a playground for a jwt version with generics aka type parameters. This basically breaks all the tests for now since they are non-typed but the examples in `example_test.go` work.
@@ -42,29 +42,29 @@ func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfun | |||
// ParseFromRequestWithClaims is an alias for ParseFromRequest but with custom Claims type. | |||
// | |||
// Deprecated: use ParseFromRequest and the WithClaims option | |||
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) { | |||
func ParseFromRequestWithClaims[T jwt.Claims](req *http.Request, extractor Extractor, claims T, keyFunc jwt.Keyfunc[T]) (token *jwt.Token[T], err error) { | |||
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -42,29 +42,29 @@ func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfun | |||
// ParseFromRequestWithClaims is an alias for ParseFromRequest but with custom Claims type. | |||
// | |||
// Deprecated: use ParseFromRequest and the WithClaims option | |||
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) { | |||
func ParseFromRequestWithClaims[T jwt.Claims](req *http.Request, extractor Extractor, claims T, keyFunc jwt.Keyfunc[T]) (token *jwt.Token[T], err error) { | |||
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@@ -42,29 +42,29 @@ func ParseFromRequest(req *http.Request, extractor Extractor, keyFunc jwt.Keyfun | |||
// ParseFromRequestWithClaims is an alias for ParseFromRequest but with custom Claims type. | |||
// | |||
// Deprecated: use ParseFromRequest and the WithClaims option | |||
func ParseFromRequestWithClaims(req *http.Request, extractor Extractor, claims jwt.Claims, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) { | |||
func ParseFromRequestWithClaims[T jwt.Claims](req *http.Request, extractor Extractor, claims T, keyFunc jwt.Keyfunc[T]) (token *jwt.Token[T], err error) { | |||
return ParseFromRequest(req, extractor, keyFunc, WithClaims(claims)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
@@ -56,13 +56,13 @@ func TestVerifyAud(t *testing.T) { | |||
|
|||
for _, test := range tests { | |||
t.Run(test.Name, func(t *testing.T) { | |||
var opts []ParserOption | |||
var opts []ParserOption[MapClaims] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
|
||
if test.Required { | ||
opts = append(opts, WithAudience(test.Comparison)) | ||
opts = append(opts, WithAudience[MapClaims](test.Comparison)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
@@ -77,7 +77,7 @@ func TestMapclaimsVerifyIssuedAtInvalidTypeString(t *testing.T) { | |||
"iat": "foo", | |||
} | |||
want := false | |||
got := newValidator(WithIssuedAt()).Validate(mapClaims) | |||
got := newValidator[MapClaims](WithIssuedAt[MapClaims]()).Validate(mapClaims) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
@@ -112,25 +112,78 @@ func TestMapClaimsVerifyExpiresAtExpire(t *testing.T) { | |||
"exp": float64(exp.Unix()), | |||
} | |||
want := false | |||
got := newValidator(WithTimeFunc(func() time.Time { | |||
got := newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
return exp | ||
})).Validate(mapClaims) | ||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
} | ||
|
||
got = newValidator(WithTimeFunc(func() time.Time { | ||
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
map_claims_test.go
Outdated
return exp.Add(1 * time.Second) | ||
})).Validate(mapClaims) | ||
if want != (got == nil) { | ||
t.Fatalf("Failed to verify claims, wanted: %v got %v", want, (got == nil)) | ||
} | ||
|
||
want = true | ||
got = newValidator(WithTimeFunc(func() time.Time { | ||
got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ParseXXX
functions
Will continue discussion in #272 |
Following the discussion in #269, this PR adds support for type parameters (or generics) in the
ParseXXX
functions, basically allowing one to directly access a specific claims type without any uncomfortable type assertions in either thekeyfunc
or the resultingToken
struct.