๐ค โฎ๏ธ ๐ฉโ๐ป¶
โฎ๏ธ ๐ ๐โโ โ๏ธ (โ ๐งข ๐ ๐ ๐ โ๏ธ) ๐ค โก ๐ ๏ธ ๐ข token
str
:
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
app = FastAPI()
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/items/")
async def read_items(token: str = Depends(oauth2_scheme)):
return {"token": token}
โ๏ธ ๐ ๐ซ ๐ โ .
โก๏ธ โ โซ๏ธ ๐ค ๐ฅ โฎ๏ธ ๐ฉโ๐ป.
โ ๐ฉโ๐ป ๐ท¶
๐ฅ, โก๏ธ โ Pydantic ๐ฉโ๐ป ๐ท.
๐ ๐ ๐ฅ โ๏ธ Pydantic ๐ฃ ๐ช, ๐ฅ ๐ช โ๏ธ โซ๏ธ ๐ ๐:
from typing import Union
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: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
def fake_decode_token(token):
return User(
username=token + "fakedecoded", email="john@example.com", 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
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="john@example.com", 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
โ get_current_user
๐¶
โก๏ธ โ ๐ get_current_user
.
๐ญ ๐ ๐ ๐ช โ๏ธ ๐ง-๐ โ
get_current_user
๐ โ๏ธ ๐ โฎ๏ธ ๐ oauth2_scheme
๐ฅ โ โญ.
๐ ๐ฅ ๐จ โญ โก ๐ ๏ธ ๐, ๐ ๐ ๐ get_current_user
๐ ๐จ token
str
โช๏ธโก๏ธ ๐ง-๐ oauth2_scheme
:
from typing import Union
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: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
def fake_decode_token(token):
return User(
username=token + "fakedecoded", email="john@example.com", 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
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="john@example.com", 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
๐ค ๐ฉโ๐ป¶
get_current_user
๐ โ๏ธ (โ) ๐ ๐ข ๐ฅ โ, ๐ โ ๐ค str
& ๐จ ๐ Pydantic User
๐ท:
from typing import Union
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: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
def fake_decode_token(token):
return User(
username=token + "fakedecoded", email="john@example.com", 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
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="john@example.com", 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
๐ โฎ๏ธ ๐ฉโ๐ป¶
๐ ๐ฅ ๐ช โ๏ธ ๐ Depends
โฎ๏ธ ๐ get_current_user
โก ๐ ๏ธ:
from typing import Union
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: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
def fake_decode_token(token):
return User(
username=token + "fakedecoded", email="john@example.com", 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
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="john@example.com", 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
๐ ๐ ๐ฅ ๐ฃ ๐ current_user
Pydantic ๐ท User
.
๐ ๐ โน ๐บ๐ฒ ๐ ๐ข โฎ๏ธ ๐ ๐ ๏ธ & ๐ โ .
Tip
๐ 5๏ธโฃ๐ ๐ญ ๐ ๐จ ๐ช ๐ฃ โฎ๏ธ Pydantic ๐ท.
๐ฅ FastAPI ๐ ๐ซ ๐ค ๐จ โฉ๏ธ ๐ โ๏ธ Depends
.
Check
๐ ๐ ๐ โ๏ธ ๐ โ ๐ฅ โ๏ธ ๐ ๐ (๐ "โ") ๐ ๐ ๐จ User
๐ท.
๐ฅ ๐ซ ๐ซ โ๏ธ ๐ด 1๏ธโฃ ๐ ๐ ๐ช ๐จ ๐ ๐ ๐ฝ.
๐ ๐ท¶
๐ ๐ช ๐ ๐ค โฎ๏ธ ๐ฉโ๐ป ๐ โก ๐ ๏ธ ๐ข & ๐
โฎ๏ธ ๐โโ ๐ ๏ธ ๐ ๐ ๐, โ๏ธ Depends
.
& ๐ ๐ช โ๏ธ ๐ ๐ท โ๏ธ ๐ฝ ๐โโ ๐ (๐ ๐ผ, Pydantic ๐ท User
).
โ๏ธ ๐ ๐ซ ๐ซ โ๏ธ ๐ฏ ๐ฝ ๐ท, ๐ โ๏ธ ๐.
๐ ๐ โ๏ธ id
& email
& ๐ซ โ๏ธ ๐ username
๐ ๐ท โ ๐ญ. ๐ ๐ช โ๏ธ ๐ ๐ ๐งฐ.
๐ ๐ โ๏ธ str
โ โ๏ธ dict
โ โ๏ธ ๐ฝ ๐ ๐ท ๐ ๐ โ โซ๏ธ ๐ ๐ท ๐ ๐.
๐ ๐ค ๐ซ โ๏ธ ๐ฉโ๐ป ๐ ๐น ๐ ๐ธ โ๏ธ ๐ค, ๐ค, โ๏ธ ๐ โ๏ธ, ๐ โ๏ธ ๐ ๐ค โ ๐, โซ๏ธ ๐ ๐ท ๐.
โ๏ธ ๐ ๐ ๐ท, ๐ ๐ ๐, ๐ ๐ ๐ฝ ๐ ๐ ๐ช ๐ ๐ธ. FastAPI โ๏ธ ๐ ๐ โฎ๏ธ ๐ ๐ โ๏ธ.
๐ ๐¶
๐ ๐ผ 5๏ธโฃ๐ ๐ ๐. โ๏ธ ๐คฏ ๐ ๐ฅ ๐ ๐โโ, ๐ ๐ท, ๐ ๐ข & โก ๐ ๏ธ ๐ ๐.
โ๏ธ ๐ฅ ๐ โ.
๐โโ & ๐ ๐ ๐ฉ โ ๐.
& ๐ ๐ช โ โซ๏ธ ๐ ๐ ๐. & , โ๏ธ โซ๏ธ โ ๐ด ๐, ๐ ๐ฅ. โฎ๏ธ ๐ ๐ช.
โ๏ธ ๐ ๐ช โ๏ธ ๐ฏ ๐ (โก ๐ ๏ธ) โ๏ธ ๐ ๐โโ โ๏ธ.
& ๐ ๐ซ (โ๏ธ ๐ โ ๐ซ ๐ ๐ ๐) ๐ช โ ๐ ๐ค-โ๏ธ ๐ซ ๐ โ๏ธ ๐ ๐ ๐ ๐ โ.
& ๐ ๐ ๐ฏ โก ๐ ๏ธ ๐ช ๐คช 3๏ธโฃ โธ:
from typing import Union
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: Union[str, None] = None
full_name: Union[str, None] = None
disabled: Union[bool, None] = None
def fake_decode_token(token):
return User(
username=token + "fakedecoded", email="john@example.com", 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
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="john@example.com", 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
๐¶
๐ ๐ช ๐ ๐ค โฎ๏ธ ๐ฉโ๐ป ๐ ๐ โก ๐ ๏ธ ๐ข.
๐ฅ โช ๐ฌ ๐ค.
๐ฅ ๐ช ๐ฎ โก ๐ ๏ธ ๐ฉโ๐ป/๐ฉโ๐ป ๐ค ๐จ username
& password
.
๐ ๐ โญ.