-
Notifications
You must be signed in to change notification settings - Fork 887
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
ref #2659 public HTTP Basic credentials extraction #2662
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c895f87
ref #2659 public HTTP Basic credentials extraction
canni 362a581
Remove WebOb related logic
canni 744bf05
Add myself to contributors list & PEP8
canni d54e0ae
Merge upstream master
canni c0ddbc3
Merge remote-tracking branch 'upstream/master' into extract_http_basic
canni f2f196d
Merge branch 'master' into extract_http_basic
canni 830bcb8
Add docs & explict tests
canni 693cb09
Add this feature to chenges & small improvement
canni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1128,7 +1128,7 @@ def __init__(self, check, realm='Realm', debug=False): | |
|
||
def unauthenticated_userid(self, request): | ||
""" The userid parsed from the ``Authorization`` request header.""" | ||
credentials = self._get_credentials(request) | ||
credentials = extract_http_basic_credentials(request) | ||
if credentials: | ||
return credentials[0] | ||
|
||
|
@@ -1145,46 +1145,61 @@ def forget(self, request): | |
return [('WWW-Authenticate', 'Basic realm="%s"' % self.realm)] | ||
|
||
def callback(self, username, request): | ||
# Username arg is ignored. Unfortunately _get_credentials winds up | ||
# Username arg is ignored. Unfortunately extract_http_basic_credentials winds up | ||
# getting called twice when authenticated_userid is called. Avoiding | ||
# that, however, winds up duplicating logic from the superclass. | ||
credentials = self._get_credentials(request) | ||
credentials = extract_http_basic_credentials(request) | ||
if credentials: | ||
username, password = credentials | ||
return self.check(username, password, request) | ||
|
||
def _get_credentials(self, request): | ||
authorization = request.headers.get('Authorization') | ||
if not authorization: | ||
return None | ||
try: | ||
authmeth, auth = authorization.split(' ', 1) | ||
except ValueError: # not enough values to unpack | ||
return None | ||
if authmeth.lower() != 'basic': | ||
return None | ||
|
||
try: | ||
authbytes = b64decode(auth.strip()) | ||
except (TypeError, binascii.Error): # can't decode | ||
return None | ||
|
||
# try utf-8 first, then latin-1; see discussion in | ||
# https://github.com/Pylons/pyramid/issues/898 | ||
try: | ||
auth = authbytes.decode('utf-8') | ||
except UnicodeDecodeError: | ||
auth = authbytes.decode('latin-1') | ||
|
||
try: | ||
username, password = auth.split(':', 1) | ||
except ValueError: # not enough values to unpack | ||
return None | ||
return username, password | ||
|
||
class _SimpleSerializer(object): | ||
def loads(self, bstruct): | ||
return native_(bstruct) | ||
|
||
def dumps(self, appstruct): | ||
return bytes_(appstruct) | ||
|
||
|
||
def extract_http_basic_credentials(request): | ||
""" A helper function for extraction of HTTP Basic credentials | ||
from a given `request`. | ||
|
||
``request`` | ||
The request object | ||
""" | ||
try: | ||
# First try authorization extraction logic from WebOb | ||
try: | ||
authmeth, auth = request.authorization | ||
except AttributeError: # Probably a DummyRequest | ||
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 don't see the point in this fallback. It's trying to reproduce all of the logic, but likely with bugs and inconsistencies from the original |
||
authorization = request.headers.get('Authorization') | ||
if not authorization: | ||
return None | ||
|
||
authmeth, auth = authorization.split(' ', 1) | ||
except (ValueError, TypeError): | ||
# not enough values to unpack or None is not iterable | ||
return None | ||
|
||
if authmeth.lower() != 'basic': | ||
return None | ||
|
||
try: | ||
authbytes = b64decode(auth.strip()) | ||
except (TypeError, binascii.Error): # can't decode | ||
return None | ||
|
||
# try utf-8 first, then latin-1; see discussion in | ||
# https://github.com/Pylons/pyramid/issues/898 | ||
try: | ||
auth = authbytes.decode('utf-8') | ||
except UnicodeDecodeError: | ||
auth = authbytes.decode('latin-1') | ||
|
||
try: | ||
username, password = auth.split(':', 1) | ||
except ValueError: # not enough values to unpack | ||
return None | ||
return username, password |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This crosses 79 chars, but I will fix it in the merge.