Download app/api/auth.py from aionaion/chatdoc: direct link, hf CLI and curl.
- Browser
- Download file 1.16 kB
-
https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/app/api/auth.py
- Command line
-
hf download hf://spaces/aionaion/chatdoc/app/api/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/aionaion/chatdoc/resolve/main/app/api/auth.py
1.16 kB
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from fastapi.security import OAuth2PasswordRequestForm | |
| from sqlalchemy.orm import Session | |
| from datetime import timedelta | |
| from app.core.security import create_access_token, verify_password | |
| from app.db.database import get_db | |
| from app.db.models import User | |
| router = APIRouter() | |
| def login_for_access_token( | |
| form_data: OAuth2PasswordRequestForm = Depends(), | |
| db: Session = Depends(get_db) | |
| ): | |
| user = db.query(User).filter(User.username == form_data.username).first() | |
| if not user or not verify_password(form_data.password, user.hashed_password): | |
| raise HTTPException( | |
| status_code=status.HTTP_401_UNAUTHORIZED, | |
| detail="Incorrect username or password", | |
| headers={"WWW-Authenticate": "Bearer"}, | |
| ) | |
| access_token_expires = timedelta(minutes=30) | |
| access_token = create_access_token( | |
| data={"sub": user.username, "scopes": user.scopes.split(',')}, | |
| expires_delta=access_token_expires | |
| ) | |
| return {"access_token": access_token, "token_type": "bearer"} | |