pythonasyncioapiasyncfastapiframeworkjsonjson-schemaopenapiopenapi3pydanticpython-typespython3redocreststarletteswaggerswagger-uiuvicornweb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
711 B
30 lines
711 B
from fastapi import Depends, FastAPI
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
|
|
class User(BaseModel):
|
|
username: str
|
|
email: str | None = None
|
|
full_name: str | None = None
|
|
disabled: bool | None = None
|
|
|
|
|
|
def fake_decode_token(token):
|
|
return User(
|
|
username=token + "fakedecoded", email="[email protected]", full_name="John Doe"
|
|
)
|
|
|
|
|
|
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
|
user = fake_decode_token(token)
|
|
return user
|
|
|
|
|
|
@app.get("/users/me")
|
|
async def read_users_me(current_user: User = Depends(get_current_user)):
|
|
return current_user
|
|
|