Initial commit

This commit is contained in:
pptx704
2025-06-14 04:28:24 +03:00
parent 813a7eaf0d
commit 52aa93d67c
22 changed files with 442 additions and 2 deletions

0
app/__init__.py Normal file
View File

0
app/const.py Normal file
View File

21
app/database.py Normal file
View File

@ -0,0 +1,21 @@
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from .const import SQLALCHEMY_DATABASE_URL
engine = create_engine(
SQLALCHEMY_DATABASE_URL
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()

7
app/models.py Normal file
View File

@ -0,0 +1,7 @@
from app.database import Base
from sqlalchemy.sql import func
import uuid
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey, PrimaryKeyConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.schema import Index

View File

6
app/repositories/auth.py Normal file
View File

@ -0,0 +1,6 @@
from fastapi import HTTPException, status
from sqlalchemy.orm import Session
from .. import schemas
from ..models import User
from ..security import get_password_hash, verify_password, create_jwt_token

0
app/routers/__init__.py Normal file
View File

14
app/routers/auth.py Normal file
View File

@ -0,0 +1,14 @@
from app import database, schemas
from fastapi import APIRouter, Depends, status
from sqlalchemy.orm import Session
from ..repositories import auth
from .. import schemas
from ..security import get_user
from ..database import get_db
router = APIRouter(
prefix = "",
tags = ['auth']
)

14
app/schemas.py Normal file
View File

@ -0,0 +1,14 @@
from typing import List, Optional
from pydantic import BaseModel, EmailStr
import uuid
import enum
import datetime
class BaseResponse(BaseModel):
message: str
class User(BaseModel):
id: uuid.UUID
class Config:
orm_mode = True

52
app/security.py Normal file
View File

@ -0,0 +1,52 @@
import os
from passlib.context import CryptContext
import jwt
from datetime import datetime, timedelta, UTC
from app.models import User
from fastapi import Security, HTTPException
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from .database import get_db
from .models import User
from .settings import settings
from . import schemas
pwd_context = CryptContext(schemes=["bcrypt"])
def get_password_hash(password: str) -> str:
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
def create_jwt_token(data: dict) -> str:
_ed = timedelta(minutes=settings.JWT_EXPIRE_MINUTES)
iat = datetime.now(UTC)
exp = datetime.now(UTC) + _ed
token_payload = data
token_payload.update({"iat": iat, "exp": exp})
token = jwt.encode(token_payload, settings.JWT_SECRET, algorithm=settings.JWT_ALGORITHM)
return token
def get_user_from_token(token: str) -> schemas.User:
try:
payload = jwt.decode(token, settings.JWT_SECRET, algorithms=[settings.JWT_ALGORITHM])
except jwt.ExpiredSignatureError:
raise HTTPException(status_code=401, detail="Token has expired")
except jwt.JWTError:
raise HTTPException(status_code=401, detail="Invalid authentication credentials")
user_id = payload.get("user_id")
...
def get_user(authorization: HTTPAuthorizationCredentials = Security(HTTPBearer())) -> schemas.User:
if authorization.scheme.lower() != "bearer":
raise HTTPException(status_code=401, detail="Invalid authentication scheme")
token = authorization.credentials
return get_user_from_token(token)

13
app/settings.py Normal file
View File

@ -0,0 +1,13 @@
import os
from pydantic_settings import BaseSettings
from dotenv import load_dotenv
load_dotenv()
class Settings(BaseSettings):
JWT_SECRET: str
JWT_ALGORITHM: str
JWT_EXPIRE_MINUTES: int
SQLALCHEMY_DATABASE_URL: str
settings = Settings()

0
app/utils.py Normal file
View File