2017-04-14 01:05:38 +00:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2022-01-24 10:30:22 +00:00
|
|
|
"time"
|
2017-04-14 01:05:38 +00:00
|
|
|
// "fmt"
|
|
|
|
)
|
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
// MapClaims is a claims type that uses the map[string]interface{} for JSON decoding.
|
2017-04-14 01:05:38 +00:00
|
|
|
// This is the default claims type if you don't supply one
|
|
|
|
type MapClaims map[string]interface{}
|
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
// VerifyAudience Compares the aud claim against cmp.
|
2017-04-14 01:05:38 +00: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 {
|
2020-04-01 15:47:41 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2017-04-14 01:05:38 +00:00
|
|
|
return verifyAud(aud, cmp, req)
|
|
|
|
}
|
|
|
|
|
2022-01-24 10:30:22 +00:00
|
|
|
// VerifyExpiresAt compares the exp claim against cmp (cmp <= exp).
|
|
|
|
// If req is false, it will return true, if exp is unset.
|
2017-04-14 01:05:38 +00:00
|
|
|
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
|
2022-01-24 10:30:22 +00:00
|
|
|
cmpTime := time.Unix(cmp, 0)
|
|
|
|
|
|
|
|
v, ok := m["exp"]
|
2020-04-01 15:47:41 +00:00
|
|
|
if !ok {
|
|
|
|
return !req
|
|
|
|
}
|
2022-01-24 10:30:22 +00:00
|
|
|
|
|
|
|
switch exp := v.(type) {
|
2017-04-14 01:05:38 +00:00
|
|
|
case float64:
|
2022-01-24 10:30:22 +00:00
|
|
|
if exp == 0 {
|
|
|
|
return verifyExp(nil, cmpTime, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return verifyExp(&newNumericDateFromSeconds(exp).Time, cmpTime, req)
|
2017-04-14 01:05:38 +00:00
|
|
|
case json.Number:
|
2022-01-24 10:30:22 +00:00
|
|
|
v, _ := exp.Float64()
|
|
|
|
|
|
|
|
return verifyExp(&newNumericDateFromSeconds(v).Time, cmpTime, req)
|
2017-04-14 01:05:38 +00:00
|
|
|
}
|
2022-01-24 10:30:22 +00:00
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
return false
|
2017-04-14 01:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:30:22 +00:00
|
|
|
// VerifyIssuedAt compares the exp claim against cmp (cmp >= iat).
|
|
|
|
// If req is false, it will return true, if iat is unset.
|
2017-04-14 01:05:38 +00:00
|
|
|
func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
|
2022-01-24 10:30:22 +00:00
|
|
|
cmpTime := time.Unix(cmp, 0)
|
|
|
|
|
|
|
|
v, ok := m["iat"]
|
2020-04-01 15:47:41 +00:00
|
|
|
if !ok {
|
|
|
|
return !req
|
|
|
|
}
|
2022-01-24 10:30:22 +00:00
|
|
|
|
|
|
|
switch iat := v.(type) {
|
2017-04-14 01:05:38 +00:00
|
|
|
case float64:
|
2022-01-24 10:30:22 +00:00
|
|
|
if iat == 0 {
|
|
|
|
return verifyIat(nil, cmpTime, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return verifyIat(&newNumericDateFromSeconds(iat).Time, cmpTime, req)
|
2017-04-14 01:05:38 +00:00
|
|
|
case json.Number:
|
2022-01-24 10:30:22 +00:00
|
|
|
v, _ := iat.Float64()
|
|
|
|
|
|
|
|
return verifyIat(&newNumericDateFromSeconds(v).Time, cmpTime, req)
|
2017-04-14 01:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:30:22 +00:00
|
|
|
return false
|
2017-04-14 01:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:30:22 +00:00
|
|
|
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
|
|
|
|
// If req is false, it will return true, if nbf is unset.
|
2017-04-14 01:05:38 +00:00
|
|
|
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
|
2022-01-24 10:30:22 +00:00
|
|
|
cmpTime := time.Unix(cmp, 0)
|
|
|
|
|
|
|
|
v, ok := m["nbf"]
|
2020-04-01 15:47:41 +00:00
|
|
|
if !ok {
|
|
|
|
return !req
|
|
|
|
}
|
2022-01-24 10:30:22 +00:00
|
|
|
|
|
|
|
switch nbf := v.(type) {
|
2017-04-14 01:05:38 +00:00
|
|
|
case float64:
|
2022-01-24 10:30:22 +00:00
|
|
|
if nbf == 0 {
|
|
|
|
return verifyNbf(nil, cmpTime, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return verifyNbf(&newNumericDateFromSeconds(nbf).Time, cmpTime, req)
|
2017-04-14 01:05:38 +00:00
|
|
|
case json.Number:
|
2022-01-24 10:30:22 +00:00
|
|
|
v, _ := nbf.Float64()
|
|
|
|
|
|
|
|
return verifyNbf(&newNumericDateFromSeconds(v).Time, cmpTime, req)
|
2017-04-14 01:05:38 +00:00
|
|
|
}
|
2022-01-24 10:30:22 +00:00
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
return false
|
2017-04-14 01:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 10:30:22 +00:00
|
|
|
// VerifyIssuer compares the iss claim against cmp.
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Valid validates time based claims "exp, iat, nbf".
|
2017-04-14 01:05:38 +00: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()
|
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
if !m.VerifyExpiresAt(now, false) {
|
2020-02-21 03:58:17 +00:00
|
|
|
// TODO(oxisto): this should be replaced with ErrTokenExpired
|
2017-04-14 01:05:38 +00:00
|
|
|
vErr.Inner = errors.New("Token is expired")
|
|
|
|
vErr.Errors |= ValidationErrorExpired
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
if !m.VerifyIssuedAt(now, false) {
|
2020-02-21 03:58:17 +00:00
|
|
|
// TODO(oxisto): this should be replaced with ErrTokenUsedBeforeIssued
|
2017-04-14 01:05:38 +00:00
|
|
|
vErr.Inner = errors.New("Token used before issued")
|
|
|
|
vErr.Errors |= ValidationErrorIssuedAt
|
|
|
|
}
|
|
|
|
|
2020-04-01 15:47:41 +00:00
|
|
|
if !m.VerifyNotBefore(now, false) {
|
2020-02-21 03:58:17 +00:00
|
|
|
// TODO(oxisto): this should be replaced with ErrTokenNotValidYet
|
2017-04-14 01:05:38 +00:00
|
|
|
vErr.Inner = errors.New("Token is not valid yet")
|
|
|
|
vErr.Errors |= ValidationErrorNotValidYet
|
|
|
|
}
|
|
|
|
|
|
|
|
if vErr.valid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return vErr
|
|
|
|
}
|