Debian Codegraph @

docs_src/security/tutorial005_an_py310.py

raw
1 from datetime import datetime, timedelta, timezone
2 from typing import Annotated
3
4 import jwt
5 from fastapi import Depends, FastAPI, HTTPException, Security, status
6 from fastapi.security import (
7 OAuth2PasswordBearer,
8 OAuth2PasswordRequestForm,
9 SecurityScopes,
10 )
11 from jwt.exceptions import InvalidTokenError
12 from pwdlib import PasswordHash
13 from pydantic import BaseModel, ValidationError
14
15 # to get a string like this run:
16 # openssl rand -hex 32
17 SECRET_KEY = "09d25e094faa6ca2556c818166b7a9563b93f7099f6f0f4caa6cf63b88e8d3e7"
18 ALGORITHM = "HS256"
19 ACCESS_TOKEN_EXPIRE_MINUTES = 30
20
21
22 fake_users_db = {
23 "johndoe": {
24 "username": "johndoe",
25 "full_name": "John Doe",
26 "email": "[email protected]",
27 "hashed_password": "$argon2id$v=19$m=65536,t=3,p=4$wagCPXjifgvUFBzq4hqe3w$CYaIb8sB+wtD+Vu/P4uod1+Qof8h+1g7bbDlBID48Rc",
28 "disabled": False,
29 },
30 "alice": {
31 "username": "alice",
32 "full_name": "Alice Chains",
33 "email": "[email protected]",
34 "hashed_password": "$argon2id$v=19$m=65536,t=3,p=4$g2/AV1zwopqUntPKJavBFw$BwpRGDCyUHLvHICnwijyX8ROGoiUPwNKZ7915MeYfCE",
35 "disabled": True,
36 },
37 }
38
39
40 class Token(BaseModel):
41 access_token: str
42 token_type: str
43
44
45 class TokenData(BaseModel):
46 username: str | None = None
47 scopes: list[str] = []
48
49
50 class User(BaseModel):
51 username: str
52 email: str | None = None
53 full_name: str | None = None
54 disabled: bool | None = None
55
56
57 class UserInDB(User):
58 hashed_password: str
59
60
61 password_hash = PasswordHash.recommended()
62
63 DUMMY_HASH = password_hash.hash("dummypassword")
64
65 oauth2_scheme = OAuth2PasswordBearer(
66 tokenUrl="token",
67 scopes={"me": "Read information about the current user.", "items": "Read items."},
68 )
69
70 app = FastAPI()
71
72
73 def verify_password(plain_password, hashed_password):
74 return password_hash.verify(plain_password, hashed_password)
75
76
77 def get_password_hash(password):
78 return password_hash.hash(password)
79
80
81 def get_user(db, username: str):
82 if username in db:
83 user_dict = db[username]
84 return UserInDB(**user_dict)
85
86
87 def authenticate_user(fake_db, username: str, password: str):
88 user = get_user(fake_db, username)
89 if not user:
90 verify_password(password, DUMMY_HASH)
91 return False
92 if not verify_password(password, user.hashed_password):
93 return False
94 return user
95
96
97 def create_access_token(data: dict, expires_delta: timedelta | None = None):
98 to_encode = data.copy()
99 if expires_delta:
100 expire = datetime.now(timezone.utc) + expires_delta
101 else:
102 expire = datetime.now(timezone.utc) + timedelta(minutes=15)
103 to_encode.update({"exp": expire})
104 encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
105 return encoded_jwt
106
107
108 async def get_current_user(
109 security_scopes: SecurityScopes, token: Annotated[str, Depends(oauth2_scheme)]
110 ):
111 if security_scopes.scopes:
112 authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
113 else:
114 authenticate_value = "Bearer"
115 credentials_exception = HTTPException(
116 status_code=status.HTTP_401_UNAUTHORIZED,
117 detail="Could not validate credentials",
118 headers={"WWW-Authenticate": authenticate_value},
119 )
120 try:
121 payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
122 username = payload.get("sub")
123 if username is None:
124 raise credentials_exception
125 scope: str = payload.get("scope", "")
126 token_scopes = scope.split(" ")
127 token_data = TokenData(scopes=token_scopes, username=username)
128 except (InvalidTokenError, ValidationError):
129 raise credentials_exception
130 user = get_user(fake_users_db, username=token_data.username)
131 if user is None:
132 raise credentials_exception
133 for scope in security_scopes.scopes:
134 if scope not in token_data.scopes:
135 raise HTTPException(
136 status_code=status.HTTP_401_UNAUTHORIZED,
137 detail="Not enough permissions",
138 headers={"WWW-Authenticate": authenticate_value},
139 )
140 return user
141
142
143 async def get_current_active_user(
144 current_user: Annotated[User, Security(get_current_user, scopes=["me"])],
145 ):
146 if current_user.disabled:
147 raise HTTPException(status_code=400, detail="Inactive user")
148 return current_user
149
150
151 @app.post("/token")
152 async def login_for_access_token(
153 form_data: Annotated[OAuth2PasswordRequestForm, Depends()],
154 ) -> Token:
155 user = authenticate_user(fake_users_db, form_data.username, form_data.password)
156 if not user:
157 raise HTTPException(status_code=400, detail="Incorrect username or password")
158 access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
159 access_token = create_access_token(
160 data={"sub": user.username, "scope": " ".join(form_data.scopes)},
161 expires_delta=access_token_expires,
162 )
163 return Token(access_token=access_token, token_type="bearer")
164
165
166 @app.get("/users/me/")
167 async def read_users_me(
168 current_user: Annotated[User, Depends(get_current_active_user)],
169 ) -> User:
170 return current_user
171
172
173 @app.get("/users/me/items/")
174 async def read_own_items(
175 current_user: Annotated[User, Security(get_current_active_user, scopes=["items"])],
176 ):
177 return [{"item_id": "Foo", "owner": current_user.username}]
178
179
180 @app.get("/status/")
181 async def read_system_status(current_user: Annotated[User, Depends(get_current_user)]):
182 return {"status": "ok"}