geopython / geopython/stetl

Base64 encoding in HTTP Auth modules requires Python3 Unicode string handling

Open
#130 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
88
Forks
33
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
Base64 encoding requires proper Python3 string handling

**To Reproduce**
Steps to reproduce the behavior, e.g.:

1. Configure a `stetl.inputs.HttpInput` with Basic Auth
2. Running it
3. Expected to see successful
4. Seeing error : something like *TypeError: expected bytes-like object, not str*

**Expected Behavior**
No error.

**Screenshots or Logfiles**
This is a left-over from the Python2 to Python3 migration: need to use Unicode Strings in Python3.
Many examples like https://stackoverflow.com/questions/53340627/typeerror-expected-bytes-like-object-not-str

**Context (please complete one or more from the following information):**
- OS: any
- Python Version: 3.7
- Stetl Version 2.1
- Stetl Input/Output/Filter Component: `stetl.inputs.HttpInput` and `stetl.outputs.HttpOutput` (maybe more)
- Stetl Config file NA

If running with Docker:
- Docker installed version
- Stetl Docker Image version: 2.1

**Additional context**
Current version `stetl.inputs.HttpInput`.

```
def add_authorization(self, request):
"""
Add authorization from config data. Authorization scheme-specific.
May be extended or overloaded for additional schemes.

:param request: the HTTP Request
:return:
"""
auth_creds = self.auth
auth_type = auth_creds['type']
auth_val = None
if auth_type == 'basic':
# Basic auth: http://mozgovipc.blogspot.nl/2012/06/python-http-basic-authentication-with.html
# base64 encode username and password
# write the Authorization header like: 'Basic base64encode(username + ':' + password)
auth_val = base64.encodestring('%s:%s' % (auth_creds['user'], auth_creds['password']))
auth_val = "Basic %s" % auth_val
elif auth_type == 'token':
# Bearer Type, see eg. https://tools.ietf.org/html/rfc6750
auth_val = "%s %s" % (auth_creds['keyword'], auth_creds['token'])

request.add_header("Authorization", auth_val.replace('\n', ''))
```

must become something like:

```
def add_authorization(self, request):
"""
Add authorization from config data. Authorization scheme-specific.
May be extended or overloaded for additional schemes.

:param request: the HTTP Request
:return:
"""
auth_creds = self.auth
auth_type = auth_creds['type']
auth_val = None
if auth_type == 'basic':
# Basic auth: http://mozgovipc.blogspot.nl/2012/06/python-http-basic-authentication-with.html
# base64 encode username and password
# write the Authorization header like: 'Basic base64encode(username + ':' + password)
auth_val = base64.encodebytes(
'{}:{}'.format(auth_creds['user'], auth_creds['password']).encode())
auth_val = 'Basic {}'.format(auth_val.decode())
elif auth_type == 'token':
# Bearer Type, see eg. https://tools.ietf.org/html/rfc6750
auth_val = "%s %s" % (auth_creds['keyword'], auth_creds['token'])

request.add_header("Authorization", auth_val.replace('\n', ''))

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.