- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.go
More file actions
Latest commit
247 lines (216 loc) · 6.27 KB
/
Copy pathparser.go
File metadata and controls
247 lines (216 loc) · 6.27 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package httpsig
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
)
// DefaultClockSkew specifies the allowed time difference between requests and signature parsing
varDefaultClockSkew=300*time.Second
// DefaultHeaders specify the headers to be parsed by default
varDefaultHeaders= []string{"date"}
// ParseStrict specifies sprict parsing. If true, then obsolete versions of the HTTP-Signature standard will not be supported.
// Currently, this controls whether to allow the "request-line" psuedo-header.
varParseStrict=false
const (
stateNew=0
stateParams=1
)
const (
paramsStateName=0
paramsStateQuote=1
paramsStateValue=2
paramsStateComma=3
)
// ParsedSignature contains the details of a parsed signature
typeParsedSignaturestruct {
schemestring
paramsmap[string]string
signingStringstring
}
// ParseRequest parses an http.Request and returns a ParsedSignature with the values
// from the Authorization Header.
funcParseRequest(request*http.Request) (*ParsedSignature, error) {
state:=stateNew
substate:=paramsStateName
tmpName:=""
tmpValue:=""
parsed:=ParsedSignature{
params: make(map[string]string),
}
varauthzstring
ifauth, ok:=request.Header["Authorization"]; ok {
authz=auth[0]
} else {
returnnil, errors.New("Missing Header error: no authorization header present in the request")
}
for_, code:=rangeauthz {
c:=string(code)
switchstate {
casestateNew:
ifc!=" " {
parsed.scheme+=c
} else {
state=stateParams
}
break
casestateParams:
switchsubstate {
caseparamsStateName:
if (code>=0x41&&code<=0x5a) ||// A-Z
(code>=0x61&&code<=0x7a) { // a-z
tmpName+=c
} elseifc=="=" {
iftmpName=="" {
returnnil, errors.New("Invalid Header Error: bad param format")
}
substate=paramsStateQuote
} else {
returnnil, errors.New("Invalid Header Error: bad param format")
}
break
caseparamsStateQuote:
ifc=="\"" {
tmpValue=""
substate=paramsStateValue
} else {
returnnil, errors.New("Invalid Header Error: bad param format")
}
break
caseparamsStateValue:
ifc=="\"" {
parsed.params[tmpName] =tmpValue
substate=paramsStateComma
} else {
tmpValue+=c
}
break
caseparamsStateComma:
ifc=="," {
tmpName=""
substate=paramsStateName
} else {
returnnil, errors.New("Invalid Header Error: bad param format")
}
break
}
break
}
}
varhstring
ifval, ok:=parsed.params["headers"]; ok {
h=strings.ToLower(val)
} else {
h="date"
}
parsed.params["headers"] =h
headers:=strings.Split(h, " ")
ifparsed.scheme!="Signature" {
returnnil, errors.New("Scheme was not \"Signature\"")
}
if_, ok:=parsed.params["keyId"]; !ok {
returnnil, errors.New("Invalid Header Error: keyId was not specified")
}
if_, ok:=parsed.params["algorithm"]; !ok {
returnnil, errors.New("Invalid Header Error: algorithm was not specified")
}
if_, ok:=parsed.params["signature"]; !ok {
returnnil, errors.New("Invalid Header Error: signature was not specified'")
}
if_, err:=validateAlgorithm(parsed.Algorithm()); err!=nil {
returnnil, err
}
// Build the signingString
varsigningParts=make([]string, len(headers), len(headers))
fori, h:=rangeheaders {
ifh=="request-line" {
if!ParseStrict {
/*
* We allow headers from the older spec drafts if strict parsing isn't
* specified in options.
*/
signingParts[i] =fmt.Sprintf("%s %s %s", request.Method, request.RequestURI, request.Proto)
} else {
/* Strict parsing doesn't allow older draft headers. */
returnnil, errors.New("Strict Parsing error: request-line is not a valid header with strict parsing enabled.")
}
} elseifh=="(request-target)" {
signingParts[i] =fmt.Sprintf("(request-target): %s %s", strings.ToLower(request.Method), request.RequestURI)
} elseifh=="host" {
signingParts[i] =fmt.Sprintf("%s: %s", h, request.Host)
} elseifh=="content-length" {
signingParts[i] =fmt.Sprintf("%s: %d", h, request.ContentLength)
} else {
ifvalue, ok:=request.Header[http.CanonicalHeaderKey(h)]; ok {
signingParts[i] =h+": "+value[0]
} else {
returnnil, fmt.Errorf("Missing Header error: \"%s\" was not in the request.", h)
}
}
}
parsed.signingString=strings.Join(signingParts, "\n")
vardateStringstring
hasDate:=false
ifds, ok:=request.Header["Date"]; ok {
dateString=ds[0]
hasDate=true
}
ifhasDate {
date, _:=time.Parse(time.RFC1123, dateString)
now:=time.Now()
skew:=now.Sub(date)
ifskew<0 {
skew=-skew
}
ifskew>DefaultClockSkew {
returnnil, fmt.Errorf("Expired Request error: clock skew of %v was greater than %v", skew, DefaultClockSkew)
}
}
for_, dh:=rangeDefaultHeaders {
found:=false
for_, h:=rangeheaders {
ifh==dh {
found=true
}
}
if!found {
returnnil, fmt.Errorf("%s was not a signed header", dh)
}
}
return&parsed, nil
}
// Scheme is the scheme of the parsed signature. Currently, the only supported scheme is "Signature"
func (s*ParsedSignature) Scheme() string {
returns.scheme
}
// Params is a map of sttrings containing the signature parameters
func (s*ParsedSignature) Params() map[string]string {
returns.params
}
// SigningString is the string that was signed by the client. Verifiers should use this to check that a singature is valid
func (s*ParsedSignature) SigningString() string {
returns.signingString
}
// Algorithm is the signing and hash algorithm, like "rsa-sha256"
func (s*ParsedSignature) Algorithm() string {
returnstrings.ToUpper(s.params["algorithm"])
}
// KeyId is the keyId parameter from the signature
//
// Deprecated: since 1.1
func (s*ParsedSignature) KeyId() string {
returns.params["keyId"]
}
// KeyID is the keyId parameter from the signature
func (s*ParsedSignature) KeyID() string {
returns.params["keyId"]
}
// Signature is the base-64 encoded signature
func (s*ParsedSignature) Signature() string {
returns.params["signature"]
}
// Headers is the slice of headers that were signed by the client
func (s*ParsedSignature) Headers() []string {
returnstrings.Split(s.params["headers"], " ")
}