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

240 lines
3.7 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package jwt
import (
"encoding/json"
"errors"
"time"
)
// MapClaims is a 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
}
// VerifyExpiresAt compares the exp claim against cmp (cmp <= exp).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if exp 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
cmpTime := time.Unix(cmp, 0)
v, 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
}
switch exp := v.(type) {
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
if exp == 0 {
2024-02-18 10:42:21 +00:00
return verifyExp(nil, cmpTime, req)
2024-02-18 10:42:21 +00:00
}
return verifyExp(&newNumericDateFromSeconds(exp).Time, cmpTime, req)
2024-02-18 10:42:21 +00:00
case json.Number:
2024-02-18 10:42:21 +00:00
v, _ := exp.Float64()
return verifyExp(&newNumericDateFromSeconds(v).Time, cmpTime, req)
2024-02-18 10:42:21 +00:00
}
return false
2024-02-18 10:42:21 +00:00
}
// VerifyIssuedAt compares the exp claim against cmp (cmp >= iat).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if iat 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
cmpTime := time.Unix(cmp, 0)
v, 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
}
switch iat := v.(type) {
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
if iat == 0 {
2024-02-18 10:42:21 +00:00
return verifyIat(nil, cmpTime, req)
2024-02-18 10:42:21 +00:00
}
return verifyIat(&newNumericDateFromSeconds(iat).Time, cmpTime, req)
2024-02-18 10:42:21 +00:00
case json.Number:
2024-02-18 10:42:21 +00:00
v, _ := iat.Float64()
return verifyIat(&newNumericDateFromSeconds(v).Time, cmpTime, req)
2024-02-18 10:42:21 +00:00
}
return false
2024-02-18 10:42:21 +00:00
}
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
2024-02-18 10:42:21 +00:00
// If req is false, it will return true, if nbf 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
cmpTime := time.Unix(cmp, 0)
v, 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
}
switch nbf := v.(type) {
2024-02-18 10:42:21 +00:00
case float64:
2024-02-18 10:42:21 +00:00
if nbf == 0 {
2024-02-18 10:42:21 +00:00
return verifyNbf(nil, cmpTime, req)
2024-02-18 10:42:21 +00:00
}
return verifyNbf(&newNumericDateFromSeconds(nbf).Time, cmpTime, req)
2024-02-18 10:42:21 +00:00
case json.Number:
2024-02-18 10:42:21 +00:00
v, _ := nbf.Float64()
return verifyNbf(&newNumericDateFromSeconds(v).Time, cmpTime, req)
2024-02-18 10:42:21 +00:00
}
return false
2024-02-18 10:42:21 +00:00
}
// VerifyIssuer 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
}
// Valid 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
// TODO(oxisto): this should be replaced with ErrTokenExpired
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
// TODO(oxisto): this should be replaced with ErrTokenUsedBeforeIssued
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
// TODO(oxisto): this should be replaced with ErrTokenNotValidYet
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
}