Skip to content

Commit d1eecaa

Browse files
sakshamarora1zzacharo
authored andcommitted
contrib: fix CERN contrib login with token
* fetch extra roles, cached in the DB, when the user login using a personal access token. * related: inveniosoftware/invenio-oauthclient#291
1 parent 2bba4c2 commit d1eecaa

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

cds/modules/oauthclient/cern_openid.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -255,23 +255,31 @@ def on_identity_changed(sender, identity):
255255
disconnect_identity(identity)
256256
return
257257

258-
remote = g.get("oauth_logged_in_with_remote", None)
259-
if not remote or remote.name != "cern_cdsvideos_openid":
260-
# signal coming from another remote app
261-
return
262-
258+
# This is not ideal: it assumes that the personal token used this CERN contrib
259+
# method to login, which might not be the case.
260+
# However, it is not harmful because it will simply fetch the extra roles cached
261+
# in the DB.
262+
# Changing this requires large refactoring.
263263
logged_in_via_token = hasattr(current_user, "login_via_oauth2") and getattr(
264264
current_user, "login_via_oauth2"
265265
)
266266

267+
remote = g.get("oauth_logged_in_with_remote", None)
268+
logged_in_with_cern_openid = remote and remote.name == "cern_cdsvideos_openid"
269+
267270
client_id = current_app.config["CERN_APP_OPENID_CREDENTIALS"]["consumer_key"]
268271
remote_account = RemoteAccount.get(
269272
user_id=current_user.get_id(), client_id=client_id
270273
)
271274
roles = []
272275
groups = []
273276

274-
if remote_account and not logged_in_via_token:
277+
if remote_account and logged_in_via_token:
278+
# use cached roles, fetched from the DB
279+
roles.extend(remote_account.extra_data["roles"])
280+
groups.extend(remote_account.extra_data["groups"])
281+
elif remote_account and logged_in_with_cern_openid:
282+
# new login, fetch roles remotely
275283
refresh = current_app.config["OAUTHCLIENT_CERN_OPENID_REFRESH_TIMEDELTA"]
276284
if refresh:
277285
resource = get_resource(remote)
@@ -283,10 +291,7 @@ def on_identity_changed(sender, identity):
283291
else:
284292
roles.extend(remote_account.extra_data["roles"])
285293
groups.extend(remote_account.extra_data["groups"])
286-
elif remote_account and logged_in_via_token:
287-
roles.extend(remote_account.extra_data["roles"])
288-
groups.extend(remote_account.extra_data["groups"])
289-
294+
# must be always called, to add the user email in the roles
290295
extend_identity(identity, roles, groups)
291296

292297

0 commit comments

Comments
 (0)