jwt/map_claims.go

121 lines
2.9 KiB
Go
Raw Normal View History

2016-04-13 02:22:14 +03:00
package jwt
import (
"encoding/json"
"errors"
// "fmt"
2016-04-13 02:22:14 +03:00
)
// MapClaims is a claims type that uses the map[string]interface{} for JSON decoding.
2016-04-13 02:22:14 +03:00
// This is the default claims type if you don't supply one
type MapClaims map[string]interface{}
// VerifyAudience Compares the aud claim against cmp.
2016-04-13 02:22:14 +03:00
// If required is false, this method will return true if the value matches or is unset
func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
var aud []string
switch v := m["aud"].(type) {
case string:
aud = append(aud, v)
case []string:
aud = v
case []interface{}:
for _, a := range v {
vs, ok := a.(string)
if !ok {
return false
}
aud = append(aud, vs)
}
}
2016-04-13 02:22:14 +03:00
return verifyAud(aud, cmp, req)
}
// VerifyExpiresAt compares the exp claim against cmp (cmp <= exp).
// If req is false, it will return true, if exp is unset.
2016-04-13 02:22:14 +03:00
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
exp, ok := m["exp"]
if !ok {
return !req
}
switch expType := exp.(type) {
2016-04-13 02:22:14 +03:00
case float64:
return verifyExp(int64(expType), cmp, req)
2016-04-13 02:22:14 +03:00
case json.Number:
v, _ := expType.Int64()
2016-04-13 02:22:14 +03:00
return verifyExp(v, cmp, req)
}
return false
2016-04-13 02:22:14 +03:00
}
// VerifyIssuedAt compares the exp claim against cmp (cmp >= iat).
// If req is false, it will return true, if iat is unset.
2016-04-13 02:22:14 +03:00
func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
iat, ok := m["iat"]
if !ok {
return !req
}
switch iatType := iat.(type) {
2016-04-13 02:22:14 +03:00
case float64:
return verifyIat(int64(iatType), cmp, req)
2016-04-13 02:22:14 +03:00
case json.Number:
v, _ := iatType.Int64()
2016-04-13 02:22:14 +03:00
return verifyIat(v, cmp, req)
}
return false
2016-04-13 02:22:14 +03:00
}
// VerifyIssuer compares the iss claim against cmp.
2016-04-13 02:22:14 +03:00
// If required is false, this method will return true if the value matches or is unset
func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
iss, _ := m["iss"].(string)
return verifyIss(iss, cmp, req)
}
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
// If req is false, it will return true, if nbf is unset.
2016-04-13 02:22:14 +03:00
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
nbf, ok := m["nbf"]
if !ok {
return !req
}
switch nbfType := nbf.(type) {
2016-04-13 02:22:14 +03:00
case float64:
return verifyNbf(int64(nbfType), cmp, req)
2016-04-13 02:22:14 +03:00
case json.Number:
v, _ := nbfType.Int64()
2016-04-13 02:22:14 +03:00
return verifyNbf(v, cmp, req)
}
return false
2016-04-13 02:22:14 +03:00
}
// Valid calidates time based claims "exp, iat, nbf".
2016-04-13 02:22:14 +03:00
// There is no accounting for clock skew.
// As well, if any of the above claims are not in the token, it will still
// be considered a valid claim.
func (m MapClaims) Valid() error {
vErr := new(ValidationError)
now := TimeFunc().Unix()
2021-05-28 02:26:21 +03:00
if !m.VerifyExpiresAt(now, false) {
vErr.Inner = errors.New("Token is expired")
2016-04-13 02:22:14 +03:00
vErr.Errors |= ValidationErrorExpired
}
2021-05-28 02:26:21 +03:00
if !m.VerifyIssuedAt(now, false) {
vErr.Inner = errors.New("Token used before issued")
2016-04-13 02:22:14 +03:00
vErr.Errors |= ValidationErrorIssuedAt
}
2021-05-28 02:26:21 +03:00
if !m.VerifyNotBefore(now, false) {
vErr.Inner = errors.New("Token is not valid yet")
2016-04-13 02:22:14 +03:00
vErr.Errors |= ValidationErrorNotValidYet
}
if vErr.valid() {
return nil
}
return vErr
}