2014-04-26 15:06:51 +02:00
|
|
|
from base64 import b64encode
|
|
|
|
|
2013-09-21 23:46:15 +02:00
|
|
|
import requests.auth
|
|
|
|
|
2014-04-28 23:33:30 +02:00
|
|
|
from httpie.plugins.base import AuthPlugin
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
|
2016-07-04 20:30:55 +02:00
|
|
|
# noinspection PyAbstractClass
|
2013-09-21 23:46:15 +02:00
|
|
|
class BuiltinAuthPlugin(AuthPlugin):
|
|
|
|
package_name = '(builtin)'
|
|
|
|
|
|
|
|
|
2014-04-26 15:06:51 +02:00
|
|
|
class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
|
|
|
|
|
2019-08-31 18:33:54 +02:00
|
|
|
def __call__(
|
|
|
|
self,
|
|
|
|
request: requests.PreparedRequest
|
|
|
|
) -> requests.PreparedRequest:
|
2014-04-26 15:06:51 +02:00
|
|
|
"""
|
|
|
|
Override username/password serialization to allow unicode.
|
|
|
|
|
2017-03-10 11:27:38 +01:00
|
|
|
See https://github.com/jakubroztocil/httpie/issues/212
|
2014-04-26 15:06:51 +02:00
|
|
|
|
|
|
|
"""
|
2020-06-16 11:05:00 +02:00
|
|
|
# noinspection PyTypeChecker
|
2019-08-31 18:33:54 +02:00
|
|
|
request.headers['Authorization'] = type(self).make_header(
|
2014-04-26 18:16:30 +02:00
|
|
|
self.username, self.password).encode('latin1')
|
2019-08-31 18:33:54 +02:00
|
|
|
return request
|
2014-04-26 15:06:51 +02:00
|
|
|
|
2014-04-26 18:16:30 +02:00
|
|
|
@staticmethod
|
2019-08-31 18:33:54 +02:00
|
|
|
def make_header(username: str, password: str) -> str:
|
2014-04-26 18:16:30 +02:00
|
|
|
credentials = u'%s:%s' % (username, password)
|
|
|
|
token = b64encode(credentials.encode('utf8')).strip().decode('latin1')
|
|
|
|
return 'Basic %s' % token
|
|
|
|
|
2014-04-26 15:06:51 +02:00
|
|
|
|
2013-09-21 23:46:15 +02:00
|
|
|
class BasicAuthPlugin(BuiltinAuthPlugin):
|
|
|
|
name = 'Basic HTTP auth'
|
|
|
|
auth_type = 'basic'
|
2020-06-16 11:05:00 +02:00
|
|
|
netrc_parse = True
|
2013-09-21 23:46:15 +02:00
|
|
|
|
2016-11-23 22:01:58 +01:00
|
|
|
# noinspection PyMethodOverriding
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_auth(self, username: str, password: str) -> HTTPBasicAuth:
|
2014-04-26 15:06:51 +02:00
|
|
|
return HTTPBasicAuth(username, password)
|
2013-09-21 23:46:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DigestAuthPlugin(BuiltinAuthPlugin):
|
|
|
|
name = 'Digest HTTP auth'
|
|
|
|
auth_type = 'digest'
|
2020-06-16 11:05:00 +02:00
|
|
|
netrc_parse = True
|
2013-09-21 23:46:15 +02:00
|
|
|
|
2016-11-23 22:01:58 +01:00
|
|
|
# noinspection PyMethodOverriding
|
2019-08-31 18:33:54 +02:00
|
|
|
def get_auth(
|
|
|
|
self,
|
|
|
|
username: str,
|
|
|
|
password: str
|
|
|
|
) -> requests.auth.HTTPDigestAuth:
|
2013-09-21 23:46:15 +02:00
|
|
|
return requests.auth.HTTPDigestAuth(username, password)
|