Skip to content

Commit e372210

Browse files
committed
fix(logout): remove unmanage_roles from session
1 parent 976a07b commit e372210

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

invenio_oauthclient/handlers/token.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,20 @@ def token_delete(remote, token=""):
246246

247247

248248
def oauth_logout_handler(sender_app, user=None):
249-
"""Remove all access tokens from session on logout."""
249+
"""Remove all access tokens and OAuth session data on logout.
250+
251+
This includes:
252+
- OAuth tokens for all remote apps
253+
- Unmanaged roles (groups) from the session
254+
"""
250255
oauth = current_oauthclient.oauth
251256
for remote in oauth.remote_apps.values():
252257
token_delete(remote)
258+
259+
# Clear unmanaged roles (groups) from session
260+
# These are set during OAuth login when a groups handler is configured
261+
session.pop("unmanaged_roles_ids", None)
262+
253263
db.session.commit()
254264

255265

tests/test_handlers_ui.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,56 @@ def test_dummy_handler(base_app):
227227
base_app.test_client().get(
228228
url_for("invenio_oauthclient.signup", remote_app="github", next="/someurl/")
229229
)
230+
231+
232+
def test_logout_clears_unmanaged_roles(remote, app, models_fixture):
233+
"""Test that logout handler clears unmanaged_roles_ids from session."""
234+
datastore = app.extensions["invenio-accounts"].datastore
235+
existing_email = "[email protected]"
236+
user = datastore.find_user(email=existing_email)
237+
238+
example_groups = [
239+
{
240+
"id": "cern-group-1",
241+
"name": "cern-group-1",
242+
"description": "CERN test group 1",
243+
},
244+
{
245+
"id": "cern-group-2",
246+
"name": "cern-group-2",
247+
"description": "CERN test group 2",
248+
},
249+
]
250+
example_response = {"access_token": "test_access_token"}
251+
example_account_info = {
252+
"user": {
253+
"email": existing_email,
254+
},
255+
"external_id": "1234",
256+
"external_method": "test_method",
257+
}
258+
259+
# Mock remote app's handler with groups
260+
current_oauthclient.signup_handlers[remote.name] = {
261+
"info": lambda resp: example_account_info,
262+
"groups": lambda resp: example_groups,
263+
}
264+
265+
_security.confirmable = True
266+
_security.login_without_confirmation = False
267+
user.confirmed_at = None
268+
269+
# Perform login with groups
270+
authorized_signup_handler(example_response, remote)
271+
272+
# Verify that groups are in session after login
273+
assert "unmanaged_roles_ids" in session
274+
assert len(session["unmanaged_roles_ids"]) == 2
275+
assert "cern-group-1" in session["unmanaged_roles_ids"]
276+
assert "cern-group-2" in session["unmanaged_roles_ids"]
277+
278+
# Perform logout
279+
logout_user()
280+
281+
# Verify that groups are cleared from session after logout
282+
assert "unmanaged_roles_ids" not in session

0 commit comments

Comments
 (0)