@@ -19,15 +19,18 @@ var DecodePaddingAllowed bool
1919// To use strict decoding, set this boolean to `true` prior to using this package.
2020var DecodeStrict bool
2121
22- // Keyfunc will be used by the Parse methods as a callback function to supply
22+ // KeyfuncFor[T] will be used by the Parse methods as a callback function to supply
2323// the key for verification. The function receives the parsed,
24- // but unverified Token . This allows you to use properties in the
24+ // but unverified TokenFor[T] . This allows you to use properties in the
2525// Header of the token (such as `kid`) to identify which key to use.
26- type Keyfunc [T Claims ] func (* Token [T ]) (interface {}, error )
26+ type KeyfuncFor [T Claims ] func (* TokenFor [T ]) (interface {}, error )
2727
28- // Token represents a JWT Token. Different fields will be used depending on whether you're
28+ // Keyfunc is an alias for KeyfuncFor[Claims], for backward compatibility.
29+ type Keyfunc = KeyfuncFor [MapClaims ]
30+
31+ // TokenFor represents a JWT TokenFor. Different fields will be used depending on whether you're
2932// creating or parsing/verifying a token.
30- type Token [T Claims ] struct {
33+ type TokenFor [T Claims ] struct {
3134 Raw string // The raw token. Populated when you Parse a token
3235 Method SigningMethod // The signing method used or to be used
3336 Header map [string ]interface {} // The first segment of the token
@@ -36,14 +39,17 @@ type Token[T Claims] struct {
3639 Valid bool // Is the token valid? Populated when you Parse/Verify a token
3740}
3841
42+ // Token is an alias for TokenFor[Claims], for backward compatibility.
43+ type Token = TokenFor [MapClaims ]
44+
3945// New creates a new Token with the specified signing method and an empty map of claims.
40- func New (method SigningMethod ) * Token [ MapClaims ] {
46+ func New (method SigningMethod ) * Token {
4147 return NewWithClaims (method , MapClaims {})
4248}
4349
4450// NewWithClaims creates a new Token with the specified signing method and claims.
45- func NewWithClaims [T Claims ](method SigningMethod , claims T ) * Token [T ] {
46- return & Token [T ]{
51+ func NewWithClaims [T Claims ](method SigningMethod , claims T ) * TokenFor [T ] {
52+ return & TokenFor [T ]{
4753 Header : map [string ]interface {}{
4854 "typ" : "JWT" ,
4955 "alg" : method .Alg (),
@@ -55,7 +61,7 @@ func NewWithClaims[T Claims](method SigningMethod, claims T) *Token[T] {
5561
5662// SignedString creates and returns a complete, signed JWT.
5763// The token is signed using the SigningMethod specified in the token.
58- func (t * Token [T ]) SignedString (key interface {}) (string , error ) {
64+ func (t * TokenFor [T ]) SignedString (key interface {}) (string , error ) {
5965 var sig , sstr string
6066 var err error
6167 if sstr , err = t .SigningString (); err != nil {
@@ -71,7 +77,7 @@ func (t *Token[T]) SignedString(key interface{}) (string, error) {
7177// most expensive part of the whole deal. Unless you
7278// need this for something special, just go straight for
7379// the SignedString.
74- func (t * Token [T ]) SigningString () (string , error ) {
80+ func (t * TokenFor [T ]) SigningString () (string , error ) {
7581 var err error
7682 var jsonValue []byte
7783
@@ -95,7 +101,7 @@ func (t *Token[T]) SigningString() (string, error) {
95101// validate the 'alg' claim in the token matches the expected algorithm.
96102// For more details about the importance of validating the 'alg' claim,
97103// see https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
98- func Parse (tokenString string , keyFunc Keyfunc [ MapClaims ] , options ... ParserOption ) (* Token [ MapClaims ] , error ) {
104+ func Parse (tokenString string , keyFunc Keyfunc , options ... ParserOption ) (* Token , error ) {
99105 return NewParser (options ... ).Parse (tokenString , keyFunc )
100106}
101107
@@ -104,7 +110,7 @@ func Parse(tokenString string, keyFunc Keyfunc[MapClaims], options ...ParserOpti
104110// Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
105111// make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
106112// proper memory for it before passing in the overall claims, otherwise you might run into a panic.
107- func ParseWithClaims [T Claims ](tokenString string , keyFunc Keyfunc [T ], options ... ParserOption ) (* Token [T ], error ) {
113+ func ParseWithClaims [T Claims ](tokenString string , keyFunc KeyfuncFor [T ], options ... ParserOption ) (* TokenFor [T ], error ) {
108114 return NewParserFor [T ](options ... ).Parse (tokenString , keyFunc )
109115}
110116
0 commit comments