28 lines
921 B
Python
28 lines
921 B
Python
from datetime import timedelta
|
|
from modules.app import app
|
|
from modules.utils import configGet
|
|
|
|
from fastapi import Depends, HTTPException
|
|
from fastapi.security import (
|
|
OAuth2PasswordRequestForm,
|
|
)
|
|
|
|
from modules.security import (
|
|
ACCESS_TOKEN_EXPIRE_DAYS,
|
|
Token,
|
|
authenticate_user,
|
|
create_access_token
|
|
)
|
|
|
|
|
|
@app.post("/token", response_model=Token)
|
|
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
user = authenticate_user(form_data.username, form_data.password)
|
|
if not user:
|
|
raise HTTPException(status_code=400, detail=configGet("credentials_invalid", "messages"))
|
|
access_token_expires = timedelta(days=ACCESS_TOKEN_EXPIRE_DAYS)
|
|
access_token = create_access_token(
|
|
data={"sub": user.user, "scopes": form_data.scopes},
|
|
expires_delta=access_token_expires,
|
|
)
|
|
return {"access_token": access_token, "token_type": "bearer"} |