add ability to create user, add password auth, add logging and work on dev containers

This commit is contained in:
maxid
2025-02-16 20:52:05 +01:00
parent ab6fc52e42
commit 5ac7e0a0d4
8 changed files with 63 additions and 34 deletions

View File

@@ -1,6 +1,7 @@
from datetime import datetime, timedelta, timezone
from typing import Annotated
import bcrypt
import jwt
from fastapi import Depends, FastAPI, HTTPException, status, APIRouter
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
@@ -37,7 +38,6 @@ class Token(BaseModel):
class TokenData(BaseModel):
username: str | None = None
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@@ -45,11 +45,17 @@ app = APIRouter()
def verify_password(plain_password, hashed_password):
return pwd_context.verify(plain_password, hashed_password)
return bcrypt.checkpw(
bytes(plain_password, encoding="utf-8"),
bytes(hashed_password, encoding="utf-8"),
)
def get_password_hash(password):
return pwd_context.hash(password)
return bcrypt.hashpw(
bytes(password, encoding="utf-8"),
bcrypt.gensalt(),
)
def authenticate_user(email: str, password: str) -> UserInternal:
@@ -105,6 +111,6 @@ async def login_for_access_token(
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
data={"sub": user.email}, expires_delta=access_token_expires
)
return Token(access_token=access_token, token_type="bearer")