Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
411 lines (338 loc) · 11.4 KB
/
Copy patherrors.go
File metadata and controls
411 lines (338 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package jwt
import (
"errors"
"fmt"
"time"
)
// errUnknownPayloadType is the sentinel for unknown payload type errors.
var errUnknownPayloadType = errors.New(`unknown payload type (payload is not JWT?)`)
// UnknownPayloadTypeError returns the opaque error value that is returned when
// jwt.Parse fails due to not being able to deduce the format of
// the incoming buffer.
//
// This value should only be used for comparison using errors.Is().
func UnknownPayloadTypeError() error {
return errUnknownPayloadType
}
//-------------------------------------------------------------------
// ClaimNotFoundError
//-------------------------------------------------------------------
// ClaimNotFoundError is returned when jwt.Get fails to find the requested claim.
type ClaimNotFoundError struct {
// Name is the name of the claim that was not found.
Name string
}
func (e ClaimNotFoundError) Error() string {
return fmt.Sprintf(`field "%s" not found`, e.Name)
}
func (e ClaimNotFoundError) Is(target error) bool {
_, ok := target.(ClaimNotFoundError)
return ok
}
//-------------------------------------------------------------------
// ClaimTypeMismatchError
//-------------------------------------------------------------------
// ClaimTypeMismatchError is returned when jwt.Get finds the requested
// claim but the stored value cannot be converted to the requested type.
//
// Callers that need to distinguish "claim missing" from "claim present
// but wrong type" should use errors.Is with ClaimNotFoundError{} /
// ClaimTypeMismatchError{}, or errors.AsType to recover the Name, Got,
// and Want fields.
type ClaimTypeMismatchError struct {
// Name is the name of the claim whose value could not be converted.
Name string
// Got is the value currently stored under the claim. Use %T to
// inspect its concrete type.
Got any
// Want is a zero value of the requested type T. Use %T to inspect
// its concrete type.
Want any
}
func (e ClaimTypeMismatchError) Error() string {
return fmt.Sprintf(`field "%s" is %T, not %T`, e.Name, e.Got, e.Want)
}
func (e ClaimTypeMismatchError) Is(target error) bool {
_, ok := target.(ClaimTypeMismatchError)
return ok
}
//-------------------------------------------------------------------
// ClaimAssignmentFailedError
//-------------------------------------------------------------------
// ClaimAssignmentFailedError is returned when jwt.Get fails to assign
// the value to the destination.
type ClaimAssignmentFailedError struct {
// Err is the underlying error.
Err error
}
func (e ClaimAssignmentFailedError) Error() string {
return fmt.Sprintf(`failed to assign value to dst: %s`, e.Err.Error())
}
func (e ClaimAssignmentFailedError) Unwrap() error {
return e.Err
}
func (e ClaimAssignmentFailedError) Is(target error) bool {
_, ok := target.(ClaimAssignmentFailedError)
return ok
}
//-------------------------------------------------------------------
// ParseError
//-------------------------------------------------------------------
// ParseError is returned when jwt.Parse fails.
type ParseError struct {
error
}
func (e ParseError) Unwrap() error {
return e.error
}
func (ParseError) Is(err error) bool {
_, ok := err.(ParseError)
return ok
}
func parseErrorf(prefix, f string, args ...any) error {
return ParseError{fmt.Errorf(prefix+": "+f, args...)}
}
//-------------------------------------------------------------------
// ValidationError
//-------------------------------------------------------------------
// ValidationError is the blanket error returned by jwt.Validate.
// It wraps the specific validation failure(s).
type ValidationError struct {
error
}
func (ValidationError) Is(err error) bool {
_, ok := err.(ValidationError)
return ok
}
func (err ValidationError) Unwrap() error {
return err.error
}
func validateErrorf(f string, args ...any) error {
return ValidationError{fmt.Errorf(`jwt.Validate: `+f, args...)}
}
func validateErrorJoin(errs ...error) error {
return ValidationError{fmt.Errorf("jwt.Validate: validation failed: %w", errors.Join(errs...))}
}
//-------------------------------------------------------------------
// InvalidIssuerError
//-------------------------------------------------------------------
// InvalidIssuerError is returned when the iss claim is not satisfied.
type InvalidIssuerError struct {
error
}
func (err InvalidIssuerError) Is(target error) bool {
_, ok := target.(InvalidIssuerError)
return ok
}
func (err InvalidIssuerError) Unwrap() error {
return err.error
}
func issuerErrorf(f string, args ...any) error {
return InvalidIssuerError{fmt.Errorf(`"iss" not satisfied: `+f, args...)}
}
//-------------------------------------------------------------------
// TokenExpiredError
//-------------------------------------------------------------------
// TokenExpiredError is returned when the exp claim is not satisfied.
// The structured fields allow callers to inspect what values were
// compared without re-parsing the token.
type TokenExpiredError struct {
error
// Expiration is the token's exp claim value (after truncation).
Expiration time.Time
// Now is the time used for comparison (after truncation).
Now time.Time
// Skew is the acceptable skew duration.
Skew time.Duration
}
func (err TokenExpiredError) Is(target error) bool {
_, ok := target.(TokenExpiredError)
return ok
}
func (err TokenExpiredError) Unwrap() error {
return err.error
}
func newTokenExpiredError(expiration, now time.Time, skew time.Duration) error {
return TokenExpiredError{
error: errors.New(`"exp" not satisfied: token is expired`),
Expiration: expiration,
Now: now,
Skew: skew,
}
}
//-------------------------------------------------------------------
// InvalidIssuedAtError
//-------------------------------------------------------------------
// InvalidIssuedAtError is returned when the iat claim is not satisfied.
type InvalidIssuedAtError struct {
error
// IssuedAt is the token's iat claim value (after truncation).
IssuedAt time.Time
// Now is the time used for comparison (after truncation).
Now time.Time
// Skew is the acceptable skew duration.
Skew time.Duration
}
func (err InvalidIssuedAtError) Is(target error) bool {
_, ok := target.(InvalidIssuedAtError)
return ok
}
func (err InvalidIssuedAtError) Unwrap() error {
return err.error
}
func newInvalidIssuedAtError(issuedAt, now time.Time, skew time.Duration) error {
return InvalidIssuedAtError{
error: errors.New(`"iat" not satisfied`),
IssuedAt: issuedAt,
Now: now,
Skew: skew,
}
}
//-------------------------------------------------------------------
// TokenNotYetValidError
//-------------------------------------------------------------------
// TokenNotYetValidError is returned when the nbf claim is not satisfied.
type TokenNotYetValidError struct {
error
// NotBefore is the token's nbf claim value (after truncation).
NotBefore time.Time
// Now is the time used for comparison (after truncation).
Now time.Time
// Skew is the acceptable skew duration.
Skew time.Duration
}
func (err TokenNotYetValidError) Is(target error) bool {
_, ok := target.(TokenNotYetValidError)
return ok
}
func (err TokenNotYetValidError) Unwrap() error {
return err.error
}
func newTokenNotYetValidError(notBefore, now time.Time, skew time.Duration) error {
return TokenNotYetValidError{
error: errors.New(`"nbf" not satisfied: token is not yet valid`),
NotBefore: notBefore,
Now: now,
Skew: skew,
}
}
//-------------------------------------------------------------------
// InvalidAudienceError
//-------------------------------------------------------------------
// InvalidAudienceError is returned when the aud claim is not satisfied.
type InvalidAudienceError struct {
error
}
func (err InvalidAudienceError) Is(target error) bool {
_, ok := target.(InvalidAudienceError)
return ok
}
func (err InvalidAudienceError) Unwrap() error {
return err.error
}
func audienceErrorf(f string, args ...any) error {
return InvalidAudienceError{fmt.Errorf(`"aud" not satisfied: `+f, args...)}
}
//-------------------------------------------------------------------
// MissingRequiredClaimError
//-------------------------------------------------------------------
// MissingRequiredClaimError is returned when a required claim is missing.
type MissingRequiredClaimError struct {
error
// Claim is the name of the missing required claim.
Claim string
}
func (err MissingRequiredClaimError) Is(target error) bool {
switch target.(type) {
case MissingRequiredClaimError, *MissingRequiredClaimError:
return true
default:
return false
}
}
func (err MissingRequiredClaimError) Unwrap() error {
return err.error
}
func missingRequiredClaimErrorf(name string) error {
return MissingRequiredClaimError{Claim: name, error: fmt.Errorf(`required claim "%s" is missing`, name)}
}
//-------------------------------------------------------------------
// ClaimValidationError
//-------------------------------------------------------------------
// ClaimValidationError is returned when a generic claim validator
// (ClaimValueIs, ClaimContainsString) detects a mismatch.
//
// The Error() message intentionally omits the Expected and Actual
// values so that arbitrary claim payloads do not end up in log output
// via "%v"/"%s" formatting. Callers that need to inspect the mismatched
// values can access the fields directly, but should treat them as
// potentially sensitive (PII, secrets, etc.) and avoid logging them
// verbatim.
type ClaimValidationError struct {
error
// Claim is the name of the claim that failed validation.
Claim string
// Expected is the value the validator expected. May contain
// sensitive data; see the type-level godoc.
Expected any
// Actual is the value found in the token. May contain sensitive
// data (PII, secrets, etc.); see the type-level godoc.
Actual any
}
func (err ClaimValidationError) Is(target error) bool {
_, ok := target.(ClaimValidationError)
return ok
}
func (err ClaimValidationError) Unwrap() error {
return err.error
}
func newClaimValidationError(claim string, expected, actual any, msg string) error {
return ClaimValidationError{
error: errors.New(msg),
Claim: claim,
Expected: expected,
Actual: actual,
}
}
//-------------------------------------------------------------------
// TimeDeltaError
//-------------------------------------------------------------------
// TimeDeltaError is returned when a time delta validator
// (MaxDeltaIs, MinDeltaIs) detects that the delta between two
// claims is out of range.
type TimeDeltaError struct {
error
// Claim1 is the name of the first claim (or "" for current time).
Claim1 string
// Claim2 is the name of the second claim (or "" for current time).
Claim2 string
// Value1 is the resolved time value for Claim1.
Value1 time.Time
// Value2 is the resolved time value for Claim2.
Value2 time.Time
// Delta is the actual duration between Value1 and Value2.
Delta time.Duration
// Limit is the threshold duration that was exceeded or not met.
Limit time.Duration
// Skew is the acceptable skew duration.
Skew time.Duration
}
func (err TimeDeltaError) Is(target error) bool {
_, ok := target.(TimeDeltaError)
return ok
}
func (err TimeDeltaError) Unwrap() error {
return err.error
}
func newTimeDeltaError(msg, c1, c2 string, v1, v2 time.Time, delta, limit, skew time.Duration) error {
return TimeDeltaError{
error: errors.New(msg),
Claim1: c1,
Claim2: c2,
Value1: v1,
Value2: v2,
Delta: delta,
Limit: limit,
Skew: skew,
}
}