| 1 |
from typing import Annotated |
| 2 |
|
| 3 |
from fastapi import Depends, FastAPI, HTTPException, status |
| 4 |
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm |
| 5 |
from pydantic import BaseModel |
| 6 |
|
| 7 |
fake_users_db = { |
| 8 |
"johndoe": { |
| 9 |
"username": "johndoe", |
| 10 |
"full_name": "John Doe", |
| 11 |
"email": "[email protected]", |
| 12 |
"hashed_password": "fakehashedsecret", |
| 13 |
"disabled": False, |
| 14 |
}, |
| 15 |
"alice": { |
| 16 |
"username": "alice", |
| 17 |
"full_name": "Alice Wonderson", |
| 18 |
"email": "[email protected]", |
| 19 |
"hashed_password": "fakehashedsecret2", |
| 20 |
"disabled": True, |
| 21 |
}, |
| 22 |
} |
| 23 |
|
| 24 |
app = FastAPI() |
| 25 |
|
| 26 |
|
| 27 |
def fake_hash_password(password: str): |
| 28 |
return "fakehashed" + password |
| 29 |
|
| 30 |
|
| 31 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") |
| 32 |
|
| 33 |
|
| 34 |
class User(BaseModel): |
| 35 |
username: str |
| 36 |
email: str | None = None |
| 37 |
full_name: str | None = None |
| 38 |
disabled: bool | None = None |
| 39 |
|
| 40 |
|
| 41 |
class UserInDB(User): |
| 42 |
hashed_password: str |
| 43 |
|
| 44 |
|
| 45 |
def get_user(db, username: str): |
| 46 |
if username in db: |
| 47 |
user_dict = db[username] |
| 48 |
return UserInDB(**user_dict) |
| 49 |
|
| 50 |
|
| 51 |
def fake_decode_token(token): |
| 52 |
# This doesn't provide any security at all |
| 53 |
# Check the next version |
| 54 |
user = get_user(fake_users_db, token) |
| 55 |
return user |
| 56 |
|
| 57 |
|
| 58 |
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]): |
| 59 |
user = fake_decode_token(token) |
| 60 |
if not user: |
| 61 |
raise HTTPException( |
| 62 |
status_code=status.HTTP_401_UNAUTHORIZED, |
| 63 |
detail="Not authenticated", |
| 64 |
headers={"WWW-Authenticate": "Bearer"}, |
| 65 |
) |
| 66 |
return user |
| 67 |
|
| 68 |
|
| 69 |
async def get_current_active_user( |
| 70 |
current_user: Annotated[User, Depends(get_current_user)], |
| 71 |
): |
| 72 |
if current_user.disabled: |
| 73 |
raise HTTPException(status_code=400, detail="Inactive user") |
| 74 |
return current_user |
| 75 |
|
| 76 |
|
| 77 |
@app.post("/token") |
| 78 |
async def login(form_data: Annotated[OAuth2PasswordRequestForm, Depends()]): |
| 79 |
user_dict = fake_users_db.get(form_data.username) |
| 80 |
if not user_dict: |
| 81 |
raise HTTPException(status_code=400, detail="Incorrect username or password") |
| 82 |
user = UserInDB(**user_dict) |
| 83 |
hashed_password = fake_hash_password(form_data.password) |
| 84 |
if not hashed_password == user.hashed_password: |
| 85 |
raise HTTPException(status_code=400, detail="Incorrect username or password") |
| 86 |
|
| 87 |
return {"access_token": user.username, "token_type": "bearer"} |
| 88 |
|
| 89 |
|
| 90 |
@app.get("/users/me") |
| 91 |
async def read_users_me( |
| 92 |
current_user: Annotated[User, Depends(get_current_active_user)], |
| 93 |
): |
| 94 |
return current_user |