โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น
ใช้งานรูปแบบ OAuth2PasswordBearer แฮชรหัสผ่านด้วย passlib และออกโทเค็นเข้าถึงที่ลงลายมือชื่อเมื่อเข้าสู่ระบบ
โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น เป็นบทเรียน FastAPI Backend Development Bootcamp ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน FastAPI Backend Development Bootcamp และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The OAuth2 Password Flow in Plain English
The OAuth2 password flow (a.k.a. the Resource Owner Password Credentials grant) is the simplest way to authenticate a first-party client: the user sends their username and password directly to your API, and the API hands back a signed access token.
- The client posts credentials once to a
/tokenendpoint. - The server verifies them against the database.
- On success it returns a short-lived JWT access token.
- Every later request carries that token in the
Authorization: Bearer <token>header.
FastAPI gives us ready-made building blocks for exactly this: OAuth2PasswordBearer and OAuth2PasswordRequestForm.
Declaring the OAuth2PasswordBearer Scheme
OAuth2PasswordBearer is a FastAPI dependency that knows how to pull a bearer token out of the Authorization header. You create one instance and point its tokenUrl at the login endpoint that issues tokens.
tokenUrlis a relative path — it tells the docs UI where clients should request a token.- Using the scheme as a dependency makes the endpoint require a token; a missing or malformed header returns 401 automatically.
from fastapi import Depends, FastAPI
from fastapi.security import OAuth2PasswordBearer
app = FastAPI()
# 'token' matches the path of our login route below
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
@app.get("/users/me")
async def read_me(token: str = Depends(oauth2_scheme)):
# FastAPI extracts the raw bearer token string for us
return {"token": token}Hashing Passwords with passlib
You must never store raw passwords. Hash them with a strong, salted algorithm. The passlib library wraps bcrypt behind a clean CryptContext API.
hash()produces a salted digest you store in the database.verify()compares a plaintext attempt against the stored hash in constant time.- bcrypt is deliberately slow, which frustrates brute-force attacks.
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def hash_password(plain: str) -> str:
return pwd_context.hash(plain)
def verify_password(plain: str, hashed: str) -> bool:
return pwd_context.verify(plain, hashed)
stored = hash_password("s3cret")
print("stored looks like:", stored[:7], "...")
print("correct ->", verify_password("s3cret", stored))
print("wrong ->", verify_password("nope", stored))Modeling Users and a Tiny Fake Database
Before issuing tokens we need somewhere to look users up. In production this is your real database; for learning we use an in-memory dict. Notice the stored field is hashed_password, never the plaintext.
- A Pydantic model gives the user a typed shape.
- A
get_user()helper centralizes lookups.
from pydantic import BaseModel
class UserInDB(BaseModel):
username: str
hashed_password: str
disabled: bool = False
fake_users_db = {
"alice": UserInDB(
username="alice",
hashed_password="$2b$12$exampleexampleexamplehashvalue",
)
}
def get_user(username: str):
return fake_users_db.get(username)Authenticating the Credentials
Authentication ties the pieces together: find the user, then verify the supplied password against the stored hash. Return the user on success, or a falsy value on failure.
- Look the user up first; if absent, fail.
- Then call
verify_password— do not short-circuit before hashing to keep timing roughly uniform. - The caller decides how to respond (usually a 401).
def authenticate_user(db, username: str, password: str):
user = db.get(username)
if not user:
return None
if not verify_password(password, user.hashed_password):
return None
return userWhat a JWT Actually Is
A JSON Web Token is three base64url segments joined by dots: header.payload.signature.
- The header names the algorithm, e.g.
HS256. - The payload holds claims like
sub(subject) andexp(expiry). - The signature is an HMAC of header+payload using your secret key.
JWTs are signed, not encrypted — anyone can read the payload, but nobody can forge it without the secret. Never put passwords or sensitive data in the payload.
Encoding a Signed Access Token
We sign tokens with the python-jose library (or PyJWT). Always include an exp claim so tokens expire. Store the username in the sub claim — it identifies who the token belongs to.
SECRET_KEYmust be long, random, and kept out of source control.- Set a short lifetime (e.g. 15-30 minutes) for access tokens.
from datetime import datetime, timedelta, timezone
from jose import jwt
SECRET_KEY = "replace-with-a-long-random-secret"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
def create_access_token(data: dict) -> str:
to_encode = data.copy()
expire = datetime.now(timezone.utc) + timedelta(
minutes=ACCESS_TOKEN_EXPIRE_MINUTES
)
to_encode.update({"exp": expire})
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
token = create_access_token({"sub": "alice"})
print("issued token segments:", token.count(".") + 1)The Token Response Shape
The OAuth2 spec dictates the JSON your /token endpoint returns. At minimum it must include access_token and token_type, where the type is the literal string "bearer".
- Clients read
token_typeto know how to send the credential back. - A Pydantic
Tokenmodel documents and validates the response.
from pydantic import BaseModel
class Token(BaseModel):
access_token: str
token_type: str
example = Token(access_token="eyJhbGci...", token_type="bearer")
print(example.model_dump())Wiring the /token Login Endpoint
The login route depends on OAuth2PasswordRequestForm, which reads form-encoded username and password fields (not JSON) — exactly what the OAuth2 password flow requires. On success it returns the Token response.
- Failed auth raises 401 with a
WWW-Authenticate: Bearerheader. - The
subclaim carries the username forward into the token.
from fastapi import Depends, FastAPI, HTTPException, status
from fastapi.security import OAuth2PasswordRequestForm
app = FastAPI()
@app.post("/token", response_model=Token)
async def login(form: OAuth2PasswordRequestForm = Depends()):
user = authenticate_user(fake_users_db, form.username, form.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token = create_access_token({"sub": user.username})
return Token(access_token=access_token, token_type="bearer")Decoding the Token to Find the Current User
A protected route depends on oauth2_scheme to receive the raw token, then decodes it. If the signature is invalid or the token is expired, jwt.decode raises JWTError and we return 401.
- Read the username from the
subclaim. - Re-load the user from the database to confirm they still exist and are active.
from fastapi import Depends, HTTPException, status
from jose import JWTError, jwt
async def get_current_user(token: str = Depends(oauth2_scheme)):
credentials_exc = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
username = payload.get("sub")
if username is None:
raise credentials_exc
except JWTError:
raise credentials_exc
user = get_user(username)
if user is None:
raise credentials_exc
return userSecurity Practices That Matter
The mechanics work, but production hardening makes them safe:
- Secret key: load
SECRET_KEYfrom an environment variable; rotate it if leaked. - HTTPS only: tokens in headers are plaintext on the wire — TLS is mandatory.
- Short expiry: keep access tokens brief and pair them with longer-lived refresh tokens.
- Pin the algorithm: pass an explicit
algorithms=["HS256"]list tojwt.decodeto block thealg: noneattack. - Generic errors: say "Incorrect username or password", never reveal which one was wrong.
Quick Check: The /token Endpoint
Time to test your understanding of how the FastAPI /token login route consumes credentials.
Recap: From Password to Bearer Token
You implemented the full OAuth2 password flow in FastAPI:
- OAuth2PasswordBearer declares the bearer scheme and extracts tokens from the
Authorizationheader. - passlib + bcrypt hash and verify passwords so plaintext is never stored.
- authenticate_user looks up the user and verifies the hash, returning 401 on failure.
- The /token route reads form credentials via
OAuth2PasswordRequestFormand issues a signed JWT with asubclaim and anexpexpiry. - get_current_user decodes and validates the token, pinning the algorithm to block forgery.
With HTTPS, an env-loaded secret, and short token lifetimes, this is a solid, idiomatic authentication foundation.
เรียนรู้ FastAPI Backend Development Bootcamp ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 21
- บทเรียน
- 84
คำถามที่พบบ่อย
บทเรียน “โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส FastAPI Backend Development Bootcamp ให้อัปเกรดเป็น CoddyKit PRO คอร์ส FastAPI Backend Development Bootcamp มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น”
ใช้งานรูปแบบ OAuth2PasswordBearer แฮชรหัสผ่านด้วย passlib และออกโทเค็นเข้าถึงที่ลงลายมือชื่อเมื่อเข้าสู่ระบบ คุณปฏิบัติ FastAPI Backend Development Bootcamp ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน FastAPI Backend Development Bootcamp หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน FastAPI Backend Development Bootcamp บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน FastAPI Backend Development Bootcamp นี้ได้ไหม
ได้ บทเรียน FastAPI Backend Development Bootcamp ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- โฟลว์รหัสผ่าน OAuth2 และการออกโทเค็น
- การลงลายมือชื่อและตรวจสอบ JWT ด้วย python-jose
- โทเค็นรีเฟรชและการหมุนเวียนโทเค็น
- การอนุญาตตามขอบเขตและตัวป้องกันบทบาท