|
1 | 1 | package jwt |
2 | 2 |
|
3 | | -import ( |
4 | | - "crypto/subtle" |
5 | | - "fmt" |
6 | | - "time" |
7 | | -) |
8 | | - |
9 | | -// Claims must just have a Valid method that determines |
10 | | -// if the token is invalid for any supported reason |
| 3 | +// Claims represent any form of a JWT Claims Set according to |
| 4 | +// https://datatracker.ietf.org/doc/html/rfc7519#section-4. In order to have a |
| 5 | +// common basis for validation, it is required that an implementation is able to |
| 6 | +// supply at least the claim names provided in |
| 7 | +// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1 namely `exp`, |
| 8 | +// `iat`, `nbf`, `iss` and `aud`. |
11 | 9 | type Claims interface { |
12 | | - Valid() error |
13 | | -} |
14 | | - |
15 | | -// RegisteredClaims are a structured version of the JWT Claims Set, |
16 | | -// restricted to Registered Claim Names, as referenced at |
17 | | -// https://datatracker.ietf.org/doc/html/rfc7519#section-4.1 |
18 | | -// |
19 | | -// This type can be used on its own, but then additional private and |
20 | | -// public claims embedded in the JWT will not be parsed. The typical usecase |
21 | | -// therefore is to embedded this in a user-defined claim type. |
22 | | -// |
23 | | -// See examples for how to use this with your own claim types. |
24 | | -type RegisteredClaims struct { |
25 | | - // the `iss` (Issuer) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.1 |
26 | | - Issuer string `json:"iss,omitempty"` |
27 | | - |
28 | | - // the `sub` (Subject) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.2 |
29 | | - Subject string `json:"sub,omitempty"` |
30 | | - |
31 | | - // the `aud` (Audience) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.3 |
32 | | - Audience ClaimStrings `json:"aud,omitempty"` |
33 | | - |
34 | | - // the `exp` (Expiration Time) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.4 |
35 | | - ExpiresAt *NumericDate `json:"exp,omitempty"` |
36 | | - |
37 | | - // the `nbf` (Not Before) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.5 |
38 | | - NotBefore *NumericDate `json:"nbf,omitempty"` |
39 | | - |
40 | | - // the `iat` (Issued At) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.6 |
41 | | - IssuedAt *NumericDate `json:"iat,omitempty"` |
42 | | - |
43 | | - // the `jti` (JWT ID) claim. See https://datatracker.ietf.org/doc/html/rfc7519#section-4.1.7 |
44 | | - ID string `json:"jti,omitempty"` |
45 | | -} |
46 | | - |
47 | | -// Valid validates time based claims "exp, iat, nbf". |
48 | | -// There is no accounting for clock skew. |
49 | | -// As well, if any of the above claims are not in the token, it will still |
50 | | -// be considered a valid claim. |
51 | | -func (c RegisteredClaims) Valid() error { |
52 | | - vErr := new(ValidationError) |
53 | | - now := TimeFunc() |
54 | | - |
55 | | - // The claims below are optional, by default, so if they are set to the |
56 | | - // default value in Go, let's not fail the verification for them. |
57 | | - if !c.VerifyExpiresAt(now, false) { |
58 | | - delta := now.Sub(c.ExpiresAt.Time) |
59 | | - vErr.Inner = fmt.Errorf("%s by %s", ErrTokenExpired, delta) |
60 | | - vErr.Errors |= ValidationErrorExpired |
61 | | - } |
62 | | - |
63 | | - if !c.VerifyIssuedAt(now, false) { |
64 | | - vErr.Inner = ErrTokenUsedBeforeIssued |
65 | | - vErr.Errors |= ValidationErrorIssuedAt |
66 | | - } |
67 | | - |
68 | | - if !c.VerifyNotBefore(now, false) { |
69 | | - vErr.Inner = ErrTokenNotValidYet |
70 | | - vErr.Errors |= ValidationErrorNotValidYet |
71 | | - } |
72 | | - |
73 | | - if vErr.valid() { |
74 | | - return nil |
75 | | - } |
76 | | - |
77 | | - return vErr |
78 | | -} |
79 | | - |
80 | | -// VerifyAudience compares the aud claim against cmp. |
81 | | -// If required is false, this method will return true if the value matches or is unset |
82 | | -func (c *RegisteredClaims) VerifyAudience(cmp string, req bool) bool { |
83 | | - return verifyAud(c.Audience, cmp, req) |
84 | | -} |
85 | | - |
86 | | -// VerifyExpiresAt compares the exp claim against cmp (cmp < exp). |
87 | | -// If req is false, it will return true, if exp is unset. |
88 | | -func (c *RegisteredClaims) VerifyExpiresAt(cmp time.Time, req bool) bool { |
89 | | - if c.ExpiresAt == nil { |
90 | | - return verifyExp(nil, cmp, req) |
91 | | - } |
92 | | - |
93 | | - return verifyExp(&c.ExpiresAt.Time, cmp, req) |
94 | | -} |
95 | | - |
96 | | -// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat). |
97 | | -// If req is false, it will return true, if iat is unset. |
98 | | -func (c *RegisteredClaims) VerifyIssuedAt(cmp time.Time, req bool) bool { |
99 | | - if c.IssuedAt == nil { |
100 | | - return verifyIat(nil, cmp, req) |
101 | | - } |
102 | | - |
103 | | - return verifyIat(&c.IssuedAt.Time, cmp, req) |
104 | | -} |
105 | | - |
106 | | -// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf). |
107 | | -// If req is false, it will return true, if nbf is unset. |
108 | | -func (c *RegisteredClaims) VerifyNotBefore(cmp time.Time, req bool) bool { |
109 | | - if c.NotBefore == nil { |
110 | | - return verifyNbf(nil, cmp, req) |
111 | | - } |
112 | | - |
113 | | - return verifyNbf(&c.NotBefore.Time, cmp, req) |
114 | | -} |
115 | | - |
116 | | -// VerifyIssuer compares the iss claim against cmp. |
117 | | -// If required is false, this method will return true if the value matches or is unset |
118 | | -func (c *RegisteredClaims) VerifyIssuer(cmp string, req bool) bool { |
119 | | - return verifyIss(c.Issuer, cmp, req) |
120 | | -} |
121 | | - |
122 | | -// ----- helpers |
123 | | - |
124 | | -func verifyAud(aud []string, cmp string, required bool) bool { |
125 | | - if len(aud) == 0 { |
126 | | - return !required |
127 | | - } |
128 | | - // use a var here to keep constant time compare when looping over a number of claims |
129 | | - result := false |
130 | | - |
131 | | - var stringClaims string |
132 | | - for _, a := range aud { |
133 | | - if subtle.ConstantTimeCompare([]byte(a), []byte(cmp)) != 0 { |
134 | | - result = true |
135 | | - } |
136 | | - stringClaims = stringClaims + a |
137 | | - } |
138 | | - |
139 | | - // case where "" is sent in one or many aud claims |
140 | | - if len(stringClaims) == 0 { |
141 | | - return !required |
142 | | - } |
143 | | - |
144 | | - return result |
145 | | -} |
146 | | - |
147 | | -func verifyExp(exp *time.Time, now time.Time, required bool) bool { |
148 | | - if exp == nil { |
149 | | - return !required |
150 | | - } |
151 | | - return now.Before(*exp) |
152 | | -} |
153 | | - |
154 | | -func verifyIat(iat *time.Time, now time.Time, required bool) bool { |
155 | | - if iat == nil { |
156 | | - return !required |
157 | | - } |
158 | | - return now.After(*iat) || now.Equal(*iat) |
159 | | -} |
160 | | - |
161 | | -func verifyNbf(nbf *time.Time, now time.Time, required bool) bool { |
162 | | - if nbf == nil { |
163 | | - return !required |
164 | | - } |
165 | | - return now.After(*nbf) || now.Equal(*nbf) |
166 | | -} |
167 | | - |
168 | | -func verifyIss(iss string, cmp string, required bool) bool { |
169 | | - if iss == "" { |
170 | | - return !required |
171 | | - } |
172 | | - return subtle.ConstantTimeCompare([]byte(iss), []byte(cmp)) != 0 |
| 10 | + GetExpirationTime() (*NumericDate, error) |
| 11 | + GetIssuedAt() (*NumericDate, error) |
| 12 | + GetNotBefore() (*NumericDate, error) |
| 13 | + GetIssuer() (string, error) |
| 14 | + GetSubject() (string, error) |
| 15 | + GetAudience() (ClaimStrings, error) |
173 | 16 | } |
0 commit comments