diff --git a/docs/quickstart.md b/docs/quickstart.md index 5ffad1c..857efcb 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -26,7 +26,14 @@ client_metadata = ClientMetadata( provider_config = ProviderConfiguration(issuer='', 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: diff --git a/src/flask_pyoidc/flask_pyoidc.py b/src/flask_pyoidc/flask_pyoidc.py index 2e09765..379a7c8 100644 --- a/src/flask_pyoidc/flask_pyoidc.py +++ b/src/flask_pyoidc/flask_pyoidc.py @@ -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: @@ -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) diff --git a/tests/test_flask_pyoidc.py b/tests/test_flask_pyoidc.py index 333a1ad..04a2fea 100644 --- a/tests/test_flask_pyoidc.py +++ b/tests/test_flask_pyoidc.py @@ -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 @@ -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()