VerifyToken checks if the token is valid and returns the token claims
(token string)
| 8 | |
| 9 | // VerifyToken checks if the token is valid and returns the token claims |
| 10 | func (m *Module) VerifyToken(token string) (map[string]interface{}, error) { |
| 11 | m.lock.RLock() |
| 12 | defer m.lock.RUnlock() |
| 13 | if m.config.IsDev { |
| 14 | return nil, nil |
| 15 | } |
| 16 | // Parse the JWT token |
| 17 | tokenObj, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { |
| 18 | // Don't forget to validate the alg is what you expect it to be |
| 19 | if token.Method.Alg() != jwt.SigningMethodHS256.Alg() { |
| 20 | return nil, errors.New("invalid signing method") |
| 21 | } |
| 22 | |
| 23 | // Return the key |
| 24 | return []byte(m.config.Secret), nil |
| 25 | }) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | |
| 30 | // Get the claims |
| 31 | if claims, ok := tokenObj.Claims.(jwt.MapClaims); ok && tokenObj.Valid { |
| 32 | tokenClaims := make(map[string]interface{}, len(claims)) |
| 33 | for key, val := range claims { |
| 34 | tokenClaims[key] = val |
| 35 | } |
| 36 | |
| 37 | return tokenClaims, nil |
| 38 | } |
| 39 | |
| 40 | return nil, errors.New("token could not be verified") |
| 41 | |
| 42 | } |
no outgoing calls
no test coverage detected