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

Lazy Load Provider Config #173

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,14 @@ client_metadata = ClientMetadata(
provider_config = ProviderConfiguration(issuer='<issuer URL of provider>',
client_metadata=client_metadata)

auth = OIDCAuthentication({'default': provider_config}, app)
auth = OIDCAuthentication(provider_configurations={'default': provider_config}, app=app)
```

Flask `app` and provider configurations can be also passed to `init_app`
to initialize `auth` later.
```python
auth = OIDCAuthentication()
auth.init_app(app=app, provider_configurations={'default': provider_config})
```

You can also use a Flask application factory:
Expand Down
12 changes: 9 additions & 3 deletions src/flask_pyoidc/flask_pyoidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class OIDCAuthentication:
OIDCAuthentication object for Flask extension.
"""

def __init__(self, provider_configurations, app=None,
def __init__(self, provider_configurations=None, app=None,
redirect_uri_config=None):
"""
Args:
Expand All @@ -72,7 +72,13 @@ def __init__(self, provider_configurations, app=None,
if app:
self.init_app(app)

def init_app(self, app):
def init_app(self, app, provider_configurations=None):
if self._provider_configurations is None and provider_configurations is None:
raise ValueError('No provider is configured.')

if provider_configurations:
self._provider_configurations = provider_configurations

if not self._redirect_uri_config:
self._redirect_uri_config = RedirectUriConfig.from_config(app.config)

Expand Down Expand Up @@ -209,7 +215,7 @@ def _show_error_response(self, error_response):

def oidc_auth(self, provider_name: str):

if provider_name not in self._provider_configurations:
if self._provider_configurations and provider_name not in self._provider_configurations:
raise ValueError(
f"Provider name '{provider_name}' not in configured providers: {self._provider_configurations.keys()}."
)
Expand Down
9 changes: 7 additions & 2 deletions tests/test_flask_pyoidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def assert_view_mock(self, callback_mock, result):
assert callback_mock.called
assert result == self.CALLBACK_RETURN_VALUE

def test_lazy_init_raises_value_error_if_no_provider_configuration(self):
authn = OIDCAuthentication()
with pytest.raises(ValueError):
authn.init_app(app=self.app)

def test_explicit_redirect_uri_config_should_be_preferred(self):
redirect_uri_config = RedirectUriConfig('https://example.com/redirect_uri', 'redirect_uri')
assert OIDCAuthentication({}, self.app, redirect_uri_config)._redirect_uri_config == redirect_uri_config
Expand Down Expand Up @@ -156,8 +161,8 @@ def test_should_register_client_if_not_registered_before(self, post_logout_redir
post_logout_redirect_uris=post_logout_redirect_uris
))
}
authn = OIDCAuthentication(provider_configurations)
authn.init_app(self.app)
authn = OIDCAuthentication()
authn.init_app(self.app, provider_configurations=provider_configurations)

# register logout view to force 'post_logout_redirect_uris' to be included in registration request
logout_view_mock = self.get_view_mock()
Expand Down
Loading