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

feat: refresh JWT whenever needed #134

Merged
merged 4 commits into from
Oct 26, 2022
Merged

feat: refresh JWT whenever needed #134

merged 4 commits into from
Oct 26, 2022

Conversation

betodealmeida
Copy link
Member

@betodealmeida betodealmeida commented Oct 25, 2022

A customer needs to run a really big import (~12 hours), but the JWT is only valid for 5. So I added some logic to refresh authentication (across all mechanisms) whenever needed.

The main auth class (simplified) now looks like this:

class Auth:  # pylint: disable=too-few-public-methods
    def __init__(self):
        self.session = Session()

        # call ``self.reauth`` after every request
        self.session.hooks["response"].append(self.reauth)

    def auth(self) -> None:
        raise NotImplementedError("Must be implemented for reauthorizing")

    def reauth(self, r: Response, *args: Any, **kwargs: Any) -> Response:
        if r.status_code != 401:
            return r

        # if the response was 401, auth and try again
        try:
            self.auth()
        except NotImplementedError:
            return r

        self.session.headers.update(self.get_headers())
        r.request.headers.update(self.get_headers())
        return self.session.send(r.request, verify=False)

On __init__ it attaches a hook to the session, calling reauth after every request. On reauth, if the response was a 401 and the derived class implements the auth method, then auth gets called to refresh cookies/tokens, and the request is resent.

This PR implements auth for the Preset and the username:password authentication mechanisms.

@@ -581,7 +581,7 @@ def __init__(self, auth: Auth):
self.graphql_client = GraphqlClient(endpoint=GRAPHQL_ENDPOINT)
self.baseurl = REST_ENDPOINT

self.session = auth.get_session()
self.session = auth.session
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got rid of get_session, since it only added noise.

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

Successfully merging this pull request may close these issues.

None yet

2 participants