forked from GetStream/stream-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_signature.py
More file actions
executable file
·84 lines (74 loc) · 3.09 KB
/
Copy pathtest_signature.py
File metadata and controls
executable file
·84 lines (74 loc) · 3.09 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
#!/usr/bin/env python
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
import json
import unittest
import stream.httpsig.sign as sign
from stream.httpsig.utils import parse_authorization_header
class TestSign(unittest.TestCase):
DEFAULT_SIGN_ALGORITHM = sign.DEFAULT_SIGN_ALGORITHM
def setUp(self):
sign.DEFAULT_SIGN_ALGORITHM = "rsa-sha256"
self.key_path = os.path.join(os.path.dirname(__file__), "rsa_private.pem")
with open(self.key_path, "rb") as f:
self.key = f.read()
def tearDown(self):
sign.DEFAULT_SIGN_ALGORITHM = self.DEFAULT_SIGN_ALGORITHM
def test_default(self):
hs = sign.HeaderSigner(key_id="Test", secret=self.key)
unsigned = {"Date": "Thu, 05 Jan 2012 21:31:40 GMT"}
signed = hs.sign(unsigned)
self.assertTrue("Date" in signed)
self.assertEqual(unsigned["Date"], signed["Date"])
self.assertTrue("Authorization" in signed)
auth = parse_authorization_header(signed["authorization"])
params = auth[1]
self.assertTrue("keyId" in params)
self.assertTrue("algorithm" in params)
self.assertTrue("signature" in params)
self.assertEqual(params["keyId"], "Test")
self.assertEqual(params["algorithm"], "rsa-sha256")
self.assertEqual(
params["signature"],
"ATp0r26dbMIxOopqw0OfABDT7CKMIoENumuruOtarj8n/97Q3htHFYpH8yOSQk3Z5zh8UxUym6FYTb5+A0Nz3NRsXJibnYi7brE/4tx5But9kkFGzG+xpUmimN4c3TMN7OFH//+r8hBf7BT9/GmHDUVZT2JzWGLZES2xDOUuMtA=",
)
def test_all(self):
hs = sign.HeaderSigner(
key_id="Test",
secret=self.key,
headers=[
"(request-target)",
"host",
"date",
"content-type",
"content-md5",
"content-length",
],
)
unsigned = {
"Host": "example.com",
"Date": "Thu, 05 Jan 2012 21:31:40 GMT",
"Content-Type": "application/json",
"Content-MD5": "Sd/dVLAcvNLSq16eXua5uQ==",
"Content-Length": "18",
}
signed = hs.sign(unsigned, method="POST", path="/foo?param=value&pet=dog")
self.assertTrue("Date" in signed)
self.assertEqual(unsigned["Date"], signed["Date"])
self.assertTrue("Authorization" in signed)
auth = parse_authorization_header(signed["authorization"])
params = auth[1]
self.assertTrue("keyId" in params)
self.assertTrue("algorithm" in params)
self.assertTrue("signature" in params)
self.assertEqual(params["keyId"], "Test")
self.assertEqual(params["algorithm"], "rsa-sha256")
self.assertEqual(
params["headers"],
"(request-target) host date content-type content-md5 content-length",
)
self.assertEqual(
params["signature"],
"G8/Uh6BBDaqldRi3VfFfklHSFoq8CMt5NUZiepq0q66e+fS3Up3BmXn0NbUnr3L1WgAAZGplifRAJqp2LgeZ5gXNk6UX9zV3hw5BERLWscWXlwX/dvHQES27lGRCvyFv3djHP6Plfd5mhPWRkmjnvqeOOSS0lZJYFYHJz994s6w=",
)