httpie-cli/httpie/plugins/builtin.py

44 lines
997 B
Python
Raw Normal View History

from base64 import b64encode
2013-09-21 23:46:15 +02:00
import requests.auth
from .base import AuthPlugin
class BuiltinAuthPlugin(AuthPlugin):
package_name = '(builtin)'
class HTTPBasicAuth(requests.auth.HTTPBasicAuth):
def __call__(self, r):
"""
Override username/password serialization to allow unicode.
See https://github.com/jkbr/httpie/issues/212
"""
credentials = u'%s:%s' % (self.username, self.password)
2014-04-26 17:16:11 +02:00
token = b64encode(credentials.encode('utf8')).strip().decode('latin1')
r.headers['Authorization'] = 'Basic %s' % token
return r
2013-09-21 23:46:15 +02:00
class BasicAuthPlugin(BuiltinAuthPlugin):
name = 'Basic HTTP auth'
auth_type = 'basic'
def get_auth(self, username, password):
return HTTPBasicAuth(username, password)
2013-09-21 23:46:15 +02:00
class DigestAuthPlugin(BuiltinAuthPlugin):
name = 'Digest HTTP auth'
auth_type = 'digest'
def get_auth(self, username, password):
return requests.auth.HTTPDigestAuth(username, password)