💥 Change read jwt SDK api

This commit is contained in:
LittleSheep 2024-10-30 23:39:09 +08:00
parent 427c83e38f
commit 06c5fadb42

View File

@ -39,8 +39,7 @@ func NewJwtReader(fp string) (*JwtReader, error) {
}, nil }, nil
} }
func ReadJwt[T jwt.Claims](v *JwtReader, in string) (T, error) { func ReadJwt(v *JwtReader, in string, out jwt.Claims) (any, error) {
var out T
token, err := jwt.ParseWithClaims(in, out, func(token *jwt.Token) (interface{}, error) { token, err := jwt.ParseWithClaims(in, out, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok { if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"]) return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
@ -53,9 +52,5 @@ func ReadJwt[T jwt.Claims](v *JwtReader, in string) (T, error) {
return out, fmt.Errorf("token is not valid") return out, fmt.Errorf("token is not valid")
} }
if claims, ok := token.Claims.(T); ok { return token.Claims, err
return claims, nil
} else {
return out, err
}
} }