Skip to content
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
4 changes: 4 additions & 0 deletions src/domain/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class InvalidConfirmPasswordError(DomainError):
"""Confirm password does not match."""
pass

class InvalidFormatEmailError(DomainError):
"""Invalid email format."""
pass

class DuplicateProfileError(DomainError):
"""Email already used."""
pass
Expand Down
13 changes: 12 additions & 1 deletion src/domain/services/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from src.domain.model.profile import Profile as DomainProfile
from src.domain.ports.profile_repository import ProfileRepository
from src.domain.ports.password_hasher import PasswordHasher
from src.domain.exceptions import DuplicateProfileError, AuthenticationError, NotFoundError, InvalidConfirmPasswordError
from src.domain.exceptions import DuplicateProfileError, AuthenticationError, NotFoundError, InvalidConfirmPasswordError, InvalidFormatEmailError
import re

class ProfileService:
def __init__(self, repo: ProfileRepository, hasher: PasswordHasher):
Expand All @@ -27,6 +28,11 @@ def create(self,
legacy: Optional[str] = None,
roles: Optional[List[str]] = None
) -> DomainProfile:

email_regex = r"^[\w\.-]+@[\w\.-]+\.\w+$"
if not re.match(email_regex, email):
raise InvalidFormatEmailError(f"Email {email} has invalid format")

if self._repo.find_by_email(email):
raise DuplicateProfileError(f"Profile with email {email} already exists")

Expand Down Expand Up @@ -62,6 +68,11 @@ def delete(self, profile_id: UUID):

def login(self, email: str, password: str) -> DomainProfile:
profile = self._repo.find_by_email(email)

email_regex = r"^[\w\.-]+@[\w\.-]+\.\w+$"
if not re.match(email_regex, email):
raise InvalidFormatEmailError(f"Email {email} has invalid format")

if not profile:
raise AuthenticationError(f"Invalid password or email")

Expand Down
8 changes: 6 additions & 2 deletions src/entrypoints/api/routers/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from src.container import container
from src.domain.lib.jwt_manager import create_access_token
from src.entrypoints.api.schemas.profile import EmailUpdate, PasswordUpdate, ProfileCreate, ProfileRead, ProfileWithToken, RolesUpdate, TokenResponse, ProfileLogin, ProfilUpdate, CoachProfileRead
from src.domain.exceptions import DuplicateProfileError, InvalidConfirmPasswordError, NotFoundError, AuthenticationError
from src.domain.exceptions import DuplicateProfileError, InvalidConfirmPasswordError, InvalidFormatEmailError, NotFoundError, AuthenticationError
from src.entrypoints.api.deps.auth import UserPayload, get_current_user, require_owner_or_admin
from src.entrypoints.api.deps.roles import require_roles

Expand Down Expand Up @@ -38,6 +38,8 @@ async def create_profile(
raise HTTPException(status_code=400, detail=str(e))
except InvalidConfirmPasswordError as e:
raise HTTPException(status_code=400, detail=str(e))
except InvalidFormatEmailError as e:
raise HTTPException(status_code=400, detail=str(e))

token = create_access_token(
subject=str(profile.id),
Expand Down Expand Up @@ -92,7 +94,9 @@ async def login(
profile = service.login(email=dto.email, password=dto.password)
except AuthenticationError as e:
raise HTTPException(status_code=401, detail=str(e))

except InvalidFormatEmailError as e:
raise HTTPException(status_code=400, detail=str(e))

token = create_access_token(
subject=str(profile.id),
roles=profile.roles,
Expand Down
4 changes: 2 additions & 2 deletions src/entrypoints/api/schemas/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import List, Optional

class ProfileCreate(BaseModel):
email: EmailStr
email: str
password: str
confirm_password: str
name: Optional[str] = None
Expand Down Expand Up @@ -55,7 +55,7 @@ class ProfileWithToken(BaseModel):
token: TokenResponse

class ProfileLogin(BaseModel):
email: EmailStr
email: str
password: str

class ProfilUpdate(BaseModel):
Expand Down