fix: revalidate authentication before bot start
This commit is contained in:
1 parent
8ea1cf16d4
commit
11b6300113
5 files changed
+101
-13
No files matched your search
+14
-3
@@ -12,12 +12,13 @@ export function botControlStatus(ctx: WebContext): ControlResponse {
|
||||
};
|
||||
}
|
||||
|
||||
export function setBotRunning(
|
||||
export async function setBotRunning(
|
||||
ctx: WebContext,
|
||||
running: boolean,
|
||||
actor: 'dashboard' | 'agent' = 'dashboard',
|
||||
): ControlResponse {
|
||||
const authenticated = hasAuthenticatedAccount(ctx);
|
||||
): Promise<ControlResponse> {
|
||||
const authenticated = hasAuthenticatedAccount(ctx)
|
||||
|| (running && await ensureAuthenticatedAccount(ctx));
|
||||
if (running && !authenticated) {
|
||||
ctx.logger.warn(`Bot start blocked for ${actor} until full account authentication`);
|
||||
return {
|
||||
@@ -60,3 +61,13 @@ function hasAuthenticatedAccount(ctx: WebContext): boolean {
|
||||
return ctx.services.accountRuntimes?.get(snapshot.accountId)?.resources.accountAuth.isFullyAuthenticated() ?? false;
|
||||
});
|
||||
}
|
||||
|
||||
async function ensureAuthenticatedAccount(ctx: WebContext): Promise<boolean> {
|
||||
if (ctx.services.accountAuth && await ctx.services.accountAuth.ensureFullyAuthenticated()) return true;
|
||||
for (const snapshot of ctx.services.accountRuntimes?.list() ?? []) {
|
||||
if (snapshot.state !== 'running') continue;
|
||||
const auth = ctx.services.accountRuntimes?.get(snapshot.accountId)?.resources.accountAuth;
|
||||
if (auth && await auth.ensureFullyAuthenticated()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -54,20 +54,20 @@ export function createAgentRouter(ctx: WebContext): Router {
|
||||
});
|
||||
|
||||
router.get('/v1/control', (_req, res) => res.json(botControlStatus(ctx)));
|
||||
router.post('/v1/control', (req, res) => {
|
||||
router.post('/v1/control', async (req, res) => {
|
||||
const body = ControlRequestSchema.safeParse(req.body);
|
||||
if (!body.success) {
|
||||
res.status(400).json({ error: 'Invalid control request', detail: body.error.format() });
|
||||
return;
|
||||
}
|
||||
const result = setBotRunning(ctx, body.data.running, 'agent');
|
||||
const result = await setBotRunning(ctx, body.data.running, 'agent');
|
||||
res.status(result.success ? 200 : 409).json(result);
|
||||
});
|
||||
router.post('/v1/control/start', (_req, res) => {
|
||||
const result = setBotRunning(ctx, true, 'agent');
|
||||
router.post('/v1/control/start', async (_req, res) => {
|
||||
const result = await setBotRunning(ctx, true, 'agent');
|
||||
res.status(result.success ? 200 : 409).json(result);
|
||||
});
|
||||
router.post('/v1/control/stop', (_req, res) => res.json(setBotRunning(ctx, false, 'agent')));
|
||||
router.post('/v1/control/stop', async (_req, res) => res.json(await setBotRunning(ctx, false, 'agent')));
|
||||
|
||||
router.get('/v1/jobs', (req, res) => {
|
||||
const limit = Number.parseInt(String(req.query.limit ?? '20'), 10);
|
||||
|
||||
@@ -5,15 +5,15 @@ import { setBotRunning } from '../bot-control.js';
|
||||
export function createControlRouter(ctx: WebContext): Router {
|
||||
const router = Router();
|
||||
|
||||
router.post('/start', (_req, res) => {
|
||||
const result = setBotRunning(ctx, true);
|
||||
router.post('/start', async (_req, res) => {
|
||||
const result = await setBotRunning(ctx, true);
|
||||
res.status(result.success ? 200 : 409).json(result.success
|
||||
? { success: true, running: result.running }
|
||||
: { success: false, running: false, authenticated: false });
|
||||
});
|
||||
|
||||
router.post('/stop', (_req, res) => {
|
||||
const result = setBotRunning(ctx, false);
|
||||
router.post('/stop', async (_req, res) => {
|
||||
const result = await setBotRunning(ctx, false);
|
||||
res.json({ success: true, running: result.running });
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user