forked from extern/httpie-cli
c6cbc7dfa5
* Uniformize UTF-8 naming
Replace `utf8` -> `utf-8` everywhere.
It should have no impact, `utf8` is an alias of `utf-8` [1].
[1] ee03bad25e/Lib/encodings/aliases.py (L534)
* Always specify the encoding
Let's be explicit over implicit. And prevent future warnings from PEP-597 [1].
[1] https://www.python.org/dev/peps/pep-0597/#using-the-default-encoding-is-a-common-mistake
* Update `UTF8` constant (`utf-8` -> `utf_8`)
* Remove default argument from `str.encode()` and `bytes.decode()`
* Clean-up
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
from base64 import b64encode
|
|
|
|
import requests.auth
|
|
|
|
from .base import AuthPlugin
|
|
|
|
|
|
# noinspection PyAbstractClass
|
|
class BuiltinAuthPlugin(AuthPlugin):
|
|
package_name = '(builtin)'
|
|
|
|
|
|
class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
|
|
|
|
def __call__(
|
|
self,
|
|
request: requests.PreparedRequest
|
|
) -> requests.PreparedRequest:
|
|
"""
|
|
Override username/password serialization to allow unicode.
|
|
|
|
See https://github.com/httpie/httpie/issues/212
|
|
|
|
"""
|
|
# noinspection PyTypeChecker
|
|
request.headers['Authorization'] = type(self).make_header(
|
|
self.username, self.password).encode('latin1')
|
|
return request
|
|
|
|
@staticmethod
|
|
def make_header(username: str, password: str) -> str:
|
|
credentials = f'{username}:{password}'
|
|
token = b64encode(credentials.encode()).strip().decode('latin1')
|
|
return f'Basic {token}'
|
|
|
|
|
|
class BasicAuthPlugin(BuiltinAuthPlugin):
|
|
name = 'Basic HTTP auth'
|
|
auth_type = 'basic'
|
|
netrc_parse = True
|
|
|
|
# noinspection PyMethodOverriding
|
|
def get_auth(self, username: str, password: str) -> HTTPBasicAuth:
|
|
return HTTPBasicAuth(username, password)
|
|
|
|
|
|
class DigestAuthPlugin(BuiltinAuthPlugin):
|
|
name = 'Digest HTTP auth'
|
|
auth_type = 'digest'
|
|
netrc_parse = True
|
|
|
|
# noinspection PyMethodOverriding
|
|
def get_auth(
|
|
self,
|
|
username: str,
|
|
password: str
|
|
) -> requests.auth.HTTPDigestAuth:
|
|
return requests.auth.HTTPDigestAuth(username, password)
|