Commit 984b5e55a47715dac9dfb0dca873d8461ad39410

Authored by Marko Tikvić
1 parent 0dd8dda340
Exists in master

DecodeJWT

Showing 1 changed file with 26 additions and 0 deletions   Show diff stats
... ... @@ -188,6 +188,32 @@ func GetTokenClaims(req *http.Request) (*TokenClaims, error) {
188 188 return claims, nil
189 189 }
190 190  
  191 +func DecodeJWT(secret, token string) (*TokenClaims, error) {
  192 + secretfunc := func(*jwt.Token) (interface{}, error) {
  193 + return []byte(secret), nil
  194 + }
  195 +
  196 + tok, err := jwt.ParseWithClaims(token, &TokenClaims{}, secretfunc)
  197 + if err != nil {
  198 + if validation, ok := err.(*jwt.ValidationError); ok {
  199 + // don't return error if token is expired
  200 + if !(validation.Errors&jwt.ValidationErrorExpired != 0) {
  201 + return nil, err
  202 + }
  203 + } else {
  204 + return nil, err
  205 + }
  206 + }
  207 +
  208 + // type assertion
  209 + claims, ok := tok.Claims.(*TokenClaims)
  210 + if !ok {
  211 + return &TokenClaims{}, errors.New("token is not valid")
  212 + }
  213 +
  214 + return claims, nil
  215 +}
  216 +
191 217 // randomSalt returns a string of 32 random characters.
192 218 func randomSalt() (s string, err error) {
193 219 const saltSize = 32
... ...