@@ -36,19 +36,21 @@ func NewParser(options ...ParserOption) *Parser {
36
36
return p
37
37
}
38
38
39
- // Parse parses, validates, verifies the signature and returns the parsed token.
40
- // keyFunc will receive the parsed token and should return the key for validating.
39
+ // Parse parses, validates, verifies the signature and returns the parsed token. keyFunc will
40
+ // receive the parsed token and should return the key for validating.
41
41
func (p * Parser ) Parse (tokenString string , keyFunc Keyfunc ) (* Token , error ) {
42
42
return p .ParseWithClaims (tokenString , MapClaims {}, keyFunc )
43
43
}
44
44
45
- // ParseWithClaims parses, validates, and verifies like Parse, but supplies a default object implementing the Claims
46
- // interface. This provides default values which can be overridden and allows a caller to use their own type, rather
47
- // than the default MapClaims implementation of Claims.
45
+ // ParseWithClaims parses, validates, and verifies like Parse, but supplies a default object
46
+ // implementing the Claims interface. This provides default values which can be overridden and
47
+ // allows a caller to use their own type, rather than the default MapClaims implementation of
48
+ // Claims.
48
49
//
49
- // Note: If you provide a custom claim implementation that embeds one of the standard claims (such as RegisteredClaims),
50
- // make sure that a) you either embed a non-pointer version of the claims or b) if you are using a pointer, allocate the
51
- // proper memory for it before passing in the overall claims, otherwise you might run into a panic.
50
+ // Note: If you provide a custom claim implementation that embeds one of the standard claims (such
51
+ // as RegisteredClaims), make sure that a) you either embed a non-pointer version of the claims or
52
+ // b) if you are using a pointer, allocate the proper memory for it before passing in the overall
53
+ // claims, otherwise you might run into a panic.
52
54
func (p * Parser ) ParseWithClaims (tokenString string , claims Claims , keyFunc Keyfunc ) (* Token , error ) {
53
55
token , parts , err := p .ParseUnverified (tokenString , claims )
54
56
if err != nil {
@@ -85,35 +87,32 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
85
87
return token , & ValidationError {Inner : err , Errors : ValidationErrorUnverifiable }
86
88
}
87
89
90
+ // Perform validation
91
+ token .Signature = parts [2 ]
92
+ if err := token .Method .Verify (strings .Join (parts [0 :2 ], "." ), token .Signature , key ); err != nil {
93
+ return token , & ValidationError {Inner : err , Errors : ValidationErrorSignatureInvalid }
94
+ }
95
+
88
96
vErr := & ValidationError {}
89
97
90
98
// Validate Claims
91
99
if ! p .SkipClaimsValidation {
92
100
if err := token .Claims .Valid (); err != nil {
93
-
94
101
// If the Claims Valid returned an error, check if it is a validation error,
95
102
// If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set
96
103
if e , ok := err .(* ValidationError ); ! ok {
97
104
vErr = & ValidationError {Inner : err , Errors : ValidationErrorClaimsInvalid }
98
105
} else {
99
106
vErr = e
100
107
}
108
+ return token , vErr
101
109
}
102
110
}
103
111
104
- // Perform validation
105
- token .Signature = parts [2 ]
106
- if err = token .Method .Verify (strings .Join (parts [0 :2 ], "." ), token .Signature , key ); err != nil {
107
- vErr .Inner = err
108
- vErr .Errors |= ValidationErrorSignatureInvalid
109
- }
110
-
111
- if vErr .valid () {
112
- token .Valid = true
113
- return token , nil
114
- }
112
+ // No errors so far, token is valid.
113
+ token .Valid = true
115
114
116
- return token , vErr
115
+ return token , nil
117
116
}
118
117
119
118
// ParseUnverified parses the token but doesn't validate the signature.
0 commit comments