Skip to Content

Account Events

Tell another service when a session ends or an account is deleted

A backend that exchanges a token from Entro Auth for a session of its own has no way to know that the person signed out here. Its session lives on until its own time runs out. The account events plugin tells it: each time a session ends or an account is deleted, it posts one signed request to an address you give.

Use it for a service that trusts this one without being an OAuth client of it, for example an API that verifies the JWT plugin's token and then keeps its own session. Clients of the OAuth provider plugin have OpenID Connect back-channel logout for the same purpose.

Installation

auth.ts
import { entroAuth } from "@entro314labs/entro-auth"
import { accountEvents } from "@entro314labs/entro-auth/plugins"

export const auth = entroAuth({
    // ... other config options
    plugins: [
        accountEvents({ 
            url: "https://api.example.com/auth/events", 
            secret: process.env.ACCOUNT_EVENTS_SECRET!, 
        }), 
    ]
})

The secret is whsec_ followed by the base64 of the signing key, and the receiver holds the same value:

echo "whsec_$(openssl rand -base64 32)"

A secret in another shape is refused when the server starts.

Events

EventdataSent when
session.endedid, user_ida session is removed: sign-out, revoking one or all, deleting the user
user.deletedidan account is deleted, after its sessions
{ "type": "session.ended", "data": { "id": "<session id>", "user_id": "<user id>" } }

To let the receiver match a session, put its id in the token it exchanges. With the JWT plugin:

auth.ts
jwt({
    jwt: {
        definePayload: ({ user, session }) => ({
            email: user.email,
            sid: session.id, 
        }),
    },
})

The receiver records sid with the session it opens, and ends that session when session.ended names it.

A changed password

There is no event for a password change. Set emailAndPassword.revokeSessionsOnPasswordReset and a reset ends every session of the person, which sends one session.ended for each.

Verifying a delivery

Each event is a Standard Webhooks request:

HeaderValue
webhook-idunique per delivery
webhook-timestampseconds since the epoch
webhook-signaturev1, and the base64 HMAC-SHA256 of id.timestamp.body

The key is the base64 after whsec_, decoded. Any Standard Webhooks library verifies it; by hand:

import { createHmac, timingSafeEqual } from "node:crypto"

function verify(secret: string, headers: Headers, body: string) {
    const key = Buffer.from(secret.slice("whsec_".length), "base64")
    const signed = `${headers.get("webhook-id")}.${headers.get("webhook-timestamp")}.${body}`
    const expected = createHmac("sha256", key).update(signed).digest()
    const given = Buffer.from((headers.get("webhook-signature") ?? "").slice("v1,".length), "base64")
    return given.length === expected.length && timingSafeEqual(given, expected)
}

Refuse a delivery whose timestamp is more than a few minutes old, so a captured one cannot be replayed.

What it does not do

A delivery the receiver does not take (an error status, a timeout, no connection) is written to the log with the event's type. It never fails the request that caused it: the session or the account is already gone here. On its own this plugin does not send it again. Add the outbox plugin and the delivery is kept and tried again, under the same webhook-id with a new timestamp and signature. Either way, give the receiver's own session a lifetime you can live with.

  • An expired session sends nothing. Events come from a session being removed. One that simply runs out is not removed at that moment, so the receiver's session needs an expiry of its own.
  • Sessions that are not rows send nothing. With no database, or with secondary storage and no session.storeSessionInDatabase, ending a session deletes no row. The plugin warns when the server starts in such a setup.
  • Deliveries are awaited unless advanced.backgroundTasks.handler is set (for example waitUntil on a serverless platform), in which case they run after the response.

Options

url: where each event is posted.

secret: the signing secret, whsec_ followed by base64.

timeout: how long one delivery may take, in milliseconds. Default 5000.

On this page