-
Notifications
You must be signed in to change notification settings - Fork 9
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
Refactor(oauth): resolve RuntimeWarning #2152
Changes from all commits
b4e9f44
c9e7494
770f8e1
4d2e1ec
06186fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,9 +6,6 @@ | |
|
||
from authlib.integrations.django_client import OAuth | ||
|
||
from benefits.core.models import AuthProvider | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
oauth = OAuth() | ||
|
@@ -42,23 +39,32 @@ def _authorize_params(scheme): | |
return params | ||
|
||
|
||
def register_providers(oauth_registry): | ||
def _register_provider(oauth_registry, provider): | ||
""" | ||
Register OAuth clients into the given registry, using configuration from AuthProvider models. | ||
Register OAuth clients into the given registry, using configuration from AuthProvider model. | ||
|
||
Adapted from https://stackoverflow.com/a/64174413. | ||
""" | ||
logger.info("Registering OAuth clients") | ||
logger.debug(f"Registering OAuth client: {provider.client_name}") | ||
|
||
client = oauth_registry.register( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first I was wondering about this line since the Authlib documentation does not mention a return type for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, so you'd want to use I used Ideally I would've liked to somehow check if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What happens when we try to get a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think an exception could mean a misconfiguration, so I'll leave that case out. But I went ahead and made the helper function that registers the client if we got |
||
provider.client_name, | ||
client_id=provider.client_id, | ||
server_metadata_url=_server_metadata_url(provider.authority), | ||
client_kwargs=_client_kwargs(provider.scope), | ||
authorize_params=_authorize_params(provider.scheme), | ||
) | ||
|
||
return client | ||
|
||
providers = AuthProvider.objects.all() | ||
|
||
for provider in providers: | ||
logger.debug(f"Registering OAuth client: {provider.client_name}") | ||
def create_client(oauth_registry, provider): | ||
""" | ||
Returns an OAuth client, registering it if needed. | ||
""" | ||
client = oauth_registry.create_client(provider.client_name) | ||
|
||
if client is None: | ||
client = _register_provider(oauth_registry, provider) | ||
|
||
oauth_registry.register( | ||
provider.client_name, | ||
client_id=provider.client_id, | ||
server_metadata_url=_server_metadata_url(provider.authority), | ||
client_kwargs=_client_kwargs(provider.scope), | ||
authorize_params=_authorize_params(provider.scheme), | ||
) | ||
return client |
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍