Skip to content

Commit 19aa4ec

Browse files
authored
Gate gateway auth by client capabilities (#111)
Only advertise the gateway auth method when the client opts in via `clientCapabilities.auth._meta.gateway`. Mirrors the setup we have in claude-agent-acp.
1 parent 4dfef48 commit 19aa4ec

4 files changed

Lines changed: 44 additions & 7 deletions

File tree

src/CodexAcpServer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import {CodexEventHandler} from "./CodexEventHandler";
99
import {CodexApprovalHandler} from "./CodexApprovalHandler";
1010
import {CodexElicitationHandler} from "./CodexElicitationHandler";
11-
import {CodexAuthMethods, type CodexAuthRequest} from "./CodexAuthMethod";
11+
import {getCodexAuthMethods, type CodexAuthRequest} from "./CodexAuthMethod";
1212
import {CodexAcpClient, type SessionMetadata, type SessionMetadataWithThread} from "./CodexAcpClient";
1313
import type {McpStartupResult} from "./CodexAppServerClient";
1414
import {ACPSessionConnection, type UpdateSessionEvent} from "./ACPSessionConnection";
@@ -109,7 +109,7 @@ export class CodexAcpServer implements acp.Agent {
109109
sse: false
110110
}
111111
},
112-
authMethods: CodexAuthMethods,
112+
authMethods: getCodexAuthMethods(_params.clientCapabilities),
113113
};
114114
}
115115

src/CodexAuthMethod.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import type {AuthenticateRequest, AuthMethod} from "@agentclientprotocol/sdk";
2+
import type {AuthenticateRequest, AuthMethod, ClientCapabilities} from "@agentclientprotocol/sdk";
33

44
const ApiKeyAuthMethod: AuthMethod = {
55
id: "api-key",
@@ -54,7 +54,14 @@ export interface GatewayAuthRequest extends AuthenticateRequest {
5454
};
5555
}
5656

57-
export const CodexAuthMethods: AuthMethod[] = [ApiKeyAuthMethod, ChatGptAuthMethod, GatewayAuthMethod];
57+
export function getCodexAuthMethods(clientCapabilities?: ClientCapabilities | null): AuthMethod[] {
58+
const authMethods: AuthMethod[] = [ApiKeyAuthMethod, ChatGptAuthMethod];
59+
const supportsGatewayAuth = clientCapabilities?.auth?._meta?.["gateway"] === true;
60+
if (supportsGatewayAuth) {
61+
authMethods.push(GatewayAuthMethod);
62+
}
63+
return authMethods;
64+
}
5865

5966
export type CodexAuthRequest = ApiKeyAuthRequest | ChatGPTAuthRequest | GatewayAuthRequest;
6067

src/__tests__/CodexACPAgent/CodexAcpClient.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,16 @@ describe('ACP server test', { timeout: 40_000 }, () => {
112112
const gatewayFixture = createTestFixture();
113113
const codexAcpAgent = gatewayFixture.getCodexAcpAgent();
114114

115-
await codexAcpAgent.initialize({protocolVersion: 1});
115+
await codexAcpAgent.initialize({
116+
protocolVersion: 1,
117+
clientCapabilities: {
118+
auth: {
119+
_meta: {
120+
gateway: true,
121+
}
122+
}
123+
}
124+
});
116125
await gatewayFixture.getCodexAcpClient().logout();
117126

118127
const authRequest: CodexAuthRequest = {

src/__tests__/CodexACPAgent/initialize.test.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
22
import { CodexAcpServer } from '../../CodexAcpServer';
33
import * as acp from '@agentclientprotocol/sdk';
44
import { createMockConnections } from './test-utils';
5-
import {CodexAuthMethods} from "../../CodexAuthMethod";
5+
import {getCodexAuthMethods} from "../../CodexAuthMethod";
66
import {CodexAcpClient} from "../../CodexAcpClient";
77
import {CodexAppServerClient} from "../../CodexAppServerClient";
88

@@ -45,7 +45,28 @@ describe('CodexACPAgent - initialize', () => {
4545
sse: false,
4646
},
4747
},
48-
authMethods: CodexAuthMethods,
48+
authMethods: getCodexAuthMethods(),
4949
});
5050
});
51+
52+
it('should advertise gateway auth when the client opts into gateway auth metadata', async () => {
53+
const params: acp.InitializeRequest = {
54+
protocolVersion: acp.PROTOCOL_VERSION,
55+
clientCapabilities: {
56+
auth: {
57+
_meta: {
58+
gateway: true,
59+
}
60+
}
61+
}
62+
};
63+
64+
const result = await agent.initialize(params);
65+
66+
expect(result.authMethods).toEqual(expect.arrayContaining([
67+
expect.objectContaining({
68+
id: "gateway",
69+
})
70+
]));
71+
});
5172
});

0 commit comments

Comments
 (0)