Skip to content

Commit

Permalink
client: add logout blueprint
Browse files Browse the repository at this point in the history
  • Loading branch information
jrcastro2 committed Jun 19, 2023
1 parent 45d4720 commit d159d6e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions invenio_oauthclient/contrib/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(
request_token_url=None,
precedence_mask=None,
signup_options=None,
logout_url=None,
**kwargs,
):
"""The constructor."""
Expand Down Expand Up @@ -57,6 +58,7 @@ def __init__(
icon=icon,
precedence_mask=precedence_mask,
signup_options=signup_options,
logout_url=logout_url,
params=dict(
base_url=self.base_url,
request_token_params=request_token_params,
Expand Down
4 changes: 3 additions & 1 deletion invenio_oauthclient/handlers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def base_authorized_signup_handler(resp, remote, *args, **kwargs):
"""
# Remove any previously stored auto register session key
session.pop(token_session_key(remote.name) + "_autoregister", None)

# We set the remote in the session to be aware of which one is being used and, on log out redirect to
# the correct URL set in the OAUTHCLIENT_REMOTE_APPS for each remote
session["OAUTHCLIENT_SESSION_REMOTE_NAME"] = remote.name
# Store token in session
# ----------------------
# Set token in session - token object only returned if
Expand Down
19 changes: 18 additions & 1 deletion invenio_oauthclient/views/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

"""Client blueprint used to handle OAuth callbacks."""

from flask import Blueprint, abort, current_app, redirect, request, url_for
from flask import Blueprint, abort, current_app, redirect, request, session, url_for
from flask_oauthlib.client import OAuthException
from invenio_accounts.views import login as base_login
from invenio_db import db
Expand Down Expand Up @@ -271,3 +271,20 @@ def rest_disconnect(remote_app):
return _disconnect(remote_app)
except OAuthRemoteNotFound:
abort(404)


@blueprint.route("/logout")
def post_logout():
"""Client logout view.
This URL should be called by setting `SECURITY_POST_LOGOUT_VIEW = /oauth/logout`
"""
remote_name = session.pop("OAUTHCLIENT_SESSION_REMOTE_NAME", None)
if remote_name:
logout_url = current_app.config["OAUTHCLIENT_REMOTE_APPS"][remote_name].get(
"logout_url"
)
if logout_url:
return redirect(logout_url, code=302)

return redirect("/")

0 comments on commit d159d6e

Please sign in to comment.