@@ -2,120 +2,48 @@ package jwt
2
2
3
3
import (
4
4
"errors"
5
+ "strings"
5
6
)
6
7
7
- // Error constants
8
8
var (
9
- ErrInvalidKey = errors .New ("key is invalid" )
10
- ErrInvalidKeyType = errors .New ("key is of invalid type" )
11
- ErrHashUnavailable = errors .New ("the requested hash function is unavailable" )
12
-
13
- ErrTokenMalformed = errors .New ("token is malformed" )
14
- ErrTokenUnverifiable = errors .New ("token is unverifiable" )
15
- ErrTokenSignatureInvalid = errors .New ("token signature is invalid" )
16
-
17
- ErrTokenInvalidAudience = errors .New ("token has invalid audience" )
18
- ErrTokenExpired = errors .New ("token is expired" )
19
- ErrTokenUsedBeforeIssued = errors .New ("token used before issued" )
20
- ErrTokenInvalidIssuer = errors .New ("token has invalid issuer" )
21
- ErrTokenInvalidSubject = errors .New ("token has invalid subject" )
22
- ErrTokenNotValidYet = errors .New ("token is not valid yet" )
23
- ErrTokenInvalidId = errors .New ("token has invalid id" )
24
- ErrTokenInvalidClaims = errors .New ("token has invalid claims" )
25
-
26
- ErrInvalidType = errors .New ("invalid type for claim" )
27
- )
28
-
29
- // The errors that might occur when parsing and validating a token
30
- const (
31
- ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
32
- ValidationErrorUnverifiable // Token could not be verified because of signing problems
33
- ValidationErrorSignatureInvalid // Signature validation failed
34
-
35
- // Registered Claim validation errors
36
- ValidationErrorAudience // AUD validation failed
37
- ValidationErrorExpired // EXP validation failed
38
- ValidationErrorIssuedAt // IAT validation failed
39
- ValidationErrorIssuer // ISS validation failed
40
- ValidationErrorSubject // SUB validation failed
41
- ValidationErrorNotValidYet // NBF validation failed
42
- ValidationErrorId // JTI validation failed
43
- ValidationErrorClaimsInvalid // Generic claims validation error
9
+ ErrInvalidKey = errors .New ("key is invalid" )
10
+ ErrInvalidKeyType = errors .New ("key is of invalid type" )
11
+ ErrHashUnavailable = errors .New ("the requested hash function is unavailable" )
12
+ ErrTokenMalformed = errors .New ("token is malformed" )
13
+ ErrTokenUnverifiable = errors .New ("token is unverifiable" )
14
+ ErrTokenSignatureInvalid = errors .New ("token signature is invalid" )
15
+ ErrTokenRequiredClaimMissing = errors .New ("token is missing required claim" )
16
+ ErrTokenInvalidAudience = errors .New ("token has invalid audience" )
17
+ ErrTokenExpired = errors .New ("token is expired" )
18
+ ErrTokenUsedBeforeIssued = errors .New ("token used before issued" )
19
+ ErrTokenInvalidIssuer = errors .New ("token has invalid issuer" )
20
+ ErrTokenInvalidSubject = errors .New ("token has invalid subject" )
21
+ ErrTokenNotValidYet = errors .New ("token is not valid yet" )
22
+ ErrTokenInvalidId = errors .New ("token has invalid id" )
23
+ ErrTokenInvalidClaims = errors .New ("token has invalid claims" )
24
+ ErrInvalidType = errors .New ("invalid type for claim" )
44
25
)
45
26
46
- // NewValidationError is a helper for constructing a ValidationError with a string error message
47
- func NewValidationError (errorText string , errorFlags uint32 ) * ValidationError {
48
- return & ValidationError {
49
- text : errorText ,
50
- Errors : errorFlags ,
51
- }
27
+ // joinedError is an error type that works similar to what [errors.Join]
28
+ // produces, with the exception that it has a nice error string; mainly its
29
+ // error messages are concatenated using a comma, rather than a newline.
30
+ type joinedError struct {
31
+ errs []error
52
32
}
53
33
54
- // ValidationError represents an error from Parse if token is not valid
55
- type ValidationError struct {
56
- // Inner stores the error returned by external dependencies, e.g.: KeyFunc
57
- Inner error
58
- // Errors is a bit-field. See ValidationError... constants
59
- Errors uint32
60
- // Text can be used for errors that do not have a valid error just have text
61
- text string
62
- }
63
-
64
- // Error is the implementation of the err interface.
65
- func (e ValidationError ) Error () string {
66
- if e .Inner != nil {
67
- return e .Inner .Error ()
68
- } else if e .text != "" {
69
- return e .text
70
- } else {
71
- return "token is invalid"
34
+ func (je joinedError ) Error () string {
35
+ msg := []string {}
36
+ for _ , err := range je .errs {
37
+ msg = append (msg , err .Error ())
72
38
}
73
- }
74
-
75
- // Unwrap gives errors.Is and errors.As access to the inner error.
76
- func (e * ValidationError ) Unwrap () error {
77
- return e .Inner
78
- }
79
39
80
- // No errors
81
- func (e * ValidationError ) valid () bool {
82
- return e .Errors == 0
40
+ return strings .Join (msg , ", " )
83
41
}
84
42
85
- // Is checks if this ValidationError is of the supplied error. We are first
86
- // checking for the exact error message by comparing the inner error message. If
87
- // that fails, we compare using the error flags. This way we can use custom
88
- // error messages (mainly for backwards compatibility) and still leverage
89
- // errors.Is using the global error variables.
90
- func (e * ValidationError ) Is (err error ) bool {
91
- // Check, if our inner error is a direct match
92
- if errors .Is (errors .Unwrap (e ), err ) {
93
- return true
43
+ // joinErrors joins together multiple errors. Useful for scenarios where
44
+ // multiple errors next to each other occur, e.g., in claims validation.
45
+ func joinErrors (errs ... error ) error {
46
+ return & joinedError {
47
+ errs : errs ,
94
48
}
95
-
96
- // Otherwise, we need to match using our error flags
97
- switch err {
98
- case ErrTokenMalformed :
99
- return e .Errors & ValidationErrorMalformed != 0
100
- case ErrTokenUnverifiable :
101
- return e .Errors & ValidationErrorUnverifiable != 0
102
- case ErrTokenSignatureInvalid :
103
- return e .Errors & ValidationErrorSignatureInvalid != 0
104
- case ErrTokenInvalidAudience :
105
- return e .Errors & ValidationErrorAudience != 0
106
- case ErrTokenExpired :
107
- return e .Errors & ValidationErrorExpired != 0
108
- case ErrTokenUsedBeforeIssued :
109
- return e .Errors & ValidationErrorIssuedAt != 0
110
- case ErrTokenInvalidIssuer :
111
- return e .Errors & ValidationErrorIssuer != 0
112
- case ErrTokenNotValidYet :
113
- return e .Errors & ValidationErrorNotValidYet != 0
114
- case ErrTokenInvalidId :
115
- return e .Errors & ValidationErrorId != 0
116
- case ErrTokenInvalidClaims :
117
- return e .Errors & ValidationErrorClaimsInvalid != 0
118
- }
119
-
120
- return false
121
49
}
0 commit comments