Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Base64 encoding in HTTP Auth modules requires Python3 Unicode string handling #130

Open
justb4 opened this issue Apr 5, 2024 · 0 comments
Labels
Milestone

Comments

@justb4
Copy link
Member

justb4 commented Apr 5, 2024

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', ''))

@justb4 justb4 added the bug label Apr 5, 2024
@justb4 justb4 added this to the Version 2.2 milestone Apr 5, 2024
@justb4 justb4 changed the title (replace with your title) Base64 encoding in HTTP Auth modules requires Python3 Unicode string handling Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant