Skip to content

Commit

Permalink
Fixed error with connection to the MongoDB Cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
iduseev committed Sep 15, 2023
1 parent 7c47b17 commit e034c7a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
18 changes: 14 additions & 4 deletions backend/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ def authenticate_user(mongo_adapter: MongoAdapter, username: AnyStr, password: A
return user


def get_user(mongo_adapter: MongoAdapter, username: AnyStr, logger: Optional[Any] = None) -> Union[UserInDB, None]:
def get_user(
mongo_adapter: MongoAdapter,
username: AnyStr,
logger: Optional[Any] = None
) -> Union[UserInDB, None]:
"""
Returns a user if it is found within the database
Expand All @@ -83,7 +87,11 @@ def get_user(mongo_adapter: MongoAdapter, username: AnyStr, logger: Optional[Any
if logger: logger.debug("No user found in the database!")


async def get_current_user(mongo_adapter: MongoAdapter, token: Annotated[AnyStr, Depends(oauth2_scheme)], logger: Optional[Any] = None) -> Union[UserInDB, NoReturn]:
async def get_current_user(
mongo_adapter: MongoAdapter,
token: Annotated[AnyStr, Depends(oauth2_scheme)],
logger: Optional[Any] = None
) -> Union[UserInDB, NoReturn]:
"""
Receives token, attempts to decode the received token, verifies it and returns the current user.
If the token is invalid, returns an HTTP error right away.
Expand All @@ -104,7 +112,6 @@ async def get_current_user(mongo_adapter: MongoAdapter, token: Annotated[AnyStr,
detail="Could not validate credentials!",
headers={"WWW-Authenticate": "Bearer"},
)

try:
payload = decode_jwt_token(encoded_token=token)
username: str = payload.get("sub")
Expand All @@ -120,7 +127,10 @@ async def get_current_user(mongo_adapter: MongoAdapter, token: Annotated[AnyStr,
return user


async def get_current_active_user(current_user: Annotated[UserInDB, Depends(get_current_user)], logger: Optional[Any] = None) -> Union[UserInDB, NoReturn]:
async def get_current_active_user(
current_user: Annotated[UserInDB, Depends(get_current_user)],
logger: Optional[Any] = None
) -> Union[UserInDB, NoReturn]:
"""
Checks the current user's 'disabled' attribute value and returns user pydantic model if current user is not disabled
Expand Down
7 changes: 4 additions & 3 deletions backend/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def __init__(
requires_auth: Optional[bool] = True,
username: Optional[AnyStr] = None,
password: Optional[AnyStr] = None,
auth_source: Optional[AnyStr] = None,
auth_source: Optional[AnyStr] = "admin",
auth_mechanism: Optional[AnyStr] = "DEFAULT",
recreate_indexes: Optional[bool] = True,
required_index_params: Optional[List[Tuple[str, bool]]] = None,
Expand All @@ -46,8 +46,9 @@ def __init__(
self.username: AnyStr = username
self.password: AnyStr = password

self.auth_source: AnyStr = self.db_name
if auth_source: self.auth_source = auth_source
self.auth_source: AnyStr = auth_source
# self.auth_source: AnyStr = self.db_name
# if auth_source: self.auth_source = auth_source
self.auth_mechanism = auth_mechanism

self.client: pymongo.MongoClient
Expand Down
11 changes: 8 additions & 3 deletions backend/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,15 @@ async def login_for_access_token(
return Token(**access_token_data)


@app.get("/user/me", tags=["user"])
@app.get(
"/user/me",
summary="Information about current user",
response_model=UserInDB,
tags=["user"]
)
async def read_user_me(
current_user: Annotated[User, Depends(get_current_active_user)]
) -> UserInDB:
current_user: Annotated[User, Depends(get_current_active_user)]
) -> UserInDB:
"""
Returns info about currently logged user
Expand Down

0 comments on commit e034c7a

Please sign in to comment.