2014-04-26 16:06:51 +03:00
|
|
|
from base64 import b64encode
|
|
|
|
|
2013-09-22 00:46:15 +03:00
|
|
|
import requests.auth
|
|
|
|
|
2014-04-29 00:33:30 +03:00
|
|
|
from httpie.plugins.base import AuthPlugin
|
2013-09-22 00:46:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BuiltinAuthPlugin(AuthPlugin):
|
|
|
|
|
|
|
|
package_name = '(builtin)'
|
|
|
|
|
|
|
|
|
2014-04-26 16:06:51 +03:00
|
|
|
class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
|
|
|
|
|
|
|
|
def __call__(self, r):
|
|
|
|
"""
|
|
|
|
Override username/password serialization to allow unicode.
|
|
|
|
|
2015-07-03 18:55:45 +02:00
|
|
|
See https://github.com/jkbrzt/httpie/issues/212
|
2014-04-26 16:06:51 +03:00
|
|
|
|
|
|
|
"""
|
2014-04-26 19:16:30 +03:00
|
|
|
r.headers['Authorization'] = type(self).make_header(
|
|
|
|
self.username, self.password).encode('latin1')
|
2014-04-26 16:06:51 +03:00
|
|
|
return r
|
|
|
|
|
2014-04-26 19:16:30 +03:00
|
|
|
@staticmethod
|
|
|
|
def make_header(username, password):
|
|
|
|
credentials = u'%s:%s' % (username, password)
|
|
|
|
token = b64encode(credentials.encode('utf8')).strip().decode('latin1')
|
|
|
|
return 'Basic %s' % token
|
|
|
|
|
2014-04-26 16:06:51 +03:00
|
|
|
|
2013-09-22 00:46:15 +03:00
|
|
|
class BasicAuthPlugin(BuiltinAuthPlugin):
|
|
|
|
|
|
|
|
name = 'Basic HTTP auth'
|
|
|
|
auth_type = 'basic'
|
|
|
|
|
|
|
|
def get_auth(self, username, password):
|
2014-04-26 16:06:51 +03:00
|
|
|
return HTTPBasicAuth(username, password)
|
2013-09-22 00:46:15 +03:00
|
|
|
|
|
|
|
|
|
|
|
class DigestAuthPlugin(BuiltinAuthPlugin):
|
|
|
|
|
|
|
|
name = 'Digest HTTP auth'
|
|
|
|
auth_type = 'digest'
|
|
|
|
|
|
|
|
def get_auth(self, username, password):
|
|
|
|
return requests.auth.HTTPDigestAuth(username, password)
|