Skip to content

Commit 6f1b91b

Browse files
committed
Merge branch 'master' of https://github.com/AntonioMrtz/SpotifyElectron into feat/246-Playlist-Update-Fields-To-Change
2 parents 4d36fc1 + 8dadd54 commit 6f1b91b

34 files changed

+712
-262
lines changed

.github/disabled_workflows/backend-tests-streaming-serverless.yml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ jobs:
3333
python -m pytest tests/
3434
env:
3535
MONGO_URI: mongodb://root:root@localhost:27017/
36+
SECRET_KEY_SIGN: "f24e2f3ac557d487b6d879fb2d86f2b2"
3637
SERVERLESS_FUNCTION_URL: ${{ secrets.SERVERLESS_FUNCTION_URL }}
3738
ARCH: "SERVERLESS"
3839
ENV_VALUE: "PROD"

.github/disabled_workflows/generate-docs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
python generate-docs.py
3131
env:
3232
MONGO_URI : ${{ secrets.MONGO_URI }}
33+
SECRET_KEY_SIGN : none
3334
SERVERLESS_FUNCTION_URL : none
3435
ARCH : BLOB
3536
ENV_VALUE : TEST

.github/disabled_workflows/generate-openapi-schema-client.yml

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ jobs:
3030
python -m app.tools.generate_openapi
3131
env:
3232
MONGO_URI: ${{ secrets.MONGO_URI }}
33+
SECRET_KEY_SIGN: none
3334
ARCH: BLOB
3435
ENV_VALUE: PROD
3536

.github/workflows/backend-tests-database-blob.yml

+1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ jobs:
3333
python -m pytest tests/
3434
env:
3535
MONGO_URI: mongodb://root:root@localhost:27017/
36+
SECRET_KEY_SIGN: "f24e2f3ac557d487b6d879fb2d86f2b2"
3637
ARCH: "BLOB"
3738
ENV_VALUE: "PROD"

.github/workflows/openapi-check-updated.yml

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ jobs:
4747
python -m app.tools.generate_openapi
4848
env:
4949
MONGO_URI: mongodb://root:root@localhost:27017/
50+
SECRET_KEY_SIGN: "f24e2f3ac557d487b6d879fb2d86f2b2"
5051
ARCH: "BLOB"
5152
ENV_VALUE: "PROD"
5253

Backend/Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ EXPOSE 8000
2626
# Docker build command
2727
# docker build -t spotify_electron_backend_image .
2828
# Docker run command
29-
# docker run -d --name spotify_electron_backend -e MONGO_URI=mongo-uri SERVERLESS_FUNCTION_URL=serverless-function-url ARCH=BLOB ENV_VALUE=PROD -p 8000:8000 spotify_electron_backend_image
29+
# docker run -d --name spotify_electron_backend -e MONGO_URI=mongo-uri SECRET_KEY_SIGN=secret-key-sign SERVERLESS_FUNCTION_URL=serverless-function-url ARCH=BLOB ENV_VALUE=PROD -p 8000:8000 spotify_electron_backend_image

Backend/app/__main__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from app.spotify_electron.song import song_controller
3737
from app.spotify_electron.song.providers.song_service_provider import SongServiceProvider
3838
from app.spotify_electron.stream import stream_controller
39-
from app.spotify_electron.user import user_controller
39+
from app.spotify_electron.user import base_user_controller
4040
from app.spotify_electron.user.artist import artist_controller
4141

4242
main_logger = SpotifyElectronLogger(LOGGING_MAIN).getLogger()
@@ -52,10 +52,12 @@ async def lifespan_handler(app: FastAPI) -> AsyncGenerator[None, Any]:
5252
main_logger.info("Spotify Electron Backend Started")
5353

5454
environment = PropertiesManager.get_environment()
55+
secret_key_sign = getattr(PropertiesManager, AppEnvironment.SECRET_KEY_SIGN_ENV_NAME)
5556
connection_uri = getattr(PropertiesManager, AppEnvironment.MONGO_URI_ENV_NAME)
5657

5758
AuthConfig.init_auth_config(
5859
access_token_expire_minutes=AppAuthConfig.ACCESS_TOKEN_EXPIRE_MINUTES,
60+
secret_key_sign=secret_key_sign,
5961
verification_algorithm=AppAuthConfig.VERTIFICATION_ALGORITHM,
6062
days_to_expire_cookie=AppAuthConfig.DAYS_TO_EXPIRE_COOKIE,
6163
)
@@ -68,7 +70,7 @@ async def lifespan_handler(app: FastAPI) -> AsyncGenerator[None, Any]:
6870
app.include_router(playlist_controller.router)
6971
app.include_router(song_controller.router)
7072
app.include_router(genre_controller.router)
71-
app.include_router(user_controller.router)
73+
app.include_router(base_user_controller.router)
7274
app.include_router(artist_controller.router)
7375
app.include_router(login_controller.router)
7476
app.include_router(search_controller.router)

Backend/app/auth/auth_schema.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
Authentication schema for domain model
33
"""
44

5-
import binascii
6-
import os
75
from dataclasses import dataclass
86

97
from app.exceptions.base_exceptions_schema import SpotifyElectronException
@@ -24,7 +22,9 @@ class TokenData:
2422

2523

2624
class FakeRequest:
27-
"""Fake Request Object for bypassing authentication token HTTP incoming format"""
25+
"""Fake Request Object for bypassing authentication token HTTP incoming format
26+
TODO this must go with https://trello.com/c/rSvoOwPn/452-jwt-auth-backend-performance-improvements
27+
"""
2828

2929
headers: dict[str, str] = {}
3030

@@ -45,19 +45,20 @@ class AuthConfig:
4545
def init_auth_config(
4646
cls,
4747
verification_algorithm: str,
48+
secret_key_sign: str,
4849
access_token_expire_minutes: int,
4950
days_to_expire_cookie: int,
5051
) -> None:
5152
"""Init authentication configuration, required to start the app successfully
5253
5354
Args:
5455
verification_algorithm (str): JWT verification algorithm
56+
secret_key_sign (str): 32 byte key(16 characters) for signing JWT Tokens that\
57+
will authenticate the user
5558
access_token_expire_minutes (int): minutes until the JWT expires
5659
days_to_expire_cookie (int): days until cookies expire
5760
"""
58-
random_bytes = os.urandom(16)
59-
hex_string = binascii.hexlify(random_bytes).decode("utf-8")
60-
cls.SIGNING_SECRET_KEY = hex_string
61+
cls.SIGNING_SECRET_KEY = secret_key_sign
6162

6263
cls.VERTIFICATION_ALGORITHM = verification_algorithm
6364
cls.ACCESS_TOKEN_EXPIRE_MINUTES = access_token_expire_minutes

Backend/app/auth/auth_service.py

+16-16
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
from app.logging.logging_constants import LOGGING_AUTH_SERVICE
2929
from app.logging.logging_schema import SpotifyElectronLogger
3030
from app.spotify_electron.login.login_schema import InvalidCredentialsLoginException
31-
from app.spotify_electron.user.user.user_schema import (
32-
UserDTO,
33-
UserNotFoundException,
34-
UserServiceException,
31+
from app.spotify_electron.user.base_user_schema import (
32+
BaseUserDTO,
33+
BaseUserNotFoundException,
34+
BaseUserServiceException,
3535
)
3636
from app.spotify_electron.utils.validations.validation_utils import validate_parameter
3737

@@ -132,7 +132,7 @@ def get_jwt_token_data(
132132

133133
def get_current_user(
134134
token: TokenData,
135-
) -> UserDTO:
135+
) -> BaseUserDTO:
136136
"""Get current user from JWT Token
137137
138138
Args:
@@ -141,7 +141,7 @@ def get_current_user(
141141
142142
Raises:
143143
------
144-
UserNotFoundException: token user not found
144+
BaseUserNotFoundException: token user not found
145145
JWTGetUserException: if error while retrieving user from token
146146
Returns:
147147
Artist | User: the user or artist associated with the JWT Token
@@ -151,9 +151,9 @@ def get_current_user(
151151
jwt_username = token.username
152152

153153
user = base_user_service.get_user(jwt_username)
154-
except UserNotFoundException as exception:
154+
except BaseUserNotFoundException as exception:
155155
auth_service_logger.exception(f"User {jwt_username} not found")
156-
raise UserNotFoundException from exception
156+
raise BaseUserNotFoundException from exception
157157
except Exception as exception:
158158
auth_service_logger.exception(f"Unexpected exception getting user from token {token}")
159159
raise JWTGetUserException from exception
@@ -218,7 +218,7 @@ def login_user(name: str, password: str) -> str:
218218
------
219219
InvalidCredentialsLoginException: bad user credentials
220220
VerifyPasswordException: failing authenticating user and password
221-
UserNotFoundException: user doesn't exists
221+
BaseUserNotFoundException: user doesn't exists
222222
UnexpectedLoginUserException: unexpected error during user login
223223
224224
Returns:
@@ -252,10 +252,10 @@ def login_user(name: str, password: str) -> str:
252252
except CreateJWTException as exception:
253253
auth_service_logger.exception(f"Error creating JWT Token from data: {jwt_data}")
254254
raise VerifyPasswordException from exception
255-
except UserNotFoundException as exception:
255+
except BaseUserNotFoundException as exception:
256256
auth_service_logger.exception(f"User {name} doesn't exists")
257-
raise UserNotFoundException from exception
258-
except UserServiceException as exception:
257+
raise BaseUserNotFoundException from exception
258+
except BaseUserServiceException as exception:
259259
auth_service_logger.exception(
260260
f"Unexpected error in User service while login user: {name}"
261261
)
@@ -276,7 +276,7 @@ def login_user_with_token(raw_token: str) -> None:
276276
277277
Raises:
278278
JWTValidationException: invalid JWT credentials
279-
UserNotFoundException: user doesn't exists
279+
BaseUserNotFoundException: user doesn't exists
280280
UnexpectedLoginUserException: unexpected error during user login
281281
"""
282282
try:
@@ -288,10 +288,10 @@ def login_user_with_token(raw_token: str) -> None:
288288
except (JWTValidationException, BadJWTTokenProvidedException) as exception:
289289
auth_service_logger.exception(f"Error validating jwt token data: {raw_token}")
290290
raise JWTValidationException from exception
291-
except UserNotFoundException as exception:
291+
except BaseUserNotFoundException as exception:
292292
auth_service_logger.exception(f"User {token_data.username} not found")
293-
raise UserNotFoundException from exception
294-
except UserServiceException as exception:
293+
raise BaseUserNotFoundException from exception
294+
except BaseUserServiceException as exception:
295295
auth_service_logger.exception(
296296
f"Unexpected error in User service while auto login user: {token_data.username}"
297297
)

Backend/app/common/PropertiesManager.py

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def __init__(self) -> None:
3131
self.config_sections = [AppConfig.APP_INI_SECTION]
3232
self.env_variables = [
3333
AppEnvironment.MONGO_URI_ENV_NAME,
34+
AppEnvironment.SECRET_KEY_SIGN_ENV_NAME,
3435
AppEnvironment.SERVERLESS_URL_ENV_NAME,
3536
AppEnvironment.ENV_VALUE_ENV_NAME,
3637
]

Backend/app/common/app_schema.py

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class AppEnvironment:
7070
ARCHITECTURE_ENV_NAME = "ARCH"
7171

7272
DEFAULT_ARCHITECTURE = AppArchitecture.ARCH_BLOB
73+
SECRET_KEY_SIGN_ENV_NAME = "SECRET_KEY_SIGN"
7374
MONGO_URI_ENV_NAME = "MONGO_URI"
7475
SERVERLESS_URL_ENV_NAME = "SERVERLESS_FUNCTION_URL"
7576
ENV_VALUE_ENV_NAME = "ENV_VALUE"

Backend/app/spotify_electron/login/login_controller.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
)
2626
from app.common.PropertiesMessagesManager import PropertiesMessagesManager
2727
from app.spotify_electron.login.login_schema import InvalidCredentialsLoginException
28-
from app.spotify_electron.user.user.user_schema import UserNotFoundException
28+
from app.spotify_electron.user.base_user_schema import BaseUserNotFoundException
2929

3030
router = APIRouter(
3131
prefix="/login",
@@ -60,7 +60,7 @@ def login_user(
6060
status_code=HTTP_403_FORBIDDEN,
6161
content=PropertiesMessagesManager.loginVerifyPassword,
6262
)
63-
except UserNotFoundException:
63+
except BaseUserNotFoundException:
6464
return Response(
6565
status_code=HTTP_404_NOT_FOUND,
6666
content=PropertiesMessagesManager.userNotFound,
@@ -100,7 +100,7 @@ def login_user_with_jwt(token: str) -> Response:
100100
status_code=HTTP_403_FORBIDDEN,
101101
content=PropertiesMessagesManager.tokenInvalidCredentialsAutoLogin,
102102
)
103-
except UserNotFoundException:
103+
except BaseUserNotFoundException:
104104
return Response(
105105
status_code=HTTP_404_NOT_FOUND,
106106
content=PropertiesMessagesManager.userNotFound,

0 commit comments

Comments
 (0)