Poppy-Session header on every request. When signed out, the agent only sends the Poppy-Session header. When signed in, it also sends an access token for the user issued by Auth0 to the agent. Your API needs to:
- Verify the
Poppy-Sessionheader when it uses the Session, the agent, or the User ID. - Verify the access token, and check that both belong to the same agent.
- Enforce scopes, and return the errors agents expect.
- Describe its operations in an OpenAPI document that agents can read.
@auth0/auth0-api-js, the SDK verifies the header and the token and builds most of the error responses, including the sign-in challenge. You check the scopes, and send the 403 and 429 responses yourself.
Prerequisites
- Your tenant set up for the Personal Agent Protocol, with the Auth0 CLI or manually.
- Your API registered in Auth0 with the Personal Agent Protocol enabled, and set as the tenant’s default audience.
- The public
httpsorigin agents call, such ashttps://api.shop.example. Agents sign each request for that origin and path. - The beta release of your SDK:
@auth0/auth0-express-api@1.1.0-beta.0or@auth0/auth0-api-js@1.8.0-beta.0.
Verify agent requests
The following example configures your API to verifyPoppy-Session JWTs, then protects two routes: the cart, which any agent with a verified Session can use, and order placement, which also requires a signed-in user with the orders:write scope:
- Express
- Node.js (api-js)
app.js
req.auth0.poppy holds the verified Session, which includes its:keysessionIduserIdagentstate(signed_outorsigned_in)source(headerwhen the Session came in thePoppy-Sessionheader ortokenwhen it came in your web application’s access token).
version, the Poppy-Version of the request. req.auth0.user holds the claims of the access token, when there is one.Set requireAccount: true on an operation that acts on the user’s account. It answers a signed-out agent and a guest with 401 and the sign-in challenge. A guest is the anonymous user that Auth0 derives from the agent and the User ID when the agent joins your web application signed out. Its access token is valid and holds any scope you grant to guests, but it isn’t an account. A route that only checks scopes lets a guest through. Leave requireAccount off for operations that serve guests too, such as the cart.When the SDK meets a failure it doesn’t recognize, it calls next(error) with a 500 error whose cause is the original error. Add an Express error handler that logs the cause and keeps it out of the response.verifySession: true makes the SDK verify a Poppy-Session header wherever it checks a request, not only where a Session is required: on every requiresAuth() route in Express, and on every verifyPoppyRequest() call. It’s already on when you set agents. With it off, and no requireSession on the route, the SDK ignores the header and takes the Session from the access token’s act claim, if the token has one.
requireSession: true makes an operation an agent operation: a request with no Poppy-Session header and no Session in its access token, such as a person’s own login to your website, gets 400 and invalid_session. Serve your website’s own logins from other routes.
Agents sign each Session for your Auth0 issuer. Set domain to that issuer’s domain. If your issuer is a custom domain, use the custom domain, not the *.auth0.com domain, or the SDK refuses every Session with invalid_session. domain is also the issuer of the access tokens your API accepts, so your web application must log in through the same domain.
The SDK checks, in order: the Poppy-Version header, the Session header’s format, the agent against agents, the delegation and its signature with the agent’s published keys, the claims against your issuer and the request’s method and URL, the binding to the access token, the access token itself, that the token was issued to the same agent, and, with a Session store, the Session’s record.
Agents send a Poppy-Version: 1.0 header with every request that carries a Poppy-Session header, and the SDK refuses a request without it with version_unsupported. When you test your API with curl or your own script, make sure to also send Poppy-Version: 1.0.
The User ID in the
Poppy-Session header is a pseudonymous ID the agent chooses. It is not an account. Act on the user’s account only with the sub of a verified access token.Correlate activity with Sessions
Record the Session each request belongs to, so you can correlate what the agent does across requests, for example a cart filled signed out and the checkout after sign-in. Key state and events by the SDK’s Sessionkey, which the SDK builds from the Session ID and the User ID. Never key state by sessionId alone: agents choose their own Session IDs, so the Sessions of two users can have the same one. When the user signs in, a Session keeps its key, so state from before sign-in carries over.
The SDK takes the Session from two sources:
- the
Poppy-Sessionheader when the request has one - the
actclaim of the access token only when it has no header, such as a call from your web application after a browser join
key, including when the agent is linked to an Auth0 agent. State saved under the key from one path is found from the other, and ending a Session ends it on both.
For a linked agent, agent.id differs between the paths: it is the agent’s CIMD URL in the header and the Auth0 agent ID in the token, so don’t compare it across paths. The agent isn’t part of the key. Two agents that send the same Session ID and User ID get the same key, so treat the key as the name of a Session, not as a secret or as proof of which agent sent the request. This is a beta limitation, and it may change in a later release.
If your API runs on more than one instance, or you want to end Sessions, pass a Session store to the SDK as poppy.sessionStore, which is an instance of a class that extends PoppySessionStore. It has a get method, and a set method that stores a record only if the key has none and returns the stored record. To end a Session, mark its record as ended: the SDK then refuses the Session’s requests with session_ended. Without a store, the SDK keeps no Session records. To learn more, read Correlate activity with Sessions.
Protect your API
- Rate limit by agent. The SDK doesn’t rate limit. Limit by the verified agent,
poppy.agent.id, and answer with429and aRetry-Afterheader. - List your agents. Without
agents, the SDK fetches the metadata of any agent that sends a header, so anyone can make your API fetch any publichttpsURL they choose. Without a list, the SDK runs at most 32 loads of agent metadata at one time, and answers a request that needs one more with503and aRetry-Afterheader. Someone who sends many agent URLs that never answer can make requests from new agents fail until those fetches time out. Setagentsin production. Signed-in requests are already limited to the agents you registered, because Auth0 issues tokens only to them. During the beta, the list applies to the agent named in thePoppy-Sessionheader, not to a Session that a website’s token carries in itsactclaim. If your website calls your API with its own tokens, checkpoppy.agent.idyourself for those. For a linked agent it’s the Auth0 agent ID, such asagt_..., not the URL you list inagents. - Make changes safe to repeat. A copied header works again for the same method and URL for about a minute. Accept an
Idempotency-Keyon operations that change data. - Don’t log credentials. Never log the
Poppy-Session,Authorization, orDPoPheaders.
Errors agents expect
The Express API SDK sends these responses itself, except the429. With @auth0/auth0-api-js, toHttpErrorResponse() returns them all except the 403 and the 429, which you send:
Session errors use
400, not 401, so OAuth libraries don’t discard a valid access token.
Describe your API for agents
Agents learn your API from the OpenAPI document listed inpoppy.json, and send the Poppy-Session header only to the operations it describes. Make sure your document:
- Lists the public URL agents call in
servers. - Declares an OAuth 2.0 security scheme with a description for each scope.
- Sets security requirements on each operation with the scopes it needs. Use
[{}]as an alternative for operations that work signed out. - Gives each operation an
operationIdand a description written for a language model: what it does, when to use it, and what it changes.
Check it works
Call an operation that needs a Session, with noPoppy-Session header. Your API should refuse the request with 400 and invalid_session, and no WWW-Authenticate header:
curl can’t sign a Poppy-Session header. To see what a signed-out agent gets, call the operation from an agent, such as the Poppy Playground. In your API’s tests, createTestAgent() from @auth0/auth0-api-js/testing or @auth0/auth0-express-api/testing signs requests for you. Pass its fetch as poppy.agentMetadata.fetch, so your API reads the test agent’s keys without a network. A signed Session with no access token, sent to an operation that needs the account, should get 401 and a challenge for each scheme that names the scope:
For more troubleshooting steps, read Test Your Setup.