Revoke connections
An API revokes the access keys of a connection, and handles a connection that Tempo ID refuses.
Overview
A connection gives an API an access key for one user of one app. The API revokes its access keys with its own client authentication and no user token, as an OAuth client revokes its own grant. Tempo ID then refuses a new access key for the connection until the user approves the API again.
The demo signs a user in for this site's API, which pays 1 OUSD, revokes the connection, and refuses a second payment with 403.
Walkthrough
The steps add two parts to the API of Pay for users.
Revoke the access keys of a connection
The token that connections.connect returns names the connection: the app's client ID and the user's ID. connections.revoke revokes each of the connection's access keys on the chain, and deletes the connection from the store.
import { Accounts, Store } from 'accounts'
import { viem } from 'accounts/plugins'
import { Hono } from 'hono'
const accounts = Accounts.create({
chain: 'testnet',
connections: {
metadata: {
accessKey: { limits: [{ limit: '25', token: 'ousd' }] },
privateKey: JSON.parse(process.env.API_PRIVATE_KEY!),
},
resource: 'https://api.example/pay',
store: Store.memory(),
},
plugins: [viem()],
})
const app = new Hono()
app.all('/api/*', (c) => accounts.handler.fetch(c.req.raw))
app.get('/.well-known/oauth-protected-resource/*', (c) =>
accounts.handler.fetch(c.req.raw),
)
app.post('/revoke', async (c) => {
const accessToken = c.req.header('authorization')?.replace(/^Bearer /, '')
if (!accessToken) return c.json({ error: 'invalid_token' }, 401)
const { token } = await accounts.connections.connect({ accessToken })
const receipts = await accounts.connections.revoke({
clientId: token.clientId,
userId: token.userId,
})
return c.json({
transactionHashes: receipts.map((receipt) => receipt.transactionHash),
})
})
export default appHandle a refused connection
connections.connect throws for an access token that does not verify, and for a connection that Tempo ID refuses.
| Error | reason | Cause | Status |
|---|---|---|---|
AccessToken.InvalidError | The access token does not verify, or names another audience | 401 | |
Accounts.RequestError | connection_mismatch | The approval is for another access key, such as after a revocation | 403 |
Accounts.RequestError | unknown_allowance | The user approved no connection, or the approval ended | 403 |
import { AccessToken, Accounts, Store } from 'accounts'
import { viem } from 'accounts/plugins'
import { Hono } from 'hono'
const accounts = Accounts.create({
chain: 'testnet',
connections: {
metadata: {
accessKey: { limits: [{ limit: '25', token: 'ousd' }] },
privateKey: JSON.parse(process.env.API_PRIVATE_KEY!),
},
resource: 'https://api.example/pay',
store: Store.memory(),
},
plugins: [viem()],
})
const app = new Hono()
app.all('/api/*', (c) => accounts.handler.fetch(c.req.raw))
app.get('/.well-known/oauth-protected-resource/*', (c) =>
accounts.handler.fetch(c.req.raw),
)
app.post('/revoke', async (c) => {
const accessToken = c.req.header('authorization')?.replace(/^Bearer /, '')
if (!accessToken) return c.json({ error: 'invalid_token' }, 401)
const { token } = await accounts.connections.connect({ accessToken })
const receipts = await accounts.connections.revoke({
clientId: token.clientId,
userId: token.userId,
})
return c.json({
transactionHashes: receipts.map((receipt) => receipt.transactionHash),
})
})
app.onError((error, c) => {
if (error instanceof AccessToken.InvalidError)
return c.json({ error: 'invalid_token' }, 401)
if (
error instanceof Accounts.RequestError &&
(error.reason === 'connection_mismatch' ||
error.reason === 'unknown_allowance')
)
return c.json({ error: error.reason }, 403)
return c.json({ error: 'server_error' }, 500)
})
export default appIf the API responds with 403, the app signs the user in again with resource. After the approval, the next request redeems a new access key.
Use cases
Each use case revokes the access keys of a connection with another member of accounts.connections.
Revoke one access key
connections.revokeAccessKey revokes one of the connection's access keys. It returns the receipt, or null if the connection holds no such access key, or Tempo ID holds it no longer.
import { Accounts, Store } from 'accounts'
import { viem } from 'accounts/plugins'
import { Hono } from 'hono'
const accounts = Accounts.create({
chain: 'testnet',
connections: {
metadata: {
accessKey: { limits: [{ limit: '25', token: 'ousd' }] },
privateKey: JSON.parse(process.env.API_PRIVATE_KEY!),
},
resource: 'https://api.example/pay',
store: Store.memory(),
},
plugins: [viem()],
})
const app = new Hono()
app.all('/api/*', (c) => accounts.handler.fetch(c.req.raw))
app.get('/.well-known/oauth-protected-resource/*', (c) =>
accounts.handler.fetch(c.req.raw),
)
app.post('/revoke-access-key', async (c) => {
const accessToken = c.req.header('authorization')?.replace(/^Bearer /, '')
if (!accessToken) return c.json({ error: 'invalid_token' }, 401)
const { connection, token } = await accounts.connections.connect({
accessToken,
})
const receipt = await accounts.connections.revokeAccessKey({
accessKey: connection.accessKey.keyAuthorization.keyId,
clientId: token.clientId,
userId: token.userId,
})
return c.json({ transactionHash: receipt?.transactionHash ?? null })
})
export default app