From 2ec48046728a4388527e0775acf29a34d41db28a Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 12 Mar 2020 19:14:33 +0000 Subject: [PATCH] utf8 encode textual basic auth username/password, pass bytes unchanged --- setup.py | 2 +- src/treq/auth.py | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 4a0985fd..2d1025b5 100644 --- a/setup.py +++ b/setup.py @@ -29,7 +29,7 @@ install_requires=[ "incremental", "requests >= 2.1.0", - "six", + "six >= 1.12.0", "Twisted[tls] >= 16.4.0 ; python_version < '3.7'", "Twisted[tls] >= 18.7.0 ; python_version >= '3.7'", "attrs", diff --git a/src/treq/auth.py b/src/treq/auth.py index d693ff33..e1b905da 100644 --- a/src/treq/auth.py +++ b/src/treq/auth.py @@ -1,8 +1,11 @@ from __future__ import absolute_import, division, print_function -from twisted.web.http_headers import Headers import base64 +import six + +from twisted.web.http_headers import Headers + class UnknownAuthConfig(Exception): def __init__(self, config): @@ -28,7 +31,8 @@ def request(self, method, uri, headers=None, bodyProducer=None): def add_basic_auth(agent, username, password): creds = base64.b64encode( - '{0}:{1}'.format(username, password).encode('ascii')) + b':'.join([six.ensure_binary(username), six.ensure_binary(password)]) + ) return _RequestHeaderSettingAgent( agent, Headers({b'Authorization': [b'Basic ' + creds]}))