Skip to content
This repository has been archived by the owner on Oct 25, 2024. It is now read-only.

Add additional key tests #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions httpsig/tests/rsa_private_2.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpQIBAAKCAQEA42O9foFVWSgIjh+uUn3Up2hF54tbv48deni0ju+kFqYJVzxw
G23fFSplbhUzL1eilBLRQWC18fGKe1Ae28mgfvtjHM+oN3omuKZMyqzTppOQKMt8
GifGiX9mzdaOhYDozkER16b4UptFuUYYeOZR397jpvQD485YZ/bQoZRoBe641HWe
MeryU/4ByIFnJXLnezURe+SbIlQWmt87FPrTVAuCMFP9IlN8fGBZ5DOeudkrW/vd
Hkh9MG+CGjiNAFDgko0zJr24YS0KotCGA8g/0ARs5KWnIRpiAkvJD9WrLORfLeM/
3GDWVO9bg4bDjh53ktSmMegUlXZEU4mYtMSorwIDAQABAoIBAQCMQbFYnp3uaGse
fxwfdc39lKvJroPhHH2qVVnJ8Lbd4XxRes4LnTHE9GmsBReVVBDMFO9NzEFuAvbQ
KRNfrnZ43Ma9trijv4yyyKEFQqUqUkrRXvIlj/M+TH805+rxHhp7XleLFUqGaZAQ
P9RIfohx9iGlkU307gpJ1kvJ3qhrPzLM1oaMIGzB26LnUeiFRx2nhi2mMv/wqkhm
muZ+vFLABJQu+CLDAz112EwkjLyhzv+aGVEdpNU6jVeG7ej9Bxm5chau9SIWj4Sg
Eg6Jq/YswQ1QFb/RBoNuftr7Pr8x+DnOx0AW4P/nrqsVvmCf2QKhHE+bB01IvMn9
F8Ss97DBAoGBAPK0QcOGwLYTYkzcRSuA4RWlUq9huQ96Ssy3y5vwgrzosfV1T9Y/
yF6vwijZeTV/B/Ew/pBcRPgEl1exNibByg0qPApfL9V9xv+oMjEZr5WQoeQhff8D
hpeP1znbNZKaJzFAAHE1p8mAmSPjggKd+M1F/zH86sads2istp7kE5oNAoGBAO/Y
tW259w9mtEx82Pw3zGsMhn+ca6Vetn+igthhmfjBBm+TrKVe1RTTN+Pz7JxOp3jU
wVhdw8Sa7pigMpSVDvcSHXt0cVs9d5pOoEQFInGnC9Y4A9auvL+5uT/AspKQqlwJ
8lE4ugU5xelfnd4UnTbTZsA0pEmpRWYz9cUleUqrAoGBAJb3MtTpc+ONiR6gANJ/
lwJ81ELNOWx8OBzA0oCMCPZyk4dH0kJFaGAlWVNJetCg1JLdU+r9ydSwHmdojnNr
JUAtHi2hacKVOvv5ZkcOYB5bWafqgVHnooeR7GFLjjZDds92rQ++PUDkod0l/c+c
7Y7Z1FQGqw6iN1+L1whcfCjdAoGALQwhv1MfQQhEMt18D5sXTL/UfyFM4olwc3Z9
ZtCmD5+Jaulskr7F/JxTMgA9X3oyYDJtggBSZZnH2Gi2N0vrvKHtYaOUKtPXo+bC
mSPxvTDnuztYFTlKgCQM1BmIMJj1NkYz+edSoOqh7lwkFXL/uC1qbg01EhS10SVP
MtD///MCgYEA4dMkdsM4pDauvHsfiGH0kuSV8doWpyZIcRXpZk5aSVKxQY1X0RXu
ra38P+PzSfAx1Wpo1tU5k+I0aRKImZv7EB7gwrsGnpFoDL5VzTfJrZh8u9TKrqaz
RuPfYlljHQ7GXO528c6ePdaaDCwNvtqAXRPm0AFiemaQP5s5cc2u2l8=
-----END RSA PRIVATE KEY-----
9 changes: 9 additions & 0 deletions httpsig/tests/rsa_public_2.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA42O9foFVWSgIjh+uUn3U
p2hF54tbv48deni0ju+kFqYJVzxwG23fFSplbhUzL1eilBLRQWC18fGKe1Ae28mg
fvtjHM+oN3omuKZMyqzTppOQKMt8GifGiX9mzdaOhYDozkER16b4UptFuUYYeOZR
397jpvQD485YZ/bQoZRoBe641HWeMeryU/4ByIFnJXLnezURe+SbIlQWmt87FPrT
VAuCMFP9IlN8fGBZ5DOeudkrW/vdHkh9MG+CGjiNAFDgko0zJr24YS0KotCGA8g/
0ARs5KWnIRpiAkvJD9WrLORfLeM/3GDWVO9bg4bDjh53ktSmMegUlXZEU4mYtMSo
rwIDAQAB
-----END PUBLIC KEY-----
24 changes: 24 additions & 0 deletions httpsig/tests/test_signature.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def setUp(self):
with open(self.key_path, 'rb') as f:
self.key = f.read()

self.other_key_path = os.path.join(
os.path.dirname(__file__), 'rsa_private_2.pem')
with open(self.other_key_path, 'rb') as f:
self.other_key = f.read()

def test_default(self):
hs = sign.HeaderSigner(key_id='Test', secret=self.key)
unsigned = {
Expand All @@ -46,6 +51,25 @@ def test_default(self):
self.assertEqual(params['algorithm'], 'rsa-sha256')
self.assertEqual(params['signature'], 'jKyvPcxB4JbmYY4mByyBY7cZfNl4OW9HpFQlG7N4YcJPteKTu4MWCLyk+gIr0wDgqtLWf9NLpMAMimdfsH7FSWGfbMFSrsVTHNTk0rK3usrfFnti1dxsM4jl0kYJCKTGI/UWkqiaxwNiKqGcdlEDrTcUhhsFsOIo8VhddmZTZ8w=') # noqa: E501

def test_other_default(self):
hs = sign.HeaderSigner(key_id='Test', secret=self.other_key)
unsigned = {
'Date': self.header_date
}
signed = hs.sign(unsigned)
self.assertIn('Date', signed)
self.assertEqual(unsigned['Date'], signed['Date'])
self.assertIn('Authorization', signed)
auth = parse_authorization_header(signed['authorization'])
params = auth[1]
self.assertIn('keyId', params)
self.assertIn('algorithm', params)
self.assertIn('signature', params)
self.assertEqual(params['keyId'], 'Test')
self.assertEqual(params['algorithm'], 'rsa-sha256')
self.assertEqual(params['signature'],
'GY3Yyuj92xScIb2QbDUWxIW/fg7ZP8rxURltbpouTGxTo+ZRDHO9BbfN6YQeP1Z0VJBEA0dgynuzQs2bVBJavTcoEgvttzznAIj9ypfI6n35Uzeid+9gepa0pfBom6qnoNbblMNsHt7hXBfrpe5EwfEKmpqZgivjJZ53p9gD1NAhlioty/m1MFu1J5wEjpgX466R2PmR10yl22rMcv3mbEPV5ijqLTViDW18DchLyHR+fItzRtor2yLv7QgBSw+gVJu0dVeKeL9kwPxsaurzODgYsFsjZOJvuP9nKPJOdH3PI6eDhpfwjmwhjbSTte3bjkbw0w5tlWuA8m5l1gzyBQ==')

def test_basic(self):
hs = sign.HeaderSigner(key_id='Test', secret=self.key, headers=[
'(request-target)',
Expand Down
70 changes: 70 additions & 0 deletions httpsig/tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,73 @@ def setUp(self):

class TestVerifyRSASHA512ChangeHeader(TestVerifyRSASHA1):
sign_header = 'Signature'


class TestSignAndVerify(unittest.TestCase):
header_date = 'Thu, 05 Jan 2014 21:31:40 GMT'
sign_header = 'authorization'

def setUp(self):
with open(os.path.join(os.path.dirname(__file__), 'rsa_private.pem'), 'rb') as f:
self.private_key = f.read()

with open(os.path.join(os.path.dirname(__file__), 'rsa_public.pem'), 'rb') as f:
self.public_key = f.read()

with open(os.path.join(os.path.dirname(__file__), 'rsa_private_2.pem'), 'rb') as f:
self.other_private_key = f.read()

with open(os.path.join(os.path.dirname(__file__), 'rsa_public_2.pem'), 'rb') as f:
self.other_public_key = f.read()

def test_default(self):
unsigned = {
'Date': self.header_date
}

hs = HeaderSigner(
key_id="Test", secret=self.private_key, algorithm='rsa-sha1',
sign_header=self.sign_header)
signed = hs.sign(unsigned)
hv = HeaderVerifier(
headers=signed, secret=self.public_key, sign_header=self.sign_header)
self.assertTrue(hv.verify())

def test_other_default(self):
unsigned = {
'Date': self.header_date
}

hs = HeaderSigner(
key_id="Test", secret=self.other_private_key, algorithm='rsa-sha256',
sign_header=self.sign_header)
signed = hs.sign(unsigned)
hv = HeaderVerifier(
headers=signed, secret=self.other_public_key, sign_header=self.sign_header)
self.assertTrue(hv.verify())

def test_mix_default_1_256(self):
unsigned = {
'Date': self.header_date
}

hs = HeaderSigner(
key_id="Test", secret=self.private_key, algorithm='rsa-sha1',
sign_header=self.sign_header)
signed = hs.sign(unsigned)
hv = HeaderVerifier(
headers=signed, secret=self.other_public_key, sign_header=self.sign_header)
self.assertFalse(hv.verify())

def test_mix_default_256_1(self):
unsigned = {
'Date': self.header_date
}

hs = HeaderSigner(
key_id="Test", secret=self.other_private_key, algorithm='rsa-sha256',
sign_header=self.sign_header)
signed = hs.sign(unsigned)
hv = HeaderVerifier(
headers=signed, secret=self.public_key, sign_header=self.sign_header)
self.assertFalse(hv.verify())