9 // For a type to be a Claims object, it must just have a Valid method that determines
10 // if the token is invalid for any supported reason
11 type Claims interface {
15 // Structured version of Claims Section, as referenced at
16 // https://tools.ietf.org/html/rfc7519#section-4.1
17 // See examples for how to use this with your own claim types
18 type StandardClaims struct {
19 Audience string `json:"aud,omitempty"`
20 ExpiresAt int64 `json:"exp,omitempty"`
21 Id string `json:"jti,omitempty"`
22 IssuedAt int64 `json:"iat,omitempty"`
23 Issuer string `json:"iss,omitempty"`
24 NotBefore int64 `json:"nbf,omitempty"`
25 Subject string `json:"sub,omitempty"`
28 // Validates time based claims "exp, iat, nbf".
29 // There is no accounting for clock skew.
30 // As well, if any of the above claims are not in the token, it will still
31 // be considered a valid claim.
32 func (c StandardClaims) Valid() error {
33 vErr := new(ValidationError)
34 now := TimeFunc().Unix()
36 // The claims below are optional, by default, so if they are set to the
37 // default value in Go, let's not fail the verification for them.
38 if c.VerifyExpiresAt(now, false) == false {
39 delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
40 vErr.Inner = fmt.Errorf("token is expired by %v", delta)
41 vErr.Errors |= ValidationErrorExpired
44 if c.VerifyIssuedAt(now, false) == false {
45 vErr.Inner = fmt.Errorf("Token used before issued")
46 vErr.Errors |= ValidationErrorIssuedAt
49 if c.VerifyNotBefore(now, false) == false {
50 vErr.Inner = fmt.Errorf("token is not valid yet")
51 vErr.Errors |= ValidationErrorNotValidYet
61 // Compares the aud claim against cmp.
62 // If required is false, this method will return true if the value matches or is unset
63 func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
64 return verifyAud(c.Audience, cmp, req)
67 // Compares the exp claim against cmp.
68 // If required is false, this method will return true if the value matches or is unset
69 func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
70 return verifyExp(c.ExpiresAt, cmp, req)
73 // Compares the iat claim against cmp.
74 // If required is false, this method will return true if the value matches or is unset
75 func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
76 return verifyIat(c.IssuedAt, cmp, req)
79 // Compares the iss claim against cmp.
80 // If required is false, this method will return true if the value matches or is unset
81 func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
82 return verifyIss(c.Issuer, cmp, req)
85 // Compares the nbf claim against cmp.
86 // If required is false, this method will return true if the value matches or is unset
87 func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
88 return verifyNbf(c.NotBefore, cmp, req)
93 func verifyAud(aud string, cmp string, required bool) bool {
97 if subtle.ConstantTimeCompare([]byte(aud), []byte(cmp)) != 0 {
104 func verifyExp(exp int64, now int64, required bool) bool {
111 func verifyIat(iat int64, now int64, required bool) bool {
118 func verifyIss(iss string, cmp string, required bool) bool {
122 if subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0 {
129 func verifyNbf(nbf int64, now int64, required bool) bool {