2021-08-12 19:03:24 +00:00
|
|
|
package jwt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
// "fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Claims type that uses the map[string]interface{} for JSON decoding
|
|
|
|
// This is the default claims type if you don't supply one
|
|
|
|
type MapClaims map[string]interface{}
|
|
|
|
|
2021-09-08 19:20:06 +00:00
|
|
|
// VerifyAudience Compares the aud claim against cmp.
|
2021-08-12 19:03:24 +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 {
|
2021-09-08 19:20:06 +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)
|
|
|
|
}
|
|
|
|
}
|
2021-08-12 19:03:24 +00:00
|
|
|
return verifyAud(aud, cmp, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compares the exp claim against cmp.
|
|
|
|
// If required is false, this method will return true if the value matches or is unset
|
|
|
|
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
|
2021-09-10 12:42:14 +00:00
|
|
|
exp, ok := m["exp"]
|
|
|
|
if !ok {
|
|
|
|
return !req
|
|
|
|
}
|
|
|
|
switch expType := exp.(type) {
|
2021-08-12 19:03:24 +00:00
|
|
|
case float64:
|
2021-09-10 12:42:14 +00:00
|
|
|
return verifyExp(int64(expType), cmp, req)
|
2021-08-12 19:03:24 +00:00
|
|
|
case json.Number:
|
2021-09-10 12:42:14 +00:00
|
|
|
v, _ := expType.Int64()
|
2021-08-12 19:03:24 +00:00
|
|
|
return verifyExp(v, cmp, req)
|
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
return false
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compares the iat claim against cmp.
|
|
|
|
// If required is false, this method will return true if the value matches or is unset
|
|
|
|
func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
|
2021-09-10 12:42:14 +00:00
|
|
|
iat, ok := m["iat"]
|
|
|
|
if !ok {
|
|
|
|
return !req
|
|
|
|
}
|
|
|
|
switch iatType := iat.(type) {
|
2021-08-12 19:03:24 +00:00
|
|
|
case float64:
|
2021-09-10 12:42:14 +00:00
|
|
|
return verifyIat(int64(iatType), cmp, req)
|
2021-08-12 19:03:24 +00:00
|
|
|
case json.Number:
|
2021-09-10 12:42:14 +00:00
|
|
|
v, _ := iatType.Int64()
|
2021-08-12 19:03:24 +00:00
|
|
|
return verifyIat(v, cmp, req)
|
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
return false
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compares the nbf claim against cmp.
|
|
|
|
// If required is false, this method will return true if the value matches or is unset
|
|
|
|
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
|
2021-09-10 12:42:14 +00:00
|
|
|
nbf, ok := m["nbf"]
|
|
|
|
if !ok {
|
|
|
|
return !req
|
|
|
|
}
|
|
|
|
switch nbfType := nbf.(type) {
|
2021-08-12 19:03:24 +00:00
|
|
|
case float64:
|
2021-09-10 12:42:14 +00:00
|
|
|
return verifyNbf(int64(nbfType), cmp, req)
|
2021-08-12 19:03:24 +00:00
|
|
|
case json.Number:
|
2021-09-10 12:42:14 +00:00
|
|
|
v, _ := nbfType.Int64()
|
2021-08-12 19:03:24 +00:00
|
|
|
return verifyNbf(v, cmp, req)
|
|
|
|
}
|
2021-09-10 12:42:14 +00:00
|
|
|
return false
|
2021-08-12 19:03:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validates time based claims "exp, iat, nbf".
|
|
|
|
// 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-09-08 19:20:06 +00:00
|
|
|
if !m.VerifyExpiresAt(now, false) {
|
2021-08-12 19:03:24 +00:00
|
|
|
vErr.Inner = errors.New("Token is expired")
|
|
|
|
vErr.Errors |= ValidationErrorExpired
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:20:06 +00:00
|
|
|
if !m.VerifyIssuedAt(now, false) {
|
2021-08-12 19:03:24 +00:00
|
|
|
vErr.Inner = errors.New("Token used before issued")
|
|
|
|
vErr.Errors |= ValidationErrorIssuedAt
|
|
|
|
}
|
|
|
|
|
2021-09-08 19:20:06 +00:00
|
|
|
if !m.VerifyNotBefore(now, false) {
|
2021-08-12 19:03:24 +00:00
|
|
|
vErr.Inner = errors.New("Token is not valid yet")
|
|
|
|
vErr.Errors |= ValidationErrorNotValidYet
|
|
|
|
}
|
|
|
|
|
|
|
|
if vErr.valid() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return vErr
|
|
|
|
}
|