mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
Merge dev into feat/native-apps
Brings the native apps branch up to dev (NestJS-only server, MikroORM
repositories, tracked sessions, per-domain locales, the lint ratchets) and
ports its new code onto that architecture:
- server: /api/auth/native moves onto injected services. The session is
minted by the auth domain's new LoginSessionService (login bookkeeping and
SessionsService.issue in one transaction), the one-time codes live behind a
NativeHandoffStore port with a capped in-memory implementation that drops
expired codes on write instead of running a timer, both routes declare
response contracts, and the exchange answers { success: true }. Unit and
e2e suites run on createTestModule and the ORM factories.
- shared: native strings for az, et, sk and th, native wired into every
locale, the address placeholder as a full URL.
- client: the account and export changes go into dev's shared hooks
(useAccountSettings, useTripExport), the native server card is a
desktop/phone pair on useNativeServer, the hand-off call moves out of the
page hook into native/, best-effort native failures go through
reportError instead of the console, and the address screen is known to
knip and lint:i18n-keys.
- package-lock.json regenerated on Linux.
This commit is contained in:
@@ -1,135 +1,138 @@
|
||||
/**
|
||||
* Native sign-in hand-off e2e: the real CookieAuthGuard, the real cookie
|
||||
* service and the real AuthService against a temp SQLite db. Walks the whole
|
||||
* round trip the apps make: the browser side mints a code for a challenge, the
|
||||
* WebView side redeems it with the verifier and walks away with a session
|
||||
* cookie that /api/auth/me accepts.
|
||||
* helpers, the real AuthService and session table over a migrated temp SQLite
|
||||
* db. Walks the whole round trip the apps make: the browser side mints a code
|
||||
* for a challenge, the WebView side redeems it with the verifier and walks
|
||||
* away with a session cookie that /api/auth/me accepts.
|
||||
*/
|
||||
import { describe, it, expect, beforeAll, afterAll, beforeEach, vi } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import crypto from 'crypto';
|
||||
import type { Server } from 'http';
|
||||
import { Test } from '@nestjs/testing';
|
||||
import { sessionCookie, signSession } from './harness';
|
||||
|
||||
const { db } = vi.hoisted(() => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
||||
const Database = require('better-sqlite3');
|
||||
const tmp = new Database(':memory:');
|
||||
tmp.exec('PRAGMA journal_mode = WAL');
|
||||
tmp.exec('PRAGMA foreign_keys = ON');
|
||||
return { db: tmp };
|
||||
});
|
||||
|
||||
vi.mock('../../src/db/database', () => ({
|
||||
db,
|
||||
closeDb: () => {},
|
||||
reinitialize: () => {},
|
||||
getPlaceWithTags: () => null,
|
||||
canAccessTrip: () => undefined,
|
||||
isOwner: () => false,
|
||||
}));
|
||||
vi.mock('../../src/websocket', () => ({ broadcastToUser: vi.fn(), broadcast: vi.fn() }));
|
||||
vi.mock('../../src/nest/audit/audit-log.logger', () => ({ LOG_LEVEL: 'error', logInfo: vi.fn(), logDebug: vi.fn(), logError: vi.fn(), logWarn: vi.fn() }));
|
||||
|
||||
import { MailerService } from '../../src/nest/notifications/mailer/mailer.service';
|
||||
import { createTables } from '../../src/db/schema';
|
||||
import { runMigrations } from '../../src/db/migrations';
|
||||
import { createUser } from '../helpers/factories';
|
||||
import { resetRateLimits } from '../helpers/test-db';
|
||||
import { AuditLog } from '../../src/db/entities/AuditLog.entity';
|
||||
import { AuthModule } from '../../src/nest/auth/auth.module';
|
||||
import { NativeAuthModule } from '../../src/nest/native-auth/native-auth.module';
|
||||
import { DatabaseModule } from '../../src/nest/database/database.module';
|
||||
import { TrekExceptionFilter } from '../../src/nest/common/trek-exception.filter';
|
||||
import { ZodValidationPipe } from '../../src/nest/common/zod-validation.pipe';
|
||||
import { NativeAuthModule } from '../../src/nest/native-auth/native-auth.module';
|
||||
import { MailerService } from '../../src/nest/notifications/mailer/mailer.service';
|
||||
import { createSnapshotTestDb } from '../helpers/db-mock';
|
||||
import { findRows } from '../helpers/factories/rows';
|
||||
import { makeUser } from '../helpers/factories/users';
|
||||
import { resetRateLimits } from '../helpers/test-db';
|
||||
import { createTestMikroOrmModule, createTestOrm, type TestOrm } from '../helpers/test-orm';
|
||||
import { TestUnitOfWorkModule } from '../helpers/test-uow';
|
||||
import { sessionCookie, signSession } from './harness';
|
||||
import { Test } from '@nestjs/testing';
|
||||
|
||||
const verifier = crypto.randomBytes(32).toString('base64url');
|
||||
const challenge = crypto.createHash('sha256').update(verifier).digest('hex');
|
||||
import cookieParser from 'cookie-parser';
|
||||
import { createHash, randomBytes } from 'crypto';
|
||||
import type { Server } from 'http';
|
||||
import request from 'supertest';
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('../../src/nest/audit/audit-log.logger', () => ({
|
||||
LOG_LEVEL: 'error',
|
||||
logInfo: vi.fn(),
|
||||
logDebug: vi.fn(),
|
||||
logError: vi.fn(),
|
||||
logWarn: vi.fn(),
|
||||
}));
|
||||
|
||||
const verifier = randomBytes(32).toString('base64url');
|
||||
const challenge = createHash('sha256').update(verifier).digest('hex');
|
||||
|
||||
const db = createSnapshotTestDb();
|
||||
let orm: TestOrm;
|
||||
let server: Server;
|
||||
let app: Awaited<ReturnType<typeof build>>;
|
||||
let userId: number;
|
||||
|
||||
async function build() {
|
||||
const moduleRef = await Test.createTestingModule({
|
||||
imports: [
|
||||
await TestUnitOfWorkModule.forRoot(db),
|
||||
await createTestMikroOrmModule(db),
|
||||
AuthModule,
|
||||
NativeAuthModule,
|
||||
],
|
||||
})
|
||||
.overrideProvider(MailerService)
|
||||
.useValue({ sendPasswordResetEmail: vi.fn() })
|
||||
.compile();
|
||||
const nest = moduleRef.createNestApplication();
|
||||
nest.use(cookieParser());
|
||||
nest.useGlobalFilters(new TrekExceptionFilter());
|
||||
nest.useGlobalPipes(new ZodValidationPipe());
|
||||
await nest.init();
|
||||
return nest;
|
||||
}
|
||||
|
||||
const handoff = (cookie: string, body: object = { challenge }) =>
|
||||
request(server).post('/api/auth/native/handoff').set('Cookie', cookie).send(body);
|
||||
const exchange = (body: object) => request(server).post('/api/auth/native/exchange').send(body);
|
||||
|
||||
beforeAll(async () => {
|
||||
orm = await createTestOrm(db);
|
||||
userId = (await makeUser(orm, { username: 'native-e2e', email: 'native-e2e@example.test' })).user.id;
|
||||
app = await build();
|
||||
server = app.getHttpServer();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await resetRateLimits(app);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
await orm.close();
|
||||
});
|
||||
|
||||
describe('Native sign-in hand-off e2e', () => {
|
||||
let server: Server;
|
||||
let app: Awaited<ReturnType<typeof build>>;
|
||||
let userId: number;
|
||||
|
||||
async function build() {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [DatabaseModule, AuthModule, NativeAuthModule] })
|
||||
.overrideProvider(MailerService)
|
||||
.useValue({ sendPasswordResetEmail: vi.fn() })
|
||||
.compile();
|
||||
const nest = moduleRef.createNestApplication();
|
||||
nest.use(cookieParser());
|
||||
nest.useGlobalFilters(new TrekExceptionFilter());
|
||||
nest.useGlobalPipes(new ZodValidationPipe());
|
||||
await nest.init();
|
||||
return nest;
|
||||
}
|
||||
|
||||
const handoff = (cookie: string, body: unknown = { challenge }) =>
|
||||
request(server).post('/api/auth/native/handoff').set('Cookie', cookie).send(body as object);
|
||||
|
||||
beforeAll(async () => {
|
||||
createTables(db as never);
|
||||
runMigrations(db as never);
|
||||
userId = createUser(db as never, { username: 'native-e2e' }).user.id;
|
||||
app = await build();
|
||||
server = app.getHttpServer();
|
||||
});
|
||||
|
||||
beforeEach(() => resetRateLimits(app));
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('carries a browser session into a fresh session cookie for the app', async () => {
|
||||
it('NATIVE-E2E-001: carries a browser session into a fresh session cookie for the app', async () => {
|
||||
const minted = await handoff(sessionCookie(userId));
|
||||
expect(minted.status).toBe(200);
|
||||
expect(typeof minted.body.code).toBe('string');
|
||||
|
||||
const redeemed = await request(server).post('/api/auth/native/exchange').send({ code: minted.body.code, verifier });
|
||||
const redeemed = await exchange({ code: minted.body.code, verifier });
|
||||
expect(redeemed.status).toBe(200);
|
||||
expect(redeemed.body).toEqual({ ok: true });
|
||||
expect(redeemed.body).toEqual({ success: true });
|
||||
const cookie = (redeemed.headers['set-cookie'] as unknown as string[]).find((c) => c.startsWith('trek_session='));
|
||||
expect(cookie).toMatch(/HttpOnly/);
|
||||
expect(cookie).toMatch(/Max-Age=\d+/);
|
||||
|
||||
const me = await request(server).get('/api/auth/me').set('Cookie', cookie!.split(';')[0]!);
|
||||
const me = await request(server).get('/api/auth/me').set('Cookie', String(cookie).split(';')[0]);
|
||||
expect(me.status).toBe(200);
|
||||
expect(me.body.user.id).toBe(userId);
|
||||
|
||||
const audited = db.prepare("SELECT details FROM audit_log WHERE action = 'user.login' AND user_id = ? ORDER BY id DESC LIMIT 1").get(userId) as { details: string };
|
||||
expect(JSON.parse(audited.details)).toEqual({ method: 'native_app' });
|
||||
const logins = await findRows(orm, AuditLog, { user: userId, action: 'user.login' });
|
||||
expect(logins.map((row) => JSON.parse(row.details ?? '{}'))).toContainEqual({ method: 'native_app' });
|
||||
});
|
||||
|
||||
it('refuses to mint a code without a cookie session, even for a valid bearer token', async () => {
|
||||
const res = await request(server).post('/api/auth/native/handoff')
|
||||
it('NATIVE-E2E-002: refuses to mint a code without a cookie session, even for a valid bearer token', async () => {
|
||||
const res = await request(server)
|
||||
.post('/api/auth/native/handoff')
|
||||
.set('Authorization', `Bearer ${signSession(userId)}`)
|
||||
.send({ challenge });
|
||||
expect(res.status).toBe(401);
|
||||
expect(res.body.code).toBe('COOKIE_AUTH_REQUIRED');
|
||||
});
|
||||
|
||||
it('validates the challenge shape', async () => {
|
||||
it('NATIVE-E2E-003: validates the challenge shape', async () => {
|
||||
expect((await handoff(sessionCookie(userId), { challenge: 'not-a-hash' })).status).toBe(400);
|
||||
});
|
||||
|
||||
it('refuses a code presented with the wrong verifier, and the code is gone after', async () => {
|
||||
const { code } = (await handoff(sessionCookie(userId))).body;
|
||||
const wrong = await request(server).post('/api/auth/native/exchange').send({ code, verifier: 'x'.repeat(43) });
|
||||
it('NATIVE-E2E-004: refuses a code presented with the wrong verifier, and the code is gone after', async () => {
|
||||
const { code } = (await handoff(sessionCookie(userId))).body as { code: string };
|
||||
const wrong = await exchange({ code, verifier: 'x'.repeat(43) });
|
||||
expect(wrong.status).toBe(400);
|
||||
expect(wrong.body.error).toBe('Invalid or expired code');
|
||||
expect(wrong.body).toEqual({ error: 'Invalid or expired code' });
|
||||
expect(wrong.headers['set-cookie']).toBeUndefined();
|
||||
|
||||
const retry = await request(server).post('/api/auth/native/exchange').send({ code, verifier });
|
||||
const retry = await exchange({ code, verifier });
|
||||
expect(retry.status).toBe(400);
|
||||
});
|
||||
|
||||
it('rate-limits the exchange per ip', async () => {
|
||||
it('NATIVE-E2E-005: rate-limits the exchange per ip', async () => {
|
||||
for (let i = 0; i < 20; i++) {
|
||||
await request(server).post('/api/auth/native/exchange').send({ code: `nope-${i}`, verifier });
|
||||
await exchange({ code: `nope-${i}`, verifier });
|
||||
}
|
||||
const blocked = await request(server).post('/api/auth/native/exchange').send({ code: 'nope', verifier });
|
||||
const blocked = await exchange({ code: 'nope', verifier });
|
||||
expect(blocked.status).toBe(429);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user