Repository navigation
feat(anthropic): keep one OAuth account per organization #8587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
SeongwoongCho
wants to merge
5
commits into
lidge-jun:dev
Choose a base branch
from
SeongwoongCho:feat/anthropic-org-scoped-accounts
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+453
−21
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e305167
feat(anthropic): keep one OAuth account per organization
bf30126
docs(anthropic): state when another organization can still replace an…
6022bd6
fix(oauth): keep plan.ts free of node:crypto for the GUI bundle
a649430
docs(anthropic): qualify when organization evidence separates accounts
7df9912
docs(anthropic): an unknown organization still meets the held-elsewhe…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /** | ||
| * Which stored Anthropic row a login, a Claude Code import or a reauth may write. | ||
| * | ||
| * One Claude account (one `account.uuid`) can belong to several organizations, and every OAuth | ||
| * grant is scoped to exactly one of them, with its own subscription and usage limits. A login | ||
| * therefore replaces the row of the same account AND organization; a proven different | ||
| * organization gets its own row instead of overwriting the first one. | ||
| * | ||
| * The organization only scopes a slot. It never proves identity: matching still starts from | ||
| * the same account identity the store always used. When the incoming organization is unknown, | ||
| * the login keeps the historical same-account replacement, so a row stored before | ||
| * organizations were recorded is never duplicated by re-login. | ||
| */ | ||
| import { createHash } from "node:crypto"; | ||
| import { anthropicOrganization, matchesAnthropicImport, normalizeAnthropicIdentity, | ||
| resolveAnthropicAccountIdentity, type AnthropicIdentityResolver } from "./anthropic-identity"; | ||
| import type { OAuthCredentials, ProviderAccount } from "./types"; | ||
|
|
||
| /** Interactive login waits on these reads; a slow profile falls back to the historical match. */ | ||
| const LOGIN_OBSERVATION_TIMEOUT_MS = 5_000; | ||
|
|
||
| /** Organizations observed for stored rows, keyed by the SHA-256 of the bearer that proved them. */ | ||
| export type AnthropicSlotOrganizations = ReadonlyMap<string, string>; | ||
|
|
||
| function bearerSha256(access: string): string { | ||
| return createHash("sha256").update(access).digest("hex"); | ||
| } | ||
|
|
||
| function loginIdentity(credential: OAuthCredentials): string | undefined { | ||
| return credential.accountId ?? credential.email; | ||
| } | ||
|
|
||
| /** | ||
| * Before the store lock: read the organization of each same-account row that has none recorded | ||
| * (rows saved before organizations were tracked), so the login can tell its org from theirs. | ||
| * Only an unexpired bearer is asked, each answer is bound to that exact bearer, and nothing is | ||
| * written here; a row whose bearer changes before the write simply stays unknown. | ||
| */ | ||
| export async function observeAnthropicSlotOrganizations( | ||
| accounts: readonly ProviderAccount[], | ||
| incoming: OAuthCredentials, | ||
| resolve: AnthropicIdentityResolver = resolveAnthropicAccountIdentity, | ||
| now: () => number = Date.now, | ||
| ): Promise<AnthropicSlotOrganizations> { | ||
| const identity = loginIdentity(incoming); | ||
| if (!identity || !anthropicOrganization(incoming)) return new Map(); | ||
| const pending = accounts.filter(row => loginIdentity(row.credential) === identity | ||
| && anthropicOrganization(row.credential) === undefined | ||
| && row.credential.access && row.credential.expires > now()); | ||
| if (pending.length === 0) return new Map(); | ||
| const signal = AbortSignal.timeout(LOGIN_OBSERVATION_TIMEOUT_MS); | ||
| const proofs = await Promise.all(pending.map(row => resolve(row.credential.access, signal))); | ||
| const observed = new Map<string, string>(); | ||
| pending.forEach((row, index) => { | ||
| const proof = normalizeAnthropicIdentity(proofs[index], row.credential.access); | ||
| if (proof?.organizationUuid && proof.accountUuid === row.credential.accountId) { | ||
| observed.set(proof.bearerSha256, proof.organizationUuid); | ||
| } | ||
| }); | ||
| return observed; | ||
| } | ||
|
|
||
| /** | ||
| * One rule for every writer. A known incoming organization takes its own row, else a row whose | ||
| * organization is still unknown; a row proven to serve another organization is never chosen. | ||
| * An unknown incoming organization prefers an unknown row, then the active one, so it never | ||
| * displaces a proven slot while a better candidate exists. | ||
| */ | ||
| function pickSlot( | ||
| candidates: readonly ProviderAccount[], incoming: OAuthCredentials, | ||
| observed: AnthropicSlotOrganizations, activeAccountId?: string, | ||
| ): ProviderAccount | undefined { | ||
| const organizationOf = (row: ProviderAccount) => anthropicOrganization(row.credential) | ||
| ?? observed.get(bearerSha256(row.credential.access)); | ||
| const organization = anthropicOrganization(incoming); | ||
| if (organization) { | ||
| return candidates.find(row => organizationOf(row) === organization) | ||
| ?? candidates.find(row => organizationOf(row) === undefined); | ||
| } | ||
| return candidates.find(row => organizationOf(row) === undefined) | ||
| ?? candidates.find(row => row.id === activeAccountId) ?? candidates[0]; | ||
| } | ||
|
|
||
| /** Inside the store lock: the row a browser login replaces, or undefined for a new row. */ | ||
| export function findAnthropicLoginSlot( | ||
| accounts: readonly ProviderAccount[], incoming: OAuthCredentials, | ||
| observed: AnthropicSlotOrganizations = new Map(), activeAccountId?: string, | ||
| ): ProviderAccount | undefined { | ||
| const identity = loginIdentity(incoming); | ||
| return pickSlot(accounts.filter(row => loginIdentity(row.credential) === identity), incoming, observed, activeAccountId); | ||
| } | ||
|
|
||
| /** Inside the store lock: the row a Claude Code credential import enriches, or undefined for a new row. */ | ||
| export function findAnthropicImportSlot( | ||
| accounts: readonly ProviderAccount[], incoming: OAuthCredentials, | ||
| observed: AnthropicSlotOrganizations = new Map(), activeAccountId?: string, | ||
| ): ProviderAccount | undefined { | ||
| return pickSlot(accounts.filter(row => matchesAnthropicImport(row.credential, incoming)), incoming, observed, activeAccountId); | ||
| } | ||
|
|
||
| /** | ||
| * A reauth must not turn a row into a second copy of an organization another row of the same | ||
| * account already proves; that would leave two rows on one quota and drop the first grant. | ||
| */ | ||
| export function anthropicOrganizationHeldElsewhere( | ||
| accounts: readonly ProviderAccount[], accountId: string, incoming: OAuthCredentials, | ||
| ): boolean { | ||
| const organization = anthropicOrganization(incoming); | ||
| const identity = loginIdentity(incoming); | ||
| return organization !== undefined && accounts.some(row => row.id !== accountId | ||
| && loginIdentity(row.credential) === identity && anthropicOrganization(row.credential) === organization); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.