add logging to security
This commit is contained in:
27
database.py
27
database.py
@@ -2,8 +2,6 @@ from models import User
|
||||
from settings import settings
|
||||
from fastapi import HTTPException, status, Request
|
||||
import sqlite3
|
||||
import jwt
|
||||
import datetime
|
||||
import security
|
||||
|
||||
|
||||
@@ -60,18 +58,15 @@ def register(user: User) -> None:
|
||||
|
||||
def get_user_by_token(request: Request) -> User:
|
||||
"""Retrieves a user from the database using a JWT token."""
|
||||
token = request.headers.get("Authorization")
|
||||
if not token or not token.startswith("Bearer "):
|
||||
|
||||
payload = security.decode_jwt(
|
||||
request.headers.get("Authorization"))
|
||||
|
||||
if not payload:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Not authenticated"
|
||||
)
|
||||
token = token.split(" ")[1]
|
||||
payload = jwt.decode(
|
||||
token,
|
||||
key=settings.jwt_secret,
|
||||
algorithms=[
|
||||
settings.jwt_algorithm])
|
||||
|
||||
connection, cursor = connect()
|
||||
cursor.execute(
|
||||
@@ -100,14 +95,4 @@ def login(user: User) -> str:
|
||||
detail="Invalid credentials"
|
||||
)
|
||||
|
||||
exp = datetime.datetime.now(
|
||||
datetime.timezone.utc) + datetime.timedelta(hours=1)
|
||||
payload = {
|
||||
"id": row["id"],
|
||||
"exp": exp
|
||||
}
|
||||
|
||||
return jwt.encode(
|
||||
payload=payload,
|
||||
key=settings.jwt_secret,
|
||||
algorithm=settings.jwt_algorithm)
|
||||
return security.sign_jwt(row)
|
||||
|
||||
Reference in New Issue
Block a user