Skip to content
This repository was archived by the owner on Oct 25, 2024. It is now read-only.
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
18 changes: 9 additions & 9 deletions drf_httpsig/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
FAILED = exceptions.AuthenticationFailed('Invalid signature.')


class SignatureAuthentication(authentication.BaseAuthentication):
"""
DRF authentication class for HTTP Signature support.
Expand All @@ -38,7 +39,7 @@ class SignatureAuthentication(authentication.BaseAuthentication):
www_authenticate_realm = "api"
required_headers = ["(request-target)", "date"]

def fetch_user_data(self, keyId, algorithm=None):
def fetch_user_data(self, key_id, algorithm=None):
"""Retuns a tuple (User, secret) or (None, None)."""
raise NotImplementedError()

Expand All @@ -47,7 +48,7 @@ def authenticate_header(self, request):
DRF sends this for unauthenticated responses if we're the primary
authenticator.
"""
h = " ".join(required_headers)
h = " ".join(self.required_headers)
return 'Signature realm="%s",headers="%s"' % (self.www_authenticate_realm, h)

def authenticate(self, request):
Expand All @@ -73,14 +74,14 @@ def authenticate(self, request):
raise FAILED

# Ensure all required fields were included.
if len(set(("keyid","algorithm","signature")) - set(fields.keys())) > 0:
if len({"keyid", "algorithm", "signature"} - set(fields.keys())) > 0:
raise FAILED

# Fetch the secret associated with the keyid
user, secret = self.fetch_user_data(
fields["keyid"],
algorithm=fields["algorithm"]
)
)

if not (user and secret):
raise FAILED
Expand All @@ -90,22 +91,21 @@ def authenticate(self, request):
headers = {}
for key in request.META.keys():
if key.startswith("HTTP_") or \
key in ("CONTENT_TYPE", "CONTENT_LENGTH"):

key in ("CONTENT_TYPE", "CONTENT_LENGTH"):
header = key[5:].lower().replace('_', '-')
headers[header] = request.META[key]

# Verify headers
hs = HeaderVerifier(
headers,
secret,
required_headers=self.required_headers,
method=request.method.lower(),
path=request.get_full_path()
)
)

# All of that just to get to this.
if not hs.verify():
raise FAILED

return (user, fields["keyid"])
return user, fields["keyid"]
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Known to work with these versions. Versions bracketed for safety.
httpsig<2
djangorestframework<3
django<=1.7
djangorestframework>3
django>=1.7
httpsig>=1.3.0
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
setup_requires=['pytest-runner', 'setuptools_scm'],
tests_require=['pytest', 'pytest-django'],
install_requires=[
'djangorestframework<3',
'django<=1.7',
'httpsig<2'
'djangorestframework>3',
'django>=1.7',
'httpsig>=1.3.0'
]
)