File size: 9,378 Bytes
be86a81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
"""
Auth Routes Module

This module defines FastAPI routes for JWT-based authentication operations.
All authentication is handled by the backend with custom JWT tokens.

Security Features:
- JWT access tokens (15 minutes expiry)
- JWT refresh tokens (7 days expiry)
- Token rotation on refresh
- Password hashing with bcrypt (cost factor 12)
- Token revocation on logout
"""

from fastapi import APIRouter, Depends, status
from sqlalchemy.ext.asyncio import AsyncSession

from src.models.database import get_db
from src.models.schemas import (
    UserCreate,
    UserLogin,
    RefreshTokenRequest,
    TokenResponse,
    UserResponse,
    MessageResponse
)
from src.services.auth_service import (
    register_user,
    login_user,
    refresh_tokens,
    logout_user
)
from src.api.deps import get_current_user
from src.models.models import User


# Create router with prefix and tags
router = APIRouter(
    prefix="/api/auth",
    tags=["auth"]
)


@router.post(
    "/signup",
    response_model=TokenResponse,
    status_code=status.HTTP_201_CREATED,
    summary="Register a new user",
    description="Create a new user account with email and password. Returns JWT tokens."
)
async def signup(
    user_data: UserCreate,
    db: AsyncSession = Depends(get_db)
) -> TokenResponse:
    """
    Register a new user and create JWT tokens.

    This endpoint creates a new user account with the provided email and password.
    The password is hashed using bcrypt before storage. Returns both access and
    refresh tokens for immediate login.

    Args:
        user_data: User registration data (email, password)
        db: Database session

    Returns:
        TokenResponse: Contains access_token, refresh_token, token_type, expires_in, and user info

    Raises:
        400: If email already exists
        422: If validation fails (email format, password length)

    Security:
        - Password hashed with bcrypt (cost factor 12)
        - Email validated with regex pattern
        - Password minimum 8 characters
        - User created as unverified (is_verified=False)

    Example:
        POST /api/auth/signup
        {
            "email": "user@example.com",
            "password": "securepassword123"
        }

        Response:
        {
            "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
            "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
            "token_type": "bearer",
            "expires_in": 900,
            "user": {
                "id": "uuid",
                "email": "user@example.com",
                "role": "user",
                "is_verified": false
            }
        }
    """
    # Register user (hashes password, creates user in DB)
    user = await register_user(
        db,
        email=user_data.email,
        password=user_data.password
    )

    # Login user (create JWT tokens and session)
    tokens = await login_user(
        db,
        email=user_data.email,
        password=user_data.password
    )

    return tokens


@router.post(
    "/login",
    response_model=TokenResponse,
    status_code=status.HTTP_200_OK,
    summary="Login with email and password",
    description="Authenticate with credentials and receive JWT tokens."
)
async def login(
    user_data: UserLogin,
    db: AsyncSession = Depends(get_db)
) -> TokenResponse:
    """
    Login a user and create JWT tokens.

    This endpoint authenticates a user with email and password.
    If credentials are valid, it creates new access and refresh tokens.

    Args:
        user_data: User login data (email, password)
        db: Database session

    Returns:
        TokenResponse: Contains access_token, refresh_token, token_type, expires_in, and user info

    Raises:
        401: If email or password is incorrect

    Security:
        - Password verified against bcrypt hash
        - Timing attack protection (bcrypt is slow)
        - Generic error message (prevents user enumeration)
        - Old refresh tokens invalidated on new login

    Example:
        POST /api/auth/login
        {
            "email": "user@example.com",
            "password": "securepassword123"
        }

        Response:
        {
            "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
            "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
            "token_type": "bearer",
            "expires_in": 900,
            "user": {
                "id": "uuid",
                "email": "user@example.com",
                "role": "user",
                "is_verified": false
            }
        }
    """
    # Login user (verifies password, creates JWT tokens)
    tokens = await login_user(
        db,
        email=user_data.email,
        password=user_data.password
    )

    return tokens


@router.post(
    "/refresh",
    response_model=TokenResponse,
    status_code=status.HTTP_200_OK,
    summary="Refresh access token",
    description="Get a new access token using a valid refresh token."
)
async def refresh(
    refresh_data: RefreshTokenRequest,
    db: AsyncSession = Depends(get_db)
) -> TokenResponse:
    """
    Refresh access token using refresh token.

    This endpoint validates the refresh token and issues a new access token
    and refresh token. The old refresh token is revoked (token rotation).

    Args:
        refresh_data: Refresh token request
        db: Database session

    Returns:
        TokenResponse: Contains new access_token, new refresh_token, token_type, expires_in, and user info

    Raises:
        401: If refresh token is invalid, expired, or revoked

    Security:
        - Refresh token validated against database
        - Old refresh token immediately revoked
        - New refresh token generated (token rotation)
        - Prevents token reuse attacks
        - All old tokens invalidated on new login

    Example:
        POST /api/auth/refresh
        {
            "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
        }

        Response:
        {
            "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
            "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
            "token_type": "bearer",
            "expires_in": 900,
            "user": {
                "id": "uuid",
                "email": "user@example.com",
                "role": "user",
                "is_verified": false
            }
        }
    """
    # Refresh tokens (validates refresh token, creates new tokens, revokes old token)
    tokens = await refresh_tokens(
        db,
        refresh_token=refresh_data.refresh_token
    )

    return tokens


@router.post(
    "/logout",
    response_model=MessageResponse,
    status_code=status.HTTP_200_OK,
    summary="Logout current user",
    description="Revoke the refresh token and logout the user."
)
async def logout(
    refresh_data: RefreshTokenRequest,
    db: AsyncSession = Depends(get_db)
) -> MessageResponse:
    """
    Logout a user by revoking their refresh token.

    This endpoint revokes the provided refresh token in the database.
    The access token will expire naturally (15 minutes). Frontend should
    delete both tokens from storage.

    Args:
        refresh_data: Refresh token to revoke
        db: Database session

    Returns:
        MessageResponse: Success message

    Security:
        - Refresh token revoked in database
        - All user sessions revoked (not just this token)
        - Access token expires naturally (cannot be revoked instantly)
        - Generic success response (prevents token enumeration)

    Example:
        POST /api/auth/logout
        {
            "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
        }

        Response:
        {
            "message": "Successfully logged out"
        }
    """
    # Logout user (revokes all refresh tokens)
    await logout_user(
        db,
        refresh_token=refresh_data.refresh_token
    )

    return MessageResponse(
        message="Successfully logged out"
    )


@router.get(
    "/me",
    response_model=UserResponse,
    status_code=status.HTTP_200_OK,
    summary="Get current user",
    description="Get information about the currently authenticated user."
)
async def get_current_user_info(
    current_user: User = Depends(get_current_user)
) -> UserResponse:
    """
    Get information about the currently authenticated user.

    This endpoint validates the JWT access token and returns user information
    from the database.

    Args:
        current_user: Authenticated user object (injected from JWT)

    Returns:
        UserResponse: Current user information (id, email, role, timestamps)

    Raises:
        401: If JWT access token is missing or invalid

    Security:
        - JWT access token validated
        - User fetched from database (fresh data)
        - No password_hash returned
        - Token must be valid (not expired)

    Example:
        GET /api/auth/me
        Headers: Authorization: Bearer <access_token>

        Response:
        {
            "id": "uuid",
            "email": "user@example.com",
            "role": "user",
            "is_verified": false,
            "created_at": "2024-01-18T10:30:00Z",
            "updated_at": "2024-01-18T10:30:00Z"
        }
    """
    return UserResponse.model_validate(current_user)