-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
322 lines (278 loc) · 13.3 KB
/
tests.py
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
from base64 import b64encode
import hashlib
import unittest
from http.server import HTTPServer, BaseHTTPRequestHandler
import socketserver
import threading
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.serialization import load_pem_private_key
from freezegun import freeze_time
import httpx
import requests
import urllib3
from http_signature_client import sign_headers
class TestIntegration(unittest.TestCase):
def test_canonicalisation(self):
key_id = 'my-key'
pem_private_key = \
b'-----BEGIN PRIVATE KEY-----\n' \
b'MC4CAQAwBQYDK2VwBCIEINQG5lNt1bE8TZa68mV/WZdpqsXaOXBHvgPQGm5CcjHp\n' \
b'-----END PRIVATE KEY-----\n'
private_key = load_pem_private_key(
pem_private_key, password=None, backend=default_backend())
method = 'post'
url = '/some-path?a=b&a=c&d=e'
body_sha512 = b64encode(hashlib.sha512(b'some-data').digest()).decode('ascii')
headers = (
('digest', f'SHA512={body_sha512}'),
('connection', 'close'),
('x-custom', 'first '),
('x-custom', ' second'),
('(request-target)', 'some-value'),
('(created)', 'some-other-value'),
)
with freeze_time('2012-01-14 03:21:34'):
signed_headers = sign_headers(key_id, private_key.sign, method, url, headers)
self.maxDiff = 10000
correct_signature = \
'keyId="my-key", created=1326511294, headers="(created) (request-target) digest ' \
'x-custom", signature="yiZbaEqcpfUbPfvoHJsvX3ypzZdtFjQTTjAi3ZpjtLbYO2ZTY2+TNydQCw8D/' \
'PgRvg9is/kJaaPTe0RnHiosBw=="'
self.assertEqual(signed_headers, (
(
'signature',
correct_signature,
),
(
'digest',
'SHA512=4cT8Z/GQnjUIPMUwn8ujbSdDM6PEAJqUqXBSc+QfyIthia0VdVHj050dqkQSJk0TEgtnE8mdO+'
'TWTH306npMew==',
),
(
'connection', 'close',
),
(
'x-custom', 'first ',
),
(
'x-custom', ' second',
),
(
'(request-target)', 'some-value',
),
(
'(created)', 'some-other-value',
),
))
headers_same_canonicalisation = (
('Digest', f'SHA512={body_sha512}'),
('connection', 'keep-alive'),
('X-Custom', 'first '),
('x-custom', ' second'),
('(request-Target)', 'some-value'),
('(creaTed)', 'some-other-value'),
)
with freeze_time('2012-01-14 03:21:34'):
signed_headers_mixed = sign_headers(key_id, private_key.sign, method, url,
headers_same_canonicalisation)
self.assertEqual(signed_headers_mixed, (
(
'signature',
correct_signature,
),
(
'Digest',
'SHA512=4cT8Z/GQnjUIPMUwn8ujbSdDM6PEAJqUqXBSc+QfyIthia0VdVHj050dqkQSJk0TEgtnE8mdO+'
'TWTH306npMew==',
),
(
'connection', 'keep-alive',
),
(
'X-Custom', 'first ',
),
(
'x-custom', ' second',
),
(
'(request-Target)', 'some-value',
),
(
'(creaTed)', 'some-other-value',
),
))
def test_requests(self):
received_headers = ()
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
nonlocal received_headers
received_headers = tuple(self.headers.items())
self.send_response(200)
self.end_headers()
class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
daemon_threads = True
server = ThreadingHTTPServer(('0.0.0.0', 8080), Handler)
thread = threading.Thread(target=server.serve_forever)
thread.start()
def cleanup():
server.shutdown()
server.server_close()
thread.join()
self.addCleanup(cleanup)
def HttpSignatureWithBodyDigest(key_id, pem_private_key):
private_key = load_pem_private_key(
pem_private_key, password=None, backend=default_backend())
def sign(r):
body_sha512 = b64encode(hashlib.sha512(r.body).digest()).decode('ascii')
headers_to_sign = tuple(r.headers.items()) + (('digest', f'SHA512={body_sha512}'),)
parsed_url = urllib3.util.url.parse_url(r.path_url)
path = parsed_url.path + (f'?{parsed_url.query}' if parsed_url.query else '')
r.headers = dict(sign_headers(
key_id, private_key.sign, r.method, path, headers_to_sign))
return r
return sign
def make_request():
key_id = 'my-key'
pem_private_key = \
b'-----BEGIN PRIVATE KEY-----\n' \
b'MC4CAQAwBQYDK2VwBCIEINQG5lNt1bE8TZa68mV/WZdpqsXaOXBHvgPQGm5CcjHp\n' \
b'-----END PRIVATE KEY-----\n'
requests.post('http://localhost:8080/path?a=b', data=b'The bytes',
auth=HttpSignatureWithBodyDigest(key_id, pem_private_key))
with freeze_time('2012-01-14 03:21:34'):
make_request()
received_headers_dict = dict((key.lower(), value) for key, value in received_headers)
self.assertEqual(received_headers_dict, {
'host': 'localhost:8080',
'signature': 'keyId="my-key", created=1326511294, headers="(created) (request-target) '
'user-agent accept-encoding '
'accept content-length digest", signature="5E9AExVYVmMLQetvKgF1HkE394fF90'
'X0oaazWIoM1wAIfPCi7En658gIhrEfnJ9FHm/zt2loSG6RVGtMma3HDQ=="',
'user-agent': 'python-requests/2.23.0',
'accept-encoding': 'gzip, deflate',
'accept': '*/*',
'connection': 'keep-alive',
'content-length': '9',
'digest': 'SHA512=Jpu2uP4aOrJMRxr5j9NKiqwK0ksXiftpjdHOGJTU4v7BxYvf/nEYHxeWL7YCsFXE3XJ9'
'q2luOWXKpCQmDaQxCg=='
})
def test_httpx(self):
received_headers = ()
class Handler(BaseHTTPRequestHandler):
def do_POST(self):
nonlocal received_headers
received_headers = tuple(self.headers.items())
self.send_response(200)
self.send_header('content-length', '0')
self.end_headers()
class ThreadingHTTPServer(socketserver.ThreadingMixIn, HTTPServer):
daemon_threads = True
server = ThreadingHTTPServer(('0.0.0.0', 8080), Handler)
thread = threading.Thread(target=server.serve_forever)
thread.start()
def cleanup():
server.shutdown()
server.server_close()
thread.join()
self.addCleanup(cleanup)
class HttpSignatureWithBodyDigest(httpx.Auth):
requires_request_body = True
def __init__(self, key_id, pem_private_key):
self.key_id = key_id
self.private_key = load_pem_private_key(
pem_private_key, password=None, backend=default_backend())
def auth_flow(self, request):
body_sha512 = b64encode(hashlib.sha512(request.content).digest()).decode('ascii')
headers_to_sign = tuple(
request.headers.items()) + (('digest', f'SHA512={body_sha512}'),)
request.headers = httpx.Headers(sign_headers(
self.key_id, self.private_key.sign, request.method,
request.url.full_path, headers_to_sign))
yield request
def make_request():
key_id = 'my-key'
pem_private_key = \
b'-----BEGIN PRIVATE KEY-----\n' \
b'MC4CAQAwBQYDK2VwBCIEINQG5lNt1bE8TZa68mV/WZdpqsXaOXBHvgPQGm5CcjHp\n' \
b'-----END PRIVATE KEY-----\n'
httpx.post('http://localhost:8080/path?a=b', data=b'The bytes',
auth=HttpSignatureWithBodyDigest(key_id, pem_private_key))
with freeze_time('2012-01-14 03:21:34'):
make_request()
received_headers_dict = dict((key.lower(), value) for key, value in received_headers)
self.assertEqual(received_headers_dict, {
'host': 'localhost:8080',
'signature': 'keyId="my-key", created=1326511294, headers="(created) (request-target) '
'host user-agent accept accept-encoding content-length digest", '
'signature="jgNe5f7OFtQqxhaBUbxGedyrEbyPihe/ux/B/B6T0xbkvHnDKPg/bvlINBWDf'
'eM3r0bmlKG9eazjkr10iIIfCw=="',
'user-agent': 'python-httpx/0.13.3',
'accept-encoding': 'gzip, deflate',
'accept': '*/*',
'connection': 'keep-alive',
'content-length': '9',
'digest': 'SHA512=Jpu2uP4aOrJMRxr5j9NKiqwK0ksXiftpjdHOGJTU4v7BxYvf/nEYHxeWL7YCsFXE3XJ9'
'q2luOWXKpCQmDaQxCg=='
})
def test_case_hs2019(self):
# The test case in the draft specifies SHA-512, however, from trial and error,
# the given signature must have been generated using SHA-256
rsa_private_key = \
b'-----BEGIN RSA PRIVATE KEY-----\n' \
b'MIIEqAIBAAKCAQEAhAKYdtoeoy8zcAcR874L8cnZxKzAGwd7v36APp7Pv6Q2jdsP\n' \
b'BRrwWEBnez6d0UDKDwGbc6nxfEXAy5mbhgajzrw3MOEt8uA5txSKobBpKDeBLOsd\n' \
b'JKFqMGmXCQvEG7YemcxDTRPxAleIAgYYRjTSd/QBwVW9OwNFhekro3RtlinV0a75\n' \
b'jfZgkne/YiktSvLG34lw2zqXBDTC5NHROUqGTlML4PlNZS5Ri2U4aCNx2rUPRcKI\n' \
b'lE0PuKxI4T+HIaFpv8+rdV6eUgOrB2xeI1dSFFn/nnv5OoZJEIB+VmuKn3DCUcCZ\n' \
b'SFlQPSXSfBDiUGhwOw76WuSSsf1D4b/vLoJ10wIDAQABAoIBAG/JZuSWdoVHbi56\n' \
b'vjgCgkjg3lkO1KrO3nrdm6nrgA9P9qaPjxuKoWaKO1cBQlE1pSWp/cKncYgD5WxE\n' \
b'CpAnRUXG2pG4zdkzCYzAh1i+c34L6oZoHsirK6oNcEnHveydfzJL5934egm6p8DW\n' \
b'+m1RQ70yUt4uRc0YSor+q1LGJvGQHReF0WmJBZHrhz5e63Pq7lE0gIwuBqL8SMaA\n' \
b'yRXtK+JGxZpImTq+NHvEWWCu09SCq0r838ceQI55SvzmTkwqtC+8AT2zFviMZkKR\n' \
b'Qo6SPsrqItxZWRty2izawTF0Bf5S2VAx7O+6t3wBsQ1s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key, password=None, backend=default_backend())
def sign(data):
return private_key.sign(
data,
padding=padding.PKCS1v15(),
algorithm=hashes.SHA256(),
)
path = '/foo?param=value&pet=dog'
headers = (
('host', 'example.com'),
('date', 'Tue, 07 Jun 2014 20:51:35 GMT'),
('content-type', 'application/json'),
('digest', 'SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE='),
('content-length', '18'),
)
with freeze_time('2014-06-07 19:51:35 UTC'):
signed_headers = sign_headers('test-key-a', sign, 'POST', path, headers)
sig_header = dict(signed_headers)['signature']
expected_signature = \
'KXUj1H3ZOhv3Nk4xlRLTn4bOMlMOmFiud3VXrMa9MaLCxnVmrqOX5BulRvB65YW/wQp0o' \
'T/nNQpXgOYeY8ovmHlpkRyz5buNDqoOpRsCpLGxsIJ9cX8XVsM9jy+Q1+RIlD9wfWoPHh' \
'qhoXt35ZkasuIDPF/AETuObs9QydlsqONwbK+TdQguDK/8Va1Pocl6wK1uLwqcXlxhPEb' \
'55EmdYB9pddDyHTADING7K4qMwof2mC3t8Pb0yoLZoZX5a4Or4FrCCKK/9BHAhq/RsVk0' \
'dTENMbTB4i7cHvKQu+o9xuYWuxyvBa0Z6NdOb0di70cdrSDEsL5Gz7LBY5J2N9KdGg=='
expected_sig_header = \
f'keyId="test-key-a", created=1402170695, headers="(created) (request-target) host ' \
f'date content-type digest content-length", signature="{expected_signature}"'
self.assertEqual(expected_sig_header, sig_header)