File size: 4,891 Bytes
bbeedce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bde2e2
 
 
 
e8252b1
 
 
 
bbeedce
 
 
 
 
e8252b1
 
 
 
bbeedce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from sqlmodel import Session, select
from ..models.user import User, UserCreate
from ..utils.security import SecurityValidator
from passlib.context import CryptContext
from fastapi import HTTPException, status

# Password hashing context
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

class UserService:
    """Service class for user-related operations"""

    @staticmethod
    def get_password_hash(password: str) -> str:
        """Hash a plain text password (bcrypt has 72 byte limit)"""
        # Ensure password is a string
        if not isinstance(password, str):
            raise ValueError(f"Password must be a string, got {type(password)}")

        # Truncate password to 72 bytes (not characters) to avoid bcrypt error
        password_bytes = password.encode('utf-8')[:72]
        # Decode back, ignoring any incomplete multi-byte sequences at the end
        truncated_password = password_bytes.decode('utf-8', errors='ignore')
        return pwd_context.hash(truncated_password)

    @staticmethod
    def verify_password(plain_password: str, hashed_password: str) -> bool:
        """Verify a plain text password against its hash"""
        # Truncate password to 72 bytes (not characters) to match hashing
        password_bytes = plain_password.encode('utf-8')[:72]
        # Decode back, ignoring any incomplete multi-byte sequences at the end
        truncated_password = password_bytes.decode('utf-8', errors='ignore')
        return pwd_context.verify(truncated_password, hashed_password)

    @classmethod
    def create_user(cls, db: Session, user: UserCreate) -> User:
        """Create a new user in the database with security validation"""
        # Validate email
        is_valid, error_msg = SecurityValidator.validate_email(user.email)
        if not is_valid:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error_msg)

        # Validate username
        is_valid, error_msg = SecurityValidator.validate_username(user.username)
        if not is_valid:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error_msg)

        # Validate password
        is_valid, error_msg = SecurityValidator.validate_password(user.password)
        if not is_valid:
            raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error_msg)

        # Hash the password
        hashed_password = cls.get_password_hash(user.password)

        # Create user instance
        db_user = User(
            email=user.email.lower().strip(),
            username=user.username.strip(),
            password_hash=hashed_password
        )

        db.add(db_user)
        db.commit()
        db.refresh(db_user)
        return db_user

    @classmethod
    def get_user_by_email(cls, db: Session, email: str) -> User:
        """Retrieve a user by email"""
        statement = select(User).where(User.email == email.lower().strip())
        return db.exec(statement).first()

    @classmethod
    def get_user_by_username(cls, db: Session, username: str) -> User:
        """Retrieve a user by username"""
        statement = select(User).where(User.username == username.strip())
        return db.exec(statement).first()

    @classmethod
    def get_user_by_id(cls, db: Session, user_id: str) -> User:
        """Retrieve a user by ID (Better Auth uses string IDs)"""
        # For Better Auth compatibility, we query the Better Auth user table
        # This is a simplified version - in production you'd query the actual Better Auth table
        return None  # Disabled for Better Auth - user verification happens via JWT

    @classmethod
    def authenticate_user(cls, db: Session, email: str, password: str) -> User:
        """Authenticate a user by email and password"""
        user = cls.get_user_by_email(db, email)
        if not user or not cls.verify_password(password, user.password_hash):
            return None
        return user

    @classmethod
    def update_user(cls, db: Session, user_id: int, user_update) -> User:
        """Update user information with validation"""
        user = cls.get_user_by_id(db, user_id)
        if not user:
            return None

        if user_update.email:
            is_valid, error_msg = SecurityValidator.validate_email(user_update.email)
            if not is_valid:
                raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error_msg)
            user.email = user_update.email.lower().strip()

        if user_update.username:
            is_valid, error_msg = SecurityValidator.validate_username(user_update.username)
            if not is_valid:
                raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=error_msg)
            user.username = user_update.username.strip()

        db.add(user)
        db.commit()
        db.refresh(user)
        return user