File size: 1,511 Bytes
b7ed26f
 
 
96b013c
 
2ea4a10
b7ed26f
 
96b013c
b7ed26f
 
60dc045
b7ed26f
96b013c
 
b7ed26f
2ea4a10
96b013c
 
b7ed26f
 
96b013c
b7ed26f
96b013c
 
b7ed26f
96b013c
b7ed26f
 
 
ef35448
96b013c
 
 
 
 
 
b7ed26f
 
 
 
 
96b013c
b7ed26f
96b013c
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
"""
Router for handling user authentication and issuing JWT tokens.
"""
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
from fastapi.concurrency import run_in_threadpool
from sqlalchemy.orm import Session
from datetime import timedelta

from src import models
from src.database import get_db
from src.auth import create_access_token, authenticate_user
from src.config import settings

router = APIRouter(
    prefix="/api",
    tags=["Authentication Token"]
)

@router.post("/token", response_model=models.Token)
async def login_for_access_token(
    form_data: OAuth2PasswordRequestForm = Depends(),
    db: Session = Depends(get_db)
):
    """
    Provides a JWT token for authenticated users.
    
    This is the primary login endpoint. It takes a username and password
    and returns an access token if the credentials are valid.
    """
    user = authenticate_user(db, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    
    access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = create_access_token(
        data={"sub": user.username, "role": user.role.value},
        expires_delta=access_token_expires
    )
    
    return {"access_token": access_token, "token_type": "bearer"}