Free Python developer course
Go from your first line of code to working as a Python developer: projects, testing, data, databases, web apps and APIs, all in your browser.
A session per request, bcrypt sign-ups, signed tokens, ownership checks and tests on a fresh database.
Where should a trip service confirm ownership before an update?
A signed token identifies the caller and each request works with its own database session. The update must apply only to a trip that the caller owns.
Account A requests an update to trip T
Look up T using its ID and A's ownership
Commit a matching change or return the documented denial
Account A requests an update to trip T
Look up T using its ID and A's ownership
Commit a matching change or return the documented denial
Given Account A requests an update to trip T and Commit a matching change or return the documented denial, write down the missing step that connects them.
Look up T using its ID and A's ownership
Now take Look up T using its ID and A's ownership and the result above. Write an input and an implementation that could produce this trace.
Account A requests an update to trip T. Check that your implementation produces the result shown above.
How do you test an update using a valid token from the wrong account?
Create the trip under A, attempt the update as B, check the denial, then read the trip as A and verify its original data.
Check this when debugging. Checking that a user is logged in and assuming that means they may see the record.
Most backend jobs are some version of this project: an API with a database behind it, accounts in front of it, and rules about who may see what. Each piece appeared on its own in earlier lessons (FastAPI routes, SQLAlchemy models, password hashing, signed tokens). This project wires them into one service for the trail club's trip planner, the way a real codebase does, and builds the habits that separate a demo from something you'd deploy: no secrets in the code, no passwords in responses, no one reading another member's trips, and tests that start from a clean database every time.
In each query, filtered by the current user unless they're an admin
The rule has to run on the server, in every query that touches trips, with an exception for admins. Docs and client apps can't enforce anything, because a request can come from anywhere.
This rests on Authentication and authorization, which built a signed token from scratch, and APIs with FastAPI, which introduced dependencies.
Members sign up, log in, and keep a list of planned trips that only they can see; a club admin can see everyone's.
| method and path | who may call it | what it does |
|---|---|---|
POST /signup | anyone | creates a member; 409 if the email is taken |
POST /token | anyone | checks email and password, returns a signed token; 401 if wrong |
GET /me | a logged-in member | who the token belongs to |
POST /trips, GET /trips | a logged-in member | add a trip, list your trips (an admin sees all) |
GET /trips/{id}, DELETE /trips/{id} | the trip's owner, or an admin | one trip; 404 for anyone else |
In a real repository each layer gets its own file: models.py (database tables), schemas.py (the shapes that come in and go out), security.py (passwords and tokens), deps.py (the dependencies below) and main.py (the routes). Here each cell builds on the one before, so run them in order.
Every protected route runs this chain before its own code. Three gates answer 401 and the last answers 404.
Write a sentence or two, then compare it with a good answer in the lesson.
Each cell builds on the one before, so run them in order. First, the database session that every route asks for.
Every request should get its own database session, and that session must be closed when the request ends, even if the route raises an error. A FastAPI dependency written with yield does exactly this: the code before yield runs before the route, the route gets the yielded value, and the finally part runs afterward, always.
import asyncio import httpx from fastapi import Depends, FastAPI, HTTPException from pydantic import BaseModel from sqlalchemy import ForeignKey, String, create_engine, func, select from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, sessionmaker from sqlalchemy.pool import StaticPool class Base(DeclarativeBase): pass class Member(Base): __tablename__ = "members" id: Mapped[int] = mapped_column(primary_key=True) email: Mapped[str] = mapped_column(String, unique=True) password_hash: Mapped[bytes] role: Mapped[str] = mapped_column(default="member") class Trip(Base): __tablename__ = "trips" id: Mapped[int] = mapped_column(primary_key=True) name: Mapped[str] day: Mapped[str] owner_id: Mapped[int] = mapped_column(ForeignKey("members.id")) def make_sessions(url="sqlite://"): # StaticPool keeps one connection, so an in-memory database # lives as long as the engine engine = create_engine( url, connect_args={"check_same_thread": False}, poolclass=StaticPool ) Base.metadata.create_all(engine) return sessionmaker(bind=engine) SessionLocal = make_sessions() def get_db(): db = SessionLocal() try: yield db finally: db.close() class MemberOut(BaseModel): id: int email: str role: str app = FastAPI() @app.get("/members/count") def count_members(db: Session = Depends(get_db)): return {"members": db.scalar(select(func.count()).select_from(Member))} async def call(method, url, **kw): async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://test" ) as client: return await client.request(method, url, **kw) print(asyncio.run(call("GET", "/members/count")).json())
A few details matter. sessionmaker(bind=engine) is a factory: each call makes a fresh Session, just like the ORM lesson's Session(engine), so get_db can make one per request. check_same_thread=False lets a SQLite connection be used from another thread, which real FastAPI needs because it runs plain def routes in a pool of threads. StaticPool keeps a single connection open, because an in-memory SQLite database ("sqlite://") exists only while its connection does; on a real server the URL would point at PostgreSQL and the default pool would do. The count route asks the database to count (func.count()) instead of loading every row. And MemberOut is the shape the API will return for a member: it has no password_hash, so even a route that returns the database row can't leak the hash, once it declares response_model=MemberOut.
def get_db(): print("open") try: yield "db" finally: print("close") steps = get_db() db = next(steps) print("route uses", db) next(steps, None)
open route uses db close
next() runs the function up to yield, printing open and handing over the session. The route uses it. The second next() resumes after yield, so the finally block prints close; the function then ends, and next's default None stops it raising StopIteration.
FastAPI does the same with get_db: it runs the dependency up to yield, passes the session to the route, and resumes the function once the route is done, so the finally block closes the session. If the route raises an error, FastAPI throws that error into the dependency at the yield, and finally still runs. That's why the try/finally is there: without it, every failed request would leave a session open, and on a busy server those open sessions pile up until the connection pool runs dry and every new request waits.
Every column FastAPI can serialize, including password_hash, unless a response model filters it
FastAPI converts what you return. Declaring response_model=MemberOut makes the output shape explicit, so fields like password_hash never leave the server, whatever the route returns.
row = {
"id": 7,
"email": "ana@example.com",
"password_hash": "$2b$12$abc",
"is_admin": False,
}
MEMBER_OUT = ["id", "email"]
print({field: row[field] for field in MEMBER_OUT}){'id': 7, 'email': 'ana@example.com'}
The row holds four columns, and only the two that are named go out. The password hash stays on the server though the route returned the whole row. A response model is a list of what may leave, so a column added to the table later doesn't leak by accident.
Signing up stores a bcrypt hash of the password, never the password itself, using a cost (the number of rounds) that makes each guess slow. Logging in checks the password with bcrypt.checkpw and hands back a token: the signed HS256 kind from the auth lesson, carrying the member's id (sub), role and an expiry about 30 minutes away.
The signing secret comes from the environment, not the source code. Code is shared, copied and committed; a secret written in it is a secret every reader of the repository has. The cell below falls back to a practice value only so it runs here.
import base64, hashlib, hmac, json, os, time import bcrypt SECRET = os.environ.get("TOKEN_SECRET", "practice-only-secret").encode() ROUNDS = 4 # fast for the browser; real services use 12 or more def hash_password(password): return bcrypt.hashpw(password.encode(), bcrypt.gensalt(rounds=ROUNDS)) def check_password(password, hashed): return bcrypt.checkpw(password.encode(), hashed) def b64(data): return base64.urlsafe_b64encode(data).rstrip(b"=").decode() def make_token(member_id, role, minutes=30, now=None): now = time.time() if now is None else now head = b64(json.dumps({"alg": "HS256", "typ": "JWT"}).encode()) body = b64( json.dumps( {"sub": str(member_id), "role": role, "exp": int(now + minutes * 60)} ).encode() ) sig = b64(hmac.new(SECRET, f"{head}.{body}".encode(), hashlib.sha256).digest()) return f"{head}.{body}.{sig}" def read_token(token, now=None): """The token's claims, or None if it's malformed, forged or expired.""" now = time.time() if now is None else now try: head, body, sig = token.split(".") good = b64(hmac.new(SECRET, f"{head}.{body}".encode(), hashlib.sha256).digest()) if not hmac.compare_digest(sig.encode(), good.encode()): return None claims = json.loads(base64.urlsafe_b64decode(body + "=" * (-len(body) % 4))) except (ValueError, json.JSONDecodeError): return None return claims if claims.get("exp", 0) > now else None h = hash_password("pine-gap-ridge") print(check_password("pine-gap-ridge", h), check_password("pine-gap-rigde", h)) t = make_token(7, "member") print( read_token(t)["sub"], read_token(t[:-2] + "xx"), read_token(make_token(7, "member", minutes=-1)), )
The last line shows the three outcomes that matter: a good token gives its claims; a tampered signature and an expired token both give None. A route never has to know which went wrong: either way the answer is 401.
Compared with the auth lesson's checklist, two checks are left out on purpose. This service never reads alg from the token: it always verifies with HS256, so a forged header can't switch the algorithm. And one service issues and reads its own tokens, so there's no audience to check; add aud when several services share a secret, and use a maintained library such as PyJWT in production. One more habit: a failed login gets the same 401 whether the email is unknown or the password is wrong, so the login form can't be used to test who's a member. That isn't airtight (signup's 409 says an email is taken, and skipping bcrypt for an unknown email answers faster), which is why real services also limit how often these routes can be called.
So the login form can't be used to find out who is a member
Different answers would let anyone test an email address against the form. One answer for both hides that, though not perfectly: signup's 409 and timing still leak a little, which is why real services also limit how often these routes can be called.
A forged header switching the algorithm the check uses
If the verifier believed the token's own header, an attacker could name a weaker algorithm, or none. Fixing HS256 in the code means the token can't choose how it is checked.
Write can(role, action, owner_id, user_id): members may "read", "update" and "delete" only their own records (owner_id == user_id); admins may "read" any record and "delete" any, but "update" only their own. Unknown roles or actions get False.
def can(role, action, owner_id, user_id): return role == "admin" or owner_id == user_id print(can("admin", "update", 7, 1), can("member", "read", 7, 7))
get_current_user is the dependency every protected route lists. It turns a token into a member, or stops the request with 401 and the header WWW-Authenticate: Bearer, which tells clients which kind of credential to send. OAuth2PasswordBearer(tokenUrl="token") reads the token from the Authorization header, and records in the API's generated docs that these routes need one. One catch on a real server: the docs page's Authorize button sends username and password as form fields to tokenUrl, and this project's /token reads JSON, so the button gets a 422 and logs no one in. Test with your own requests, or have /token accept OAuth2PasswordRequestForm, FastAPI's form version, if you want the button to work.
401, with WWW-Authenticate: Bearer
No token means no member, so the dependency stops the request with 401, and the header tells the client to send a bearer token. 404 is the answer for someone else's trip, which comes next.
Authorization is the next step, and the most common serious bug in real APIs is skipping it: a route that loads /trips/42 without checking that trip 42 belongs to the caller. Anyone logged in can then read anyone's data by counting through ids, a hole called an insecure direct object reference (IDOR). It's the same hole Authentication and authorization calls broken object-level authorization (BOLA). The fix is to filter by owner in the query itself, and to answer 404 rather than 403 for someone else's trip, so the API doesn't even confirm that the trip exists.
from fastapi.security import OAuth2PasswordBearer oauth2 = OAuth2PasswordBearer(tokenUrl="token") def get_current_user( token: str = Depends(oauth2), db: Session = Depends(get_db) ) -> Member: claims = read_token(token) member = db.get(Member, int(claims["sub"])) if claims else None if member is None: raise HTTPException( 401, "Not logged in", headers={"WWW-Authenticate": "Bearer"} ) return member @app.get("/trips/{trip_id}") def get_trip( trip_id: int, me: Member = Depends(get_current_user), db: Session = Depends(get_db) ): trip = db.scalar(select(Trip).where(Trip.id == trip_id, Trip.owner_id == me.id)) if trip is None: raise HTTPException(404, "Trip not found") return trip
Notice that owner_id == me.id sits inside the query: it isn't an afterthought checked once the trip is loaded, so a later edit can't easily drop it.
TRIPS = {17: {"owner": "bo"}, 18: {"owner": "ana"}}
def get_trip(trip_id, user, is_admin=False):
trip = TRIPS.get(trip_id)
if trip is None or (trip["owner"] != user and not is_admin):
return 404
return 200
print(
get_trip(17, "ana"),
get_trip(18, "ana"),
get_trip(17, "root", is_admin=True),
get_trip(99, "ana"),
)404 200 200 404
Ana gets a 404 for Bo's trip, the same as for trip 99, which doesn't exist. From the outside the two can't be told apart, so nobody learns which ids are real. Her own trip works, and an admin can read Bo's.
404: the query filters by owner, so for Ana trip 17 doesn't exist, and nobody learns it exists
Her token is fine, so it isn't 401. Answering 403 would reveal that trip 17 exists; 404 treats someone else's trip exactly like a missing one, which leaks nothing.
The starter's trip routes check the token but not the owner, so any member can read or delete anyone's trip. Fix list_trips (a member sees only their own trips, an admin sees all) and delete_trip (404 unless the trip is the caller's; admins may delete any). The models, tokens and current_user are given; a tiny in-memory store stands in for the database so the fix is all you see.
from fastapi import Depends, FastAPI, Header, HTTPException TRIPS = { 1: {"name": "Fern Hollow loop", "owner": "ana"}, 2: {"name": "Red Mesa climb", "owner": "bo"}, 3: {"name": "Pine Gap walk", "owner": "ana"}, } TOKENS = { "t-ana": {"user": "ana", "role": "member"}, "t-bo": {"user": "bo", "role": "member"}, "t-cy": {"user": "cy", "role": "admin"}, } app = FastAPI() def current_user(authorization: str = Header(default="")): user = TOKENS.get(authorization.removeprefix("Bearer ")) if user is None: raise HTTPException( 401, "Not logged in", headers={"WWW-Authenticate": "Bearer"} ) return user @app.get("/trips") def list_trips(me: dict = Depends(current_user)): return [{"id": i, **t} for i, t in TRIPS.items()] @app.delete("/trips/{trip_id}", status_code=204) def delete_trip(trip_id: int, me: dict = Depends(current_user)): if trip_id not in TRIPS: raise HTTPException(404, "Trip not found") del TRIPS[trip_id]
Write a sentence or two, then compare it with a good answer in the lesson.
A service that works on your laptop is not shipped. Three things stand between the two: tests that do not depend on each other, a record of who uses what, and a real deployment.
A test that depends on what an earlier test left in the database passes or fails depending on the order you run them in, the most frustrating kind of flaky test. FastAPI's app.dependency_overrides fixes this: map the real get_db to a replacement, and every route that asks for get_db gets the replacement instead. Give each test a brand-new in-memory database and nothing leaks between them:
def fresh_app(): sessions = make_sessions() # a new, empty in-memory database def test_db(): db = sessions() try: yield db finally: db.close() app.dependency_overrides[get_db] = test_db return app
The routes don't change at all: they still say Depends(get_db). That's the payoff of getting the session from a dependency instead of a global: the same seam lets tests swap the database, and lets production swap SQLite for PostgreSQL. Remember to clear the overrides afterward (app.dependency_overrides.clear()) if other tests use the same app.
Each "test" adds one member and reports the count. Run them against the app's own database, then again with an override that gives each one a brand-new database.
import asyncio import httpx from fastapi import Depends, FastAPI from sqlalchemy import String, create_engine, func, select from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, sessionmaker from sqlalchemy.pool import StaticPool class Base(DeclarativeBase): pass class Member(Base): __tablename__ = "members" id: Mapped[int] = mapped_column(primary_key=True) email: Mapped[str] = mapped_column(String, unique=True) def make_sessions(): engine = create_engine( "sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool ) Base.metadata.create_all(engine) return sessionmaker(bind=engine) SessionLocal = make_sessions() def get_db(): db = SessionLocal() try: yield db finally: db.close() app = FastAPI() @app.post("/members/{email}", status_code=201) def add_member(email: str, db: Session = Depends(get_db)): db.add(Member(email=email)) db.commit() return {"members": db.scalar(select(func.count()).select_from(Member))} async def call(url): async with httpx.AsyncClient( transport=httpx.ASGITransport(app=app), base_url="http://test" ) as client: return (await client.post(url)).json() def fresh_database(): sessions = make_sessions() def test_db(): db = sessions() try: yield db finally: db.close() app.dependency_overrides[get_db] = test_db for name in ["first", "second"]: print( name, "test, shared database:", asyncio.run(call(f"/members/{name}@club.test")) ) for name in ["first", "second"]: fresh_database() print( name, "test, fresh database: ", asyncio.run(call(f"/members/{name}@club.test")) ) app.dependency_overrides.clear()
Against the shared database, the second test sees the first test's member and counts 2. Run the tests in the other order, or one alone, and the numbers change: that's how order-dependent, flaky tests are born.
With app.dependency_overrides[get_db] pointing at a new in-memory database before each test, both start empty and both count 1, whatever ran before. The route didn't change at all; it still says Depends(get_db). Clearing the overrides at the end puts the real database back for anything that runs next.
When this service one day calls a model for each member, you will need to know who spent what. The cheapest start is a table with a row per member per day, written as work happens, and a query that sums it. The cell builds the table in memory and finds who is over a daily budget of 1,000 tokens.
from sqlalchemy import create_engine, func, select from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column class Base(DeclarativeBase): pass class Usage(Base): __tablename__ = "usage" id: Mapped[int] = mapped_column(primary_key=True) member_id: Mapped[int] day: Mapped[str] tokens: Mapped[int] engine = create_engine("sqlite://") Base.metadata.create_all(engine) with Session(engine) as db: db.add_all([ Usage(member_id=1, day="2026-11-13", tokens=600), Usage(member_id=1, day="2026-11-13", tokens=500), Usage(member_id=2, day="2026-11-13", tokens=700), ]) db.commit() for member in (1, 2): total = select(func.sum(Usage.tokens)).where( Usage.member_id == member, Usage.day == "2026-11-13" ) used = db.scalar(total) print(member, used, "over" if used > 1000 else "ok")
Member 1 spent 1,100 of 1,000 and is over; member 2 spent 700. The query filters by member, the same owner filter as the trips route, so one member's usage can never be read as another's. Serving a model behind FastAPI puts a daily budget like this in front of a model.
Put your FastAPI trip planner on the internet. Pick any host with a free or cheap tier that runs Python web apps (Railway, Render and Fly.io all do), deploy from your GitHub repository, and set the secrets there rather than in the code.
Paste the public address and your deploy notes into the starter, then ask the tutor to review your deployment against the rubric. A working URL is the strongest line a beginner's CV can have.
import re URL = "https://trip-planner-yourname.up.railway.app" NOTES = '''Deployed on Railway from GitHub (main branch). Secrets: TOKEN_SECRET and DATABASE_URL are set in the Railway dashboard. Health check: GET /health returns {"ok": true}. ''' def check(url, notes): problems = [] if not re.fullmatch(r"https://[A-Za-z0-9.-]+\.[A-Za-z]{2,}(/.*)?", url.strip()): problems.append("Use the public https:// address") if re.search(r"(localhost|127\.0\.0\.1)", url): problems.append( "That's your own computer: deploy it and use the host's address" ) lowered = notes.lower() if "health" not in lowered: problems.append("Mention the /health route") if "secret" not in lowered and "environment" not in lowered: problems.append( "Say where the secrets live (environment variables on the host)" ) if re.search( r"(sk-[A-Za-z0-9_-]{10,}|secret\s*=\s*['\"]?[A-Za-z0-9]{12,})", notes, re.IGNORECASE, ): problems.append( "Your notes seem to include a real secret: remove it and rotate it" ) return problems for p in check(URL, NOTES) or [ "Ready: ask a friend to open the address, then ask the tutor to review it." ]: print("-", p)
Write a sentence or two, then compare it with a good answer in the lesson.
Start with a short baseline or choose guided, independent, or AI-assisted practice.
Why is get_db written with yield and a finally block?
575 lessons in thirteen phases. 176 are free without an account, and a free account opens the rest. See every lesson.
Go from your first line of code to working as a Python developer: projects, testing, data, databases, web apps and APIs, all in your browser.
Design, test, deploy, and operate an HTTP API that another engineer can use.
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write