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

Added support for type parameters in the ParseXXX functions #271

Closed
wants to merge 7 commits into from

Conversation

oxisto
Copy link
Collaborator

@oxisto oxisto commented Feb 17, 2023

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 the keyfunc or the resulting Token struct.

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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
cannot use ParseFromRequest(req, extractor, keyFunc, WithClaims(claims)) (value of type *jwt.Token[jwt.MapClaims]) as *jwt.Token[T] value in return statement

@@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
cannot use keyFunc (variable of type jwt.Keyfunc[T]) as jwt.Keyfunc[jwt.MapClaims] value in argument to ParseFromRequest

@@ -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))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
cannot use WithClaims(claims) (value of type ParseFromRequestOption[T]) as ParseFromRequestOption[jwt.MapClaims] value in argument to ParseFromRequest

@@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: ParserOption[MapClaims] (ParserOption is not a generic type)


if test.Required {
opts = append(opts, WithAudience(test.Comparison))
opts = append(opts, WithAudience[MapClaims](test.Comparison))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithAudience (value of type func(aud string) ParserOption)

@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithIssuedAt (value of type func() ParserOption)

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithTimeFunc (value of type func(f func() time.Time) ParserOption)

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithTimeFunc (value of type func(f func() time.Time) ParserOption)

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [staticcheck] <compile> reported by reviewdog 🐶
invalid operation: cannot index WithTimeFunc (value of type func(f func() time.Time) ParserOption)

@oxisto oxisto changed the title generics Added support for type parameters in the ParseXXX functions Feb 17, 2023
@oxisto
Copy link
Collaborator Author

oxisto commented Feb 20, 2023

Will continue discussion in #272

@oxisto oxisto closed this Feb 20, 2023
@oxisto oxisto deleted the generics branch March 27, 2023 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant