stock-bot/libs/trading-runtime/src/paper-operator.ts

23 lines
891 B
TypeScript

import { getCredentialHttpPrincipal } from '@stock-bot/di';
import { PaperDeploymentError } from './paper-deployment-custody';
/** Defense in depth for both public services, including bearer requests carrying browser Origin. */
export function requirePaperOperator(request: Request, origins: readonly string[]) {
if (getCredentialHttpPrincipal(request) !== 'operator') {
throw new PaperDeploymentError(
403,
'Credential-backed human operator required for paper commands'
);
}
const origin = request.headers.get('origin');
if (origin && (!origins.length || !origins.includes(origin))) {
throw new PaperDeploymentError(
403,
'Paper commands require an explicitly configured trusted Origin'
);
}
}
export const paperAllowedOrigins = () =>
process.env.STOCK_BOT_ALLOWED_ORIGINS?.split(',')
.map(v => v.trim())
.filter(Boolean) ?? [];