From 43ce7eae56327fcd70ef033afd2c47ef7b8d0aeb Mon Sep 17 00:00:00 2001 From: Sahi Mohamed Francis Gonsangbeu Date: Fri, 2 Feb 2024 16:45:07 +0100 Subject: [PATCH] Refactor main.py: Remove unused imports and authentication code --- app/main.py | 79 +---------------------------------------------------- 1 file changed, 1 insertion(+), 78 deletions(-) diff --git a/app/main.py b/app/main.py index 515e7ba..f8ca7e8 100644 --- a/app/main.py +++ b/app/main.py @@ -1,17 +1,9 @@ -import os -from datetime import datetime, timedelta from io import BytesIO import uvicorn -from doten import load_dotenv # type: ignore -from fastapi import Depends, FastAPI, File, HTTPException, UploadFile, status -from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm -from jose import jwt # type: ignore -from passlib.context import CryptContext # type: ignore +from fastapi import FastAPI, File, HTTPException, UploadFile from PIL import Image from pydantic import BaseModel -from sqlalchemy import Column, Integer, String # type: ignore -from sqlalchemy.ext.declarative import declarative_base # type: ignore from .model import load_model, predict, prepare_image @@ -19,75 +11,6 @@ model = load_model() -load_dotenv() - -SECRET_KEY = os.getenv("SECRET_KEY") -ALGORITHM = os.getenv("ALGORITHM") -ACCESS_TOKEN_EXPIRE_MINUTES = os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES") - - -Base = declarative_base() - - -class User(Base): - __tablename__ = "users" - - id = Column(Integer, primary_key=True, index=True) - username = Column(String, unique=True, index=True) - hashed_password = Column(String) - - -class Token(BaseModel): - access_token: str - token_type: str - - -class UserInDB(User): - hashed_password: str - - -pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") -oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") - - -def verify_password(plain_password, hashed_password): - return pwd_context.verify(plain_password, hashed_password) - - -def get_password_hash(password): - return pwd_context.hash(password) - - -def authenticate_user(users, username: str, password: str): - user = users.get(username) - if not user: - return False - if not verify_password(password, user.hashed_password): - return False - return user - - -def create_acess_token(data: dict, expires_delta: timedelta = timedelta(minutes=15)): - to_encode = data.copy() - expire = datetime.utcnow() + expires_delta - to_encode.update({"exp": expire}) - encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) - return encoded_jwt - - -@app.post("/token", response_model=Token) -def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()): - user = authenticate_user(users, form_data.username, form_data.password) - if not user: - raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail="Incorrect username or password", - headers={"WWW-Authenticate": "Bearer"}, - ) - access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) - access_token = create_acess_token(data={"sub": user.username}, expires_delta=access_token_expires) - return {"access_token": access_token, "token_type": "bearer"} - @app.get("/", tags=["Welcome"]) def greeting():