GET /v1/restaurants: signed cursors
You build signed next_cursor values for GET /v1/restaurants, authenticating each pagination position before decoding it. Modified, truncated and extended cursors return 400 invalid_cursor.
Ticket
You extend GET /v1/restaurants from W2B4 by signing each non-empty next_cursor when CURSOR_SECRET is configured. The client sends the cursor back unchanged, and the endpoint continues the same keyset pagination walk. When CURSOR_SECRET is absent, keep the existing unsigned cursor behaviour.
Base64url encoding makes a cursor convenient to transmit, but it does not stop a client from editing its contents. A Signed cursor lets the API detect those edits before trusting the pagination position. This optional extension hardens the existing restaurant list without replacing its pagination implementation.
Endpoint
Active restaurants near a point, one page at a time
latnumberrequired-90 to 90
lngnumberrequired-180 to 180
radius_kmnumberabove 0, at most 50, default 5
limitintegerdefault 20. Integers below 1 count as 1 and above 50 as 50.
cursorstringnext_cursor from the previous page of the same query.
atstring (date-time)RFC 3339 time that is_open is evaluated at. Defaults to now.
itemsRestaurantSummary[]requiredidintegerrequirednamestringrequiredlatnumberrequiredlngnumberrequiredis_openbooleanrequiredWhether one of the restaurant's open_slots contains the request's at time, in Asia/Kolkata local minutes. A slot covers open_min up to but not including close_min; when close_min is not after open_min the slot runs past midnight. Before the hours are first loaded this is false.
next_cursorstringrequiredEmpty on the last page.
invalid_parameterinvalid_cursor
Acceptance criteria
- A signed walk with
lat=28.46,lng=77.03,radius_km=5andlimit=7visits every matching restaurant exactly once and returns at least 40 restaurants. - Two complete walks of the same query produce identical
next_cursorvalues at every corresponding pagination position. - Changing any cursor character before its final two characters returns
400 Bad Requestwith error codeinvalid_cursor. - Truncating the first cursor to half its length, removing its final four characters, appending
AAAA, or appending the complete cursor to itself returns400 Bad Requestwith error codeinvalid_cursor.
Constraints
- When set,
CURSOR_SECRETmust contain at least 32 bytes. The API must refuse to start with a shorter secret.
Design direction
Keep cursor signing and verification together behind the cursor abstraction used by restaurant listing. The restaurant service can select signed or unsigned cursor handling from the configuration. The HTTP handler should continue mapping cursor decoding failures to one 400 invalid_cursor response.
Consider these trade-offs:
- HMAC provides integrity, not confidentiality. The pagination position does not need to be encrypted.
- Include the version information in the authenticated data, so changing the token version also invalidates its signature.
- Verify the authentication tag before decoding the pagination position. Use Constant-time comparison rather than ordinary string equality.
Any internal package or file layout passes as long as it meets the contract.
How to check your work
Run these commands from the repository root:
cd backend && make up
cd backend && make test W=2O1Read the first failing assertion to identify whether the pagination walk, cursor determinism or invalid-cursor rejection is failing.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- A signed walk visits every matching restaurant exactly once and returns at least 40 restaurants.
- Two walks of the same query produce identical cursors at corresponding pagination positions.
- Changing any character before the final two cursor characters returns
400 invalid_cursor. - Truncated and extended cursors return
400 invalid_cursor.
Commands
cd backend && make upcd backend && make test W=2O1Tests
Following signed cursors through all pages visits every restaurant exactly once.
GET/v1/restaurants?lat=28.46&lng=77.03&radius_km=5&limit=7&cursor={next_cursor}Paramslat 28.46 lng 77.03 radius_km 5 limit 7 cursor {next_cursor} Response 200 OK{ "items": [ { "id": 12, "name": "…", "lat": 28.4612, "lng": 77.0291 }, … ], "next_cursor": "eyJ…" }Input Request restaurant pages using
lat=28.46,lng=77.03,radius_km=5andlimit=7, following each returnednext_cursor.Expected Every restaurant ID appears exactly once, with at least 40 restaurants returned from the seeded dataset.
TestW2O1_SignedCursorsStillWalkEveryRestaurantOnceinbackend/e2e/w2_o1_test.go