Skip to content
Open
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
35 changes: 35 additions & 0 deletions antarest/login/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
# This file is part of the Antares project.

import contextlib
import re
import uuid
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

import bcrypt
from pydantic import field_validator
from sqlalchemy import Boolean, Enum, ForeignKey, Integer, Sequence, String
from sqlalchemy.engine.base import Engine
from sqlalchemy.exc import IntegrityError
Expand Down Expand Up @@ -65,6 +67,39 @@ class UserCreateDTO(AntaresBaseModel):
name: str
password: str

@field_validator("password")
@classmethod
def validate_password_strength(cls, v: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would need some unit tests

"""
Validate password meets security requirements.

Requirements (must match frontend validation):
- Length: 8-50 characters
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character

Raises:
ValueError: If password doesn't meet requirements
"""
if not (8 <= len(v) <= 50):
raise ValueError("Password must be between 8 and 50 characters")

if not re.search(r"[a-z]", v):
raise ValueError("Password must contain at least one lowercase letter")

if not re.search(r"[A-Z]", v):
raise ValueError("Password must contain at least one uppercase letter")

if not re.search(r"\d", v):
raise ValueError("Password must contain at least one digit")

if not re.search(r'[!@#$%^&*(),.?":{}|<>]', v):
raise ValueError("Password must contain at least one special character (!@#$%^&*(),.?\":{}|<>)")

return v


class GroupDTO(AntaresBaseModel):
id: str
Expand Down