Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit c183b8e

Browse files
committed
Unified the default signing algo under one setting.
Setting httpsig.sign.DEFAULT_SIGN_ALGORITHM changes it for all future classes.
1 parent 76d2536 commit c183b8e

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

httpsig/requests_auth.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ class HTTPSignatureAuth(AuthBase):
1919
algorithm is one of the six specified algorithms
2020
headers is a list of http headers to be included in the signing string, defaulting to "Date" alone.
2121
'''
22-
def __init__(self, key_id='', secret='', algorithm='hmac-sha256',
23-
headers=None, allow_agent=False):
22+
def __init__(self, key_id='', secret='', algorithm=None, headers=None):
2423
headers = headers or []
2524
self.header_signer = HeaderSigner(key_id=key_id, secret=secret,
2625
algorithm=algorithm, headers=headers)

httpsig/sign.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
from .utils import *
99

1010

11+
DEFAULT_SIGN_ALGORITHM = "hmac-sha256"
12+
13+
1114
class Signer(object):
1215
"""
1316
When using an RSA algo, the secret is a PEM-encoded private key.
1417
When using an HMAC algo, the secret is the HMAC signing secret.
1518
1619
Password-protected keyfiles are not supported.
1720
"""
18-
def __init__(self, secret, algorithm='rsa-sha256'):
21+
def __init__(self, secret, algorithm=None):
22+
if algorithm is None:
23+
algorithm = DEFAULT_SIGN_ALGORITHM
24+
1925
assert algorithm in ALGORITHMS, "Unknown algorithm"
2026
if isinstance(secret, six.string_types): secret = secret.encode("ascii")
2127

@@ -67,12 +73,15 @@ class HeaderSigner(Signer):
6773
Generic object that will sign headers as a dictionary using the http-signature scheme.
6874
https://github.com/joyent/node-http-signature/blob/master/http_signing.md
6975
70-
key_id is the mandatory label indicating to the server which secret to use
71-
secret is the filename of a pem file in the case of rsa, a password string in the case of an hmac algorithm
72-
algorithm is one of the six specified algorithms
73-
headers is a list of http headers to be included in the signing string, defaulting to ['date'].
76+
:arg key_id: the mandatory label indicating to the server which secret to use
77+
:arg secret: a PEM-encoded RSA private key or an HMAC secret (must match the algorithm)
78+
:arg algorithm: one of the six specified algorithms
79+
:arg headers: a list of http headers to be included in the signing string, defaulting to ['date'].
7480
'''
75-
def __init__(self, key_id, secret, algorithm='rsa-sha256', headers=None):
81+
def __init__(self, key_id, secret, algorithm=None, headers=None):
82+
if algorithm is None:
83+
algorithm = DEFAULT_SIGN_ALGORITHM
84+
7685
super(HeaderSigner, self).__init__(secret=secret, algorithm=algorithm)
7786
self.headers = headers or ['date']
7887
self.signature_template = build_signature_template(key_id, algorithm, headers)

httpsig/tests/test_signature.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@
66
import json
77
import unittest
88

9-
from httpsig.sign import HeaderSigner
9+
import httpsig.sign as sign
1010
from httpsig.utils import parse_authorization_header
1111

1212

13+
sign.DEFAULT_SIGN_ALGORITHM = "rsa-sha256"
14+
15+
1316
class TestSign(unittest.TestCase):
1417

1518
def setUp(self):
1619
self.key_path = os.path.join(os.path.dirname(__file__), 'rsa_private.pem')
1720
self.key = open(self.key_path, 'rb').read()
1821

1922
def test_default(self):
20-
hs = HeaderSigner(key_id='Test', secret=self.key)
23+
hs = sign.HeaderSigner(key_id='Test', secret=self.key)
2124
unsigned = {
2225
'Date': 'Thu, 05 Jan 2012 21:31:40 GMT'
2326
}
@@ -35,7 +38,7 @@ def test_default(self):
3538
self.assertEqual(params['signature'], 'ATp0r26dbMIxOopqw0OfABDT7CKMIoENumuruOtarj8n/97Q3htHFYpH8yOSQk3Z5zh8UxUym6FYTb5+A0Nz3NRsXJibnYi7brE/4tx5But9kkFGzG+xpUmimN4c3TMN7OFH//+r8hBf7BT9/GmHDUVZT2JzWGLZES2xDOUuMtA=')
3639

3740
def test_all(self):
38-
hs = HeaderSigner(key_id='Test', secret=self.key, headers=[
41+
hs = sign.HeaderSigner(key_id='Test', secret=self.key, headers=[
3942
'(request-line)',
4043
'host',
4144
'date',

0 commit comments

Comments
 (0)