Skip to content

Commit 0fd1b4a

Browse files
fix(opencode): Console API keys keep their key fingerprint
Only OAuth tokens rotate, so only Console OAuth logins are fingerprinted by workspace. A Console API key now fingerprints the same as that key supplied inline, and one allowance shows as one account. AI-assisted; verified with the tests and tools listed above.
1 parent ee41108 commit 0fd1b4a

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

‎packages/provider-opencode/src/server/status.test.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@ it.effect(
125125
key: "console-key",
126126
metadata: { orgID: "workspace-id" },
127127
};
128-
// Console credentials share one workspace fingerprint across token refreshes.
128+
// Console OAuth logins share one workspace fingerprint across token refreshes.
129129
const workspaceFingerprint =
130130
"b57cd91082439e0caec0a8016b42c874b7c0cb734814d5fedfca398f62a3614f";
131131
const fingerprints = new Map([
132+
// A Console API key keeps the fingerprint it has when supplied inline.
133+
["console-key", "3e45cfde9158185930d4cc7020a4e390895574fd54bd8f9631d23a336bbf94f0"],
134+
["oauth-access", workspaceFingerprint],
132135
["go-key", "d0e590e93fc9bd9353f2b993c0c287391cae505da2a78cb698b75418d2f5367e"],
133136
["inline-key", "85649de18e63305548b6407e185f708483727ee6189cc4bc9512ddb17e23e776"],
134137
]);
@@ -142,6 +145,7 @@ it.effect(
142145
workspaces = requested.map((key) =>
143146
key === "console-key" || key === "oauth-access" ? "workspace-id" : undefined,
144147
),
148+
fingerprint = fingerprints.get(requested.at(-1)!),
145149
} of [
146150
{
147151
consoleRow: {
@@ -173,6 +177,7 @@ it.effect(
173177
},
174178
requested: ["go-key"],
175179
workspaces: ["workspace-id"],
180+
fingerprint: workspaceFingerprint,
176181
},
177182
{
178183
consoleRow: {
@@ -253,11 +258,7 @@ it.effect(
253258
NodeAssert.equal(limits.windows.length, consoleStatus === 500 ? 0 : 3);
254259
NodeAssert.equal(
255260
limits.credentialFingerprint,
256-
consoleStatus === 500
257-
? undefined
258-
: workspaces.at(-1)
259-
? workspaceFingerprint
260-
: fingerprints.get(requested.at(-1)!),
261+
consoleStatus === 500 ? undefined : fingerprint,
261262
);
262263
}
263264
}).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))),

‎packages/provider-opencode/src/server/usageLimits.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class OpenCodeCredentialReadError extends Schema.TaggedError<OpenCodeCredentialR
6464
type StoredCredential = {
6565
readonly token: string;
6666
readonly workspaceID?: string;
67+
readonly oauth?: true;
6768
};
6869

6970
const readStoredCredential = (databasePath: string, integrationId: "opencode" | "opencode-go") =>
@@ -95,6 +96,7 @@ const readStoredCredential = (databasePath: string, integrationId: "opencode" |
9596
return {
9697
token,
9798
...(typeof orgID === "string" ? { workspaceID: orgID } : {}),
99+
...(stored?.type === "oauth" ? { oauth: true as const } : {}),
98100
};
99101
} finally {
100102
database.close();
@@ -210,10 +212,11 @@ export const readOpenCodeGoUsageLimits = Effect.fn("readOpenCodeGoUsageLimits")(
210212
];
211213
const digest = yield* Digest.Digest;
212214
// Unkeyed hashes match across environments; random Go keys resist offline guessing.
213-
// Console tokens rotate on refresh, so their usage is keyed by workspace instead.
214-
const fingerprintSource = credential.workspaceID
215-
? `opencode-console\0${credential.workspaceID}`
216-
: `opencode-go\0${credential.token}`;
215+
// Console OAuth tokens rotate on refresh, so their usage is keyed by workspace instead.
216+
const fingerprintSource =
217+
credential.oauth && credential.workspaceID
218+
? `opencode-console\0${credential.workspaceID}`
219+
: `opencode-go\0${credential.token}`;
217220
const credentialFingerprint = yield* digest
218221
.digest(Digest.sha256, new TextEncoder().encode(fingerprintSource))
219222
.pipe(Effect.map(Hex.encode), Effect.orDie);

0 commit comments

Comments
 (0)