Skip to content

Commit

Permalink
Lazy loading of provider config
Browse files Browse the repository at this point in the history
Adds lazy loading of provider config to allow blueprint views to be decorated by auth decorators
  • Loading branch information
infohash committed Feb 29, 2024
1 parent 26b1235 commit b8ccc2e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
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
10 changes: 8 additions & 2 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
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

0 comments on commit b8ccc2e

Please sign in to comment.