From f9b6cd2e2027fa1ffb65ed18ee9abeabb4af4516 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 30 Oct 2024 23:44:25 +0800 Subject: [PATCH] :bug: Fix reading jwt issue --- pkg/nex/sec/jwt_reader.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/nex/sec/jwt_reader.go b/pkg/nex/sec/jwt_reader.go index ac80b8f..89b1a96 100644 --- a/pkg/nex/sec/jwt_reader.go +++ b/pkg/nex/sec/jwt_reader.go @@ -39,7 +39,7 @@ func NewJwtReader(fp string) (*JwtReader, error) { }, 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) { if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok { 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 token.Claims, err + if claims, ok := token.Claims.(T); ok { + return claims, nil + } else { + return out, err + } }