@@ -28,11 +28,14 @@ var DecodeStrict bool
2828// the key for verification. The function receives the parsed, but unverified
2929// Token. This allows you to use properties in the Header of the token (such as
3030// `kid`) to identify which key to use.
31- type Keyfunc [T Claims ] func (* Token [T ]) (interface {}, error )
31+ type KeyfuncFor [T Claims ] func (* TokenFor [T ]) (interface {}, error )
32+
33+ // Keyfunc is an alias for KeyfuncFor[Claims], for backward compatibility.
34+ type Keyfunc = KeyfuncFor [MapClaims ]
3235
3336// Token represents a JWT Token. Different fields will be used depending on
3437// whether you're creating or parsing/verifying a token.
35- type Token [T Claims ] struct {
38+ type TokenFor [T Claims ] struct {
3639 Raw string // Raw contains the raw token. Populated when you [Parse] a token
3740 Method SigningMethod // Method is the signing method used or to be used
3841 Header map [string ]interface {} // Header is the first segment of the token
@@ -41,16 +44,19 @@ type Token[T Claims] struct {
4144 Valid bool // Valid specifies if the token is valid. Populated when you Parse/Verify a token
4245}
4346
47+ // Token is an alias for TokenFor[Claims], for backward compatibility.
48+ type Token = TokenFor [MapClaims ]
49+
4450// New creates a new [Token] with the specified signing method and an empty map of
4551// claims.
46- func New (method SigningMethod ) * Token [ MapClaims ] {
52+ func New (method SigningMethod ) * Token {
4753 return NewWithClaims (method , MapClaims {})
4854}
4955
5056// NewWithClaims creates a new [Token] with the specified signing method and
5157// claims.
52- func NewWithClaims [T Claims ](method SigningMethod , claims T ) * Token [T ] {
53- return & Token [T ]{
58+ func NewWithClaims [T Claims ](method SigningMethod , claims T ) * TokenFor [T ] {
59+ return & TokenFor [T ]{
5460 Header : map [string ]interface {}{
5561 "typ" : "JWT" ,
5662 "alg" : method .Alg (),
@@ -62,7 +68,7 @@ func NewWithClaims[T Claims](method SigningMethod, claims T) *Token[T] {
6268
6369// SignedString creates and returns a complete, signed JWT. The token is signed
6470// using the SigningMethod specified in the token.
65- func (t * Token [T ]) SignedString (key interface {}) (string , error ) {
71+ func (t * TokenFor [T ]) SignedString (key interface {}) (string , error ) {
6672 sstr , err := t .SigningString ()
6773 if err != nil {
6874 return "" , err
@@ -79,7 +85,7 @@ func (t *Token[T]) SignedString(key interface{}) (string, error) {
7985// SigningString generates the signing string. This is the most expensive part
8086// of the whole deal. Unless you need this for something special, just go
8187// straight for the SignedString.
82- func (t * Token [T ]) SigningString () (string , error ) {
88+ func (t * TokenFor [T ]) SigningString () (string , error ) {
8389 h , err := json .Marshal (t .Header )
8490 if err != nil {
8591 return "" , err
@@ -100,7 +106,7 @@ func (t *Token[T]) SigningString() (string, error) {
100106// expected algorithm. For more details about the importance of validating the
101107// 'alg' claim, see
102108// https://auth0.com/blog/critical-vulnerabilities-in-json-web-token-libraries/
103- func Parse (tokenString string , keyFunc Keyfunc [ MapClaims ] , options ... ParserOption ) (* Token [ MapClaims ] , error ) {
109+ func Parse (tokenString string , keyFunc Keyfunc , options ... ParserOption ) (* Token , error ) {
104110 return NewParser (options ... ).Parse (tokenString , keyFunc )
105111}
106112
@@ -111,7 +117,7 @@ func Parse(tokenString string, keyFunc Keyfunc[MapClaims], options ...ParserOpti
111117// embed a non-pointer version of the claims or b) if you are using a pointer,
112118// allocate the proper memory for it before passing in the overall claims,
113119// otherwise you might run into a panic.
114- func ParseWithClaims [T Claims ](tokenString string , keyFunc Keyfunc [T ], options ... ParserOption ) (* Token [T ], error ) {
120+ func ParseWithClaims [T Claims ](tokenString string , keyFunc KeyfuncFor [T ], options ... ParserOption ) (* TokenFor [T ], error ) {
115121 return NewParserFor [T ](options ... ).Parse (tokenString , keyFunc )
116122}
117123
0 commit comments