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.
| // 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)) |
Contributor
| // 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)) |
Contributor
| // 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)) |
Contributor
| for _, test := range tests { | ||
| t.Run(test.Name, func(t *testing.T) { | ||
| var opts []ParserOption | ||
| var opts []ParserOption[MapClaims] |
Contributor
|
|
||
| if test.Required { | ||
| opts = append(opts, WithAudience(test.Comparison)) | ||
| opts = append(opts, WithAudience[MapClaims](test.Comparison)) |
Contributor
| } | ||
| want := false | ||
| got := newValidator(WithIssuedAt()).Validate(mapClaims) | ||
| got := newValidator[MapClaims](WithIssuedAt[MapClaims]()).Validate(mapClaims) |
Contributor
| } | ||
| want := false | ||
| got := newValidator(WithTimeFunc(func() time.Time { | ||
| got := newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
Contributor
| } | ||
|
|
||
| got = newValidator(WithTimeFunc(func() time.Time { | ||
| got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
Contributor
|
|
||
| want = true | ||
| got = newValidator(WithTimeFunc(func() time.Time { | ||
| got = newValidator[MapClaims](WithTimeFunc[MapClaims](func() time.Time { |
Contributor
ParseXXX functions
Collaborator
Author
|
Will continue discussion in #272 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Following the discussion in #269, this PR adds support for type parameters (or generics) in the
ParseXXXfunctions, basically allowing one to directly access a specific claims type without any uncomfortable type assertions in either thekeyfuncor the resultingTokenstruct.