Coverage for docs_src/security/tutorial003.py: 100%
45 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-12-04 08:29 +0000
1from typing import Union 1abcdefg
3from fastapi import Depends, FastAPI, HTTPException, status 1abcdefg
4from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm 1abcdefg
5from pydantic import BaseModel 1abcdefg
7fake_users_db = { 1abcdefg
8 "johndoe": {
9 "username": "johndoe",
10 "full_name": "John Doe",
11 "email": "johndoe@example.com",
12 "hashed_password": "fakehashedsecret",
13 "disabled": False,
14 },
15 "alice": {
16 "username": "alice",
17 "full_name": "Alice Wonderson",
18 "email": "alice@example.com",
19 "hashed_password": "fakehashedsecret2",
20 "disabled": True,
21 },
22}
24app = FastAPI() 1abcdefg
27def fake_hash_password(password: str): 1abcdefg
28 return "fakehashed" + password 1vwxyzABCDEFGHI
31oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token") 1abcdefg
34class User(BaseModel): 1abcdefg
35 username: str 1abcdefg
36 email: Union[str, None] = None 1abcdefg
37 full_name: Union[str, None] = None 1abcdefg
38 disabled: Union[bool, None] = None 1abcdefg
41class UserInDB(User): 1abcdefg
42 hashed_password: str 1abcdefg
45def get_user(db, username: str): 1abcdefg
46 if username in db: 1oJhpKiqLjrMksNltOmuPn
47 user_dict = db[username] 1ohpiqjrksltmun
48 return UserInDB(**user_dict) 1ohpiqjrksltmun
51def fake_decode_token(token): 1abcdefg
52 # This doesn't provide any security at all
53 # Check the next version
54 user = get_user(fake_users_db, token) 1oJhpKiqLjrMksNltOmuPn
55 return user 1oJhpKiqLjrMksNltOmuPn
58async def get_current_user(token: str = Depends(oauth2_scheme)): 1abcdefg
59 user = fake_decode_token(token) 1oJhpKiqLjrMksNltOmuPn
60 if not user: 1oJhpKiqLjrMksNltOmuPn
61 raise HTTPException( 1JKLMNOP
62 status_code=status.HTTP_401_UNAUTHORIZED,
63 detail="Not authenticated",
64 headers={"WWW-Authenticate": "Bearer"},
65 )
66 return user 1ohpiqjrksltmun
69async def get_current_active_user(current_user: User = Depends(get_current_user)): 1abcdefg
70 if current_user.disabled: 1ohpiqjrksltmun
71 raise HTTPException(status_code=400, detail="Inactive user") 1opqrstu
72 return current_user 1hijklmn
75@app.post("/token") 1abcdefg
76async def login(form_data: OAuth2PasswordRequestForm = Depends()): 1abcdefg
77 user_dict = fake_users_db.get(form_data.username) 1vwQxyRzASBCTDEUFGVHIW
78 if not user_dict: 1vwQxyRzASBCTDEUFGVHIW
79 raise HTTPException(status_code=400, detail="Incorrect username or password") 1QRSTUVW
80 user = UserInDB(**user_dict) 1vwxyzABCDEFGHI
81 hashed_password = fake_hash_password(form_data.password) 1vwxyzABCDEFGHI
82 if not hashed_password == user.hashed_password: 1vwxyzABCDEFGHI
83 raise HTTPException(status_code=400, detail="Incorrect username or password") 1wyACEGI
85 return {"access_token": user.username, "token_type": "bearer"} 1vxzBDFH
88@app.get("/users/me") 1abcdefg
89async def read_users_me(current_user: User = Depends(get_current_active_user)): 1abcdefg
90 return current_user 1hijklmn