From 984b5e55a47715dac9dfb0dca873d8461ad39410 Mon Sep 17 00:00:00 2001 From: "marko.tikvic" Date: Wed, 27 Nov 2019 11:08:32 +0100 Subject: [PATCH] DecodeJWT --- auth.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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 -- 1.8.1.2