🐛 Fix reading jwt issue
This commit is contained in:
parent
06c5fadb42
commit
f9b6cd2e20
@ -39,7 +39,7 @@ func NewJwtReader(fp string) (*JwtReader, error) {
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadJwt(v *JwtReader, in string, out jwt.Claims) (any, error) {
|
func ReadJwt[T jwt.Claims](v *JwtReader, in string, out T) (T, error) {
|
||||||
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"])
|
||||||
@ -52,5 +52,9 @@ func ReadJwt(v *JwtReader, in string, out jwt.Claims) (any, error) {
|
|||||||
return out, fmt.Errorf("token is not valid")
|
return out, fmt.Errorf("token is not valid")
|
||||||
}
|
}
|
||||||
|
|
||||||
return token.Claims, err
|
if claims, ok := token.Claims.(T); ok {
|
||||||
|
return claims, nil
|
||||||
|
} else {
|
||||||
|
return out, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user