beanstalk.py: Encode and decode strings correctly.

This commit is contained in:
Eklavya Sharma
2016-07-05 09:18:40 +05:30
committed by Tim Abbott
parent 26b8e7357a
commit 6c3f1bb967
2 changed files with 6 additions and 5 deletions

View File

@@ -42,7 +42,6 @@ exclude_py2 = []
exclude_py3 = """ exclude_py3 = """
zerver/lib/ccache.py zerver/lib/ccache.py
zerver/tests/test_i18n.py zerver/tests/test_i18n.py
zerver/views/webhooks/beanstalk.py
""".split() """.split()
parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.") parser = argparse.ArgumentParser(description="Run mypy on files tracked by git.")

View File

@@ -14,6 +14,7 @@ from functools import wraps
from .github import build_message_from_gitlog from .github import build_message_from_gitlog
from typing import Any, Callable, Dict from typing import Any, Callable, Dict
from zerver.lib.str_utils import force_str, force_bytes
# Beanstalk's web hook UI rejects url with a @ in the username section of a url # Beanstalk's web hook UI rejects url with a @ in the username section of a url
@@ -25,12 +26,13 @@ def beanstalk_decoder(view_func):
def _wrapped_view_func(request, *args, **kwargs): def _wrapped_view_func(request, *args, **kwargs):
# type: (HttpRequest, *Any, **Any) -> HttpResponse # type: (HttpRequest, *Any, **Any) -> HttpResponse
try: try:
auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() auth_type, encoded_value = request.META['HTTP_AUTHORIZATION'].split() # type: str, str
if auth_type.lower() == "basic": if auth_type.lower() == "basic":
email, api_key = base64.b64decode(encoded_value).split(":") email, api_key = base64.b64decode(force_bytes(encoded_value)).decode('utf-8').split(":")
email = email.replace('%40', '@') email = email.replace('%40', '@')
request.META['HTTP_AUTHORIZATION'] = "Basic %s" % (base64.b64encode("%s:%s" % (email, api_key))) credentials = u"%s:%s" % (email, api_key)
except: request.META['HTTP_AUTHORIZATION'] = "Basic " + force_str(base64.b64encode(credentials.encode('utf-8')))
except Exception:
pass pass
return view_func(request, *args, **kwargs) return view_func(request, *args, **kwargs)