- Notifications
You must be signed in to change notification settings - Fork 371
/
Copy patherrors.go
112 lines (98 loc) · 3.95 KB
/
errors.go
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
package jwt
import (
"errors"
)
// Error constants
var (
ErrInvalidKey=errors.New("key is invalid")
ErrInvalidKeyType=errors.New("key is of invalid type")
ErrHashUnavailable=errors.New("the requested hash function is unavailable")
ErrTokenMalformed=errors.New("token is malformed")
ErrTokenUnverifiable=errors.New("token is unverifiable")
ErrTokenSignatureInvalid=errors.New("token signature is invalid")
ErrTokenInvalidAudience=errors.New("token has invalid audience")
ErrTokenExpired=errors.New("token is expired")
ErrTokenUsedBeforeIssued=errors.New("token used before issued")
ErrTokenInvalidIssuer=errors.New("token has invalid issuer")
ErrTokenNotValidYet=errors.New("token is not valid yet")
ErrTokenInvalidId=errors.New("token has invalid id")
ErrTokenInvalidClaims=errors.New("token has invalid claims")
)
// The errors that might occur when parsing and validating a token
const (
ValidationErrorMalformeduint32=1<<iota// Token is malformed
ValidationErrorUnverifiable// Token could not be verified because of signing problems
ValidationErrorSignatureInvalid// Signature validation failed
// Standard Claim validation errors
ValidationErrorAudience// AUD validation failed
ValidationErrorExpired// EXP validation failed
ValidationErrorIssuedAt// IAT validation failed
ValidationErrorIssuer// ISS validation failed
ValidationErrorNotValidYet// NBF validation failed
ValidationErrorId// JTI validation failed
ValidationErrorClaimsInvalid// Generic claims validation error
)
// NewValidationError is a helper for constructing a ValidationError with a string error message
funcNewValidationError(errorTextstring, errorFlagsuint32) *ValidationError {
return&ValidationError{
text: errorText,
Errors: errorFlags,
}
}
// ValidationError represents an error from Parse if token is not valid
typeValidationErrorstruct {
Innererror// stores the error returned by external dependencies, i.e.: KeyFunc
Errorsuint32// bitfield. see ValidationError... constants
textstring// errors that do not have a valid error just have text
}
// Error is the implementation of the err interface.
func (eValidationError) Error() string {
ife.Inner!=nil {
returne.Inner.Error()
} elseife.text!="" {
returne.text
} else {
return"token is invalid"
}
}
// Unwrap gives errors.Is and errors.As access to the inner error.
func (e*ValidationError) Unwrap() error {
returne.Inner
}
// No errors
func (e*ValidationError) valid() bool {
returne.Errors==0
}
// Is checks if this ValidationError is of the supplied error. We are first checking for the exact error message
// by comparing the inner error message. If that fails, we compare using the error flags. This way we can use
// custom error messages (mainly for backwards compatability) and still leverage errors.Is using the global error variables.
func (e*ValidationError) Is(errerror) bool {
// Check, if our inner error is a direct match
iferrors.Is(errors.Unwrap(e), err) {
returntrue
}
// Otherwise, we need to match using our error flags
switcherr {
caseErrTokenMalformed:
returne.Errors&ValidationErrorMalformed!=0
caseErrTokenUnverifiable:
returne.Errors&ValidationErrorUnverifiable!=0
caseErrTokenSignatureInvalid:
returne.Errors&ValidationErrorSignatureInvalid!=0
caseErrTokenInvalidAudience:
returne.Errors&ValidationErrorAudience!=0
caseErrTokenExpired:
returne.Errors&ValidationErrorExpired!=0
caseErrTokenUsedBeforeIssued:
returne.Errors&ValidationErrorIssuedAt!=0
caseErrTokenInvalidIssuer:
returne.Errors&ValidationErrorIssuer!=0
caseErrTokenNotValidYet:
returne.Errors&ValidationErrorNotValidYet!=0
caseErrTokenInvalidId:
returne.Errors&ValidationErrorId!=0
caseErrTokenInvalidClaims:
returne.Errors&ValidationErrorClaimsInvalid!=0
}
returnfalse
}