Skip to content

Commit 877f42a

Browse files
committed
fix(auth): align data db structure with funcs (reanahub#741)
1 parent b1656bf commit 877f42a

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

reana_server/oauth.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ def fetch_user_info(token: str) -> UserInfo:
4747
raise ValueError(f"Error communicating with IdP: {str(e)}")
4848

4949

50-
def create_or_update_user(idp_id: str, user_info: Dict) -> User:
50+
def create_or_update_user(sub: str, iss: str, user_info: Dict) -> User:
5151
"""Create or update user record with information from IdP.
5252
5353
Args:
54-
idp_id: Subject identifier from IdP
54+
sub: Subject identifier from IdP
55+
iss: Issuer identifier from IdP
5556
user_info: User information from IdP's UserInfo endpoint
5657
5758
Returns:
@@ -65,16 +66,18 @@ def create_or_update_user(idp_id: str, user_info: Dict) -> User:
6566
if not email:
6667
raise ValueError("Email is required in UserInfo response from IdP")
6768

68-
user = Session.query(User).filter_by(idp_id=idp_id).one_or_none()
69+
user = Session.query(User).filter_by(idp_subject=sub, idp_issuer=iss).one_or_none()
6970

7071
if not user:
7172
user = Session.query(User).filter_by(email=email).one_or_none()
7273
if user:
73-
user.idp_id = idp_id
74+
user.idp_subject = sub
75+
user.idp_issuer = iss
7476
else:
7577
user_parameters = {
7678
"email": email,
77-
"idp_id": idp_id,
79+
"idp_subject": sub,
80+
"idp_issuer": iss,
7881
"full_name": user_info.get("name", email),
7982
"username": user_info.get("preferred_username", email),
8083
}
@@ -90,12 +93,13 @@ def create_or_update_user(idp_id: str, user_info: Dict) -> User:
9093
raise ValueError(f"Error creating or updating user: {str(e)}")
9194

9295

93-
def create_or_update_user_from_idp(token: str, user_idp_id: str) -> User:
96+
def create_or_update_user_from_idp(token: str, sub: str, iss: str) -> User:
9497
"""Create or update user record by fetching info from IdP.
9598
9699
Args:
97100
token: Access token to fetch user info
98-
user_idp_id: Subject identifier from IdP (e.g., sub claim)
101+
sub: Subject identifier from IdP (e.g., sub claim)
102+
iss: Issuer identifier from IdP (e.g., iss claim)
99103
100104
Returns:
101105
User: Created or updated user record
@@ -105,6 +109,6 @@ def create_or_update_user_from_idp(token: str, user_idp_id: str) -> User:
105109
"""
106110
try:
107111
user_info = fetch_user_info(token)
108-
return create_or_update_user(user_idp_id, user_info)
112+
return create_or_update_user(sub, iss, user_info)
109113
except Exception as e:
110114
raise ValueError(f"Failed to create/update user: {str(e)}")

reana_server/utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@
8181
GitLabClient,
8282
GitLabClientException,
8383
)
84-
from reana_server.validation import validate_retention_rule, validate_workflow
8584
from reana_server.oauth import create_or_update_user_from_idp
85+
from reana_server.validation import validate_retention_rule, validate_workflow
8686

8787

8888
def is_uuid_v4(uuid_or_name):
@@ -437,8 +437,8 @@ def _get_user_from_invenio_user(id):
437437
return user
438438

439439

440-
def _get_user_by_idpid(idp_id):
441-
user = Session.query(User).filter_by(idp_id=idp_id).one_or_none()
440+
def _get_user_by_sub_and_iss(sub, iss):
441+
user = Session.query(User).filter_by(idp_subject=sub, idp_issuer=iss).one_or_none()
442442
if not user:
443443
raise ValueError("No users registered with this idp_id")
444444
return user
@@ -712,22 +712,22 @@ def _get_user_from_jwt(header: str) -> User:
712712

713713
token = header.split(" ")[1]
714714

715-
# Validate JWT token
716715
jwks = fetch_and_parse_jwk()
717716
key_set = JsonWebKey.import_key_set(jwks)
717+
718718
claims = jwt.decode(token, key_set)
719719
claims.validate()
720720

721-
idp_id = claims.get("sub")
722-
if not idp_id:
723-
raise ValueError("Token missing subject claim")
724-
721+
sub = claims.get("sub")
722+
iss = claims.get("iss")
723+
if not sub or not iss:
724+
raise ValueError("Token missing subject claim or iss")
725725
try:
726-
user = _get_user_by_idpid(idp_id)
726+
user = _get_user_by_sub_and_iss(sub, iss)
727727
return user
728728
except ValueError:
729729
# User not found, create/update from IdP
730-
return create_or_update_user_from_idp(token, idp_id)
730+
return create_or_update_user_from_idp(token, sub, iss)
731731

732732
except JoseError as e:
733733
raise ValueError(f"Invalid token: {str(e)}")

0 commit comments

Comments
 (0)