blob: 1c93024aad2ea97b1bd2b944066d20145a69f1ce [file] [log] [blame]
khenaidooffe076b2019-01-15 16:08:08 -05001package jwt
2
3import (
4 "errors"
5)
6
7// Error constants
8var (
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
14// The errors that might occur when parsing and validating a token
15const (
16 ValidationErrorMalformed uint32 = 1 << iota // Token is malformed
17 ValidationErrorUnverifiable // Token could not be verified because of signing problems
18 ValidationErrorSignatureInvalid // Signature validation failed
19
20 // Standard Claim validation errors
21 ValidationErrorAudience // AUD validation failed
22 ValidationErrorExpired // EXP validation failed
23 ValidationErrorIssuedAt // IAT validation failed
24 ValidationErrorIssuer // ISS validation failed
25 ValidationErrorNotValidYet // NBF validation failed
26 ValidationErrorId // JTI validation failed
27 ValidationErrorClaimsInvalid // Generic claims validation error
28)
29
30// Helper for constructing a ValidationError with a string error message
31func NewValidationError(errorText string, errorFlags uint32) *ValidationError {
32 return &ValidationError{
33 text: errorText,
34 Errors: errorFlags,
35 }
36}
37
38// The error from Parse if token is not valid
39type ValidationError struct {
40 Inner error // stores the error returned by external dependencies, i.e.: KeyFunc
41 Errors uint32 // bitfield. see ValidationError... constants
42 text string // errors that do not have a valid error just have text
43}
44
45// Validation error is an error type
46func (e ValidationError) Error() string {
47 if e.Inner != nil {
48 return e.Inner.Error()
49 } else if e.text != "" {
50 return e.text
51 } else {
52 return "token is invalid"
53 }
54}
55
56// No errors
57func (e *ValidationError) valid() bool {
58 return e.Errors == 0
59}