Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(oauth): issue tokens on a schema without oauth_tokens.grant_type (GST-359 C1)

garrytan9f910aa33f48
diff --git a/src/core/oauth-grants.ts b/src/core/oauth-grants.ts
index f5404c71ff..f62978ad06 100644
--- a/src/core/oauth-grants.ts
+++ b/src/core/oauth-grants.ts
@@ -346,20 +346,29 @@ export class OAuthGrants {
});
}
- /** GST-359 C1: each token records the grant that issued it (oauth_tokens.grant_type); only client_credentials tokens may attest an actor. */
+ /**
+ * GST-359 C1: each token records the grant that issued it (oauth_tokens.grant_type); only client_credentials tokens
+ * may attest an actor. grant_type is migration-only (v283, beside oauth_clients.attest_actor), so a schema without it
+ * issues unmarked tokens, which never attest. The locked client row tells which: a failed INSERT would abort the
+ * surrounding transaction, so the schema is inspected before the statement.
+ */
private async issue(sql: SqlQuery, client: ClientRow, scopes: string[], resource: URL | undefined, refresh: boolean, grant: 'authorization_code' | 'refresh_token' | 'client_credentials'): Promise<OAuthTokens> {
const clientId = client.client_id as string;
const override = Number(client.token_ttl);
const ttl = Math.min(Number.isFinite(override) && override > 0 ? override : this.options.tokenTtl, TOKEN_TTL_MAX_SECONDS);
const now = Math.floor(this.now() / 1000);
+ const marked = 'attest_actor' in client;
+ const insert = (token: string, type: 'access' | 'refresh', expiresAt: number) => marked
+ ? sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource, created_at, grant_type)
+ VALUES (${hashToken(token)}, ${type}, ${clientId}, ${oauthPgArray(scopes)}, ${expiresAt}, ${resource?.toString() ?? null}, to_timestamp(${now}), ${grant})`
+ : sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource, created_at)
+ VALUES (${hashToken(token)}, ${type}, ${clientId}, ${oauthPgArray(scopes)}, ${expiresAt}, ${resource?.toString() ?? null}, to_timestamp(${now}))`;
const access = generateToken('gbrain_at_');
- await sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource, created_at, grant_type)
- VALUES (${hashToken(access)}, ${'access'}, ${clientId}, ${oauthPgArray(scopes)}, ${now + ttl}, ${resource?.toString() ?? null}, to_timestamp(${now}), ${grant})`;
+ await insert(access, 'access', now + ttl);
const result: OAuthTokens = { access_token: access, token_type: 'bearer', expires_in: ttl, scope: scopes.join(' ') };
if (refresh) {
const token = generateToken('gbrain_rt_');
- await sql`INSERT INTO oauth_tokens (token_hash, token_type, client_id, scopes, expires_at, resource, created_at, grant_type)
- VALUES (${hashToken(token)}, ${'refresh'}, ${clientId}, ${oauthPgArray(scopes)}, ${now + this.options.refreshTtl}, ${resource?.toString() ?? null}, to_timestamp(${now}), ${grant})`;
+ await insert(token, 'refresh', now + this.options.refreshTtl);
result.refresh_token = token;
}
return result;
diff --git a/test/schema-bootstrap-coverage.test.ts b/test/schema-bootstrap-coverage.test.ts
index 0b773523bd..0a221ef0f2 100644
--- a/test/schema-bootstrap-coverage.test.ts
+++ b/test/schema-bootstrap-coverage.test.ts
@@ -1208,6 +1208,15 @@ const COLUMN_EXEMPTIONS = new Set<string>([
'access_tokens.may_act_for',
'oauth_clients.member_id',
'oauth_clients.may_act_for',
+ // GST-359 C1 (migration v283) - the attest right and the issuing grant. Same posture as v281: absent from both schema
+ // blobs, so fresh and upgraded brains share their ordinals; no index or function in either blob references them.
+ // Readers treat a missing column as no right (attestRightOf catches, verifiers read to_jsonb(row)), and token
+ // issuance writes grant_type only when the locked client row shows v283 ran, so such tokens never attest.
+ 'access_tokens.attest_actor',
+ 'access_tokens.attest_ops',
+ 'oauth_clients.attest_actor',
+ 'oauth_clients.attest_ops',
+ 'oauth_tokens.grant_type',
// GBRA-218 A2 (migration v282) - folder rules. Same posture as v281: absent from both schema blobs; the migration
// creates the folder_rule_ids trigger and functions itself, and no index or function in either blob references it.
'pages.folder_rule_ids',
diff --git a/test/serve-http-mcp-dispatch-context.serial.test.ts b/test/serve-http-mcp-dispatch-context.serial.test.ts
index 55f0b44f1b..04e2baee3a 100644
--- a/test/serve-http-mcp-dispatch-context.serial.test.ts
+++ b/test/serve-http-mcp-dispatch-context.serial.test.ts
@@ -89,7 +89,7 @@ async function rpc(base: string, token: string, method: string, params?: unknown
return JSON.parse(dataLine ? dataLine.slice('data:'.length) : text).result;
}
-const FULL_SURFACE_KEYS = ['admissionOrigin', 'auth', 'config', 'inboundFrameBytes', 'isListed', 'logger', 'metaHook', 'noticeLedger', 'remote', 'resultRows', 'sourceId', 'surface', 'surfaceCeiling', 'takesHoldersAllowList', 'transport'];
+const FULL_SURFACE_KEYS = ['actor', 'admissionOrigin', 'auth', 'config', 'inboundFrameBytes', 'isListed', 'logger', 'metaHook', 'noticeLedger', 'remote', 'resultRows', 'sourceId', 'surface', 'surfaceCeiling', 'takesHoldersAllowList', 'transport'];
describe('POST /mcp dispatch context', () => {
test('full surface: remote http dispatch with the verified auth and token source scope', async () => {