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

Authorization with OAuth2 #1430

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

Authorization with OAuth2 #1430

wants to merge 1 commit into from

Conversation

binarin
Copy link

@binarin binarin commented May 16, 2023

This snippet shows a few possible approaches for regularly updating secrets for SelectConnection's using OAuth2. It's not a finished work, but a point to start a discussion about this feature, and how/whether it'd be possible to support it properly in API, instead of every user hand-rolling their own implementation. (For reference, Java client already has it - https://www.rabbitmq.com/api-guide.html#oauth2-support).

Turns out implementing OAuth2-flow is not completely trivial. While fetching a new secret is easy (it's implemented here using requests_oauthlib , but to reduce dependencies this can be done in pure http), doing this regularly is a bit more complex task. I've tried the following 2 approaches:

  • Doing a blocking http call in connection thread (see BlockingConnectionSecretRefresher). This is simple, but an OAuth2 http call takes ~0.3 second in my tests, which can be too much blocking.
  • Running a separate thread (see ThreadedConnectionSecretRefresher). In this case it has no negative effect on processing speed, but introduces a lot of moving parts.

There is also a question of what to do with all other connection adapters. It looks like methods similar call_later/add_callback_threadsafe are universally available, but e.g. for aio/twisted adapters it should be possible to do the same using non-blocking http calls.

This snippet shows a few possible approaches for regularly updating secrets for SelectConnection's using OAuth2.

The OAuth2-related parted is simple and is implemented using requests_oauthlib (but to reduce dependencies this can be done in pure http).

The more interesting part is how to update this secrets regularly, and here is the 2 approaches that I've tried:
- Doing a blocking http call in connection thread (see BlockingConnectionSecretRefresher). This is simple, but an OAuth2 http call takes ~0.3 second in my tests, which can be too much blocking.
- Running a separate thread (see ThreadedConnectionSecretRefresher). In this case it has no negative effect on processing speed, but introduces a lot of moving parts.

There is also a question of what to do with all other connection adapters. It looks like methods similar `call_later`/`add_callback_threadsafe` are universally available, but e.g. for aio/twisted adapters it should be possible to do the same using non-blocking http calls.
@binarin binarin marked this pull request as draft May 16, 2023 13:29
@binarin
Copy link
Author

binarin commented May 16, 2023

One way I envision is to sublclass PlainCredentials as OAuth2Credentials, and then do a special-case in every adapter, where a new thread for updates will be started automatically.

@lukebakken lukebakken self-assigned this May 18, 2023
@lukebakken lukebakken added this to the 2.0.0 milestone Dec 1, 2023
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