diff --git a/auth.go b/auth.go index d2351f6..b454f97 100644 --- a/auth.go +++ b/auth.go @@ -188,6 +188,32 @@ func GetTokenClaims(req *http.Request) (*TokenClaims, error) { return claims, nil } +func DecodeJWT(secret, token string) (*TokenClaims, error) { + secretfunc := func(*jwt.Token) (interface{}, error) { + return []byte(secret), nil + } + + tok, err := jwt.ParseWithClaims(token, &TokenClaims{}, secretfunc) + if err != nil { + if validation, ok := err.(*jwt.ValidationError); ok { + // don't return error if token is expired + if !(validation.Errors&jwt.ValidationErrorExpired != 0) { + return nil, err + } + } else { + return nil, err + } + } + + // type assertion + claims, ok := tok.Claims.(*TokenClaims) + if !ok { + return &TokenClaims{}, errors.New("token is not valid") + } + + return claims, nil +} + // randomSalt returns a string of 32 random characters. func randomSalt() (s string, err error) { const saltSize = 32