UserApiKey (apiKeyHash->userId); CLI 'user apikey --email'; POST
/auth/agent/login {apiKey} -> normal user session (api-key-guard exempt).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
73 lines
2.4 KiB
TypeScript
73 lines
2.4 KiB
TypeScript
import 'reflect-metadata';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { AppModule } from './app.module';
|
|
import { AuthService } from './auth/auth.service';
|
|
import { NodeAdminService } from './nodes/node-admin.service';
|
|
|
|
function getArg(flag: string): string | null {
|
|
const idx = process.argv.indexOf(flag);
|
|
if (idx === -1) return null;
|
|
return process.argv[idx + 1] ?? null;
|
|
}
|
|
|
|
function printUsageAndExit(): never {
|
|
console.error('Usage:');
|
|
console.error(' node dist/cli.js user create --email <email> --password <password>');
|
|
console.error(' node dist/cli.js user apikey --email <email> [--label <label>]');
|
|
console.error(' node dist/cli.js node register --node-id <id> --name <name> --endpoint <url>');
|
|
process.exit(1);
|
|
}
|
|
|
|
async function main() {
|
|
const [subject, action] = process.argv.slice(2);
|
|
if (!subject || !action) printUsageAndExit();
|
|
|
|
const app = await NestFactory.createApplicationContext(AppModule, { logger: ['error', 'warn'] });
|
|
try {
|
|
if (subject === 'user' && action === 'create') {
|
|
const email = getArg('--email');
|
|
const password = getArg('--password');
|
|
if (!email || !password) printUsageAndExit();
|
|
|
|
const auth = app.get(AuthService);
|
|
const user = await auth.register({ email, password });
|
|
process.stdout.write(JSON.stringify({ ok: true, user }) + '\n');
|
|
return;
|
|
}
|
|
|
|
if (subject === 'user' && action === 'apikey') {
|
|
const email = getArg('--email');
|
|
const label = getArg('--label');
|
|
if (!email) printUsageAndExit();
|
|
|
|
const auth = app.get(AuthService);
|
|
const res = await auth.createUserApiKey(email, label ?? undefined);
|
|
process.stdout.write(JSON.stringify({ ok: true, ...res }) + '\n');
|
|
return;
|
|
}
|
|
|
|
if (subject === 'node' && action === 'register') {
|
|
const nodeId = getArg('--node-id');
|
|
const name = getArg('--name');
|
|
const endpoint = getArg('--endpoint');
|
|
if (!nodeId || !name || !endpoint) printUsageAndExit();
|
|
|
|
const nodes = app.get(NodeAdminService);
|
|
const result = await nodes.registerNode({ nodeId, name, endpoint });
|
|
process.stdout.write(JSON.stringify({ ok: true, ...result }) + '\n');
|
|
return;
|
|
}
|
|
|
|
printUsageAndExit();
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
}
|
|
|
|
void main().catch((error: unknown) => {
|
|
const message = error instanceof Error ? error.message : 'unknown error';
|
|
process.stderr.write(JSON.stringify({ ok: false, error: message }) + '\n');
|
|
process.exit(1);
|
|
});
|
|
|