refactor: remove dead code (#3337)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-09 01:41:26 +02:00
1 parent a19002652b
commit 93d1f8af46
1527 files changed
+21170 -73846

No files matched your search

@@ -1,43 +0,0 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {timingSafeEqual} from 'node:crypto';
import {UnauthorizedError} from '@fluxer/errors/src/domains/core/UnauthorizedError';
import {matchesAnyExactOrNestedPath} from '@fluxer/hono/src/middleware/utils/PathMatchers';
import type {MiddlewareHandler} from 'hono';
interface InternalAuthOptions {
secret: string;
skipPaths?: Array<string>;
}
function timingSafeCompare(a: string, b: string): boolean {
const bufferA = Buffer.from(a);
const bufferB = Buffer.from(b);
if (bufferA.length !== bufferB.length) {
return false;
}
return timingSafeEqual(bufferA, bufferB);
}
export function createInternalAuth(options: InternalAuthOptions): MiddlewareHandler {
const {secret, skipPaths = ['/_health']} = options;
return async (c, next) => {
const path = c.req.path;
if (matchesAnyExactOrNestedPath(path, skipPaths)) {
await next();
return;
}
const authHeader = c.req.header('Authorization');
if (!authHeader) {
throw new UnauthorizedError({message: 'Missing Authorization header'});
}
if (!authHeader.startsWith('Bearer ')) {
throw new UnauthorizedError({message: 'Invalid Authorization header format'});
}
const token = authHeader.slice(7);
if (!timingSafeCompare(token, secret)) {
throw new UnauthorizedError({message: 'Invalid token'});
}
await next();
};
}
+1 -1
View File
@@ -16,7 +16,7 @@ export interface RateLimitService {
checkLimit(params: {identifier: string; maxAttempts: number; windowMs: number}): Promise<RateLimitResult>;
}
export type KeyGenerator = (request: Request) => string | null | Promise<string | null>;
type KeyGenerator = (request: Request) => string | null | Promise<string | null>;
export interface RateLimitOptions {
enabled?: boolean;
+1 -1
View File
@@ -4,7 +4,7 @@ import {randomUUID} from 'node:crypto';
import {Headers} from '@fluxer/constants/src/Headers';
import type {MiddlewareHandler} from 'hono';
export type RequestIdGenerator = () => string;
type RequestIdGenerator = () => string;
export interface RequestIdOptions {
headerName?: string;
@@ -1,255 +0,0 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {createErrorHandler} from '@fluxer/hono/src/middleware/ErrorHandler';
import {createInternalAuth} from '@fluxer/hono/src/middleware/InternalAuth';
import {Hono} from 'hono';
import {describe, expect, test} from 'vitest';
interface ErrorResponse {
code: string;
message: string;
}
describe('InternalAuth Middleware', () => {
const TEST_SECRET = 'test-secret-token';
describe('missing authorization', () => {
test('returns 401 when no Authorization header is present', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test');
expect(response.status).toBe(401);
const body = (await response.json()) as ErrorResponse;
expect(body.code).toBe('UNAUTHORIZED');
expect(body.message).toBe('Missing Authorization header');
});
});
describe('invalid authorization format', () => {
test('returns 401 when Authorization header does not start with Bearer', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test', {
headers: {
Authorization: 'Basic dXNlcjpwYXNz',
},
});
expect(response.status).toBe(401);
const body = (await response.json()) as ErrorResponse;
expect(body.code).toBe('UNAUTHORIZED');
expect(body.message).toBe('Invalid Authorization header format');
});
test('returns 401 when Authorization header is just Bearer without token', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test', {
headers: {
Authorization: 'Bearer',
},
});
expect(response.status).toBe(401);
const body = (await response.json()) as ErrorResponse;
expect(body.code).toBe('UNAUTHORIZED');
expect(body.message).toBe('Invalid Authorization header format');
});
});
describe('invalid token', () => {
test('returns 401 when token does not match secret', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test', {
headers: {
Authorization: 'Bearer wrong-token',
},
});
expect(response.status).toBe(401);
const body = (await response.json()) as ErrorResponse;
expect(body.code).toBe('UNAUTHORIZED');
expect(body.message).toBe('Invalid token');
});
test('returns 401 when token is empty after Bearer prefix', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test', {
headers: {
Authorization: 'Bearer ',
},
});
expect(response.status).toBe(401);
const body = (await response.json()) as ErrorResponse;
expect(body.code).toBe('UNAUTHORIZED');
expect(body.message).toBe('Invalid Authorization header format');
});
test('returns 401 when token has different length than secret', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test', {
headers: {
Authorization: 'Bearer short',
},
});
expect(response.status).toBe(401);
const body = (await response.json()) as ErrorResponse;
expect(body.code).toBe('UNAUTHORIZED');
expect(body.message).toBe('Invalid token');
});
});
describe('valid token', () => {
test('allows request through when token matches secret', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const response = await app.request('/test', {
headers: {
Authorization: `Bearer ${TEST_SECRET}`,
},
});
expect(response.status).toBe(200);
const body = (await response.json()) as {
ok: boolean;
};
expect(body.ok).toBe(true);
});
test('allows POST requests with valid token', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.post('/test', (c) => c.json({created: true}));
const response = await app.request('/test', {
method: 'POST',
headers: {
Authorization: `Bearer ${TEST_SECRET}`,
},
});
expect(response.status).toBe(200);
const body = (await response.json()) as {
created: boolean;
};
expect(body.created).toBe(true);
});
});
describe('skipPaths option', () => {
test('skips auth for exact path match in skipPaths', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET, skipPaths: ['/public']}));
app.onError(createErrorHandler());
app.get('/public', (c) => c.json({ok: true}));
const response = await app.request('/public');
expect(response.status).toBe(200);
const body = (await response.json()) as {
ok: boolean;
};
expect(body.ok).toBe(true);
});
test('skips auth for paths starting with skipPath prefix', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET, skipPaths: ['/public']}));
app.onError(createErrorHandler());
app.get('/public/nested', (c) => c.json({ok: true}));
const response = await app.request('/public/nested');
expect(response.status).toBe(200);
const body = (await response.json()) as {
ok: boolean;
};
expect(body.ok).toBe(true);
});
test('does not skip auth for paths that do not match skipPaths', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET, skipPaths: ['/public']}));
app.onError(createErrorHandler());
app.get('/private', (c) => c.json({ok: true}));
const response = await app.request('/private');
expect(response.status).toBe(401);
});
test('supports multiple skipPaths', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET, skipPaths: ['/public', '/health']}));
app.onError(createErrorHandler());
app.get('/public', (c) => c.json({ok: true}));
app.get('/health', (c) => c.json({ok: true}));
app.get('/private', (c) => c.json({ok: true}));
const publicResponse = await app.request('/public');
expect(publicResponse.status).toBe(200);
const healthResponse = await app.request('/health');
expect(healthResponse.status).toBe(200);
const privateResponse = await app.request('/private');
expect(privateResponse.status).toBe(401);
});
});
describe('default skipPaths', () => {
test('default skipPaths includes /_health', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/_health', (c) => c.json({ok: true}));
const response = await app.request('/_health');
expect(response.status).toBe(200);
const body = (await response.json()) as {
ok: boolean;
};
expect(body.ok).toBe(true);
});
test('default skipPaths allows nested /_health paths', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/_health/detailed', (c) => c.json({ok: true}));
const response = await app.request('/_health/detailed');
expect(response.status).toBe(200);
const body = (await response.json()) as {
ok: boolean;
};
expect(body.ok).toBe(true);
});
test('default skipPaths still requires auth for non-health paths', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/api', (c) => c.json({ok: true}));
const response = await app.request('/api');
expect(response.status).toBe(401);
});
});
describe('multiple routes', () => {
test('handles multiple routes with proper auth', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/api/users', (c) => c.json({users: []}));
app.get('/api/posts', (c) => c.json({posts: []}));
const usersResponse = await app.request('/api/users', {
headers: {Authorization: `Bearer ${TEST_SECRET}`},
});
expect(usersResponse.status).toBe(200);
const postsResponse = await app.request('/api/posts', {
headers: {Authorization: `Bearer ${TEST_SECRET}`},
});
expect(postsResponse.status).toBe(200);
});
});
describe('error recovery', () => {
test('does not affect subsequent successful requests', async () => {
const app = new Hono();
app.use('*', createInternalAuth({secret: TEST_SECRET}));
app.onError(createErrorHandler());
app.get('/test', (c) => c.json({ok: true}));
const errorResponse = await app.request('/test');
expect(errorResponse.status).toBe(401);
const successResponse = await app.request('/test', {
headers: {Authorization: `Bearer ${TEST_SECRET}`},
});
expect(successResponse.status).toBe(200);
});
});
});
@@ -15,16 +15,3 @@ export function matchesAnyPathPattern(path: string, patterns: Array<string>): bo
}
return false;
}
function matchesExactOrNestedPath(path: string, prefix: string): boolean {
return path === prefix || path.startsWith(`${prefix}/`);
}
export function matchesAnyExactOrNestedPath(path: string, prefixes: Array<string>): boolean {
for (const prefix of prefixes) {
if (matchesExactOrNestedPath(path, prefix)) {
return true;
}
}
return false;
}