Skip to content

Commit

Permalink
endpoint to retrieve user id from token
Browse files Browse the repository at this point in the history
  • Loading branch information
violetaperezandrade committed May 30, 2024
1 parent 4134552 commit b78a5ca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/controller/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,10 @@ def handle_update_notification(self, user_id: int, notification_id: int,
"status": status.HTTP_200_OK,
}),
)

def handle_get_user_auth(self, user_data: dict):
response = self.users_service.get_user_auth(user_data)
return JSONResponse(
status_code=status.HTTP_200_OK,
content=response
)
8 changes: 7 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
UpdateUserSchema,
LoginRequest,
CreateNotificationSchema,
UpdateNotificationSchema
UpdateNotificationSchema,
GetUserSchema
)
from query_params.QueryParams import GetUsersQueryParams
from security.JWTBearer import get_current_user_id
Expand All @@ -30,6 +31,11 @@ def root():
return {"message": "users service"}


@app.post("/user", tags=["Auth"])
def get_user_auth(user_data: GetUserSchema):
return users_controller.handle_get_user_auth(user_data.dict())


@app.get("/users/{user_id}", tags=["Users"])
def get_user(user_id: int):
return users_controller.handle_get_user(user_id)
Expand Down
4 changes: 4 additions & 0 deletions app/schemas/Schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ def validate_date_time(cls, v):
if v.minute % 5 != 0:
raise ValueError('Minutes must be multiples of 5')
return v


class GetUserSchema(BaseModel):
token: str
11 changes: 11 additions & 0 deletions app/service/Users.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import jwt
import uuid

JWT_SECRET = os.environ.get("JWT_SECRET")
HASH_ALGORITHM = os.environ.get("HASH_ALGORITHM")
TOKEN_FIELD_NAME = "x-access-token"


Expand Down Expand Up @@ -104,6 +106,14 @@ def delete_notification(self, user_id: int, notification_id: int):
self.user_repository.rollback()
raise e

def get_user_auth(self, user_data):
token = user_data.get("token")
print(f"Token: {token}")
decoded_token = jwt.decode(token,

Check warning on line 112 in app/service/Users.py

View check run for this annotation

Codecov / codecov/patch

app/service/Users.py#L110-L112

Added lines #L110 - L112 were not covered by tests
os.environ["JWT_SECRET"],
algorithms=["HS256"])
return {"user_id": decoded_token.get("user_id")}

Check warning on line 115 in app/service/Users.py

View check run for this annotation

Codecov / codecov/patch

app/service/Users.py#L115

Added line #L115 was not covered by tests

def _generate_nickname(self, name): # pragma: no cover

name_without_spaces = name.replace(" ", "")
Expand Down Expand Up @@ -140,6 +150,7 @@ def login(self, auth_code: str):
jwt_token = jwt.encode(payload,
os.environ["JWT_SECRET"],
algorithm="HS256")
print(f"Token: {jwt_token}")

Check warning on line 153 in app/service/Users.py

View check run for this annotation

Codecov / codecov/patch

app/service/Users.py#L153

Added line #L153 was not covered by tests
return user, jwt_token

def _get_access_token(self, authorization_code): # pragma: no cover
Expand Down

0 comments on commit b78a5ca

Please sign in to comment.