mirror of
https://github.com/nestriness/nestri.git
synced 2026-09-20 09:45:21 +03:00
feat(auth): keep issuer state in Postgres
The issuer kept everything behind one get/set/remove/scan interface, which is what a library that must run on any provider's cache can offer. Three of the things kept there could not actually be served by it. An authorization code must be redeemable once and a refresh token spendable once, and through get and set the check and the write are separate steps — so two requests arriving together both read an unspent record, and both mint a session. In the refresh case that also means the reuse which reveals a stolen token is never recorded, because recording it is the write that the second caller overwrites. Each now has a table and an interface of its own: redeeming is one `delete ... returning`, spending is one `update ... where time_used is null returning *`, so exactly one caller is ever told it went first. This is the same argument the device grant already made, applied to the two records that had it too. Signing keys move for a different reason. Nothing races for them; they are the one record whose loss ends every session at once, and a cache is a place things may be evicted from. They are retired by setting a column rather than deleted, so the tokens they signed stay verifiable until they expire. Both credential tables store a hash and never the credential, as the device grant does. An authorization code travels in a query string and so passes through history, referrer headers and any log along the redirect; a refresh token resumes a session outright. What is left in the generic store is the rate-limit counters — written far more often than read, meaningless within the hour, and allowed to be approximate, since a lost increment costs one guess out of ten. Those move to Postgres too, so the only key-value binding this deploys with is gone and the control plane's state is one database. That was the point: nothing here now depends on a primitive a self-hoster cannot run. The generic scan also gained the separator on its prefix, so scanning `a` cannot return what is under `ab` — subjects and email addresses are both prefixes of longer subjects and email addresses. Deploying this signs everyone out. The signing keys and refresh tokens are in a store that is being left behind, so the issuer starts with a fresh key set and every existing token stops verifying.
This commit is contained in:
75
packages/auth/src/authorization-code.ts
Normal file
75
packages/auth/src/authorization-code.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* Where an authorization code lives between the redirect and the exchange.
|
||||
*
|
||||
* A code is handed to a browser in a URL and presented back within a minute,
|
||||
* and it must be redeemable exactly once. That last part is the whole reason
|
||||
* this is an interface: taking the record away and reading it have to be the
|
||||
* same operation, because a get, a decision and a remove lets two exchanges
|
||||
* arriving together both be served — and each of them mints a full session.
|
||||
*
|
||||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
import type { StorageAdapter } from './storage/storage.js';
|
||||
import { Storage } from './storage/storage.js';
|
||||
import { sha256hex } from './util.js';
|
||||
|
||||
/** What the code stands for, recorded when it is issued. */
|
||||
export interface AuthorizationCodeRecord {
|
||||
type: string;
|
||||
properties: any;
|
||||
subject: string;
|
||||
clientID: string;
|
||||
redirectURI: string;
|
||||
ttl: { access: number; refresh: number };
|
||||
pkce?: { challenge: string; method: 'S256' };
|
||||
}
|
||||
|
||||
export interface CodeStore {
|
||||
create(codeHash: string, record: AuthorizationCodeRecord, ttl: number): Promise<void>;
|
||||
|
||||
/**
|
||||
* Take the record away and return it, or return null.
|
||||
*
|
||||
* Removal and reading are one operation on purpose. Two exchanges of the
|
||||
* same code must not both be answered, and a caller cannot arrange that by
|
||||
* reading first — so it is not offered a way to.
|
||||
*/
|
||||
consume(codeHash: string): Promise<AuthorizationCodeRecord | null>;
|
||||
}
|
||||
|
||||
/**
|
||||
* The hash a code is stored under.
|
||||
*
|
||||
* An authorization code is a bearer credential that travels in a query string,
|
||||
* which means it lands in browser history, in referrer headers and in whatever
|
||||
* logs the redirect passed through. What is kept here is enough to recognise
|
||||
* one and not enough to present it.
|
||||
*/
|
||||
export function hashAuthorizationCode(code: string): Promise<string> {
|
||||
return sha256hex(code);
|
||||
}
|
||||
|
||||
/**
|
||||
* A code store backed by the generic {@link StorageAdapter}.
|
||||
*
|
||||
* The default, and the behaviour every deployment had before `codeStore`
|
||||
* existed — including its weakness: `get` and `remove` are two operations, so
|
||||
* this cannot actually promise single use. It is kept because a store that
|
||||
* only does get and set cannot do better, and an issuer that wants the promise
|
||||
* passes one that can.
|
||||
*/
|
||||
export function StorageCodeStore(storage: StorageAdapter): CodeStore {
|
||||
return {
|
||||
async create(codeHash, record, ttl) {
|
||||
await Storage.set(storage, ['oauth:code', codeHash], record, ttl);
|
||||
},
|
||||
async consume(codeHash) {
|
||||
const key = ['oauth:code', codeHash];
|
||||
const record = await Storage.get<AuthorizationCodeRecord>(storage, key);
|
||||
if (!record) return null;
|
||||
await Storage.remove(storage, key);
|
||||
return record;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user