gopay/internal/jwt/decode.go

30 lines
581 B
Go
Raw Permalink Normal View History

2024-06-03 12:51:42 +00:00
package jwt
import (
"github.com/golang-jwt/jwt"
"github.com/rs/zerolog/log"
)
// Decode a token
func Decode(token string) (*Claims, error) {
// Create token claims
tokenClaims, err := jwt.ParseWithClaims(
token,
&Claims{},
func(token *jwt.Token) (interface{}, error) {
return secret, nil
},
)
// Found token
if tokenClaims != nil {
// Map to claims struct & if ok & valid then return
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
return claims, nil
}
}
log.Err(err).Msg("Could not decode JWT claims")
return nil, err
}