You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this case, the type assertion tests whether token.Method can be safely converted to the *jwt.SigningMethodHMAC type. But a more specific check for HS256 seems more appropriate.
token, err:=jwt.Parse(tokenString, func(token*jwt.Token) (interface{}, error) {
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")returnhmacSampleSecret, nil
}, jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
Or for all HMAC signing methods:
validMethods:= []string{
jwt.SigningMethodHS256.Alg(),
jwt.SigningMethodHS384.Alg(),
jwt.SigningMethodHS512.Alg(),
}
token, err:=jwt.Parse(tokenString, func(token*jwt.Token) (interface{}, error) {
// hmacSampleSecret is a []byte containing your secret, e.g. []byte("my_secret_key")returnhmacSampleSecret, nil
}, jwt.WithValidMethods(validMethods))
The text was updated successfully, but these errors were encountered:
Came here to ask the same question. I'm currently using jwt.WithValidMethod without the type assertion on the token Method like in the examples.
Given that the example suggests something different, it'd be great to clarify what the intended usage is and whether one or the other (or both?) should be used.
If jwt.WithValidMethod is the way to got, the linked PR #425 would be a great change as it would remove some uncertainty.
Came here to ask the same question. I'm currently using jwt.WithValidMethod without the type assertion on the token Method like in the examples.
Given that the example suggests something different, it'd be great to clarify what the intended usage is and whether one or the other (or both?) should be used.
If jwt.WithValidMethod is the way to got, the linked PR #425 would be a great change as it would remove some uncertainty.
I think per se both options are "valid", although the jwt.WithValidMethod is a lot easier to handle and less error-prone. I think some of the examples outdate our relatively new functional-style options, so that is probably why the example still uses the "old" way.
The
ExampleParse_hmac
function inhmac_example_test.go
provides the following example code:jwt/hmac_example_test.go
Lines 51 to 59 in bc8bdca
The validation performed on line 53 seems at odds with the recommendations in the
Parse
function docs:jwt/parser.go
Lines 218 to 225 in bc8bdca
In this case, the type assertion tests whether
token.Method
can be safely converted to the*jwt.SigningMethodHMAC
type. But a more specific check for HS256 seems more appropriate.Or for all HMAC signing methods:
The text was updated successfully, but these errors were encountered: