-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NXPY-218: Introduce the BasicAuth class
- Loading branch information
Showing
8 changed files
with
145 additions
and
23 deletions.
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
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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
|
||
from .basic import BasicAuth | ||
from .jwt import JWTAuth | ||
from .oauth2 import OAuth2 | ||
from .portal_sso import PortalSSOAuth | ||
from .token import TokenAuth | ||
|
||
__all__ = ("JWTAuth", "OAuth2", "PortalSSOAuth", "TokenAuth") | ||
__all__ = ("BasicAuth", "JWTAuth", "OAuth2", "PortalSSOAuth", "TokenAuth") |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# coding: utf-8 | ||
from __future__ import unicode_literals | ||
import base64 | ||
|
||
from ..compat import get_bytes, get_text | ||
from .base import AuthBase | ||
|
||
try: | ||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from typing import Text | ||
from requests import Request | ||
except ImportError: | ||
pass | ||
|
||
|
||
class BasicAuth(AuthBase): | ||
""" Attaches a simple Basic Authentication to the given Request object. """ | ||
|
||
__slots__ = ("username", "password", "_token_header") | ||
|
||
AUTHORIZATION = get_bytes("Authorization") | ||
|
||
def __init__(self, username, password): | ||
# type: (Text, Text) -> None | ||
self.username = username | ||
self.password = password | ||
self.set_token(password) | ||
|
||
def set_token(self, token): | ||
# type: (Text) -> None | ||
"""Apply the given *token*.""" | ||
self.password = token | ||
self._token_header = "Basic " + get_text( | ||
base64.b64encode(get_bytes(self.username + ":" + self.password)) | ||
) | ||
|
||
def __eq__(self, other): | ||
# type: (object) -> bool | ||
return all( | ||
[ | ||
self.username == getattr(other, "username", None), | ||
self.password == getattr(other, "password", None), | ||
] | ||
) | ||
|
||
def __ne__(self, other): | ||
# type: (object) -> bool | ||
return not self == other | ||
|
||
def __call__(self, r): | ||
# type: (Request) -> Request | ||
r.headers[self.AUTHORIZATION] = self._token_header | ||
return r |
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
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
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
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
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