Skip to content

Commit

Permalink
Merge pull request #231 from ecmwf-projects/COPDS-2284-user-email
Browse files Browse the repository at this point in the history
Add user email to request metadata
  • Loading branch information
mcucchi9 authored Dec 17, 2024
2 parents df302d9 + 4b4435e commit b043ee9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions cads_processing_api_service/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_auth_header(pat: str | None = None, jwt: str | None = None) -> tuple[str
)
def authenticate_user(
auth_header: tuple[str, str], portal_header: str | None = None
) -> tuple[str, str | None]:
) -> tuple[str, str | None, str | None]:
"""Verify user authentication.
Verify if the provided authentication header corresponds to a registered user.
Expand All @@ -88,7 +88,7 @@ def authenticate_user(
Returns
-------
tuple[str, str | None]
tuple[str, str | None, str | None]
User identifier and role.
Raises
Expand Down Expand Up @@ -119,7 +119,8 @@ def authenticate_user(
user: dict[str, str] = response.json()
user_uid: str = user["sub"]
user_role: str | None = user.get("role", None)
return user_uid, user_role
email: str | None = user.get("email", None)
return user_uid, user_role, email


def get_auth_info(
Expand Down Expand Up @@ -153,11 +154,12 @@ def get_auth_info(
Raised if none of the expected authentication headers is provided.
"""
auth_header = get_auth_header(pat, jwt)
user_uid, user_role = authenticate_user(auth_header, portal_header)
user_uid, user_role, email = authenticate_user(auth_header, portal_header)
request_origin = REQUEST_ORIGIN[auth_header[0]]
auth_info = models.AuthInfo(
user_uid=user_uid,
user_role=user_role,
email=email,
request_origin=request_origin,
auth_header=auth_header,
portal_header=portal_header,
Expand Down
5 changes: 4 additions & 1 deletion cads_processing_api_service/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ def post_process_execution(
process_id=process_id,
portal=dataset.portal,
qos_tags=dataset.qos_tags,
metadata={"costs": costs},
metadata={
"costs": costs,
"user_data": {"email": auth_info.email},
},
**job_kwargs,
)
dataset_messages = [
Expand Down
1 change: 1 addition & 0 deletions cads_processing_api_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
class AuthInfo(pydantic.BaseModel):
user_uid: str
user_role: str | None = None
email: str | None = None
request_origin: str
auth_header: tuple[str, str]
portal_header: str | None = None
Expand Down

0 comments on commit b043ee9

Please sign in to comment.