|
1 | 1 | package jwt |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "errors" |
5 | | - "fmt" |
| 4 | + jwterrs "github.com/lestrrat-go/jwx/v3/jwt/internal/errors" |
6 | 5 | ) |
7 | 6 |
|
8 | | -var errUnknownPayloadType = errors.New(`unknown payload type (payload is not JWT?)`) |
| 7 | +// ClaimNotFoundError returns the opaque error value that is returned when |
| 8 | +// `jwt.Get` fails to find the requested claim. |
| 9 | +// |
| 10 | +// This value should only be used for comparison using `errors.Is()`. |
| 11 | +func ClaimNotFoundError() error { |
| 12 | + return jwterrs.ErrClaimNotFound |
| 13 | +} |
| 14 | + |
| 15 | +// ClaimAssignmentFailedError returns the opaque error value that is returned |
| 16 | +// when `jwt.Get` fails to assign the value to the destination. For example, |
| 17 | +// this can happen when the value is a string, but you passed a &int as the |
| 18 | +// destination. |
| 19 | +// |
| 20 | +// This value should only be used for comparison using `errors.Is()`. |
| 21 | +func ClaimAssignmentFailedError() error { |
| 22 | + return jwterrs.ErrClaimAssignmentFailed |
| 23 | +} |
9 | 24 |
|
10 | 25 | // UnknownPayloadTypeError returns the opaque error value that is returned when |
11 | 26 | // `jwt.Parse` fails due to not being able to deduce the format of |
12 | | -// the incoming buffer |
| 27 | +// the incoming buffer. |
| 28 | +// |
| 29 | +// This value should only be used for comparison using `errors.Is()`. |
13 | 30 | func UnknownPayloadTypeError() error { |
14 | | - return errUnknownPayloadType |
15 | | -} |
16 | | - |
17 | | -type parseError struct { |
18 | | - error |
| 31 | + return jwterrs.ErrUnknownPayloadType |
19 | 32 | } |
20 | 33 |
|
21 | | -var errDefaultParseError = parseerr(`jwt.Parse`, `unknown error`) |
22 | | - |
23 | | -// ParseError returns an error that can be passed to `errors.Is` to check if the error is a parse error. |
| 34 | +// ParseError returns the opaque error that is returned from jwt.Parse when |
| 35 | +// the input is not a valid JWT. |
| 36 | +// |
| 37 | +// This value should only be used for comparison using `errors.Is()`. |
24 | 38 | func ParseError() error { |
25 | | - return errDefaultParseError |
| 39 | + return jwterrs.ErrParse |
26 | 40 | } |
27 | 41 |
|
28 | | -func (e parseError) Unwrap() error { |
29 | | - return e.error |
30 | | -} |
31 | | - |
32 | | -func (parseError) Is(err error) bool { |
33 | | - _, ok := err.(parseError) |
34 | | - return ok |
35 | | -} |
36 | | - |
37 | | -func parseerr(prefix string, f string, args ...any) error { |
38 | | - return parseError{fmt.Errorf(prefix+": "+f, args...)} |
39 | | -} |
40 | | - |
41 | | -type validationError struct { |
42 | | - error |
43 | | -} |
44 | | - |
45 | | -var errDefaultValidateError = validateerr(`unknown error`) |
46 | | - |
| 42 | +// ValidateError returns the immutable error used for validation errors |
| 43 | +// |
| 44 | +// This value should only be used for comparison using `errors.Is()`. |
47 | 45 | func ValidateError() error { |
48 | | - return errDefaultValidateError |
49 | | -} |
50 | | - |
51 | | -func (validationError) Is(err error) bool { |
52 | | - _, ok := err.(validationError) |
53 | | - return ok |
54 | | -} |
55 | | - |
56 | | -func (err validationError) Unwrap() error { |
57 | | - return err.error |
58 | | -} |
59 | | - |
60 | | -func validateerr(f string, args ...any) error { |
61 | | - return validationError{fmt.Errorf(`jwt.Validate: `+f, args...)} |
62 | | -} |
63 | | - |
64 | | -type invalidIssuerError struct { |
65 | | - error |
| 46 | + return jwterrs.ErrValidateDefault |
66 | 47 | } |
67 | 48 |
|
68 | | -func (err invalidIssuerError) Is(target error) bool { |
69 | | - _, ok := target.(invalidIssuerError) |
70 | | - return ok |
71 | | -} |
72 | | - |
73 | | -func (err invalidIssuerError) Unwrap() error { |
74 | | - return err.error |
75 | | -} |
76 | | - |
77 | | -func issuererr(f string, args ...any) error { |
78 | | - return invalidIssuerError{fmt.Errorf(`"iss" not satisfied: `+f, args...)} |
79 | | -} |
80 | | - |
81 | | -var errDefaultInvalidIssuer = invalidIssuerError{errors.New(`"iss" not satisfied`)} |
82 | | - |
83 | 49 | // InvalidIssuerError returns the immutable error used when `iss` claim |
84 | 50 | // is not satisfied |
85 | 51 | // |
86 | | -// The return value should only be used for comparison using `errors.Is()` |
| 52 | +// This value should only be used for comparison using `errors.Is()`. |
87 | 53 | func InvalidIssuerError() error { |
88 | | - return errDefaultInvalidIssuer |
89 | | -} |
90 | | - |
91 | | -type tokenExpiredError struct { |
92 | | - error |
| 54 | + return jwterrs.ErrInvalidIssuerDefault |
93 | 55 | } |
94 | 56 |
|
95 | | -func (err tokenExpiredError) Is(target error) bool { |
96 | | - _, ok := target.(tokenExpiredError) |
97 | | - return ok |
98 | | -} |
99 | | - |
100 | | -func (err tokenExpiredError) Unwrap() error { |
101 | | - return err.error |
102 | | -} |
103 | | - |
104 | | -var errDefaultTokenExpired = tokenExpiredError{errors.New(`"exp" not satisfied: token is expired`)} |
105 | | - |
106 | 57 | // TokenExpiredError returns the immutable error used when `exp` claim |
107 | 58 | // is not satisfied. |
108 | 59 | // |
109 | | -// The return value should only be used for comparison using `errors.Is()` |
| 60 | +// This value should only be used for comparison using `errors.Is()`. |
110 | 61 | func TokenExpiredError() error { |
111 | | - return errDefaultTokenExpired |
112 | | -} |
113 | | - |
114 | | -type invalidIssuedAtError struct { |
115 | | - error |
116 | | -} |
117 | | - |
118 | | -func (err invalidIssuedAtError) Is(target error) bool { |
119 | | - _, ok := target.(invalidIssuedAtError) |
120 | | - return ok |
| 62 | + return jwterrs.ErrTokenExpiredDefault |
121 | 63 | } |
122 | 64 |
|
123 | | -func (err invalidIssuedAtError) Unwrap() error { |
124 | | - return err.error |
125 | | -} |
126 | | - |
127 | | -var errDefaultInvalidIssuedAt = invalidIssuedAtError{errors.New(`"iat" not satisfied`)} |
128 | | - |
129 | 65 | // InvalidIssuedAtError returns the immutable error used when `iat` claim |
130 | 66 | // is not satisfied |
131 | 67 | // |
132 | | -// The return value should only be used for comparison using `errors.Is()` |
| 68 | +// This value should only be used for comparison using `errors.Is()`. |
133 | 69 | func InvalidIssuedAtError() error { |
134 | | - return errDefaultInvalidIssuedAt |
135 | | -} |
136 | | - |
137 | | -type tokenNotYetValidError struct { |
138 | | - error |
139 | | -} |
140 | | - |
141 | | -func (err tokenNotYetValidError) Is(target error) bool { |
142 | | - _, ok := target.(tokenNotYetValidError) |
143 | | - return ok |
144 | | -} |
145 | | - |
146 | | -func (err tokenNotYetValidError) Unwrap() error { |
147 | | - return err.error |
| 70 | + return jwterrs.ErrInvalidIssuedAtDefault |
148 | 71 | } |
149 | 72 |
|
150 | | -var errDefaultTokenNotYetValid = tokenNotYetValidError{errors.New(`"nbf" not satisfied: token is not yet valid`)} |
151 | | - |
152 | 73 | // TokenNotYetValidError returns the immutable error used when `nbf` claim |
153 | 74 | // is not satisfied |
154 | 75 | // |
155 | | -// The return value should only be used for comparison using `errors.Is()` |
| 76 | +// This value should only be used for comparison using `errors.Is()`. |
156 | 77 | func TokenNotYetValidError() error { |
157 | | - return errDefaultTokenNotYetValid |
158 | | -} |
159 | | - |
160 | | -type invalidAudienceError struct { |
161 | | - error |
162 | | -} |
163 | | - |
164 | | -func (err invalidAudienceError) Is(target error) bool { |
165 | | - _, ok := target.(invalidAudienceError) |
166 | | - return ok |
167 | | -} |
168 | | - |
169 | | -func (err invalidAudienceError) Unwrap() error { |
170 | | - return err.error |
171 | | -} |
172 | | - |
173 | | -func auderr(f string, args ...any) error { |
174 | | - return invalidAudienceError{fmt.Errorf(`"aud" not satisfied: `+f, args...)} |
| 78 | + return jwterrs.ErrTokenNotYetValidDefault |
175 | 79 | } |
176 | 80 |
|
177 | | -var errDefaultInvalidAudience = invalidAudienceError{errors.New(`"aud" not satisfied`)} |
178 | | - |
179 | 81 | // InvalidAudienceError returns the immutable error used when `aud` claim |
180 | 82 | // is not satisfied |
181 | 83 | // |
182 | | -// The return value should only be used for comparison using `errors.Is()` |
| 84 | +// This value should only be used for comparison using `errors.Is()`. |
183 | 85 | func InvalidAudienceError() error { |
184 | | - return errDefaultInvalidAudience |
185 | | -} |
186 | | - |
187 | | -type missingRequiredClaimError struct { |
188 | | - error |
189 | | - |
190 | | - claim string |
191 | | -} |
192 | | - |
193 | | -func (err *missingRequiredClaimError) Is(target error) bool { |
194 | | - err1, ok := target.(*missingRequiredClaimError) |
195 | | - if !ok { |
196 | | - return false |
197 | | - } |
198 | | - return err1 == errDefaultMissingRequiredClaim || err1.claim == err.claim |
199 | | -} |
200 | | - |
201 | | -var errDefaultMissingRequiredClaim = &missingRequiredClaimError{error: errors.New(`required claim is missing`)} |
202 | | - |
203 | | -func errMissingRequiredClaim(name string) error { |
204 | | - return &missingRequiredClaimError{claim: name, error: fmt.Errorf(`required claim "%s" is missing`, name)} |
| 86 | + return jwterrs.ErrInvalidAudienceDefault |
205 | 87 | } |
206 | 88 |
|
207 | 89 | // MissingRequiredClaimError returns the immutable error used when the claim |
208 | 90 | // specified by `jwt.IsRequired()` is not present. |
209 | 91 | // |
210 | | -// The return value should only be used for comparison using `errors.Is()` |
| 92 | +// This value should only be used for comparison using `errors.Is()`. |
211 | 93 | func MissingRequiredClaimError() error { |
212 | | - return errDefaultMissingRequiredClaim |
| 94 | + return jwterrs.ErrMissingRequiredClaimDefault |
213 | 95 | } |
0 commit comments