-
Notifications
You must be signed in to change notification settings - Fork 15
/
auth_request_signing.go
143 lines (118 loc) · 3.12 KB
/
auth_request_signing.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
package form3
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/base64"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/google/uuid"
)
type RequestSigningOption func(*RequestSigningTransport)
type RequestSigningTransport struct {
pubKeyID uuid.UUID
privateKey *rsa.PrivateKey
userAgent string
underlyingTransport http.RoundTripper
}
func WithUserAgent(ua string) RequestSigningOption {
return func(t *RequestSigningTransport) {
t.userAgent = ua
}
}
func WithPrivateKey(key *rsa.PrivateKey) RequestSigningOption {
return func(t *RequestSigningTransport) {
t.privateKey = key
}
}
func WithPublicKeyID(keyID uuid.UUID) RequestSigningOption {
return func(t *RequestSigningTransport) {
t.pubKeyID = keyID
}
}
func WithUnderlyingRequestSigningTransport(tr http.RoundTripper) RequestSigningOption {
return func(t *RequestSigningTransport) {
t.underlyingTransport = tr
}
}
func NewRequestSigningTransport(opts ...RequestSigningOption) *RequestSigningTransport {
t := &RequestSigningTransport{
underlyingTransport: http.DefaultTransport,
userAgent: UserAgent,
}
for _, opt := range opts {
opt(t)
}
return t
}
func (t *RequestSigningTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("User-Agent", t.userAgent)
var headers []string
hasher := sha256.New()
dt := time.Now().UTC().Format(time.RFC1123)
msgToSign := fmt.Sprintf(`(request-target): %s %s
host: %s
date: %s`,
strings.ToLower(req.Method),
req.URL.RequestURI(),
req.URL.Host,
dt,
)
headers = append(headers, "(request-target)", "host", "date")
req.Header.Set("Date", dt)
// Calculate the digest from payload for POST, PUT, PATCH requests
if (req.Method == http.MethodPost ||
req.Method == http.MethodPut ||
req.Method == http.MethodPatch) &&
req.Body != nil {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, fmt.Errorf("error reading request body: %w", err)
}
if _, err := hasher.Write(body); err != nil {
return nil, fmt.Errorf("error hashing: %w", err)
}
digest := base64.StdEncoding.EncodeToString(hasher.Sum(nil))
msgToSign += fmt.Sprintf(`
content-type: %s
digest: SHA-256=%s
content-length: %d`,
ReqMimeType,
digest,
req.ContentLength,
)
headers = append(headers, "content-type", "digest", "content-length")
req.Body = ioutil.NopCloser(bytes.NewBuffer(body))
req.Header.Set("Content-Type", ReqMimeType)
req.Header.Set("Digest", digest)
}
hasher.Reset()
if _, err := hasher.Write([]byte(msgToSign)); err != nil {
return nil, fmt.Errorf("error hashing: %w", err)
}
hashedMsgToSign := hasher.Sum(nil)
signature, err := rsa.SignPKCS1v15(
rand.Reader,
t.privateKey,
crypto.SHA256,
hashedMsgToSign,
)
if err != nil {
return nil, fmt.Errorf("failed to sign message %s: %w", msgToSign, err)
}
req.Header.Set(
"Authorization",
fmt.Sprintf(
`Signature keyId="%s",algorithm="rsa-sha256",headers="%s", signature="%s"`,
t.pubKeyID,
strings.Join(headers, " "),
base64.StdEncoding.EncodeToString(signature),
),
)
return t.underlyingTransport.RoundTrip(req)
}