Spaces:
Sleeping

File size: 8,238 Bytes
2985cd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from datetime import datetime, timedelta
from typing import Optional, Dict, Any
from sqlmodel import Session, select
from jose import JWTError, jwt
import os
from ..models.user import User
from ..config import settings
from ..utils.date_validator import validate_signup_date

SECRET_KEY = settings.secret_key
ALGORITHM = settings.algorithm


class AuthException(Exception):
    """Custom exception for authentication-related errors"""
    def __init__(self, error_type: str, message: str):
        self.error_type = error_type
        self.message = message
        super().__init__(self.message)


class AuthService:
    def __init__(self):
        pass

    def create_access_token(self, user_id: int, expires_delta: Optional[timedelta] = None):
        """Create a new JWT access token for a user"""
        to_encode = {"sub": str(user_id)}

        if expires_delta:
            expire = datetime.utcnow() + expires_delta
        else:
            expire = datetime.utcnow() + timedelta(minutes=settings.access_token_expire_minutes)

        to_encode.update({"exp": expire, "iat": datetime.utcnow()})

        encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
        return encoded_jwt

    def verify_token(self, token: str) -> Optional[int]:
        """Verify JWT token and return user ID if valid"""
        try:
            payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
            user_id: str = payload.get("sub")
            if user_id is None:
                return None
            return int(user_id)
        except JWTError:
            return None

    def authenticate_user(self, session: Session, email: str, password: str) -> Optional[Dict[str, Any]]:
        """
        Authenticate user with enhanced error handling to differentiate between email and password errors.

        Args:
            session: Database session
            email: User's email address
            password: User's password

        Returns:
            Dict with user info and token if authentication successful, None otherwise
        """
        # Find user by email
        statement = select(User).where(User.email == email)
        user = session.exec(statement).first()

        if user is None:
            # User with this email doesn't exist
            raise AuthException(
                error_type="EMAIL_NOT_FOUND",
                message="Invalid email or password"
            )

        # At this point, we know the email exists, so we check the password
        # Import here to avoid circular imports
        from passlib.context import CryptContext

        pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

        if not pwd_context.verify(password, user.password):
            # Email exists but password is incorrect
            raise AuthException(
                error_type="INVALID_PASSWORD",
                message="Invalid email or password"
            )

        # Authentication successful
        token = self.create_access_token(user.id)
        return {
            "user": user,
            "token": token
        }

    def authenticate_user_detailed_errors(self, session: Session, email: str, password: str) -> Optional[Dict[str, Any]]:
        """
        Authenticate user with detailed error messages for different error types.
        This method is used for the enhanced error handling feature.

        Args:
            session: Database session
            email: User's email address
            password: User's password

        Returns:
            Dict with user info and token if authentication successful, None otherwise
        """
        # Find user by email
        statement = select(User).where(User.email == email)
        user = session.exec(statement).first()

        if user is None:
            # User with this email doesn't exist
            raise AuthException(
                error_type="EMAIL_NOT_FOUND",
                message="No account found with this email address"
            )

        # At this point, we know the email exists, so we check the password
        # Import here to avoid circular imports
        from passlib.context import CryptContext

        pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

        if not pwd_context.verify(password, user.password):
            # Email exists but password is incorrect
            raise AuthException(
                error_type="INVALID_PASSWORD",
                message="Incorrect password"
            )

        # Authentication successful
        token = self.create_access_token(user.id)
        return {
            "user": user,
            "token": token
        }

    def authenticate_user_secure(self, session: Session, email: str, password: str) -> Optional[Dict[str, Any]]:
        """
        Secure authentication that doesn't reveal whether an email exists in the system.
        This maintains security best practices while still providing helpful feedback.

        Args:
            session: Database session
            email: User's email address
            password: User's password

        Returns:
            Dict with user info and token if authentication successful, None otherwise
        """
        # Find user by email
        statement = select(User).where(User.email == email)
        user = session.exec(statement).first()

        # Always perform password verification to prevent timing attacks
        from passlib.context import CryptContext
        pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

        # If user doesn't exist, create a fake hash to compare against for timing consistency
        if user is None:
            # Use a fake password to maintain consistent timing
            pwd_context.hash("fake_password_for_timing_attack_prevention")
            raise AuthException(
                error_type="AUTH_FAILED",
                message="Invalid email or password"
            )

        # If user exists, verify the actual password
        if not pwd_context.verify(password, user.password):
            raise AuthException(
                error_type="AUTH_FAILED",
                message="Invalid email or password"
            )

        # Authentication successful
        token = self.create_access_token(user.id)
        return {
            "user": user,
            "token": token
        }

    def register_user(self, session: Session, email: str, password: str, name: Optional[str] = None,
                     date_of_birth: Optional[str] = None, signup_date: Optional[datetime] = None) -> Optional[Dict[str, Any]]:
        """
        Register a new user with signup date validation.

        Args:
            session: Database session
            email: User's email address
            password: User's password
            name: Optional user name
            date_of_birth: Optional date of birth
            signup_date: Signup date (will be set to now if not provided)

        Returns:
            Dict with user info and token if registration successful, None otherwise
        """
        # Check if user already exists
        statement = select(User).where(User.email == email)
        existing_user = session.exec(statement).first()

        if existing_user:
            raise AuthException(
                error_type="EMAIL_EXISTS",
                message="A user with this email already exists"
            )

        # Hash the password
        from passlib.context import CryptContext
        pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
        hashed_password = pwd_context.hash(password)

        # Validate and set signup date
        validated_signup_date = validate_signup_date(signup_date)

        # Create new user
        user = User(
            email=email,
            password=hashed_password,
            name=name,
            date_of_birth=date_of_birth,
            signup_date=validated_signup_date  # Non-nullable field
        )

        session.add(user)
        session.commit()
        session.refresh(user)

        # Create access token
        token = self.create_access_token(user.id)

        return {
            "user": user,
            "token": token
        }