-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
40 lines (32 loc) · 971 Bytes
/
util.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
package aws4
import (
"net/http"
"strings"
"github.com/allaboutapps/aws4/pkg/util"
)
// AccessKeyIDFromRequest attempts to retrieve the access key ID used for signing the request,
// checking for a presigned query parameter first before trying to parse a signed Authorization header.
//
// If no credentials are found or they appear malformed, an empty string is returned.
func AccessKeyIDFromRequest(req *http.Request) string {
cred := req.URL.Query().Get("X-Amz-Credential")
if len(cred) == 0 {
authParts := strings.Split(req.Header.Get("Authorization"), ", ")
if len(authParts) != authHeaderPartsLen {
return ""
}
c := authParts[0][len(util.Algorithm)+1:]
if !strings.HasPrefix(c, "Credential=") {
return ""
}
cred = strings.TrimPrefix(c, "Credential=")
}
credParts := strings.Split(cred, "/")
if len(credParts) != credentialPartsLen {
return ""
}
if credParts[4] != util.RequestTypeAWS4 {
return ""
}
return credParts[0]
}