@clinebot/core 0.0.18 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/account/cline-account-service.d.ts +3 -2
- package/dist/account/cline-account-service.d.ts.map +1 -0
- package/dist/account/index.d.ts +1 -0
- package/dist/account/index.d.ts.map +1 -0
- package/dist/account/rpc.d.ts +1 -0
- package/dist/account/rpc.d.ts.map +1 -0
- package/dist/account/types.d.ts +1 -0
- package/dist/account/types.d.ts.map +1 -0
- package/dist/agents/agent-config-loader.d.ts +1 -0
- package/dist/agents/agent-config-loader.d.ts.map +1 -0
- package/dist/agents/agent-config-parser.d.ts +1 -0
- package/dist/agents/agent-config-parser.d.ts.map +1 -0
- package/dist/agents/hooks-config-loader.d.ts +1 -0
- package/dist/agents/hooks-config-loader.d.ts.map +1 -0
- package/dist/agents/index.d.ts +1 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/plugin-config-loader.d.ts +1 -0
- package/dist/agents/plugin-config-loader.d.ts.map +1 -0
- package/dist/agents/plugin-loader.d.ts +1 -0
- package/dist/agents/plugin-loader.d.ts.map +1 -0
- package/dist/agents/plugin-sandbox.d.ts +1 -0
- package/dist/agents/plugin-sandbox.d.ts.map +1 -0
- package/dist/agents/unified-config-file-watcher.d.ts +1 -0
- package/dist/agents/unified-config-file-watcher.d.ts.map +1 -0
- package/dist/agents/user-instruction-config-loader.d.ts +1 -0
- package/dist/agents/user-instruction-config-loader.d.ts.map +1 -0
- package/dist/auth/client.d.ts +1 -0
- package/dist/auth/client.d.ts.map +1 -0
- package/dist/auth/cline.d.ts +1 -0
- package/dist/auth/cline.d.ts.map +1 -0
- package/dist/auth/codex.d.ts +1 -0
- package/dist/auth/codex.d.ts.map +1 -0
- package/dist/auth/oca.d.ts +1 -0
- package/dist/auth/oca.d.ts.map +1 -0
- package/dist/auth/server.d.ts +1 -0
- package/dist/auth/server.d.ts.map +1 -0
- package/dist/auth/types.d.ts +1 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/utils.d.ts +1 -0
- package/dist/auth/utils.d.ts.map +1 -0
- package/dist/chat/chat-schema.d.ts +13 -12
- package/dist/chat/chat-schema.d.ts.map +1 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -0
- package/dist/index.node.d.ts +2 -0
- package/dist/index.node.d.ts.map +1 -0
- package/dist/index.node.js +303 -302
- package/dist/input/file-indexer.d.ts +1 -0
- package/dist/input/file-indexer.d.ts.map +1 -0
- package/dist/input/index.d.ts +1 -0
- package/dist/input/index.d.ts.map +1 -0
- package/dist/input/mention-enricher.d.ts +1 -0
- package/dist/input/mention-enricher.d.ts.map +1 -0
- package/dist/mcp/config-loader.d.ts +1 -0
- package/dist/mcp/config-loader.d.ts.map +1 -0
- package/dist/mcp/index.d.ts +1 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/manager.d.ts +1 -0
- package/dist/mcp/manager.d.ts.map +1 -0
- package/dist/mcp/types.d.ts +1 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/providers/local-provider-registry.d.ts +36 -0
- package/dist/providers/local-provider-registry.d.ts.map +1 -0
- package/dist/providers/local-provider-service.d.ts +2 -1
- package/dist/providers/local-provider-service.d.ts.map +1 -0
- package/dist/runtime/commands.d.ts +1 -0
- package/dist/runtime/commands.d.ts.map +1 -0
- package/dist/runtime/hook-file-hooks.d.ts +1 -0
- package/dist/runtime/hook-file-hooks.d.ts.map +1 -0
- package/dist/runtime/rules.d.ts +1 -0
- package/dist/runtime/rules.d.ts.map +1 -0
- package/dist/runtime/runtime-builder.d.ts +1 -0
- package/dist/runtime/runtime-builder.d.ts.map +1 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts +1 -0
- package/dist/runtime/sandbox/subprocess-sandbox.d.ts.map +1 -0
- package/dist/runtime/session-runtime.d.ts +2 -0
- package/dist/runtime/session-runtime.d.ts.map +1 -0
- package/dist/runtime/skills.d.ts +1 -0
- package/dist/runtime/skills.d.ts.map +1 -0
- package/dist/runtime/tool-approval.d.ts +1 -0
- package/dist/runtime/tool-approval.d.ts.map +1 -0
- package/dist/runtime/workflows.d.ts +1 -0
- package/dist/runtime/workflows.d.ts.map +1 -0
- package/dist/session/default-session-manager.d.ts +4 -0
- package/dist/session/default-session-manager.d.ts.map +1 -0
- package/dist/session/file-session-service.d.ts +1 -0
- package/dist/session/file-session-service.d.ts.map +1 -0
- package/dist/session/rpc-session-service.d.ts +1 -0
- package/dist/session/rpc-session-service.d.ts.map +1 -0
- package/dist/session/rpc-spawn-lease.d.ts +1 -0
- package/dist/session/rpc-spawn-lease.d.ts.map +1 -0
- package/dist/session/runtime-oauth-token-manager.d.ts +1 -0
- package/dist/session/runtime-oauth-token-manager.d.ts.map +1 -0
- package/dist/session/session-agent-events.d.ts +20 -1
- package/dist/session/session-agent-events.d.ts.map +1 -0
- package/dist/session/session-artifacts.d.ts +1 -0
- package/dist/session/session-artifacts.d.ts.map +1 -0
- package/dist/session/session-config-builder.d.ts +1 -0
- package/dist/session/session-config-builder.d.ts.map +1 -0
- package/dist/session/session-graph.d.ts +1 -0
- package/dist/session/session-graph.d.ts.map +1 -0
- package/dist/session/session-host.d.ts +1 -0
- package/dist/session/session-host.d.ts.map +1 -0
- package/dist/session/session-manager.d.ts +1 -0
- package/dist/session/session-manager.d.ts.map +1 -0
- package/dist/session/session-manifest.d.ts +2 -1
- package/dist/session/session-manifest.d.ts.map +1 -0
- package/dist/session/session-service.d.ts +1 -0
- package/dist/session/session-service.d.ts.map +1 -0
- package/dist/session/session-team-coordination.d.ts +1 -0
- package/dist/session/session-team-coordination.d.ts.map +1 -0
- package/dist/session/session-telemetry.d.ts +3 -1
- package/dist/session/session-telemetry.d.ts.map +1 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts +1 -0
- package/dist/session/sqlite-rpc-session-backend.d.ts.map +1 -0
- package/dist/session/unified-session-persistence-service.d.ts +1 -0
- package/dist/session/unified-session-persistence-service.d.ts.map +1 -0
- package/dist/session/utils/helpers.d.ts +1 -0
- package/dist/session/utils/helpers.d.ts.map +1 -0
- package/dist/session/utils/types.d.ts +1 -0
- package/dist/session/utils/types.d.ts.map +1 -0
- package/dist/session/utils/usage.d.ts +1 -0
- package/dist/session/utils/usage.d.ts.map +1 -0
- package/dist/session/workspace-manager.d.ts +1 -0
- package/dist/session/workspace-manager.d.ts.map +1 -0
- package/dist/session/workspace-manifest.d.ts +1 -0
- package/dist/session/workspace-manifest.d.ts.map +1 -0
- package/dist/storage/file-team-store.d.ts +1 -0
- package/dist/storage/file-team-store.d.ts.map +1 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts +1 -0
- package/dist/storage/provider-settings-legacy-migration.d.ts.map +1 -0
- package/dist/storage/provider-settings-manager.d.ts +1 -0
- package/dist/storage/provider-settings-manager.d.ts.map +1 -0
- package/dist/storage/sqlite-session-store.d.ts +1 -0
- package/dist/storage/sqlite-session-store.d.ts.map +1 -0
- package/dist/storage/sqlite-team-store.d.ts +1 -0
- package/dist/storage/sqlite-team-store.d.ts.map +1 -0
- package/dist/storage/team-store.d.ts +1 -0
- package/dist/storage/team-store.d.ts.map +1 -0
- package/dist/team/index.d.ts +1 -0
- package/dist/team/index.d.ts.map +1 -0
- package/dist/team/projections.d.ts +1 -0
- package/dist/team/projections.d.ts.map +1 -0
- package/dist/telemetry/ITelemetryAdapter.d.ts +1 -0
- package/dist/telemetry/ITelemetryAdapter.d.ts.map +1 -0
- package/dist/telemetry/LoggerTelemetryAdapter.d.ts +1 -0
- package/dist/telemetry/LoggerTelemetryAdapter.d.ts.map +1 -0
- package/dist/telemetry/OpenTelemetryAdapter.d.ts +1 -0
- package/dist/telemetry/OpenTelemetryAdapter.d.ts.map +1 -0
- package/dist/telemetry/OpenTelemetryProvider.d.ts +1 -0
- package/dist/telemetry/OpenTelemetryProvider.d.ts.map +1 -0
- package/dist/telemetry/TelemetryService.d.ts +1 -0
- package/dist/telemetry/TelemetryService.d.ts.map +1 -0
- package/dist/telemetry/core-events.d.ts +55 -22
- package/dist/telemetry/core-events.d.ts.map +1 -0
- package/dist/telemetry/opentelemetry.d.ts +1 -0
- package/dist/telemetry/opentelemetry.d.ts.map +1 -0
- package/dist/tools/constants.d.ts +1 -0
- package/dist/tools/constants.d.ts.map +1 -0
- package/dist/tools/definitions.d.ts +8 -1
- package/dist/tools/definitions.d.ts.map +1 -0
- package/dist/tools/executors/apply-patch-parser.d.ts +1 -0
- package/dist/tools/executors/apply-patch-parser.d.ts.map +1 -0
- package/dist/tools/executors/apply-patch.d.ts +1 -0
- package/dist/tools/executors/apply-patch.d.ts.map +1 -0
- package/dist/tools/executors/bash.d.ts +2 -1
- package/dist/tools/executors/bash.d.ts.map +1 -0
- package/dist/tools/executors/editor.d.ts +1 -0
- package/dist/tools/executors/editor.d.ts.map +1 -0
- package/dist/tools/executors/file-read.d.ts +1 -0
- package/dist/tools/executors/file-read.d.ts.map +1 -0
- package/dist/tools/executors/index.d.ts +14 -7
- package/dist/tools/executors/index.d.ts.map +1 -0
- package/dist/tools/executors/search.d.ts +1 -0
- package/dist/tools/executors/search.d.ts.map +1 -0
- package/dist/tools/executors/web-fetch.d.ts +1 -0
- package/dist/tools/executors/web-fetch.d.ts.map +1 -0
- package/dist/tools/helpers.d.ts +15 -0
- package/dist/tools/helpers.d.ts.map +1 -0
- package/dist/tools/index.d.ts +2 -1
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/model-tool-routing.d.ts +1 -0
- package/dist/tools/model-tool-routing.d.ts.map +1 -0
- package/dist/tools/presets.d.ts +1 -0
- package/dist/tools/presets.d.ts.map +1 -0
- package/dist/tools/schemas.d.ts +41 -0
- package/dist/tools/schemas.d.ts.map +1 -0
- package/dist/tools/types.d.ts +3 -2
- package/dist/tools/types.d.ts.map +1 -0
- package/dist/types/common.d.ts +1 -0
- package/dist/types/common.d.ts.map +1 -0
- package/dist/types/config.d.ts +1 -0
- package/dist/types/config.d.ts.map +1 -0
- package/dist/types/events.d.ts +1 -0
- package/dist/types/events.d.ts.map +1 -0
- package/dist/types/provider-settings.d.ts +1 -0
- package/dist/types/provider-settings.d.ts.map +1 -0
- package/dist/types/sessions.d.ts +1 -0
- package/dist/types/sessions.d.ts.map +1 -0
- package/dist/types/storage.d.ts +1 -0
- package/dist/types/storage.d.ts.map +1 -0
- package/dist/types/workspace.d.ts +1 -0
- package/dist/types/workspace.d.ts.map +1 -0
- package/dist/types.d.ts +1 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +8 -6
- package/src/account/cline-account-service.test.ts +0 -101
- package/src/account/cline-account-service.ts +0 -287
- package/src/account/index.ts +0 -22
- package/src/account/rpc.test.ts +0 -62
- package/src/account/rpc.ts +0 -172
- package/src/account/types.ts +0 -98
- package/src/agents/agent-config-loader.test.ts +0 -236
- package/src/agents/agent-config-loader.ts +0 -108
- package/src/agents/agent-config-parser.ts +0 -198
- package/src/agents/hooks-config-loader.test.ts +0 -20
- package/src/agents/hooks-config-loader.ts +0 -118
- package/src/agents/index.ts +0 -85
- package/src/agents/plugin-config-loader.test.ts +0 -140
- package/src/agents/plugin-config-loader.ts +0 -97
- package/src/agents/plugin-loader.test.ts +0 -228
- package/src/agents/plugin-loader.ts +0 -172
- package/src/agents/plugin-sandbox-bootstrap.ts +0 -445
- package/src/agents/plugin-sandbox.test.ts +0 -317
- package/src/agents/plugin-sandbox.ts +0 -341
- package/src/agents/unified-config-file-watcher.test.ts +0 -196
- package/src/agents/unified-config-file-watcher.ts +0 -483
- package/src/agents/user-instruction-config-loader.test.ts +0 -158
- package/src/agents/user-instruction-config-loader.ts +0 -438
- package/src/auth/client.test.ts +0 -40
- package/src/auth/client.ts +0 -25
- package/src/auth/cline.test.ts +0 -130
- package/src/auth/cline.ts +0 -420
- package/src/auth/codex.test.ts +0 -170
- package/src/auth/codex.ts +0 -491
- package/src/auth/oca.test.ts +0 -215
- package/src/auth/oca.ts +0 -573
- package/src/auth/server.ts +0 -216
- package/src/auth/types.ts +0 -81
- package/src/auth/utils.test.ts +0 -128
- package/src/auth/utils.ts +0 -247
- package/src/chat/chat-schema.ts +0 -82
- package/src/index.node.ts +0 -285
- package/src/index.ts +0 -211
- package/src/input/file-indexer.d.ts +0 -11
- package/src/input/file-indexer.test.ts +0 -127
- package/src/input/file-indexer.ts +0 -327
- package/src/input/index.ts +0 -7
- package/src/input/mention-enricher.test.ts +0 -85
- package/src/input/mention-enricher.ts +0 -122
- package/src/mcp/config-loader.test.ts +0 -238
- package/src/mcp/config-loader.ts +0 -219
- package/src/mcp/index.ts +0 -26
- package/src/mcp/manager.test.ts +0 -106
- package/src/mcp/manager.ts +0 -262
- package/src/mcp/types.ts +0 -88
- package/src/providers/local-provider-service.ts +0 -608
- package/src/runtime/commands.test.ts +0 -98
- package/src/runtime/commands.ts +0 -83
- package/src/runtime/hook-file-hooks.test.ts +0 -237
- package/src/runtime/hook-file-hooks.ts +0 -859
- package/src/runtime/index.ts +0 -37
- package/src/runtime/rules.ts +0 -34
- package/src/runtime/runtime-builder.team-persistence.test.ts +0 -202
- package/src/runtime/runtime-builder.test.ts +0 -371
- package/src/runtime/runtime-builder.ts +0 -589
- package/src/runtime/runtime-parity.test.ts +0 -143
- package/src/runtime/sandbox/subprocess-sandbox.ts +0 -231
- package/src/runtime/session-runtime.ts +0 -46
- package/src/runtime/skills.ts +0 -44
- package/src/runtime/tool-approval.ts +0 -104
- package/src/runtime/workflows.test.ts +0 -119
- package/src/runtime/workflows.ts +0 -45
- package/src/session/default-session-manager.e2e.test.ts +0 -384
- package/src/session/default-session-manager.test.ts +0 -1741
- package/src/session/default-session-manager.ts +0 -1233
- package/src/session/file-session-service.ts +0 -280
- package/src/session/index.ts +0 -42
- package/src/session/rpc-session-service.ts +0 -107
- package/src/session/rpc-spawn-lease.test.ts +0 -49
- package/src/session/rpc-spawn-lease.ts +0 -122
- package/src/session/runtime-oauth-token-manager.test.ts +0 -137
- package/src/session/runtime-oauth-token-manager.ts +0 -272
- package/src/session/session-agent-events.ts +0 -159
- package/src/session/session-artifacts.ts +0 -106
- package/src/session/session-config-builder.ts +0 -113
- package/src/session/session-graph.ts +0 -92
- package/src/session/session-host.test.ts +0 -29
- package/src/session/session-host.ts +0 -242
- package/src/session/session-manager.ts +0 -69
- package/src/session/session-manifest.ts +0 -29
- package/src/session/session-service.team-persistence.test.ts +0 -48
- package/src/session/session-service.ts +0 -673
- package/src/session/session-team-coordination.ts +0 -229
- package/src/session/session-telemetry.ts +0 -95
- package/src/session/sqlite-rpc-session-backend.ts +0 -303
- package/src/session/unified-session-persistence-service.test.ts +0 -85
- package/src/session/unified-session-persistence-service.ts +0 -996
- package/src/session/utils/helpers.ts +0 -139
- package/src/session/utils/types.ts +0 -57
- package/src/session/utils/usage.ts +0 -32
- package/src/session/workspace-manager.ts +0 -98
- package/src/session/workspace-manifest.ts +0 -100
- package/src/storage/artifact-store.ts +0 -1
- package/src/storage/file-team-store.ts +0 -257
- package/src/storage/index.ts +0 -11
- package/src/storage/provider-settings-legacy-migration.test.ts +0 -307
- package/src/storage/provider-settings-legacy-migration.ts +0 -689
- package/src/storage/provider-settings-manager.test.ts +0 -145
- package/src/storage/provider-settings-manager.ts +0 -150
- package/src/storage/session-store.ts +0 -1
- package/src/storage/sqlite-session-store.ts +0 -275
- package/src/storage/sqlite-team-store.ts +0 -454
- package/src/storage/team-store.ts +0 -40
- package/src/team/index.ts +0 -4
- package/src/team/projections.ts +0 -285
- package/src/telemetry/ITelemetryAdapter.ts +0 -94
- package/src/telemetry/LoggerTelemetryAdapter.test.ts +0 -42
- package/src/telemetry/LoggerTelemetryAdapter.ts +0 -114
- package/src/telemetry/OpenTelemetryAdapter.test.ts +0 -157
- package/src/telemetry/OpenTelemetryAdapter.ts +0 -348
- package/src/telemetry/OpenTelemetryProvider.test.ts +0 -113
- package/src/telemetry/OpenTelemetryProvider.ts +0 -322
- package/src/telemetry/TelemetryService.test.ts +0 -134
- package/src/telemetry/TelemetryService.ts +0 -141
- package/src/telemetry/core-events.ts +0 -344
- package/src/telemetry/opentelemetry.ts +0 -20
- package/src/tools/constants.ts +0 -35
- package/src/tools/definitions.test.ts +0 -658
- package/src/tools/definitions.ts +0 -726
- package/src/tools/executors/apply-patch-parser.ts +0 -520
- package/src/tools/executors/apply-patch.ts +0 -359
- package/src/tools/executors/bash.ts +0 -205
- package/src/tools/executors/editor.test.ts +0 -35
- package/src/tools/executors/editor.ts +0 -219
- package/src/tools/executors/file-read.test.ts +0 -49
- package/src/tools/executors/file-read.ts +0 -110
- package/src/tools/executors/index.ts +0 -75
- package/src/tools/executors/search.ts +0 -278
- package/src/tools/executors/web-fetch.ts +0 -259
- package/src/tools/index.ts +0 -168
- package/src/tools/model-tool-routing.test.ts +0 -86
- package/src/tools/model-tool-routing.ts +0 -132
- package/src/tools/presets.test.ts +0 -62
- package/src/tools/presets.ts +0 -168
- package/src/tools/schemas.ts +0 -284
- package/src/tools/types.ts +0 -328
- package/src/types/common.ts +0 -14
- package/src/types/config.ts +0 -84
- package/src/types/events.ts +0 -74
- package/src/types/index.ts +0 -24
- package/src/types/provider-settings.ts +0 -43
- package/src/types/sessions.ts +0 -16
- package/src/types/storage.ts +0 -64
- package/src/types/workspace.ts +0 -7
- package/src/types.ts +0 -128
package/src/auth/codex.ts
DELETED
|
@@ -1,491 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* OpenAI Codex (ChatGPT OAuth) flow
|
|
3
|
-
*
|
|
4
|
-
* NOTE: This module uses Node.js crypto and http for the OAuth callback.
|
|
5
|
-
* It is only intended for CLI use, not browser environments.
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import type { ITelemetryService } from "@clinebot/shared";
|
|
9
|
-
import { nanoid } from "nanoid";
|
|
10
|
-
import {
|
|
11
|
-
captureAuthFailed,
|
|
12
|
-
captureAuthLoggedOut,
|
|
13
|
-
captureAuthStarted,
|
|
14
|
-
captureAuthSucceeded,
|
|
15
|
-
identifyAccount,
|
|
16
|
-
} from "../telemetry/core-events";
|
|
17
|
-
import { startLocalOAuthServer } from "./server.js";
|
|
18
|
-
import type {
|
|
19
|
-
OAuthCredentials,
|
|
20
|
-
OAuthLoginCallbacks,
|
|
21
|
-
OAuthPrompt,
|
|
22
|
-
OAuthProviderInterface,
|
|
23
|
-
} from "./types.js";
|
|
24
|
-
import {
|
|
25
|
-
decodeJwtPayload,
|
|
26
|
-
getProofKey,
|
|
27
|
-
isCredentialLikelyExpired,
|
|
28
|
-
parseAuthorizationInput,
|
|
29
|
-
parseOAuthError,
|
|
30
|
-
resolveAuthorizationCodeInput,
|
|
31
|
-
} from "./utils.js";
|
|
32
|
-
|
|
33
|
-
export const OPENAI_CODEX_OAUTH_CONFIG = {
|
|
34
|
-
authorizationEndpoint: "https://auth.openai.com/oauth/authorize",
|
|
35
|
-
tokenEndpoint: "https://auth.openai.com/oauth/token",
|
|
36
|
-
clientId: "app_EMoamEEZ73f0CkXaXp7hrann",
|
|
37
|
-
redirectUri: "http://localhost:1455/auth/callback",
|
|
38
|
-
scopes: "openid profile email offline_access",
|
|
39
|
-
callbackPort: 1455,
|
|
40
|
-
jwtClaimPath: "https://api.openai.com/auth",
|
|
41
|
-
refreshBufferMs: 5 * 60 * 1000,
|
|
42
|
-
retryableTokenGraceMs: 30 * 1000,
|
|
43
|
-
httpTimeoutMs: 30 * 1000,
|
|
44
|
-
} as const;
|
|
45
|
-
|
|
46
|
-
type CodexTokenSuccess = {
|
|
47
|
-
type: "success";
|
|
48
|
-
access: string;
|
|
49
|
-
refresh: string;
|
|
50
|
-
expires: number;
|
|
51
|
-
email?: string;
|
|
52
|
-
idToken?: string;
|
|
53
|
-
};
|
|
54
|
-
type CodexTokenFailure = { type: "failed" };
|
|
55
|
-
type CodexTokenResult = CodexTokenSuccess | CodexTokenFailure;
|
|
56
|
-
export type RefreshTokenResolution = {
|
|
57
|
-
forceRefresh?: boolean;
|
|
58
|
-
refreshBufferMs?: number;
|
|
59
|
-
retryableTokenGraceMs?: number;
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
type JwtPayload = {
|
|
63
|
-
[OPENAI_CODEX_OAUTH_CONFIG.jwtClaimPath]?: {
|
|
64
|
-
chatgpt_account_id?: string;
|
|
65
|
-
};
|
|
66
|
-
[key: string]: unknown;
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
class OpenAICodexOAuthTokenError extends Error {
|
|
70
|
-
public readonly status?: number;
|
|
71
|
-
public readonly errorCode?: string;
|
|
72
|
-
|
|
73
|
-
constructor(message: string, opts?: { status?: number; errorCode?: string }) {
|
|
74
|
-
super(message);
|
|
75
|
-
this.name = "OpenAICodexOAuthTokenError";
|
|
76
|
-
this.status = opts?.status;
|
|
77
|
-
this.errorCode = opts?.errorCode;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
public isLikelyInvalidGrant(): boolean {
|
|
81
|
-
if (this.errorCode && /invalid_grant/i.test(this.errorCode)) {
|
|
82
|
-
return true;
|
|
83
|
-
}
|
|
84
|
-
if (this.status === 400 || this.status === 401 || this.status === 403) {
|
|
85
|
-
return /invalid_grant|revoked|expired|invalid refresh/i.test(
|
|
86
|
-
this.message,
|
|
87
|
-
);
|
|
88
|
-
}
|
|
89
|
-
return false;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
async function exchangeAuthorizationCode(
|
|
94
|
-
code: string,
|
|
95
|
-
verifier: string,
|
|
96
|
-
redirectUri: string = OPENAI_CODEX_OAUTH_CONFIG.redirectUri,
|
|
97
|
-
): Promise<CodexTokenResult> {
|
|
98
|
-
const response = await fetch(OPENAI_CODEX_OAUTH_CONFIG.tokenEndpoint, {
|
|
99
|
-
method: "POST",
|
|
100
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
101
|
-
body: new URLSearchParams({
|
|
102
|
-
grant_type: "authorization_code",
|
|
103
|
-
client_id: OPENAI_CODEX_OAUTH_CONFIG.clientId,
|
|
104
|
-
code,
|
|
105
|
-
code_verifier: verifier,
|
|
106
|
-
redirect_uri: redirectUri,
|
|
107
|
-
}),
|
|
108
|
-
signal: AbortSignal.timeout(OPENAI_CODEX_OAUTH_CONFIG.httpTimeoutMs),
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
if (!response.ok) {
|
|
112
|
-
return { type: "failed" };
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const json = (await response.json()) as {
|
|
116
|
-
access_token?: string;
|
|
117
|
-
refresh_token?: string;
|
|
118
|
-
expires_in?: number;
|
|
119
|
-
email?: string;
|
|
120
|
-
id_token?: string;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
if (
|
|
124
|
-
!json.access_token ||
|
|
125
|
-
!json.refresh_token ||
|
|
126
|
-
typeof json.expires_in !== "number"
|
|
127
|
-
) {
|
|
128
|
-
return { type: "failed" };
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
return {
|
|
132
|
-
type: "success",
|
|
133
|
-
access: json.access_token,
|
|
134
|
-
refresh: json.refresh_token,
|
|
135
|
-
expires: Date.now() + json.expires_in * 1000,
|
|
136
|
-
email: json.email,
|
|
137
|
-
idToken: json.id_token,
|
|
138
|
-
};
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
async function refreshAccessToken(
|
|
142
|
-
refreshToken: string,
|
|
143
|
-
): Promise<CodexTokenResult> {
|
|
144
|
-
try {
|
|
145
|
-
const response = await fetch(OPENAI_CODEX_OAUTH_CONFIG.tokenEndpoint, {
|
|
146
|
-
method: "POST",
|
|
147
|
-
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
148
|
-
body: new URLSearchParams({
|
|
149
|
-
grant_type: "refresh_token",
|
|
150
|
-
refresh_token: refreshToken,
|
|
151
|
-
client_id: OPENAI_CODEX_OAUTH_CONFIG.clientId,
|
|
152
|
-
}),
|
|
153
|
-
signal: AbortSignal.timeout(OPENAI_CODEX_OAUTH_CONFIG.httpTimeoutMs),
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
if (!response.ok) {
|
|
157
|
-
const text = await response.text().catch(() => "");
|
|
158
|
-
const details = parseOAuthError(text);
|
|
159
|
-
throw new OpenAICodexOAuthTokenError(
|
|
160
|
-
`Token refresh failed: ${response.status}${details.message ? ` - ${details.message}` : ""}`,
|
|
161
|
-
{ status: response.status, errorCode: details.code },
|
|
162
|
-
);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
const json = (await response.json()) as {
|
|
166
|
-
access_token?: string;
|
|
167
|
-
refresh_token?: string;
|
|
168
|
-
expires_in?: number;
|
|
169
|
-
email?: string;
|
|
170
|
-
id_token?: string;
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
if (
|
|
174
|
-
!json.access_token ||
|
|
175
|
-
!json.refresh_token ||
|
|
176
|
-
typeof json.expires_in !== "number"
|
|
177
|
-
) {
|
|
178
|
-
return { type: "failed" };
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return {
|
|
182
|
-
type: "success",
|
|
183
|
-
access: json.access_token,
|
|
184
|
-
refresh: json.refresh_token,
|
|
185
|
-
expires: Date.now() + json.expires_in * 1000,
|
|
186
|
-
email: json.email,
|
|
187
|
-
idToken: json.id_token,
|
|
188
|
-
};
|
|
189
|
-
} catch (error) {
|
|
190
|
-
if (error instanceof OpenAICodexOAuthTokenError) {
|
|
191
|
-
throw error;
|
|
192
|
-
}
|
|
193
|
-
return { type: "failed" };
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async function createAuthorizationFlow(
|
|
198
|
-
originator = "pi",
|
|
199
|
-
): Promise<{ verifier: string; state: string; url: string }> {
|
|
200
|
-
const { verifier, challenge } = await getProofKey();
|
|
201
|
-
const state = nanoid(32);
|
|
202
|
-
|
|
203
|
-
const url = new URL(OPENAI_CODEX_OAUTH_CONFIG.authorizationEndpoint);
|
|
204
|
-
url.searchParams.set("response_type", "code");
|
|
205
|
-
url.searchParams.set("client_id", OPENAI_CODEX_OAUTH_CONFIG.clientId);
|
|
206
|
-
url.searchParams.set("redirect_uri", OPENAI_CODEX_OAUTH_CONFIG.redirectUri);
|
|
207
|
-
url.searchParams.set("scope", OPENAI_CODEX_OAUTH_CONFIG.scopes);
|
|
208
|
-
url.searchParams.set("code_challenge", challenge);
|
|
209
|
-
url.searchParams.set("code_challenge_method", "S256");
|
|
210
|
-
url.searchParams.set("state", state);
|
|
211
|
-
url.searchParams.set("id_token_add_organizations", "true");
|
|
212
|
-
url.searchParams.set("codex_cli_simplified_flow", "true");
|
|
213
|
-
url.searchParams.set("originator", originator);
|
|
214
|
-
|
|
215
|
-
return { verifier, state, url: url.toString() };
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
function resolveCallbackServerConfig(): {
|
|
219
|
-
host: string;
|
|
220
|
-
port: number;
|
|
221
|
-
callbackPath: string;
|
|
222
|
-
redirectUri: string;
|
|
223
|
-
} {
|
|
224
|
-
try {
|
|
225
|
-
const redirect = new URL(OPENAI_CODEX_OAUTH_CONFIG.redirectUri);
|
|
226
|
-
const parsedPort =
|
|
227
|
-
redirect.port.length > 0
|
|
228
|
-
? Number.parseInt(redirect.port, 10)
|
|
229
|
-
: OPENAI_CODEX_OAUTH_CONFIG.callbackPort;
|
|
230
|
-
return {
|
|
231
|
-
host: redirect.hostname || "localhost",
|
|
232
|
-
port: Number.isFinite(parsedPort)
|
|
233
|
-
? parsedPort
|
|
234
|
-
: OPENAI_CODEX_OAUTH_CONFIG.callbackPort,
|
|
235
|
-
callbackPath: redirect.pathname || "/auth/callback",
|
|
236
|
-
redirectUri: redirect.toString(),
|
|
237
|
-
};
|
|
238
|
-
} catch {
|
|
239
|
-
return {
|
|
240
|
-
host: "localhost",
|
|
241
|
-
port: OPENAI_CODEX_OAUTH_CONFIG.callbackPort,
|
|
242
|
-
callbackPath: "/auth/callback",
|
|
243
|
-
redirectUri: OPENAI_CODEX_OAUTH_CONFIG.redirectUri,
|
|
244
|
-
};
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
function getAccountId(accessToken: string, idToken?: string): string | null {
|
|
249
|
-
const payload = (
|
|
250
|
-
idToken ? decodeJwtPayload(idToken) : decodeJwtPayload(accessToken)
|
|
251
|
-
) as JwtPayload | null;
|
|
252
|
-
const fallback = (
|
|
253
|
-
payload ? payload : decodeJwtPayload(accessToken)
|
|
254
|
-
) as JwtPayload | null;
|
|
255
|
-
const auth = fallback?.[OPENAI_CODEX_OAUTH_CONFIG.jwtClaimPath];
|
|
256
|
-
const accountId = auth?.chatgpt_account_id;
|
|
257
|
-
if (typeof accountId === "string" && accountId.length > 0) {
|
|
258
|
-
return accountId;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
const organizations = fallback?.organizations;
|
|
262
|
-
if (Array.isArray(organizations) && organizations.length > 0) {
|
|
263
|
-
const first = organizations[0] as { id?: unknown } | undefined;
|
|
264
|
-
if (typeof first?.id === "string" && first.id.length > 0) {
|
|
265
|
-
return first.id;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const rootAccountId = fallback?.chatgpt_account_id;
|
|
270
|
-
if (typeof rootAccountId === "string" && rootAccountId.length > 0) {
|
|
271
|
-
return rootAccountId;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
return null;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function toCodexCredentials(
|
|
278
|
-
result: CodexTokenSuccess,
|
|
279
|
-
fallback?: OAuthCredentials,
|
|
280
|
-
): OAuthCredentials {
|
|
281
|
-
const accountId =
|
|
282
|
-
getAccountId(result.access, result.idToken) ?? fallback?.accountId;
|
|
283
|
-
if (!accountId) {
|
|
284
|
-
throw new Error("Failed to extract accountId from token");
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
return {
|
|
288
|
-
access: result.access,
|
|
289
|
-
refresh: result.refresh || fallback?.refresh || "",
|
|
290
|
-
expires: result.expires,
|
|
291
|
-
accountId,
|
|
292
|
-
email: result.email ?? fallback?.email,
|
|
293
|
-
metadata: {
|
|
294
|
-
...(fallback?.metadata ?? {}),
|
|
295
|
-
provider: "openai-codex",
|
|
296
|
-
},
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
export async function loginOpenAICodex(options: {
|
|
301
|
-
onAuth: (info: { url: string; instructions?: string }) => void;
|
|
302
|
-
onPrompt: (prompt: OAuthPrompt) => Promise<string>;
|
|
303
|
-
onProgress?: (message: string) => void;
|
|
304
|
-
onManualCodeInput?: () => Promise<string>;
|
|
305
|
-
originator?: string;
|
|
306
|
-
telemetry?: ITelemetryService;
|
|
307
|
-
}): Promise<OAuthCredentials> {
|
|
308
|
-
captureAuthStarted(options.telemetry, "openai-codex");
|
|
309
|
-
const callbackConfig = resolveCallbackServerConfig();
|
|
310
|
-
const { verifier, state, url } = await createAuthorizationFlow(
|
|
311
|
-
options.originator,
|
|
312
|
-
);
|
|
313
|
-
const server = await startLocalOAuthServer({
|
|
314
|
-
host: callbackConfig.host,
|
|
315
|
-
ports: [callbackConfig.port],
|
|
316
|
-
callbackPath: callbackConfig.callbackPath,
|
|
317
|
-
expectedState: state,
|
|
318
|
-
});
|
|
319
|
-
|
|
320
|
-
options.onAuth({
|
|
321
|
-
url,
|
|
322
|
-
instructions: "Continue the authentication process in your browser.",
|
|
323
|
-
});
|
|
324
|
-
|
|
325
|
-
let code: string | undefined;
|
|
326
|
-
try {
|
|
327
|
-
const authResult = await resolveAuthorizationCodeInput({
|
|
328
|
-
waitForCallback: server.waitForCallback,
|
|
329
|
-
cancelWait: server.cancelWait,
|
|
330
|
-
onManualCodeInput: options.onManualCodeInput,
|
|
331
|
-
parseOptions: { allowHashCodeState: true },
|
|
332
|
-
});
|
|
333
|
-
if (authResult.state && authResult.state !== state) {
|
|
334
|
-
throw new Error("State mismatch");
|
|
335
|
-
}
|
|
336
|
-
code = authResult.code;
|
|
337
|
-
|
|
338
|
-
// Fallback to onPrompt if still no code
|
|
339
|
-
if (!code) {
|
|
340
|
-
const input = await options.onPrompt({
|
|
341
|
-
message: "Paste the authorization code (or full redirect URL):",
|
|
342
|
-
});
|
|
343
|
-
const parsed = parseAuthorizationInput(input, {
|
|
344
|
-
allowHashCodeState: true,
|
|
345
|
-
});
|
|
346
|
-
if (parsed.state && parsed.state !== state) {
|
|
347
|
-
throw new Error("State mismatch");
|
|
348
|
-
}
|
|
349
|
-
code = parsed.code;
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (!code) {
|
|
353
|
-
throw new Error("Missing authorization code");
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
const tokenResult = await exchangeAuthorizationCode(
|
|
357
|
-
code,
|
|
358
|
-
verifier,
|
|
359
|
-
callbackConfig.redirectUri,
|
|
360
|
-
);
|
|
361
|
-
if (tokenResult.type !== "success") {
|
|
362
|
-
throw new Error("Token exchange failed");
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
const credentials = toCodexCredentials(tokenResult);
|
|
366
|
-
captureAuthSucceeded(options.telemetry, "openai-codex");
|
|
367
|
-
identifyAccount(options.telemetry, {
|
|
368
|
-
id: credentials.accountId,
|
|
369
|
-
email: credentials.email,
|
|
370
|
-
provider: "openai-codex",
|
|
371
|
-
});
|
|
372
|
-
return credentials;
|
|
373
|
-
} catch (error) {
|
|
374
|
-
captureAuthFailed(
|
|
375
|
-
options.telemetry,
|
|
376
|
-
"openai-codex",
|
|
377
|
-
error instanceof Error ? error.message : String(error),
|
|
378
|
-
);
|
|
379
|
-
throw error;
|
|
380
|
-
} finally {
|
|
381
|
-
server.close();
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
export async function refreshOpenAICodexToken(
|
|
386
|
-
refreshToken: string,
|
|
387
|
-
fallback?: OAuthCredentials,
|
|
388
|
-
): Promise<OAuthCredentials> {
|
|
389
|
-
const result = await refreshAccessToken(refreshToken);
|
|
390
|
-
if (result.type !== "success") {
|
|
391
|
-
throw new Error("Failed to refresh OpenAI Codex token");
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
const normalized = toCodexCredentials(result, fallback);
|
|
395
|
-
if (!normalized.refresh) {
|
|
396
|
-
throw new Error(
|
|
397
|
-
"Failed to refresh OpenAI Codex token: missing refresh token",
|
|
398
|
-
);
|
|
399
|
-
}
|
|
400
|
-
return normalized;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
export async function getValidOpenAICodexCredentials(
|
|
404
|
-
currentCredentials: OAuthCredentials | null,
|
|
405
|
-
options?: RefreshTokenResolution & { telemetry?: ITelemetryService },
|
|
406
|
-
): Promise<OAuthCredentials | null> {
|
|
407
|
-
if (!currentCredentials) {
|
|
408
|
-
return null;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
const refreshBufferMs =
|
|
412
|
-
options?.refreshBufferMs ?? OPENAI_CODEX_OAUTH_CONFIG.refreshBufferMs;
|
|
413
|
-
const retryableTokenGraceMs =
|
|
414
|
-
options?.retryableTokenGraceMs ??
|
|
415
|
-
OPENAI_CODEX_OAUTH_CONFIG.retryableTokenGraceMs;
|
|
416
|
-
const forceRefresh = options?.forceRefresh === true;
|
|
417
|
-
|
|
418
|
-
if (
|
|
419
|
-
!forceRefresh &&
|
|
420
|
-
!isCredentialLikelyExpired(currentCredentials, refreshBufferMs)
|
|
421
|
-
) {
|
|
422
|
-
return currentCredentials;
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
try {
|
|
426
|
-
const refreshed = await refreshOpenAICodexToken(
|
|
427
|
-
currentCredentials.refresh,
|
|
428
|
-
currentCredentials,
|
|
429
|
-
);
|
|
430
|
-
return refreshed;
|
|
431
|
-
} catch (error) {
|
|
432
|
-
if (
|
|
433
|
-
error instanceof OpenAICodexOAuthTokenError &&
|
|
434
|
-
error.isLikelyInvalidGrant()
|
|
435
|
-
) {
|
|
436
|
-
captureAuthLoggedOut(options?.telemetry, "openai-codex", "invalid_grant");
|
|
437
|
-
return null;
|
|
438
|
-
}
|
|
439
|
-
if (currentCredentials.expires - Date.now() > retryableTokenGraceMs) {
|
|
440
|
-
return currentCredentials;
|
|
441
|
-
}
|
|
442
|
-
return null;
|
|
443
|
-
}
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
export function isOpenAICodexTokenExpired(
|
|
447
|
-
credentials: OAuthCredentials,
|
|
448
|
-
refreshBufferMs: number = OPENAI_CODEX_OAUTH_CONFIG.refreshBufferMs,
|
|
449
|
-
): boolean {
|
|
450
|
-
return isCredentialLikelyExpired(credentials, refreshBufferMs);
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
export function normalizeOpenAICodexCredentials(
|
|
454
|
-
credentials: OAuthCredentials,
|
|
455
|
-
): OAuthCredentials {
|
|
456
|
-
const accountId = credentials.accountId ?? getAccountId(credentials.access);
|
|
457
|
-
if (!accountId) {
|
|
458
|
-
throw new Error("Failed to extract accountId from token");
|
|
459
|
-
}
|
|
460
|
-
return {
|
|
461
|
-
...credentials,
|
|
462
|
-
accountId,
|
|
463
|
-
metadata: {
|
|
464
|
-
...(credentials.metadata ?? {}),
|
|
465
|
-
provider: "openai-codex",
|
|
466
|
-
},
|
|
467
|
-
};
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
export const openaiCodexOAuthProvider: OAuthProviderInterface = {
|
|
471
|
-
id: "openai-codex",
|
|
472
|
-
name: "ChatGPT Plus/Pro (ChatGPT Subscription)",
|
|
473
|
-
usesCallbackServer: true,
|
|
474
|
-
|
|
475
|
-
async login(callbacks: OAuthLoginCallbacks): Promise<OAuthCredentials> {
|
|
476
|
-
return loginOpenAICodex({
|
|
477
|
-
onAuth: callbacks.onAuth,
|
|
478
|
-
onPrompt: callbacks.onPrompt,
|
|
479
|
-
onProgress: callbacks.onProgress,
|
|
480
|
-
onManualCodeInput: callbacks.onManualCodeInput,
|
|
481
|
-
});
|
|
482
|
-
},
|
|
483
|
-
|
|
484
|
-
async refreshToken(credentials: OAuthCredentials): Promise<OAuthCredentials> {
|
|
485
|
-
return refreshOpenAICodexToken(credentials.refresh, credentials);
|
|
486
|
-
},
|
|
487
|
-
|
|
488
|
-
getApiKey(credentials: OAuthCredentials): string {
|
|
489
|
-
return credentials.access;
|
|
490
|
-
},
|
|
491
|
-
};
|
package/src/auth/oca.test.ts
DELETED
|
@@ -1,215 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { getValidOcaCredentials } from "./oca.js";
|
|
3
|
-
import type { OAuthCredentials } from "./types.js";
|
|
4
|
-
|
|
5
|
-
function toBase64Url(value: string): string {
|
|
6
|
-
return Buffer.from(value, "utf8").toString("base64url");
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function createJwt(payload: Record<string, unknown>): string {
|
|
10
|
-
return `${toBase64Url(JSON.stringify({ alg: "none", typ: "JWT" }))}.${toBase64Url(JSON.stringify(payload))}.sig`;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function createCredentials(
|
|
14
|
-
overrides: Partial<OAuthCredentials> = {},
|
|
15
|
-
): OAuthCredentials {
|
|
16
|
-
return {
|
|
17
|
-
access: "access-old",
|
|
18
|
-
refresh: "refresh-old",
|
|
19
|
-
expires: 0,
|
|
20
|
-
accountId: "acct-old",
|
|
21
|
-
email: "old@example.com",
|
|
22
|
-
metadata: { provider: "oca", mode: "internal" },
|
|
23
|
-
...overrides,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
describe("auth/oca getValidOcaCredentials", () => {
|
|
28
|
-
afterEach(() => {
|
|
29
|
-
vi.unstubAllGlobals();
|
|
30
|
-
vi.restoreAllMocks();
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it("returns current credentials when token is still fresh", async () => {
|
|
34
|
-
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(10_000);
|
|
35
|
-
const current = createCredentials({ expires: 400_000 });
|
|
36
|
-
const fetchMock = vi.fn();
|
|
37
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
38
|
-
|
|
39
|
-
const result = await getValidOcaCredentials(current);
|
|
40
|
-
expect(result).toBe(current);
|
|
41
|
-
expect(fetchMock).not.toHaveBeenCalled();
|
|
42
|
-
nowSpy.mockRestore();
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("refreshes an expired token after discovery", async () => {
|
|
46
|
-
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(100_000);
|
|
47
|
-
const idToken = createJwt({
|
|
48
|
-
sub: "acct-new",
|
|
49
|
-
email: "new@example.com",
|
|
50
|
-
exp: 2_000_000_000,
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const fetchMock = vi
|
|
54
|
-
.fn()
|
|
55
|
-
.mockImplementationOnce(
|
|
56
|
-
async () =>
|
|
57
|
-
new Response(
|
|
58
|
-
JSON.stringify({
|
|
59
|
-
token_endpoint: "https://idcs.example.com/oauth2/v1/token",
|
|
60
|
-
}),
|
|
61
|
-
{
|
|
62
|
-
status: 200,
|
|
63
|
-
headers: { "Content-Type": "application/json" },
|
|
64
|
-
},
|
|
65
|
-
),
|
|
66
|
-
)
|
|
67
|
-
.mockImplementationOnce(
|
|
68
|
-
async () =>
|
|
69
|
-
new Response(
|
|
70
|
-
JSON.stringify({
|
|
71
|
-
access_token: createJwt({
|
|
72
|
-
sub: "acct-new",
|
|
73
|
-
email: "new@example.com",
|
|
74
|
-
exp: 2_000_000_000,
|
|
75
|
-
}),
|
|
76
|
-
refresh_token: "refresh-new",
|
|
77
|
-
id_token: idToken,
|
|
78
|
-
}),
|
|
79
|
-
{ status: 200, headers: { "Content-Type": "application/json" } },
|
|
80
|
-
),
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
84
|
-
|
|
85
|
-
const result = await getValidOcaCredentials(
|
|
86
|
-
createCredentials({ expires: 101_000 }),
|
|
87
|
-
undefined,
|
|
88
|
-
{
|
|
89
|
-
config: {
|
|
90
|
-
internal: {
|
|
91
|
-
clientId: "client-1",
|
|
92
|
-
idcsUrl: "https://idcs.example.com",
|
|
93
|
-
scopes: "openid offline_access",
|
|
94
|
-
baseUrl: "https://oca.example.com",
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
expect(result).toMatchObject({
|
|
101
|
-
access: expect.any(String),
|
|
102
|
-
refresh: "refresh-new",
|
|
103
|
-
accountId: "acct-new",
|
|
104
|
-
email: "new@example.com",
|
|
105
|
-
metadata: {
|
|
106
|
-
provider: "oca",
|
|
107
|
-
mode: "internal",
|
|
108
|
-
},
|
|
109
|
-
});
|
|
110
|
-
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
111
|
-
nowSpy.mockRestore();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("returns null when refresh fails with invalid_grant", async () => {
|
|
115
|
-
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(100_000);
|
|
116
|
-
const fetchMock = vi
|
|
117
|
-
.fn()
|
|
118
|
-
.mockImplementationOnce(
|
|
119
|
-
async () =>
|
|
120
|
-
new Response(
|
|
121
|
-
JSON.stringify({
|
|
122
|
-
token_endpoint: "https://idcs.invalid/oauth2/v1/token",
|
|
123
|
-
}),
|
|
124
|
-
{
|
|
125
|
-
status: 200,
|
|
126
|
-
headers: { "Content-Type": "application/json" },
|
|
127
|
-
},
|
|
128
|
-
),
|
|
129
|
-
)
|
|
130
|
-
.mockImplementationOnce(
|
|
131
|
-
async () =>
|
|
132
|
-
new Response(
|
|
133
|
-
JSON.stringify({
|
|
134
|
-
error: "invalid_grant",
|
|
135
|
-
error_description: "expired refresh token",
|
|
136
|
-
}),
|
|
137
|
-
{
|
|
138
|
-
status: 400,
|
|
139
|
-
headers: { "Content-Type": "application/json" },
|
|
140
|
-
},
|
|
141
|
-
),
|
|
142
|
-
);
|
|
143
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
144
|
-
|
|
145
|
-
const result = await getValidOcaCredentials(
|
|
146
|
-
createCredentials({ expires: 101_000 }),
|
|
147
|
-
undefined,
|
|
148
|
-
{
|
|
149
|
-
config: {
|
|
150
|
-
internal: {
|
|
151
|
-
clientId: "client-2",
|
|
152
|
-
idcsUrl: "https://idcs.invalid",
|
|
153
|
-
scopes: "openid offline_access",
|
|
154
|
-
baseUrl: "https://oca.example.com",
|
|
155
|
-
},
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
);
|
|
159
|
-
expect(result).toBeNull();
|
|
160
|
-
nowSpy.mockRestore();
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it("keeps current credentials on transient refresh failures when access token is still usable", async () => {
|
|
164
|
-
const nowSpy = vi.spyOn(Date, "now").mockReturnValue(100_000);
|
|
165
|
-
const fetchMock = vi
|
|
166
|
-
.fn()
|
|
167
|
-
.mockImplementationOnce(
|
|
168
|
-
async () =>
|
|
169
|
-
new Response(
|
|
170
|
-
JSON.stringify({
|
|
171
|
-
token_endpoint: "https://idcs.retry/oauth2/v1/token",
|
|
172
|
-
}),
|
|
173
|
-
{
|
|
174
|
-
status: 200,
|
|
175
|
-
headers: { "Content-Type": "application/json" },
|
|
176
|
-
},
|
|
177
|
-
),
|
|
178
|
-
)
|
|
179
|
-
.mockImplementationOnce(
|
|
180
|
-
async () =>
|
|
181
|
-
new Response(
|
|
182
|
-
JSON.stringify({
|
|
183
|
-
error: "server_error",
|
|
184
|
-
error_description: "temporary issue",
|
|
185
|
-
}),
|
|
186
|
-
{
|
|
187
|
-
status: 500,
|
|
188
|
-
headers: { "Content-Type": "application/json" },
|
|
189
|
-
},
|
|
190
|
-
),
|
|
191
|
-
);
|
|
192
|
-
vi.stubGlobal("fetch", fetchMock);
|
|
193
|
-
|
|
194
|
-
const current = createCredentials({ expires: 150_000 });
|
|
195
|
-
const result = await getValidOcaCredentials(
|
|
196
|
-
current,
|
|
197
|
-
{
|
|
198
|
-
refreshBufferMs: 60_000,
|
|
199
|
-
retryableTokenGraceMs: 30_000,
|
|
200
|
-
},
|
|
201
|
-
{
|
|
202
|
-
config: {
|
|
203
|
-
internal: {
|
|
204
|
-
clientId: "client-3",
|
|
205
|
-
idcsUrl: "https://idcs.retry",
|
|
206
|
-
scopes: "openid offline_access",
|
|
207
|
-
baseUrl: "https://oca.example.com",
|
|
208
|
-
},
|
|
209
|
-
},
|
|
210
|
-
},
|
|
211
|
-
);
|
|
212
|
-
expect(result).toBe(current);
|
|
213
|
-
nowSpy.mockRestore();
|
|
214
|
-
});
|
|
215
|
-
});
|