Files
netris-nestri/packages/core/src/auth/device-grant.sql.ts
T
Wanjohi 36179150a1 fix(auth): make a device sign-in an answer somebody gave
Anybody could ask for a device code and be handed a link with the user
code already in it. Following that link started a sign-in, and finishing
the sign-in approved the grant. So sending somebody the link was enough:
they saw an ordinary sign-in prompt, completed it, and whoever kept the
device code polled and collected their access and refresh tokens. The
victim never saw a question, because there was not one.

There is now. Signing in says who the browser belongs to; it does not say
the person meant to hand an account to a program somewhere else. Those
are two questions and only the second authorizes anything, so the flow
ends at a page that names the program, shows the code back so it can be
compared with what the device is displaying, and offers Approve and Deny.
Approving is a POST carrying a value from the cookie, so another site
cannot submit it on somebody's behalf. Denial moved onto the same page:
it used to be a GET anyone could fire, which meant a link scanner could
cancel a real sign-in and a stranger with a user code could grief one.

Three more things that were wrong underneath.

The grant was read, modified and written back as a whole record. A poll
that read a pending grant and then wrote its bookkeeping erased an
approval that landed in between, and the client polled a dead grant until
it expired. Grants moved to a table, where approving is one conditional
update and redeeming is one delete that returns what it deleted, so
neither party can undo the other and two polls cannot both be served.

Tokens were minted when the person clicked and left sitting in storage
until collected. They are minted at redemption now, so the lifetime the
client is told about starts when it receives them, and a grant nobody
collects leaves no usable refresh token behind.

The client identifier was never checked, at either end. It is validated
when the grant is created and has to match when the code is redeemed —
without that, a leaked code is redeemable by anyone, and the identifier
the token carries is whatever the last caller claimed. The device code
is also stored as a hash now, since it is the credential the tokens are
handed to.

The store is an interface because the issuer cannot reach the database,
and because the guarantees are the point: every method is one operation,
and no caller reads a grant, decides, and writes it back.
2026-09-05 09:40:03 +03:00

63 lines
2.3 KiB
TypeScript

import { integer, jsonb, pgEnum, pgTable, text, uniqueIndex } from 'drizzle-orm/pg-core';
import { id, timestamps, utc } from '../db/types.js';
export const DeviceGrantStatusEnum = pgEnum('device_grant_status', [
'pending',
'approved',
'denied'
]);
/**
* A device authorization grant, while it is still in flight.
*
* This is short-lived state that would sit happily in a cache, and it is in a
* table anyway. The reason is that every transition here has to happen exactly
* once while two parties are touching the row — a browser somebody is clicking
* through, and a program polling every few seconds — and a store that can only
* read and write whole records cannot promise that. Here, approving is one
* conditional update and redeeming is one delete that returns what it deleted,
* so the two cannot interleave into each other.
*
* `device_code_hash` and not the code: the code is the credential the tokens
* are handed to, so what is kept is enough to recognise it and not enough to
* present it. `user_code` is stored as written, because it is read off a screen
* by the person who is looking at it and lives for minutes.
*/
export const DeviceGrantTable = pgTable(
'device_grant',
{
...id,
...timestamps,
deviceCodeHash: text('device_code_hash').notNull(),
userCode: text('user_code').notNull(),
clientId: text('client_id').notNull(),
status: DeviceGrantStatusEnum('status').notNull().default('pending'),
/** Seconds the client is currently being told to wait between polls. */
pollInterval: integer('poll_interval').notNull(),
/** Null until a poll has been given a real answer. */
lastPolledAt: utc('last_polled_at'),
expiresAt: utc('expires_at').notNull(),
/**
* Who the grant turned out to be for, written when it is approved.
*
* Not the tokens. Those are minted when the waiting program redeems the
* code, so their lifetime starts when they are handed over and a grant
* nobody collects leaves no usable credential behind.
*/
subject: jsonb('subject').$type<{
subject: string;
type: string;
properties: unknown;
ttl: { access: number; refresh: number };
}>()
},
(t) => [
uniqueIndex('device_grant_device_code_unique').on(t.deviceCodeHash),
uniqueIndex('device_grant_user_code_unique').on(t.userCode)
]
);