@@ -12,12 +12,14 @@ import (
1212// JWT represents a JWT handler using a shared secret and generic claims data.
1313type JWT [T any ] struct {
1414 secret []byte
15+ method jwt.SigningMethod
1516}
1617
1718// New creates a new JWT instance using the given secret string.
18- func New [T any ](secret string ) * JWT [T ] {
19+ func New [T any ](secret string , method jwt. SigningMethod ) * JWT [T ] {
1920 return & JWT [T ]{
2021 secret : []byte (secret ),
22+ method : method ,
2123 }
2224}
2325
@@ -31,15 +33,19 @@ type Claims[T any] struct {
3133
3234// Generate creates and signs a JWT token using the provided claims.
3335func (x * JWT [T ]) Generate (claims * Claims [T ]) (string , error ) {
34- v := jwt .NewWithClaims (jwt . SigningMethodHS256 , claims )
36+ v := jwt .NewWithClaims (x . method , claims )
3537
3638 return v .SignedString (x .secret )
3739}
3840
3941// Parse parses and validates a JWT token string and returns the claims
4042// if the token is valid.
4143func (x * JWT [T ]) Parse (tokenString string ) (* Claims [T ], error ) {
42- token , err := jwt .ParseWithClaims (tokenString , & Claims [T ]{}, func (_ * jwt.Token ) (any , error ) {
44+ token , err := jwt .ParseWithClaims (tokenString , & Claims [T ]{}, func (t * jwt.Token ) (any , error ) {
45+ if t .Method .Alg () != x .method .Alg () {
46+ return nil , jwt .ErrTokenSignatureInvalid
47+ }
48+
4349 return x .secret , nil
4450 })
4551 if err != nil {
0 commit comments