-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.go
More file actions
154 lines (137 loc) · 4.2 KB
/
Copy pathparser.go
File metadata and controls
154 lines (137 loc) · 4.2 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package jwt
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"strings"
)
// ErrDuplicateKey is returned when a JSON object in the token contains duplicate keys.
var ErrDuplicateKey = errors.New("token contains duplicate keys")
type Parser struct {
ValidMethods []string // If populated, only these methods will be considered valid
UseJSONNumber bool // Use JSON Number format in claims instead of float64
SkipClaimsValidation bool // Skip claims validation during token parsing
}
// Parse parses, validates, and returns a token.
// keyFunc will receive the parsed token and should return the key for validating.
// If everything is ok, err will be nil.
func (p *Parser) Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
return p.ParseWithClaims(tokenString, MapClaims{}, keyFunc)
}
func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyfunc) (*Token, error) {
token, parts, err := p.ParseUnverified(tokenString, claims)
if err != nil {
return token, err
}
// Verify signing method
var sigVal error
if err = token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, keyFunc); err != nil {
sigVal = err
}
// Lookup signature error
if sigVal != nil {
return token, &ValidationError{Inner: sigVal, Errors: ValidationErrorSignatureInvalid}
}
// Validate Claims
if !p.SkipClaimsValidation {
if err := token.Claims.Valid(); err != nil {
return token, &ValidationError{Inner: err, Errors: ValidationErrorClaimsInvalid}
}
}
token.Valid = true
return token, nil
}
// ParseUnverified parses the token but doesn't validate the signature. It's only
// useful if you know the signature is valid or if you've already verified it.
func (p *Parser) ParseUnverified(tokenString string, claims Claims) (token *Token, parts []string, err error) {
parts = strings.Split(tokenString, ".")
if len(parts) != 3 {
return nil, parts, &ValidationError{Inner: fmt.Errorf("token contains an invalid number of segments"), Errors: ValidationErrorMalformed}
}
token = &Token{Raw: tokenString}
// parse Header
var headerBytes []byte
if headerBytes, err = DecodeSegment(parts[0]); err != nil {
if strings.HasPrefix(err.Error(), "illegal base64 data") {
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
}
return token, parts, err
}
if err = checkDuplicateKeys(headerBytes); err != nil {
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
}
if err = json.Unmarshal(headerBytes, &token.Header); err != nil {
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
}
// parse Claims
var claimsBytes []byte
if claimsBytes, err = DecodeSegment(parts[1]); err != nil {
if strings.HasPrefix(err.Error(), "illegal base64 data") {
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
}
return token, parts, err
}
if err = checkDuplicateKeys(claimsBytes); err != nil {
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
}
if err = json.Unmarshal(claimsBytes, claims); err != nil {
return token, parts, &ValidationError{Inner: err, Errors: ValidationErrorMalformed}
}
token.Claims = claims
return token, parts, nil
}
func checkDuplicateKeys(data []byte) error {
dec := json.NewDecoder(bytes.NewReader(data))
return checkDuplicates(dec)
}
func checkDuplicates(dec *json.Decoder) error {
t, err := dec.Token()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
delim, ok := t.(json.Delim)
if !ok {
return nil
}
switch delim {
case '{':
keys := make(map[string]bool)
for dec.More() {
t, err := dec.Token()
if err != nil {
return err
}
key, ok := t.(string)
if !ok {
return fmt.Errorf("expected string key, got %T", t)
}
if keys[key] {
return ErrDuplicateKey
}
keys[key] = true
if err := checkDuplicates(dec); err != nil {
return err
}
}
// consume closing '}'
if _, err := dec.Token(); err != nil {
return err
}
case '[':
for dec.More() {
if err := checkDuplicates(dec); err != nil {
return err
}
}
// consume closing ']'
if _, err := dec.Token(); err != nil {
return err
}
}
return nil
}