MCPcopy Index your code
hub / github.com/fastapi/fastapi / get_current_username

Function get_current_username

docs_src/security/tutorial007_py310.py:11–28  ·  view source on GitHub ↗
(credentials: HTTPBasicCredentials = Depends(security))

Source from the content-addressed store, hash-verified

9
10
11def get_current_username(credentials: HTTPBasicCredentials = Depends(security)):
12 current_username_bytes = credentials.username.encode("utf8")
13 correct_username_bytes = b"stanleyjobson"
14 is_correct_username = secrets.compare_digest(
15 current_username_bytes, correct_username_bytes
16 )
17 current_password_bytes = credentials.password.encode("utf8")
18 correct_password_bytes = b"swordfish"
19 is_correct_password = secrets.compare_digest(
20 current_password_bytes, correct_password_bytes
21 )
22 if not (is_correct_username and is_correct_password):
23 raise HTTPException(
24 status_code=status.HTTP_401_UNAUTHORIZED,
25 detail="Incorrect username or password",
26 headers={"WWW-Authenticate": "Basic"},
27 )
28 return credentials.username
29
30
31@app.get("/users/me")

Callers

nothing calls this directly

Calls 2

DependsClass · 0.90
HTTPExceptionClass · 0.90

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…