mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 11:36:05 +00:00
3ca73d432b
Registration now goes through a registration dialect module: the paths of the challenge and the registration, the bytes scrypt runs over and the body and headers of the request. Each stable release will carry the dialect the TREK services issuer writes for it; until the first such release the module speaks the original registration, so nothing changes on the wire. An issuer that no longer takes the release's dialect answers 410. TREK then says so once per process, waits a day (or what Retry-After asks) and keeps searching through the shared pool. The issuer client only lets a dialect register at the issuer's own paths, with X-TREK-* headers that are not the gate's and a body of registration size. Renewal needs no dialect, and an expired key may now be renewed for a year instead of 30 days, the same period the issuer allows, so an installation that was offline for long comes back without an update. The key format ignores the new optional dialect claim; the shared vectors gain one key that carries it.
108 lines
4.1 KiB
TypeScript
108 lines
4.1 KiB
TypeScript
/**
|
|
* The tk1 key format, checked against the vectors the issuer is tested with
|
|
* too (tests/fixtures/service-identity/tk1-vectors.json), so TREK and the gate
|
|
* read the same bytes the same way.
|
|
*/
|
|
import { parseServiceKey } from '../../../../src/nest/service-identity/service-key.format';
|
|
|
|
import { createPublicKey, verify } from 'node:crypto';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
interface Vector {
|
|
name: string;
|
|
token: string;
|
|
valid: boolean;
|
|
payload?: { i: string; iat: number; exp: number; q: string; d?: string };
|
|
payload_json?: string;
|
|
}
|
|
|
|
const fixture = JSON.parse(
|
|
fs.readFileSync(path.join(__dirname, '..', '..', '..', 'fixtures', 'service-identity', 'tk1-vectors.json'), 'utf8'),
|
|
) as { kid: string; public_key_b64url: string; vectors: Vector[] };
|
|
|
|
const issuerKey = createPublicKey({
|
|
key: { kty: 'OKP', crv: 'Ed25519', x: fixture.public_key_b64url },
|
|
format: 'jwk',
|
|
});
|
|
|
|
/** What the gate checks: the issuer's signature over `tk1.<kid>.<payload>`, for a kid it knows. */
|
|
function gateAccepts(token: string): boolean {
|
|
const [version, kid, payload, signature] = token.split('.');
|
|
if (version !== 'tk1' || kid !== fixture.kid) return false;
|
|
return verify(null, Buffer.from(`tk1.${kid}.${payload}`), issuerKey, Buffer.from(signature, 'base64url'));
|
|
}
|
|
|
|
const valid = fixture.vectors.filter((v) => v.valid);
|
|
const invalid = fixture.vectors.filter((v) => !v.valid);
|
|
|
|
describe('parseServiceKey against the shared vectors', () => {
|
|
it.each(valid.map((v) => [v.name, v] as const))(
|
|
'SIKEY-001: reads the %s vector as the issuer wrote it',
|
|
(_, vector) => {
|
|
const key = parseServiceKey(vector.token);
|
|
expect(key).not.toBeNull();
|
|
expect(key?.kid).toBe(fixture.kid);
|
|
// The dialect a key was registered in is the issuer's statistic; TREK has no use for it.
|
|
const payload: NonNullable<Vector['payload']> = vector.payload ?? { i: '', iat: 0, exp: 0, q: '' };
|
|
const { d: _dialect, ...read } = payload;
|
|
expect(key?.payload).toEqual(read);
|
|
const body = vector.token.split('.')[2];
|
|
expect(Buffer.from(body, 'base64url').toString('utf8')).toBe(vector.payload_json);
|
|
},
|
|
);
|
|
|
|
it.each(valid.map((v) => [v.name, v.token] as const))(
|
|
'SIKEY-002: the %s vector is signed as the format says',
|
|
(_, token) => {
|
|
expect(gateAccepts(token)).toBe(true);
|
|
},
|
|
);
|
|
|
|
it.each(invalid.map((v) => [v.name, v.token] as const))(
|
|
'SIKEY-003: the %s vector fails the signature check',
|
|
(_, token) => {
|
|
expect(gateAccepts(token)).toBe(false);
|
|
},
|
|
);
|
|
|
|
it('SIKEY-004: leaves the signature to the gate, so a tampered key still reads', () => {
|
|
const tampered = invalid.find((v) => v.name === 'tampered_payload')?.token ?? '';
|
|
expect(parseServiceKey(tampered)?.payload.q).toBe('own');
|
|
});
|
|
|
|
it('SIKEY-005: reads a key that names the dialect it was registered in like any other', () => {
|
|
const vector = valid.find((v) => v.name === 'std_with_dialect');
|
|
expect(vector?.payload?.d).toBeDefined();
|
|
expect(parseServiceKey(vector?.token ?? '')?.payload).toEqual({
|
|
i: vector?.payload?.i,
|
|
iat: vector?.payload?.iat,
|
|
exp: vector?.payload?.exp,
|
|
q: 'std',
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('parseServiceKey on anything else', () => {
|
|
const body = Buffer.from(JSON.stringify({ i: 'AAECAwQFBgcICQoLDA0ODw', iat: 1, exp: 2, q: 'std' })).toString(
|
|
'base64url',
|
|
);
|
|
|
|
it.each([
|
|
['another version', `tk2.kid.${body}.sig`],
|
|
['three parts', `tk1.kid.${body}`],
|
|
['five parts', `tk1.kid.${body}.sig.extra`],
|
|
['a part outside base64url', `tk1.k+d.${body}.sig`],
|
|
['a payload that is no JSON', 'tk1.kid.bm90LWpzb24.sig'],
|
|
['a payload without an expiry', `tk1.kid.${Buffer.from('{"i":"a","iat":1,"q":"std"}').toString('base64url')}.sig`],
|
|
['nothing', ''],
|
|
])('SIKEY-010: refuses %s', (_, token) => {
|
|
expect(parseServiceKey(token)).toBeNull();
|
|
});
|
|
|
|
it('SIKEY-011: tolerates the whitespace an environment file leaves around it', () => {
|
|
expect(parseServiceKey(` tk1.kid.${body}.sig\n`)?.token).toBe(`tk1.kid.${body}.sig`);
|
|
});
|
|
});
|