Bind an agent token to the agent
Make an agent token work only from the process that asked for it.
What it shows
- The agent creates a DPoP key when it starts. The key stays in the memory of the process.
- The agent asks for its token with a DPoP proof, a short JWT that it signs with that key.
- LumoAuth answers with the token type
DPoP. The token carries the thumbprint of the key in itscnf.jktclaim. - The agent calls an API with the token and a new proof. The proof names the method, the address and the token.
- The same token without a proof is refused with 401. A copy of the token is therefore of no use without the key.
The API in this recipe is the LumoAuth endpoint GET /agents/me. Your own API or MCP server receives the same two headers, Authorization: DPoP … and DPoP.
Before you start
- An agent with a client ID and a secret. The setup script registers one with the name "Payout scheduler". Its API key needs the scopes
admin:agents:readandadmin:agents:write. - The setup script sets
dpopBoundAccessTokenson the agent. The portal shows this setting on the edit page of the agent as Only issue DPoP-bound tokens. - For Python, the SDK needs the
cryptographypackage.pip install "lumoauth[dpop]"installs it, and so does therequirements.txtof the recipes.
| Variable | Value |
|---|---|
LUMOAUTH_URL | The address of LumoAuth: https://app.lumoauth.dev, or https://eu.app.lumoauth.dev for the EU region. |
LUMOAUTH_ORG_ID | Your organization, for example acme-corp. |
LUMOAUTH_API_KEY | For the setup script only: an API key with the scopes admin:agents:read and admin:agents:write. The agent itself needs no API key. |
AGENT_CLIENT_ID | The client ID of the agent, which is its agent ID (agt_…). The setup script prints it. |
AGENT_CLIENT_SECRET | The client secret of the agent. The setup script prints it. |
The setup script creates what the recipe needs, through the Admin API. You can run it again at any time.
node agents/dpop-bound-agent-tokens/setup.mjs
setup.mjs
// Registers the agent of this recipe. LumoAuth issues this agent only tokens
// that are bound to a DPoP key. Run it again at any time.
//
// node agents/dpop-bound-agent-tokens/setup.mjs
//
// The API key needs the scopes admin:agents:read and admin:agents:write.
import { call, ensure, printEnv } from '../../_lib/admin.mjs';
const agent = await ensure('/admin/agents', (a) => a.name === 'Payout scheduler', {
name: 'Payout scheduler',
description: 'Reads approved payouts and schedules them at the bank.',
identityType: 'client_secret',
capabilities: ['payouts:read', 'payouts:schedule'],
// A token request without a DPoP proof is refused for this agent.
dpopBoundAccessTokens: true,
});
// LumoAuth shows a secret once, in the answer that creates it. On a second
// run the script therefore asks for a new secret, and the old one stops.
const secret =
agent.clientSecret ??
(await call('POST', `/admin/agents/${agent.agentId}/rotate-credentials`)).data.clientSecret;
console.log(`Agent "${agent.name}" is ready. Its client ID is its agent ID.`);
printEnv({ AGENT_CLIENT_ID: agent.agentId, AGENT_CLIENT_SECRET: secret });
LumoAuth shows a secret once. A second run of the setup script therefore asks for a new secret, and the old one stops at once.
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.
- Node.js
- Python
- Go
agents/dpop-bound-agent-tokens/js/agent.mjs
// An agent binds its token to a key that it creates at start, and proves
// with each call that it holds that key.
//
// node agents/dpop-bound-agent-tokens/js/agent.mjs
import { DpopProofSigner, LumoAgent, fetchWithDpop } from '@lumoauth/agent';
// 1. A new key for this process. It stays in memory and is never stored.
const signer = DpopProofSigner.generate();
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
const agent = new LumoAgent({ dpop: signer });
// 2. The token request carries a proof, so LumoAuth binds the token to the key.
const token = await agent.authenticate();
console.log(`token type: ${agent.tokenType}`);
console.log(`key thumbprint: ${signer.thumbprint}`);
// 3. A call with the token and a new proof. The proof names the method, the
// address and the token, so it is of no use for another call.
const url = `${agent.baseUrl}/orgs/${agent.orgId}/api/v1/agents/me`;
const withProof = await fetchWithDpop(fetch, url, { method: 'GET' }, signer, { accessToken: token });
console.log(`with a proof: ${withProof.status}`);
// 4. The same token without a proof, as a thief would send it.
const withoutProof = await fetch(url, { headers: { Authorization: `Bearer ${token}` } });
console.log(`without a proof: ${withoutProof.status}`);
node agents/dpop-bound-agent-tokens/js/agent.mjs
agents/dpop-bound-agent-tokens/python/agent.py
"""An agent binds its token to a key that it creates at start, and proves
with each call that it holds that key.
python agents/dpop-bound-agent-tokens/python/agent.py
"""
import requests
from lumoauth import LumoAuthAgent
# 1. A new key for this process. It stays in memory and is never stored.
# Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
agent = LumoAuthAgent(dpop=True)
# 2. The token request carries a proof, so LumoAuth binds the token to the key.
if not agent.authenticate():
raise SystemExit("The agent could not get a token. Check AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.")
print(f"token type: {agent.token_type}")
print(f"key thumbprint: {agent.dpop.thumbprint}")
# 3. A call with the token and a new proof. The proof names the method, the
# address and the token, so it is of no use for another call.
url = f"{agent.base_url}/orgs/{agent.org_id}/api/v1/agents/me"
with_proof = requests.get(url, headers=agent.dpop_headers("GET", url))
print(f"with a proof: {with_proof.status_code}")
# 4. The same token without a proof, as a thief would send it.
without_proof = requests.get(url, headers={"Authorization": f"Bearer {agent.access_token}"})
print(f"without a proof: {without_proof.status_code}")
python agents/dpop-bound-agent-tokens/python/agent.py
agents/dpop-bound-agent-tokens/go/main.go
// An agent binds its token to a key that it creates at start, and proves
// with each call that it holds that key.
//
// go run ./agents/dpop-bound-agent-tokens/go
package main
import (
"context"
"fmt"
"log"
"net/http"
lumoauth "github.com/lumoauth/lumo-auth-go"
)
func main() {
// 1. A new key for this process. It stays in memory and is never stored.
signer, err := lumoauth.NewDPoPSigner()
if err != nil {
log.Fatal(err)
}
// Reads LUMOAUTH_URL, LUMOAUTH_ORG_ID, AGENT_CLIENT_ID and AGENT_CLIENT_SECRET.
agent, err := lumoauth.NewAgent(lumoauth.AgentConfig{DPoP: signer})
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
// 2. The token request carries a proof, so LumoAuth binds the token to the key.
token, err := agent.Authenticate(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("token type: %s\n", agent.TokenType())
fmt.Printf("key thumbprint: %s\n", signer.Thumbprint())
// 3. A call with the token and a new proof. The proof names the method, the
// address and the token, so it is of no use for another call.
url := fmt.Sprintf("%s/orgs/%s/api/v1/agents/me", agent.BaseURL(), agent.OrgID())
withProof, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err := signer.AuthorizeRequest(withProof, token); err != nil {
log.Fatal(err)
}
fmt.Printf("with a proof: %d\n", status(withProof))
// 4. The same token without a proof, as a thief would send it.
withoutProof, _ := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
withoutProof.Header.Set("Authorization", "Bearer "+token)
fmt.Printf("without a proof: %d\n", status(withoutProof))
}
func status(req *http.Request) int {
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
return resp.StatusCode
}
go run ./agents/dpop-bound-agent-tokens/go
Check the result
Output
token type: DPoP
key thumbprint: j1tJzOiLv_6oCnxN4fhQo_1DopUxFNV8mkFZZQ55MVE
with a proof: 200
without a proof: 401
- Each run prints another thumbprint, because each process creates its own key.
- Run the program of An agent gets its first token with the credentials of this agent. It sends no proof, and the token endpoint answers 400 with the message
DPoP proof required for this agent. - A call with a proof from another key gets 401 too. The thumbprint of that key does not match the
cnf.jktclaim of the token.
Limits
- DPoP protects the token. The client secret can still be copied: An agent that signs in with a key replaces it.
- An API of your own must verify the proof.
verifyAccessTokenof@lumoauth/agentdoes that when you pass it the method, the address and theDPoPheader of the request. - Only issue DPoP-bound tokens covers the tokens that the agent requests. Test tokens from the agent page of the portal and just-in-time tokens are not covered.
- The Java, .NET and PHP SDKs have no DPoP signer for agents.