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

GET/v1/restaurants

Active restaurants near a point, one page at a time

Query parameters
  • latnumberrequired

    -90 to 90

  • lngnumberrequired

    -180 to 180

  • radius_kmnumber

    above 0, at most 50, default 5

  • limitinteger

    default 20. Integers below 1 count as 1 and above 50 as 50.

  • cursorstring

    next_cursor from the previous page of the same query.

  • atstring (date-time)

    RFC 3339 time that is_open is evaluated at. Defaults to now.

Response 200 OK
  • itemsRestaurantSummary[]required
    • idintegerrequired
    • namestringrequired
    • latnumberrequired
    • lngnumberrequired
    • is_openbooleanrequired

      Whether 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_cursorstringrequired

    Empty on the last page.

Response 400 Bad Request

invalid_parameterinvalid_cursor

Acceptance criteria

  • A signed walk with lat=28.46, lng=77.03, radius_km=5 and limit=7 visits every matching restaurant exactly once and returns at least 40 restaurants.
  • Two complete walks of the same query produce identical next_cursor values at every corresponding pagination position.
  • Changing any cursor character before its final two characters returns 400 Bad Request with error code invalid_cursor.
  • Truncating the first cursor to half its length, removing its final four characters, appending AAAA, or appending the complete cursor to itself returns 400 Bad Request with error code invalid_cursor.

Constraints

  • When set, CURSOR_SECRET must 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:

bash
cd backend && make up
cd backend && make test W=2O1

Read the first failing assertion to identify whether the pagination walk, cursor determinism or invalid-cursor rejection is failing.

Finished Exercise 2.22?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. A signed walk visits every matching restaurant exactly once and returns at least 40 restaurants.
  2. Two walks of the same query produce identical cursors at corresponding pagination positions.
  3. Changing any character before the final two cursor characters returns 400 invalid_cursor.
  4. Truncated and extended cursors return 400 invalid_cursor.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=2O1

Tests

  1. 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}
    Params
    lat28.46
    lng77.03
    radius_km5
    limit7
    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=5 and limit=7, following each returned next_cursor.

    Expected Every restaurant ID appears exactly once, with at least 40 restaurants returned from the seeded dataset.

    TestW2O1_SignedCursorsStillWalkEveryRestaurantOnce in backend/e2e/w2_o1_test.go