Files
2025-05-29 01:11:04 +07:00

40 lines
807 B
Go

package jwt
import (
"fmt"
"github.com/golang-jwt/jwt"
)
func VerifyJWT(
tokenString string,
secretKey string,
) (map[string]any, error) {
token, e := jwt.Parse(
tokenString,
func(token *jwt.Token) (any, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf(
"unexpexted signing method: %v",
token.Header["alg"],
)
}
return []byte(secretKey), nil
},
)
if e != nil {
return nil, fmt.Errorf("token validation failed: %w", e)
}
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
result := make(map[string]any)
for key, value := range claims {
if key == "exp" || key == "iat" || key == "nbf" {
continue
}
result[key] = value
}
return result, nil
}
return nil, fmt.Errorf("invalid token")
}