|
1 | | -import { resolveExternalAuthProfilesWithPlugins } from "../../plugins/provider-runtime.js"; |
2 | | -import type { ProviderExternalAuthProfile } from "../../plugins/types.js"; |
3 | | -import type { AuthProfileStore, OAuthCredential } from "./types.js"; |
4 | | - |
5 | | -type ExternalOAuthProfileMap = Map<string, ProviderExternalAuthProfile>; |
6 | | - |
7 | | -function normalizeExternalOAuthProfile( |
8 | | - profile: ProviderExternalAuthProfile, |
9 | | -): ProviderExternalAuthProfile | null { |
10 | | - if (!profile?.profileId || !profile.credential) { |
11 | | - return null; |
12 | | - } |
13 | | - return { |
14 | | - ...profile, |
15 | | - persistence: profile.persistence ?? "runtime-only", |
16 | | - }; |
17 | | -} |
18 | | - |
19 | | -function resolveExternalOAuthProfileMap(params: { |
20 | | - store: AuthProfileStore; |
21 | | - agentDir?: string; |
22 | | - env?: NodeJS.ProcessEnv; |
23 | | -}): ExternalOAuthProfileMap { |
24 | | - const env = params.env ?? process.env; |
25 | | - const profiles = resolveExternalAuthProfilesWithPlugins({ |
26 | | - env, |
27 | | - context: { |
28 | | - config: undefined, |
29 | | - agentDir: params.agentDir, |
30 | | - workspaceDir: undefined, |
31 | | - env, |
32 | | - store: params.store, |
33 | | - }, |
34 | | - }); |
35 | | - |
36 | | - const resolved: ExternalOAuthProfileMap = new Map(); |
37 | | - for (const rawProfile of profiles) { |
38 | | - const profile = normalizeExternalOAuthProfile(rawProfile); |
39 | | - if (!profile) { |
40 | | - continue; |
41 | | - } |
42 | | - resolved.set(profile.profileId, profile); |
43 | | - } |
44 | | - return resolved; |
45 | | -} |
46 | | - |
47 | | -function oauthCredentialMatches(a: OAuthCredential, b: OAuthCredential): boolean { |
48 | | - return ( |
49 | | - a.type === b.type && |
50 | | - a.provider === b.provider && |
51 | | - a.access === b.access && |
52 | | - a.refresh === b.refresh && |
53 | | - a.expires === b.expires && |
54 | | - a.clientId === b.clientId && |
55 | | - a.email === b.email && |
56 | | - a.displayName === b.displayName && |
57 | | - a.enterpriseUrl === b.enterpriseUrl && |
58 | | - a.projectId === b.projectId && |
59 | | - a.accountId === b.accountId |
60 | | - ); |
61 | | -} |
62 | | - |
63 | | -export function overlayExternalOAuthProfiles( |
64 | | - store: AuthProfileStore, |
65 | | - params?: { agentDir?: string; env?: NodeJS.ProcessEnv }, |
66 | | -): AuthProfileStore { |
67 | | - const profiles = resolveExternalOAuthProfileMap({ |
68 | | - store, |
69 | | - agentDir: params?.agentDir, |
70 | | - env: params?.env, |
71 | | - }); |
72 | | - if (profiles.size === 0) { |
73 | | - return store; |
74 | | - } |
75 | | - |
76 | | - const next = structuredClone(store); |
77 | | - for (const [profileId, profile] of profiles) { |
78 | | - next.profiles[profileId] = profile.credential; |
79 | | - } |
80 | | - return next; |
81 | | -} |
82 | | - |
83 | | -export function shouldPersistExternalOAuthProfile(params: { |
84 | | - store: AuthProfileStore; |
85 | | - profileId: string; |
86 | | - credential: OAuthCredential; |
87 | | - agentDir?: string; |
88 | | - env?: NodeJS.ProcessEnv; |
89 | | -}): boolean { |
90 | | - const external = resolveExternalOAuthProfileMap({ |
91 | | - store: params.store, |
92 | | - agentDir: params.agentDir, |
93 | | - env: params.env, |
94 | | - }).get(params.profileId); |
95 | | - if (!external || external.persistence === "persisted") { |
96 | | - return true; |
97 | | - } |
98 | | - return !oauthCredentialMatches(external.credential, params.credential); |
99 | | -} |
| 1 | +export { |
| 2 | + overlayExternalAuthProfiles, |
| 3 | + overlayExternalOAuthProfiles, |
| 4 | + shouldPersistExternalAuthProfile, |
| 5 | + shouldPersistExternalOAuthProfile, |
| 6 | +} from "./external-auth.js"; |
0 commit comments