docs_src/security/tutorial003_py310.py PYTHON 89 lines View on github.com → Search inside
1from fastapi import Depends, FastAPI, HTTPException, status2from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm3from pydantic import BaseModel45fake_users_db = {6    "johndoe": {7        "username": "johndoe",8        "full_name": "John Doe",9        "email": "johndoe@example.com",10        "hashed_password": "fakehashedsecret",11        "disabled": False,12    },13    "alice": {14        "username": "alice",15        "full_name": "Alice Wonderson",16        "email": "alice@example.com",17        "hashed_password": "fakehashedsecret2",18        "disabled": True,19    },20}2122app = FastAPI()232425def fake_hash_password(password: str):26    return "fakehashed" + password272829oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")303132class User(BaseModel):33    username: str34    email: str | None = None35    full_name: str | None = None36    disabled: bool | None = None373839class UserInDB(User):40    hashed_password: str414243def get_user(db, username: str):44    if username in db:45        user_dict = db[username]46        return UserInDB(**user_dict)474849def fake_decode_token(token):50    # This doesn't provide any security at all51    # Check the next version52    user = get_user(fake_users_db, token)53    return user545556async def get_current_user(token: str = Depends(oauth2_scheme)):57    user = fake_decode_token(token)58    if not user:59        raise HTTPException(60            status_code=status.HTTP_401_UNAUTHORIZED,61            detail="Not authenticated",62            headers={"WWW-Authenticate": "Bearer"},63        )64    return user656667async def get_current_active_user(current_user: User = Depends(get_current_user)):68    if current_user.disabled:69        raise HTTPException(status_code=400, detail="Inactive user")70    return current_user717273@app.post("/token")74async def login(form_data: OAuth2PasswordRequestForm = Depends()):75    user_dict = fake_users_db.get(form_data.username)76    if not user_dict:77        raise HTTPException(status_code=400, detail="Incorrect username or password")78    user = UserInDB(**user_dict)79    hashed_password = fake_hash_password(form_data.password)80    if not hashed_password == user.hashed_password:81        raise HTTPException(status_code=400, detail="Incorrect username or password")8283    return {"access_token": user.username, "token_type": "bearer"}848586@app.get("/users/me")87async def read_users_me(current_user: User = Depends(get_current_active_user)):88    return current_user

Code quality findings 7

Ensure functions have docstrings for documentation
info maintainability missing-docstring
def fake_hash_password(password: str):
Ensure functions have docstrings for documentation
info maintainability missing-docstring
def get_user(db, username: str):
Ensure functions have docstrings for documentation
info maintainability missing-docstring
def fake_decode_token(token):
Ensure functions have docstrings for documentation
info maintainability missing-docstring
async def get_current_user(token: str = Depends(oauth2_scheme)):
Ensure functions have docstrings for documentation
info maintainability missing-docstring
async def get_current_active_user(current_user: User = Depends(get_current_user)):
Ensure functions have docstrings for documentation
info maintainability missing-docstring
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
Ensure functions have docstrings for documentation
info maintainability missing-docstring
async def read_users_me(current_user: User = Depends(get_current_active_user)):

Security findings 1

Detected a potential hardcoded password literal, which may expose account credentials.
warning security generic-password medium confidence rule grades this low

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.

Generated by searchcode.com · powered by code_get_file + code_get_findings