forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/golang-jwt/jwt/map_claims.go

209 lines
3.0 KiB
Go
Raw Normal View History

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