-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement BasicAuth decode classmethod. #744
Changes from 1 commit
337346e
ef0e9f7
ef37fa9
9e8bd58
7617ffa
3eff09a
06b5f0e
12fe2c2
c351ca7
1f14c40
d398708
46db268
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this case exists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because https://github.com/rdegges/python-basicauth implements it. Happy to remove it if you want. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would be better strict to the specification where auth scheme is mandatory. Unlikely |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't encoding be passed here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops. Fixed. |
||
|
||
def encode(self): | ||
"""Encode credentials.""" | ||
creds = ('%s:%s' % (self.login, self.password)).encode(self.encoding) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use
:param str auth_header: ...
today, but aiohttp doesn't uses docstring in code, but separate files under doc/ directory.