diff --git a/flask_oidc/__init__.py b/flask_oidc/__init__.py index 1162f7e..b22a042 100644 --- a/flask_oidc/__init__.py +++ b/flask_oidc/__init__.py @@ -307,15 +307,34 @@ def require_login(self, view_func): @wraps(view_func) def decorated(*args, **kwargs): if not self.user_loggedin: - redirect_uri = "{login}?next={here}".format( - login=url_for("oidc_auth.login"), - here=quote_plus(request.url), - ) - return redirect(redirect_uri) + return self.redirect_to_auth_server() return view_func(*args, **kwargs) return decorated + def redirect_to_auth_server(self, destination=None, customstate=None): + """ + Redirect to the IdP. + + :param destination: The page that the user was going to, + before we noticed they weren't logged in. + :type destination: Url to return the client to if a custom handler is + not used. Not available with custom callback. + :param customstate: Ignored, left here for compatibility. + :returns: A redirect response to start the login process. + """ + if customstate is not None: + warnings.warn( + "The customstate argument of redirect_to_auth_server is ignored.", + DeprecationWarning, + stacklevel=2, + ) + redirect_uri = "{login}?next={here}".format( + login=url_for("oidc_auth.login"), + here=quote_plus(destination or request.url), + ) + return redirect(redirect_uri) + def logout(self, return_to=None): """ Request the browser to please forget the cookie we set, to clear the diff --git a/tests/test_flask_oidc.py b/tests/test_flask_oidc.py index 369c0d0..84e1118 100644 --- a/tests/test_flask_oidc.py +++ b/tests/test_flask_oidc.py @@ -176,6 +176,14 @@ def test_bad_token(client): assert "Internal Server Error" in resp.get_data(as_text=True) +def test_redirect_obsolete_argument(test_app): + with test_app.test_request_context(path="/somewhere"): + with pytest.warns(DeprecationWarning): + resp = test_app.oidc_ext.redirect_to_auth_server(None, "dummy") + assert resp.status_code == 302 + assert resp.location == "/login?next=http%3A%2F%2Flocalhost%2Fsomewhere" + + def test_user_getinfo(test_app, client, dummy_token): user_info = {"nickname": "dummy"} with test_app.test_request_context(path="/somewhere"):