2024-04-06 19:08:41 +00:00
|
|
|
from datetime import timedelta, datetime
|
2024-04-06 18:51:28 +00:00
|
|
|
from typing import Annotated
|
2024-04-06 19:29:08 +00:00
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
2024-04-06 18:51:28 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
from passlib.context import CryptContext
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from starlette import status
|
2024-04-06 19:29:08 +00:00
|
|
|
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer
|
|
|
|
from jose import jwt, JWTError
|
2024-04-06 18:51:28 +00:00
|
|
|
|
|
|
|
from models import User
|
|
|
|
from database import SessionLocal
|
2024-04-06 17:58:20 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2024-04-06 19:08:41 +00:00
|
|
|
SECRET_KEY = '3b004eeae34b43bd05226f210d9bdc2ad99abdd3c52bf32802906085b762ff55'
|
|
|
|
ALGORITHM = 'HS256'
|
|
|
|
|
2024-04-06 18:51:28 +00:00
|
|
|
bcrypt_context = CryptContext(schemes=['bcrypt'], deprecated='auto')
|
2024-04-06 19:29:08 +00:00
|
|
|
oauth2_bearer = OAuth2PasswordBearer(tokenUrl='validate')
|
2024-04-06 18:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_db():
|
|
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
|
|
yield db
|
|
|
|
finally:
|
|
|
|
db.close()
|
|
|
|
|
|
|
|
|
|
|
|
db_dependency = Annotated[Session, Depends(get_db)]
|
|
|
|
|
|
|
|
|
|
|
|
def authenticate_user(username: str, password: str, db):
|
|
|
|
user = db.query(User).filter(User.username == username).first()
|
|
|
|
if not user:
|
|
|
|
return False
|
|
|
|
if not bcrypt_context.verify(password, user.password):
|
|
|
|
return False
|
2024-04-06 19:08:41 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
|
|
|
def create_access_token(username: str, user_id: int, expires_delta: timedelta):
|
|
|
|
encode = {'sub': username, 'id': user_id}
|
|
|
|
expire = datetime.now() + expires_delta
|
|
|
|
encode.update({'exp': expire})
|
|
|
|
|
|
|
|
return jwt.encode(encode, SECRET_KEY, ALGORITHM)
|
2024-04-06 18:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CreateUser(BaseModel):
|
|
|
|
username: str
|
|
|
|
email: str
|
|
|
|
password: str
|
|
|
|
|
|
|
|
|
2024-04-06 19:08:41 +00:00
|
|
|
class Token(BaseModel):
|
|
|
|
access_token: str
|
|
|
|
token_type: str
|
|
|
|
|
|
|
|
|
2024-04-06 19:29:08 +00:00
|
|
|
async def get_current_user(token: Annotated[str, Depends(oauth2_bearer)]):
|
|
|
|
try:
|
|
|
|
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
|
|
|
username: str = payload.get('sub')
|
|
|
|
user_id: int = payload.get('id')
|
|
|
|
if username is None or user_id is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials")
|
|
|
|
|
|
|
|
return {'username': username, 'user_id': user_id}
|
|
|
|
except JWTError:
|
|
|
|
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials")
|
|
|
|
|
|
|
|
|
2024-04-06 18:51:28 +00:00
|
|
|
@router.post('/auth/create', status_code=status.HTTP_201_CREATED)
|
|
|
|
async def create_user(db: db_dependency, data: CreateUser):
|
|
|
|
create_user_model = User(
|
|
|
|
username=data.username,
|
|
|
|
email=data.email,
|
|
|
|
password=bcrypt_context.hash(data.password),
|
|
|
|
role='admin'
|
|
|
|
)
|
|
|
|
|
|
|
|
db.add(create_user_model)
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
|
2024-04-06 19:08:41 +00:00
|
|
|
@router.post('/auth/token', status_code=status.HTTP_200_OK, response_model=Token)
|
2024-04-06 18:51:28 +00:00
|
|
|
async def get_token(
|
|
|
|
form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
|
|
|
|
db: db_dependency
|
|
|
|
):
|
|
|
|
user = authenticate_user(form_data.username, form_data.password, db)
|
2024-04-06 19:08:41 +00:00
|
|
|
if not user:
|
|
|
|
return "authentication failed"
|
2024-04-06 17:58:20 +00:00
|
|
|
|
2024-04-06 19:08:41 +00:00
|
|
|
token = create_access_token(user.username, user.id, timedelta(minutes=20))
|
|
|
|
return {'access_token': token, 'token_type': 'bearer'}
|