mirror of
https://github.com/nestriness/nestri.git
synced 2026-11-28 18:16:15 +02:00
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.
138 lines
4.4 KiB
TypeScript
138 lines
4.4 KiB
TypeScript
import { afterAll, beforeEach, describe, expect, test } from 'bun:test';
|
|
|
|
import type { RefreshRecord } from '@nestri/auth/refresh';
|
|
|
|
import { testDb } from '../db/test.js';
|
|
import { PostgresRefreshStore } from './refresh-token.js';
|
|
|
|
const sql = testDb();
|
|
const store = PostgresRefreshStore();
|
|
|
|
const SUBJECT = 'user:refresh-fixture';
|
|
|
|
let counter = 0;
|
|
function hash(): string {
|
|
counter += 1;
|
|
return `refresh-fixture-${counter}`.padEnd(64, '0');
|
|
}
|
|
|
|
function record(overrides: Partial<RefreshRecord> = {}): RefreshRecord {
|
|
return {
|
|
type: 'user',
|
|
properties: { userID: 'usr_fixture' },
|
|
subject: SUBJECT,
|
|
clientID: 'desktop',
|
|
ttl: { access: 60, refresh: 600 },
|
|
nextToken: 'next',
|
|
...overrides
|
|
};
|
|
}
|
|
|
|
async function cleanup() {
|
|
await sql`delete from refresh_token where token_hash like 'refresh-fixture-%'`;
|
|
}
|
|
|
|
beforeEach(cleanup);
|
|
afterAll(async () => {
|
|
await cleanup();
|
|
await sql.end();
|
|
});
|
|
|
|
describe('PostgresRefreshStore', () => {
|
|
test('an unknown token is missing rather than an error', async () => {
|
|
const claim = await store.claim(SUBJECT, hash(), Date.now(), 60);
|
|
expect(claim.status).toBe('missing');
|
|
});
|
|
|
|
test('a token belonging to another subject is not spendable', async () => {
|
|
const h = hash();
|
|
await store.create(SUBJECT, h, record(), 600);
|
|
|
|
const claim = await store.claim('user:someone-else', h, Date.now(), 60);
|
|
expect(claim.status).toBe('missing');
|
|
});
|
|
|
|
test('the first claim is fresh and carries the record back', async () => {
|
|
const h = hash();
|
|
await store.create(SUBJECT, h, record(), 600);
|
|
|
|
const claim = await store.claim(SUBJECT, h, Date.now(), 60);
|
|
expect(claim.status).toBe('fresh');
|
|
if (claim.status !== 'fresh') throw new Error('unreachable');
|
|
expect(claim.record.clientID).toBe('desktop');
|
|
expect(claim.record.nextToken).toBe('next');
|
|
});
|
|
|
|
test('a second claim reports the reuse and when the token was first spent', async () => {
|
|
const h = hash();
|
|
const at = Date.now();
|
|
await store.create(SUBJECT, h, record(), 600);
|
|
|
|
await store.claim(SUBJECT, h, at, 60);
|
|
const again = await store.claim(SUBJECT, h, at + 1000, 60);
|
|
|
|
expect(again.status).toBe('reused');
|
|
if (again.status !== 'reused') throw new Error('unreachable');
|
|
// The time the *first* caller spent it, not the time of this attempt —
|
|
// which is what the reuse window is measured from.
|
|
expect(again.timeUsed).toBe(at);
|
|
});
|
|
|
|
/**
|
|
* The property the whole table exists for.
|
|
*
|
|
* Five claims of one token, started together and never awaited in turn, so
|
|
* they genuinely overlap in the database rather than queueing behind each
|
|
* other. Exactly one may be told it went first; through a store that reads
|
|
* and writes whole records, all five are.
|
|
*/
|
|
test('only one of several simultaneous claims is fresh', async () => {
|
|
const h = hash();
|
|
const at = Date.now();
|
|
await store.create(SUBJECT, h, record(), 600);
|
|
|
|
const claims = await Promise.all(
|
|
Array.from({ length: 5 }, () => store.claim(SUBJECT, h, at, 60))
|
|
);
|
|
|
|
expect(claims.filter((c) => c.status === 'fresh')).toHaveLength(1);
|
|
expect(claims.filter((c) => c.status === 'reused')).toHaveLength(4);
|
|
});
|
|
|
|
test('with no retention the token is taken away, and later claims find nothing', async () => {
|
|
const h = hash();
|
|
await store.create(SUBJECT, h, record(), 600);
|
|
|
|
const first = await store.claim(SUBJECT, h, Date.now(), 0);
|
|
const second = await store.claim(SUBJECT, h, Date.now(), 0);
|
|
|
|
expect(first.status).toBe('fresh');
|
|
// Not `reused`: nothing was retained, so there is nothing left to
|
|
// recognise. Reuse detection is what retention buys.
|
|
expect(second.status).toBe('missing');
|
|
});
|
|
|
|
test('an expired token cannot be spent', async () => {
|
|
const h = hash();
|
|
await store.create(SUBJECT, h, record(), 600);
|
|
await sql`update refresh_token set expires_at = now() - interval '1 second' where token_hash = ${h}`;
|
|
|
|
const claim = await store.claim(SUBJECT, h, Date.now(), 60);
|
|
expect(claim.status).toBe('missing');
|
|
});
|
|
|
|
test('removing a subject takes every token it has and leaves other subjects alone', async () => {
|
|
const mine = [hash(), hash()];
|
|
const theirs = hash();
|
|
for (const h of mine) await store.create(SUBJECT, h, record(), 600);
|
|
await store.create('user:other', theirs, record({ subject: 'user:other' }), 600);
|
|
|
|
await store.removeSubject(SUBJECT);
|
|
|
|
for (const h of mine) {
|
|
expect((await store.claim(SUBJECT, h, Date.now(), 60)).status).toBe('missing');
|
|
}
|
|
expect((await store.claim('user:other', theirs, Date.now(), 60)).status).toBe('fresh');
|
|
});
|
|
});
|