Skip to main content

Verify a personal agent's session in your API

Check the session header of each request from a personal agent, with one helper in each framework.

RecipeDraftView source
This page is a draft. The code exists, and not all of it has been run for this page yet. The page says what was checked.
Works with
JavaScript / TypeScriptPythonGoPHPC# / .NETExpressNext.jsFastAPIGo net/httpSymfonyLaravelASP.NET Core

What it shows​

  1. A personal agent calls GET /api/orders with the headers Poppy-Session and Poppy-Version, and an access token when its user signed in.
  2. The helper of your framework sends the method, the public URL and those headers to LumoAuth, with the scope that the route needs.
  3. A verified request reaches the handler, with the agent, the user, the Session and the account of the access token.
  4. A refused request never reaches the handler. The agent gets the response of LumoAuth unchanged: its status, its headers and its body.

Your API authenticates to LumoAuth as one of your confidential applications. It never sees the agent's keys or checks a JWT itself.

Before you start​

  • The Personal Agent Protocol is on for your organization, with the scope orders:read. See Accept your customers' personal agents.
  • A confidential application whose allowed scopes include poppy:verify. The setup script creates it. Its API key needs admin:clients:write. An application without that scope gets 403 insufficient_scope.
  • PUBLIC_ORIGIN is the address that agents call. The helpers build the URL from it and never from the Host header.
VariableValue
LUMOAUTH_URLThe address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region.
LUMOAUTH_ORG_IDYour organization, for example acme-corp.
LUMOAUTH_CLIENT_IDThe client ID of a confidential application whose allowed scopes include poppy:verify. The setup script prints it.
LUMOAUTH_CLIENT_SECRETThe secret of that application. The setup script prints it once, when it creates the application.
PUBLIC_ORIGINThe address that personal agents call, for example https://shop.example.com. The default is http://localhost:3000.

The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.

node personal-agents/verify-a-session/setup.mjs
setup.mjs
// Creates the confidential application that your API uses to ask LumoAuth
// about requests from personal agents. Its allowed scopes include
// poppy:verify. Run it again at any time: the application is created once.
//
// node personal-agents/verify-a-session/setup.mjs
//
// The API key needs the scope admin:clients:write. The client secret is
// printed once, when the application is created: keep it in your secret store.
import { call, ensure, printEnv } from '../../_lib/admin.mjs';

const application = await ensure('/admin/clients', (c) => c.name === 'Northwind Orders API', {
name: 'Northwind Orders API',
isConfidential: true,
grantTypes: ['client_credentials'],
scopes: ['poppy:verify'],
});
// Also for an application that existed before: the scope must be allowed.
await call('PATCH', `/admin/clients/${application.clientId}`, { scopes: ['poppy:verify'] });

console.log(`Application ${application.name} may call the verification endpoint.`);
printEnv({
LUMOAUTH_CLIENT_ID: application.clientId,
...(application.secret ? { LUMOAUTH_CLIENT_SECRET: application.secret } : {}),
});

Code​

Run each command from the recipes folder of the examples repository. Set the folder up once for your language. Every version below was run against a LumoAuth organization. Each version ran against an organization with the protocol on. A request without a Poppy-Session header and one with a malformed header got the refusal of LumoAuth, relayed unchanged. A verified request needs a personal agent whose metadata document is on a public HTTPS address: none was available, so the handlers themselves did not run. The Next.js version was run with next build and next start.

personal-agents/verify-a-session/js/express.mjs
// An Express API that personal agents call. LumoAuth verifies the
// Poppy-Session header of each request before the route runs.
//
// node personal-agents/verify-a-session/js/express.mjs
//
// PUBLIC_ORIGIN is the address that agents call, for example
// https://shop.example.com.
import express from 'express';
import { poppySession } from '@lumoauth/express';

const app = express();

// One check for each route, with the scopes that the route needs.
const poppy = (requiredScope) =>
poppySession({
baseUrl: process.env.LUMOAUTH_URL ?? 'https://app.lumoauth.dev',
organization: process.env.LUMOAUTH_ORG_ID,
clientId: process.env.LUMOAUTH_CLIENT_ID,
clientSecret: process.env.LUMOAUTH_CLIENT_SECRET,
// From configuration: the middleware never reads the Host header.
publicOrigin: process.env.PUBLIC_ORIGIN ?? 'http://localhost:3000',
requiredScope,
});

// A refused request never gets here: the agent receives the response that
// LumoAuth worded, unchanged.
app.get('/api/orders', poppy('orders:read'), (req, res) => {
const { clientId, userId, session, account } = req.poppy;
res.json({ agent: clientId, user: userId, session: session.id, account: account.sub, orders: [] });
});

app.listen(Number(process.env.PORT ?? 3000), () => console.log('Orders API ready'));
node personal-agents/verify-a-session/js/express.mjs

Check the result​

Start one version and send a request without the session header. LumoAuth refuses it, and the API passes the refusal on:

Output
$ curl -i http://localhost:3000/api/orders -H 'Poppy-Version: 1.0'
HTTP/1.1 400 Bad Request
Cache-Control: no-store
Content-Type: application/json

{"error":"invalid_session","error_description":"The Poppy-Session header is missing."}
  • A request from a personal agent that signed its user in reaches the handler. The answer holds the agent's client_id, its name for the user, the id of the Session and the user's id in your organization.
  • The same request without an access token gets 401 with WWW-Authenticate: Bearer scope="orders:read". The agent then signs its user in for that scope.
  • In the portal, open Personal agents, then Sessions. The Session of the agent is listed, and you can end it there. Its next request gets session_ended.

Limits​

  • Pass the scope of each route. Without requiredScope, a signed-out request reaches the handler, and account is empty.
  • A Session JWT is good for one verification. Put the helper on each route once, and do not verify the same request twice.
  • Do not log the headers Poppy-Session, Authorization and DPoP. They are secrets.
  • Spring Boot is not in this recipe. The Java SDK and its Spring Boot starter have no helper for the protocol. A Spring service can call the verification endpoint directly, as the docs show for plain HTTP.
  • While the protocol is off for your organization, LumoAuth answers the verification call with 404. The helpers then answer with a server error.
  • An error of your own call to LumoAuth, such as a wrong client secret, is never relayed to the agent. The helpers answer with a server error.