-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy patherrors.go
98 lines (75 loc) · 2.78 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
package paseto
import "fmt"
// Any cryptography issue (with the token) or formatting error.
// This does not include cryptography errors with input key material, these will
// return regular errors.
type TokenError struct {
e error
}
func newTokenError(e error) TokenError {
return TokenError{e}
}
func (e TokenError) Error() string {
return e.e.Error()
}
func (_ TokenError) Is(e error) bool {
_, ok1 := e.(TokenError)
_, ok2 := e.(*TokenError)
return ok1 || ok2
}
func (e TokenError) Unwrap() error {
return e.e
}
func (e TokenError) wrapWith(msg string) TokenError {
return TokenError{fmt.Errorf("%s: %w", msg, e)}
}
// Any error which is the result of a rule failure (distinct from a TokenError)
// Can be used to detect cryptographically valid tokens which have failed only
// due to a rule failure: which may warrant a slightly different processing
// follow up.
type RuleError struct {
e error
}
func newRuleError(e error) RuleError {
return RuleError{e}
}
func (e RuleError) Error() string {
return e.e.Error()
}
func (_ RuleError) Is(e error) bool {
_, ok1 := e.(RuleError)
_, ok2 := e.(*RuleError)
return ok1 || ok2
}
func (e RuleError) Unwrap() error {
return e.e
}
func errorKeyLength(expected, given int) error {
return fmt.Errorf("key length incorrect (%d), expected %d", given, expected)
}
var errorKeyWrongCurve = fmt.Errorf("input key was for the wrong curve")
func errorSeedLength(expected, given int) error {
return fmt.Errorf("seed length incorrect (%d), expected %d", given, expected)
}
func errorMessageParts(given int) TokenError {
return newTokenError(fmt.Errorf("invalid number of message parts in token (%d)", given))
}
func errorMessageHeader(expected Protocol, givenHeader string) TokenError {
return newTokenError(fmt.Errorf("message header `%s' is not valid, expected `%s'", givenHeader, expected.Header()))
}
func errorMessageHeaderDecrypt(expected Protocol, givenHeader string) TokenError {
return errorMessageHeader(expected, givenHeader).wrapWith("cannot decrypt message")
}
func errorMessageHeaderVerify(expected Protocol, givenHeader string) TokenError {
return errorMessageHeader(expected, givenHeader).wrapWith("cannot verify message")
}
var unsupportedPasetoVersion = fmt.Errorf("unsupported PASETO version")
var unsupportedPasetoPurpose = fmt.Errorf("unsupported PASETO purpose")
var unsupportedPayload = fmt.Errorf("unsupported payload")
var errorPayloadShort = newTokenError(fmt.Errorf("payload is not long enough to be a valid PASETO message"))
var errorBadSignature = newTokenError(fmt.Errorf("bad signature"))
var errorBadMAC = newTokenError(fmt.Errorf("bad message authentication code"))
var errorKeyInvalid = fmt.Errorf("key was not valid")
func errorDecrypt(err error) TokenError {
return newTokenError(err).wrapWith("the message could not be decrypted")
}