|
| 1 | +from fastapi import Depends, FastAPI, HTTPException, status |
| 2 | +from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm |
| 3 | +from pydantic import BaseModel |
| 4 | +from datetime import datetime, timedelta |
| 5 | +from jose import JWTError, jwt |
| 6 | +from passlib.context import CryptContext |
| 7 | + |
| 8 | +# Constants |
| 9 | +SECRET_KEY = "" |
| 10 | +ALGORITHM = "HS256" |
| 11 | +ACCESS_TOKEN_EXPIRE_MINUTES = 30 |
| 12 | + |
| 13 | +# Database simulation |
| 14 | +db = { |
| 15 | + "aman": { |
| 16 | + "username": "king04aman", |
| 17 | + "full_name": "Aman Kumar", |
| 18 | + |
| 19 | + "hashed_password": "$2b$12$HxWHkvMuL7WrZad6lcCfluNFj1/Zp63lvP5aUrKlSTYtoFzPXHOtu", |
| 20 | + "disabled": False |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +# Pydantic models |
| 25 | +class Token(BaseModel): |
| 26 | + access_token: str |
| 27 | + token_type: str |
| 28 | + |
| 29 | +class TokenData(BaseModel): |
| 30 | + username: str or None = None |
| 31 | + |
| 32 | +class User(BaseModel): |
| 33 | + username: str |
| 34 | + email: str or None = None |
| 35 | + full_name: str or None = None |
| 36 | + disabled: bool or None = None |
| 37 | + |
| 38 | +class UserInDB(User): |
| 39 | + hashed_password: str |
| 40 | + |
| 41 | +# Password hashing context |
| 42 | +pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") |
| 43 | +# OAuth2 password bearer |
| 44 | +oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") |
| 45 | + |
| 46 | +# FastAPI instance |
| 47 | +app = FastAPI() |
| 48 | + |
| 49 | +# Password helper functions |
| 50 | +def verify_password(plain_password, hashed_password): |
| 51 | + return pwd_context.verify(plain_password, hashed_password) |
| 52 | + |
| 53 | +def get_password_hash(password): |
| 54 | + return pwd_context.hash(password) |
| 55 | + |
| 56 | +# User authentication functions |
| 57 | +def get_user(db, username: str): |
| 58 | + if username in db: |
| 59 | + user_data = db[username] |
| 60 | + return UserInDB(**user_data) |
| 61 | + |
| 62 | +def authenticate_user(db, username: str, password: str): |
| 63 | + user = get_user(db, username) |
| 64 | + if not user: |
| 65 | + return False |
| 66 | + if not verify_password(password, user.hashed_password): |
| 67 | + return False |
| 68 | + return user |
| 69 | + |
| 70 | +# Token creation function |
| 71 | +def create_access_token(data: dict, expires_delta: timedelta or None = None): |
| 72 | + to_encode = data.copy() |
| 73 | + if expires_delta: |
| 74 | + expire = datetime.utcnow() + expires_delta |
| 75 | + else: |
| 76 | + expire = datetime.utcnow() + timedelta(minutes=15) |
| 77 | + to_encode.update({"exp": expire}) |
| 78 | + encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) |
| 79 | + return encoded_jwt |
| 80 | + |
| 81 | +# Dependency to get current user |
| 82 | +async def get_current_user(token: str = Depends(oauth2_scheme)): |
| 83 | + credential_exception = HTTPException( |
| 84 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 85 | + detail="Could not validate credentials", |
| 86 | + headers={"WWW-Authenticate": "Bearer"} |
| 87 | + ) |
| 88 | + try: |
| 89 | + payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) |
| 90 | + username: str = payload.get("sub") |
| 91 | + if username is None: |
| 92 | + raise credential_exception |
| 93 | + token_data = TokenData(username=username) |
| 94 | + except JWTError: |
| 95 | + raise credential_exception |
| 96 | + |
| 97 | + user = get_user(db, username=token_data.username) |
| 98 | + if user is None: |
| 99 | + raise credential_exception |
| 100 | + |
| 101 | + return user |
| 102 | + |
| 103 | +# Dependency to get current active user |
| 104 | +async def get_current_active_user(current_user: UserInDB = Depends(get_current_user)): |
| 105 | + if current_user.disabled: |
| 106 | + raise HTTPException(status_code=400, detail="Inactive user") |
| 107 | + return current_user |
| 108 | + |
| 109 | +# Route to get access token |
| 110 | +@app.post("/token", response_model=Token) |
| 111 | +async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()): |
| 112 | + user = authenticate_user(db, form_data.username, form_data.password) |
| 113 | + if not user: |
| 114 | + raise HTTPException( |
| 115 | + status_code=status.HTTP_401_UNAUTHORIZED, |
| 116 | + detail="Incorrect username or password", |
| 117 | + headers={"WWW-Authenticate": "Bearer"} |
| 118 | + ) |
| 119 | + access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) |
| 120 | + access_token = create_access_token( |
| 121 | + data={"sub": user.username}, |
| 122 | + expires_delta=access_token_expires |
| 123 | + ) |
| 124 | + return {"access_token": access_token, "token_type": "bearer"} |
| 125 | + |
| 126 | +# Route to get current user |
| 127 | +@app.get("/users/me/", response_model=User) |
| 128 | +async def read_users_me(current_user: User = Depends(get_current_active_user)): |
| 129 | + return current_user |
| 130 | + |
| 131 | +# Route to get current user's items |
| 132 | +@app.get("/users/me/items") |
| 133 | +async def read_own_items(current_user: User = Depends(get_current_active_user)): |
| 134 | + return [{"item_id": 1, "owner": current_user}] |
0 commit comments