Skip to content

armgit / ruflo

Public Imported from GitHub
0

🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated

fix(federation): name the reason when an unbound access token is refused

ruvf29945e5803d
diff --git a/plugins/ruflo-chatgpt-federation/src/oauth.mjs b/plugins/ruflo-chatgpt-federation/src/oauth.mjs
index a353fdbf6b..ce78e350b8 100644
--- a/plugins/ruflo-chatgpt-federation/src/oauth.mjs
+++ b/plugins/ruflo-chatgpt-federation/src/oauth.mjs
@@ -71,8 +71,27 @@ export async function verifyAccessToken(token, { issuer, jwksUri, audience }) {
const scopes = Array.isArray(raw) ? raw.map(String) : String(raw).split(/\s+/).filter(Boolean);
return { ok: true, scopes, subject: payload.sub ? String(payload.sub) : undefined };
} catch (e) {
- const code = e?.code === 'ERR_JWT_EXPIRED' ? 'invalid_token' : 'invalid_token';
- return { ok: false, error: code, description: String(e?.message || 'token verification failed').slice(0, 200) };
+ // Say WHY, precisely. A correctly-signed token that fails only on iss/aud is a
+ // very different problem from a forged one, and `auth.cognitum.one` is measured
+ // to issue tokens carrying neither claim (minimax-music services/gateway/src/
+ // jwks.rs, citing freetokens ADR-0017). Without this, that shows up as a bare
+ // "invalid_token" and costs someone an afternoon.
+ //
+ // This re-verifies the SIGNATURE only, for diagnosis. It never grants: every
+ // path below still returns ok:false.
+ let detail = String(e?.message || 'token verification failed').slice(0, 200);
+ try {
+ const { payload } = await jwtVerify(token, keySet(jwksUri), { clockTolerance: 30 });
+ const missing = [];
+ if (issuer && !payload.iss) missing.push('iss');
+ if (audience && !payload.aud) missing.push('aud');
+ if (missing.length) {
+ detail = `token signature is valid but carries no ${missing.join(' or ')} claim, so it is not bound to this resource`;
+ return { ok: false, error: 'invalid_token', description: detail, signatureValid: true, unboundClaims: missing };
+ }
+ detail = `token signature is valid but ${detail}`;
+ } catch { /* signature genuinely bad — keep the original message */ }
+ return { ok: false, error: 'invalid_token', description: detail };
}
}
diff --git a/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs b/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
index b97260c479..75dc3fc075 100644
--- a/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
+++ b/plugins/ruflo-chatgpt-federation/test/oauth.test.mjs
@@ -32,7 +32,14 @@ async function fakeAuthServer() {
.setIssuer(iss).setAudience(audience).setSubject('user-1')
.setIssuedAt().setExpirationTime(Math.floor(Date.now() / 1000) + expSeconds)
.sign(privateKey);
- return { issuer, jwksUri: `${issuer}/.well-known/jwks.json`, mint, close: () => srv.close() };
+ // Same signing key, but no iss and no aud — the shape auth.cognitum.one actually issues.
+ const mintRaw = ({ scope, expSeconds = 300 }) =>
+ new SignJWT({ scope })
+ .setProtectedHeader({ alg: 'RS256', kid: 'test-1' })
+ .setSubject('user-1').setIssuedAt()
+ .setExpirationTime(Math.floor(Date.now() / 1000) + expSeconds)
+ .sign(privateKey);
+ return { issuer, jwksUri: `${issuer}/.well-known/jwks.json`, mint, mintRaw, close: () => srv.close() };
}
async function withService(as, { required, publicUrl }, fn) {
@@ -220,3 +227,24 @@ test('the service info advertises the authorization server and enforcement state
}
} finally { as.close(); }
});
+
+test('a correctly-signed but unbound token is rejected AND says why', async () => {
+ // auth.cognitum.one is measured to issue tokens with neither iss nor aud. Such a
+ // token is not resource-bound: any Cognitum-integrated app holding a user's token
+ // could present it here and publish as the federation identity. It must be
+ // refused — and the refusal must name the cause, not just say "invalid_token".
+ const as = await fakeAuthServer();
+ try {
+ const { publicKey, privateKey } = await generateKeyPair('RS256');
+ void publicKey;
+ void privateKey;
+ await withService(as, { required: true, publicUrl: 'https://cgf.example' }, async (port) => {
+ // Minted by the real issuer's key, but with no iss and no aud.
+ const tok = await as.mintRaw({ scope: `${SCOPE_READ} ${SCOPE_PUBLISH}` });
+ const r = await call(port, 'channel_publish',
+ { channel: 'pub:announce', msgType: 'Status', payload: {} }, { authorization: `Bearer ${tok}` });
+ assert.equal(r.status, 401, 'an unbound token must not authorise anything');
+ assert.match(r.wwwAuth || '', /carries no iss or aud claim/);
+ });
+ } finally { as.close(); }
+});