Skip to content

Commit

Permalink
Implement BasicAuth decode classmethod.
Browse files Browse the repository at this point in the history
  • Loading branch information
luhn committed Jan 19, 2016
1 parent 078fafd commit 27a5feb
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
36 changes: 36 additions & 0 deletions aiohttp/helpers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Various helper functions"""
import base64
import binascii
import datetime
import functools
import io
Expand Down Expand Up @@ -31,6 +32,41 @@ def __new__(cls, login, password='', encoding='latin1'):

return super().__new__(cls, login, password, encoding)

@classmethod
def decode(cls, auth_header, encoding='latin1'):
"""Create a :class:`BasicAuth` object from an ``Authorization`` HTTP
header.
:param auth_header: The value of the ``Authorization`` header.
:type auth_header: str
:param encoding: The character encoding used on the password.
:type encoding: str
:returns: The decoded authentication.
:rtype: :class:`BasicAuth`
:raises ValueError: if the headers are unable to be decoded.
"""
split = auth_header.strip().split(' ')
if len(split) == 2:
if split[0].strip().lower() != 'basic':
raise ValueError('Unknown authorization method %s' % split[0])
to_decode = split[1]
elif len(split) == 1:
to_decode = split[0]
else:
raise ValueError('Could not parse authorization header.')

try:
username, _, password = base64.b64decode(
to_decode.encode('ascii')
).decode(encoding).partition(':')
except binascii.Error:
raise ValueError('Invalid base64 encoding.')

return cls(username, password)

def encode(self):
"""Encode credentials."""
creds = ('%s:%s' % (self.login, self.password)).encode(self.encoding)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ def test_basic_auth4():
assert auth.encode() == 'Basic bmtpbTpwd2Q='


def test_basic_auth_decode():
auth = helpers.BasicAuth.decode('Basic bmtpbTpwd2Q=')
assert auth.login == 'nkim'
assert auth.password == 'pwd'


def test_basic_auth_decode_not_basic():
with pytest.raises(ValueError):
helpers.BasicAuth.decode('Complex bmtpbTpwd2Q=')


def test_basic_auth_decode_bad_base64():
with pytest.raises(ValueError):
helpers.BasicAuth.decode('Basic bmtpbTpwd2Q')


def test_invalid_formdata_params():
with pytest.raises(TypeError):
helpers.FormData('asdasf')
Expand Down

0 comments on commit 27a5feb

Please sign in to comment.