Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

[Misc] Adds user data to the login response #501

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/fidesops/api/v1/endpoints/user_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
UserLogin,
UserPasswordReset,
UserResponse,
UserLoginResponse,
)

from fidesops.util.oauth_util import (
Expand Down Expand Up @@ -238,11 +239,11 @@ def delete_user(
@router.post(
urls.LOGIN,
status_code=HTTP_200_OK,
response_model=AccessToken,
response_model=UserLoginResponse,
)
def user_login(
*, db: Session = Depends(deps.get_db), user_data: UserLogin
) -> AccessToken:
) -> UserLoginResponse:
"""Login the user by creating a client if it doesn't exist, and have that client generate a token"""
user: FidesopsUser = FidesopsUser.get_by(
db, field="username", value=user_data.username
Expand All @@ -260,7 +261,10 @@ def user_login(

logger.info("Creating login access token")
access_code = client.create_access_code_jwe()
return AccessToken(access_token=access_code)
return UserLoginResponse(
user_data=user,
token_data=AccessToken(access_token=access_code),
)


@router.post(
Expand Down
8 changes: 8 additions & 0 deletions src/fidesops/schemas/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pydantic import validator
from fidesops.schemas.base_class import BaseSchema
from fidesops.schemas.oauth import AccessToken


class UserUpdate(BaseSchema):
Expand Down Expand Up @@ -69,6 +70,13 @@ class UserResponse(BaseSchema):
last_name: Optional[str]


class UserLoginResponse(BaseSchema):
"""Similar to UserResponse except with an access token"""

user_data: UserResponse
token_data: AccessToken


class UserCreateResponse(BaseSchema):
"""Response after creating a FidesopsUser"""

Expand Down
22 changes: 13 additions & 9 deletions tests/api/v1/endpoints/test_user_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -669,13 +669,16 @@ def test_login_creates_client(self, db, url, user, api_client):

db.refresh(user)
assert user.client is not None
assert list(response.json().keys()) == ["access_token"]
token = response.json()["access_token"]

assert "token_data" in list(response.json().keys())
token = response.json()["token_data"]["access_token"]
token_data = json.loads(extract_payload(token))

assert token_data["client-id"] == user.client.id
assert token_data["scopes"] == [PRIVACY_REQUEST_READ]
assert token_data["scopes"] == [
PRIVACY_REQUEST_READ
] # Uses scopes on existing client

assert "user_data" in list(response.json().keys())
assert response.json()["user_data"]["id"] == user.id

user.client.delete(db)

Expand All @@ -699,16 +702,17 @@ def test_login_uses_existing_client(self, db, url, user, api_client):

db.refresh(user)
assert user.client is not None
assert list(response.json().keys()) == ["access_token"]
token = response.json()["access_token"]

assert "token_data" in list(response.json().keys())
token = response.json()["token_data"]["access_token"]
token_data = json.loads(extract_payload(token))

assert token_data["client-id"] == existing_client_id
assert token_data["scopes"] == [
PRIVACY_REQUEST_READ
] # Uses scopes on existing client

assert "user_data" in list(response.json().keys())
assert response.json()["user_data"]["id"] == user.id


class TestUserLogout:
@pytest.fixture(scope="function")
Expand Down