-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.go
158 lines (133 loc) · 3.86 KB
/
authorization.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
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
package ls3
import (
"encoding/hex"
"github.com/relvacode/ls3/exception"
"strings"
"time"
)
const awsSignatureVersionV4 = "AWS4-HMAC-SHA256"
// growSlice grows slice b to length n.
// If cap(b) < n then a new slice is allocated and the original contents are copied into it
func growSlice(b []byte, n int) []byte {
if len(b)+n <= cap(b) {
return b[:len(b)+n]
}
grow := make([]byte, len(b)+n)
copy(grow, b)
return grow
}
func ParseCredential(value string) (*Credential, error) {
var credentialParts = strings.Split(value, "/")
if len(credentialParts) != 5 {
return nil, &exception.Error{
ErrorCode: exception.InvalidSecurity,
Message: "The provided security credentials are not valid.",
}
}
var credential = new(Credential)
credential.AccessKeyID = credentialParts[0]
var err error
credential.Date, err = time.Parse(amzDateFormat, credentialParts[1])
if err != nil {
return nil, &exception.Error{
ErrorCode: exception.InvalidSecurity,
Message: "The provided security credentials are not valid.",
}
}
credential.Region = credentialParts[2]
credential.Service = credentialParts[3]
credential.Type = credentialParts[4]
return credential, nil
}
type Credential struct {
AccessKeyID string
Date time.Time
Region string
Service string
Type string
}
func (c Credential) AppendFormat(b []byte) []byte {
b = append(b, c.AccessKeyID...)
b = append(b, '/')
b = c.Date.AppendFormat(b, amzDateFormat)
b = append(b, '/')
b = append(b, c.Region...)
b = append(b, '/')
b = append(b, c.Service...)
b = append(b, '/')
b = append(b, c.Type...)
return b
}
// ParseAuthorizationHeader parses the contents of the given Authorization header.
// Returns a non-nil *ErrInvalidAuthorizationHeader when an invalid header is given.
func ParseAuthorizationHeader(hdr string) (*Authorization, error) {
var auth = new(Authorization)
var methodParts = strings.SplitN(hdr, " ", 2)
if len(methodParts) < 2 || methodParts[0] != awsSignatureVersionV4 {
return nil, &exception.Error{
ErrorCode: exception.InvalidRequest,
Message: "The request is using the wrong signature version. Use AWS4-HMAC-SHA256 (Signature Version 4).",
}
}
var requestOptions = strings.Split(methodParts[1], ",")
for _, opt := range requestOptions {
var optionParts = strings.SplitN(strings.TrimSpace(opt), "=", 2)
if len(optionParts) < 2 {
return nil, &exception.Error{
ErrorCode: exception.AuthorizationHeaderMalformed,
Message: "The authorization header that you provided is not valid.",
}
}
var (
key = optionParts[0]
value = optionParts[1]
)
// Unrecognised options are ignored
switch key {
case "Credential":
cr, err := ParseCredential(value)
if err != nil {
return nil, &exception.Error{
ErrorCode: exception.AuthorizationHeaderMalformed,
Message: "The authorization header that you provided is not valid.",
}
}
auth.Credentials = *cr
case "SignedHeaders":
auth.SignedHeaders = strings.Split(value, ";")
case "Signature":
var err error
auth.Signature, err = hex.DecodeString(value)
if err != nil {
return nil, &exception.Error{
ErrorCode: exception.AuthorizationHeaderMalformed,
Message: "The authorization header that you provided is not valid.",
}
}
}
}
return auth, nil
}
type Authorization struct {
Credentials Credential
SignedHeaders []string
Signature []byte // raw decoded hex
}
func (a Authorization) AppendFormat(b []byte) []byte {
b = append(b, awsSignatureVersionV4...)
b = append(b, ' ')
b = append(b, "Credential="...)
b = a.Credentials.AppendFormat(b)
b = append(b, ",SignedHeaders="...)
for i, hdr := range a.SignedHeaders {
if i > 0 {
b = append(b, ';')
}
b = append(b, hdr...)
}
b = append(b, ",Signature="...)
l := len(b)
b = growSlice(b, hex.EncodedLen(len(a.Signature)))
hex.Encode(b[l:], a.Signature)
return b
}