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

Support using custom function in request.auth #564

Open
sohoffice opened this issue Jun 28, 2020 · 2 comments
Open

Support using custom function in request.auth #564

sohoffice opened this issue Jun 28, 2020 · 2 comments

Comments

@sohoffice
Copy link

sohoffice commented Jun 28, 2020

As stated in the requests document, user may pass a sub class of AuthBase as the auth parameter. This is very useful when the authentication is a little bit more complicated than passing the session token or basic auth.

I guess this can be worked around by allowing user to pass a custom function which return AuthBase subclass? Something similar to the below.

request:
  auth:
    $ext:
      function: security:prepare_auth
      extra_kwargs:
        username: "{username_variable}"

In the security.py, the caller can do the below

from requests.auth import AuthBase

class PizzaAuth(AuthBase):
    """Attaches HTTP Pizza Authentication to the given Request object."""
    def __init__(self, username):
        # setup any auth-related data here
        self.username = username

    def __call__(self, r):
        # modify and return the request
        r.headers['X-Pizza'] = self.username
        return r

def prepare_auth(**kwargs):
    username = kwargs['username']
    return PizzaAuth(username)

I hope this makes sense ?

I do have a fix which can be submitted shortly. Let me know what you think.

@sohoffice
Copy link
Author

In case anyone is interested, I've a branch for the above feature.
The branch allows us to use a custom function as request.auth.
It may be installed via pip install git+https://github.com/sohoffice/tavern.git@douglas/auth-function

@jesuslinares
Copy link

Any update on this?
It would be very useful to use along with requests-aws4auth.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants