repoburg 1.3.136 → 1.3.138
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/CODEMAP.md +3 -11
- package/README.md +4 -4
- package/backend/.env +2 -2
- package/backend/CODEMAP.md +2 -3
- package/backend/dist/src/core-entities/llm-call-log.entity.d.ts +1 -1
- package/backend/dist/src/core-entities/llm-call-log.entity.js +3 -3
- package/backend/dist/src/core-entities/llm-call-log.entity.js.map +1 -1
- package/backend/dist/src/core-entities/session.entity.js +2 -2
- package/backend/dist/src/core-entities/session.entity.js.map +1 -1
- package/backend/dist/src/core-entities/system-prompt.entity.js +1 -1
- package/backend/dist/src/core-entities/system-prompt.entity.js.map +1 -1
- package/backend/dist/src/llm-call-logs/dto/llm-call-log.dto.d.ts +1 -1
- package/backend/dist/src/llm-call-logs/dto/llm-call-log.dto.js +1 -1
- package/backend/dist/src/llm-call-logs/dto/llm-call-log.dto.js.map +1 -1
- package/backend/dist/src/llm-call-logs/llm-call-logs.controller.js +1 -1
- package/backend/dist/src/llm-orchestration/action-handlers/manage-sub-agents.handler.js +1 -1
- package/backend/dist/src/llm-orchestration/action-handlers/overwrite-file.handler.js +1 -0
- package/backend/dist/src/llm-orchestration/action-handlers/overwrite-file.handler.js.map +1 -1
- package/backend/dist/src/llm-orchestration/action-handlers/quick-edit.handler.js +3 -0
- package/backend/dist/src/llm-orchestration/action-handlers/quick-edit.handler.js.map +1 -1
- package/backend/dist/src/llm-provider/llm-provider.interface.d.ts +3 -9
- package/backend/dist/src/llm-provider/llm-provider.interface.js +2 -9
- package/backend/dist/src/llm-provider/llm-provider.interface.js.map +1 -1
- package/backend/dist/src/llm-provider/llm-provider.module.js +1 -2
- package/backend/dist/src/llm-provider/llm-provider.module.js.map +1 -1
- package/backend/dist/src/llm-provider/proxy-llm.provider.d.ts +1 -3
- package/backend/dist/src/llm-provider/proxy-llm.provider.js +4 -12
- package/backend/dist/src/llm-provider/proxy-llm.provider.js.map +1 -1
- package/backend/dist/src/llm-responses/dto/sync-conversation.dto.js +2 -2
- package/backend/dist/src/llm-responses/dto/sync-conversation.dto.js.map +1 -1
- package/backend/dist/src/sessions/dto/session.dto.d.ts +1 -1
- package/backend/dist/src/sessions/dto/session.dto.js +7 -7
- package/backend/dist/src/sessions/dto/session.dto.js.map +1 -1
- package/backend/dist/src/sessions/sessions.service.js +14 -5
- package/backend/dist/src/sessions/sessions.service.js.map +1 -1
- package/backend/dist/src/sub-agents/dto/create-sub-agent.dto.js +1 -1
- package/backend/dist/src/sub-agents/dto/create-sub-agent.dto.js.map +1 -1
- package/backend/dist/src/sub-agents/dto/update-sub-agent.dto.js +1 -1
- package/backend/dist/src/sub-agents/dto/update-sub-agent.dto.js.map +1 -1
- package/backend/dist/src/sub-agents/sub-agent.entity.js +1 -1
- package/backend/dist/src/sub-agents/sub-agent.entity.js.map +1 -1
- package/backend/dist/src/system-prompts/dto/system-prompt.dto.js +2 -2
- package/backend/dist/src/system-prompts/dto/system-prompt.dto.js.map +1 -1
- package/backend/dist/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -2
- package/backend/dist/packages/gemini-core/examples/simple.d.ts +0 -1
- package/backend/dist/packages/gemini-core/examples/simple.js +0 -73
- package/backend/dist/packages/gemini-core/examples/simple.js.map +0 -1
- package/backend/dist/packages/gemini-core/index.d.ts +0 -3
- package/backend/dist/packages/gemini-core/index.js +0 -27
- package/backend/dist/packages/gemini-core/index.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/codeAssist.d.ts +0 -5
- package/backend/dist/packages/gemini-core/src/code_assist/codeAssist.js +0 -17
- package/backend/dist/packages/gemini-core/src/code_assist/codeAssist.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/converter.d.ts +0 -69
- package/backend/dist/packages/gemini-core/src/code_assist/converter.js +0 -129
- package/backend/dist/packages/gemini-core/src/code_assist/converter.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/oauth-credential-storage.d.ts +0 -8
- package/backend/dist/packages/gemini-core/src/code_assist/oauth-credential-storage.js +0 -84
- package/backend/dist/packages/gemini-core/src/code_assist/oauth-credential-storage.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/oauth2.d.ts +0 -12
- package/backend/dist/packages/gemini-core/src/code_assist/oauth2.js +0 -371
- package/backend/dist/packages/gemini-core/src/code_assist/oauth2.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/server.d.ts +0 -28
- package/backend/dist/packages/gemini-core/src/code_assist/server.js +0 -135
- package/backend/dist/packages/gemini-core/src/code_assist/server.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/setup.d.ts +0 -10
- package/backend/dist/packages/gemini-core/src/code_assist/setup.js +0 -94
- package/backend/dist/packages/gemini-core/src/code_assist/setup.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/code_assist/types.d.ts +0 -106
- package/backend/dist/packages/gemini-core/src/code_assist/types.js +0 -28
- package/backend/dist/packages/gemini-core/src/code_assist/types.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/config/config.d.ts +0 -22
- package/backend/dist/packages/gemini-core/src/config/config.js +0 -38
- package/backend/dist/packages/gemini-core/src/config/config.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/config/models.d.ts +0 -6
- package/backend/dist/packages/gemini-core/src/config/models.js +0 -13
- package/backend/dist/packages/gemini-core/src/config/models.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/config/storage.d.ts +0 -8
- package/backend/dist/packages/gemini-core/src/config/storage.js +0 -25
- package/backend/dist/packages/gemini-core/src/config/storage.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/core/contentGenerator.d.ts +0 -23
- package/backend/dist/packages/gemini-core/src/core/contentGenerator.js +0 -63
- package/backend/dist/packages/gemini-core/src/core/contentGenerator.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/core/geminiChat.d.ts +0 -34
- package/backend/dist/packages/gemini-core/src/core/geminiChat.js +0 -247
- package/backend/dist/packages/gemini-core/src/core/geminiChat.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/core/geminiRequest.d.ts +0 -3
- package/backend/dist/packages/gemini-core/src/core/geminiRequest.js +0 -8
- package/backend/dist/packages/gemini-core/src/core/geminiRequest.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/core/prompts.d.ts +0 -7
- package/backend/dist/packages/gemini-core/src/core/prompts.js +0 -69
- package/backend/dist/packages/gemini-core/src/core/prompts.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/core/tokenLimits.d.ts +0 -5
- package/backend/dist/packages/gemini-core/src/core/tokenLimits.js +0 -25
- package/backend/dist/packages/gemini-core/src/core/tokenLimits.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/index.d.ts +0 -18
- package/backend/dist/packages/gemini-core/src/index.js +0 -37
- package/backend/dist/packages/gemini-core/src/index.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/base-token-storage.d.ts +0 -14
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/base-token-storage.js +0 -34
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/base-token-storage.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/file-token-storage.d.ts +0 -19
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/file-token-storage.js +0 -141
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/file-token-storage.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/hybrid-token-storage.d.ts +0 -18
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/hybrid-token-storage.js +0 -74
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/hybrid-token-storage.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/index.d.ts +0 -6
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/index.js +0 -24
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/index.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/keychain-token-storage.d.ts +0 -26
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/keychain-token-storage.js +0 -188
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/keychain-token-storage.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/types.d.ts +0 -27
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/types.js +0 -9
- package/backend/dist/packages/gemini-core/src/mcp/token-storage/types.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/utils/errors.d.ts +0 -34
- package/backend/dist/packages/gemini-core/src/utils/errors.js +0 -103
- package/backend/dist/packages/gemini-core/src/utils/errors.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/utils/partUtils.d.ts +0 -7
- package/backend/dist/packages/gemini-core/src/utils/partUtils.js +0 -108
- package/backend/dist/packages/gemini-core/src/utils/partUtils.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/utils/quotaErrorDetection.d.ts +0 -16
- package/backend/dist/packages/gemini-core/src/utils/quotaErrorDetection.js +0 -60
- package/backend/dist/packages/gemini-core/src/utils/quotaErrorDetection.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/utils/retry.d.ts +0 -14
- package/backend/dist/packages/gemini-core/src/utils/retry.js +0 -154
- package/backend/dist/packages/gemini-core/src/utils/retry.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/utils/session.d.ts +0 -1
- package/backend/dist/packages/gemini-core/src/utils/session.js +0 -6
- package/backend/dist/packages/gemini-core/src/utils/session.js.map +0 -1
- package/backend/dist/packages/gemini-core/src/utils/userAccountManager.d.ts +0 -10
- package/backend/dist/packages/gemini-core/src/utils/userAccountManager.js +0 -106
- package/backend/dist/packages/gemini-core/src/utils/userAccountManager.js.map +0 -1
- package/backend/dist/src/gemini/gemini-llm.provider.d.ts +0 -12
- package/backend/dist/src/gemini/gemini-llm.provider.js +0 -126
- package/backend/dist/src/gemini/gemini-llm.provider.js.map +0 -1
- package/backend/dist/src/gemini/gemini.module.d.ts +0 -2
- package/backend/dist/src/gemini/gemini.module.js +0 -21
- package/backend/dist/src/gemini/gemini.module.js.map +0 -1
- package/backend/packages/gemini-core/CODEMAP.md +0 -30
- package/backend/packages/gemini-core/examples/simple.ts +0 -120
- package/backend/packages/gemini-core/index.ts +0 -15
- package/backend/packages/gemini-core/src/code_assist/codeAssist.ts +0 -37
- package/backend/packages/gemini-core/src/code_assist/converter.ts +0 -250
- package/backend/packages/gemini-core/src/code_assist/oauth-credential-storage.ts +0 -129
- package/backend/packages/gemini-core/src/code_assist/oauth2.ts +0 -541
- package/backend/packages/gemini-core/src/code_assist/server.ts +0 -236
- package/backend/packages/gemini-core/src/code_assist/setup.ts +0 -124
- package/backend/packages/gemini-core/src/code_assist/types.ts +0 -201
- package/backend/packages/gemini-core/src/config/config.ts +0 -75
- package/backend/packages/gemini-core/src/config/models.ts +0 -23
- package/backend/packages/gemini-core/src/config/storage.ts +0 -30
- package/backend/packages/gemini-core/src/core/contentGenerator.ts +0 -136
- package/backend/packages/gemini-core/src/core/geminiChat.ts +0 -385
- package/backend/packages/gemini-core/src/core/geminiRequest.ts +0 -19
- package/backend/packages/gemini-core/src/core/prompts.ts +0 -109
- package/backend/packages/gemini-core/src/core/tokenLimits.ts +0 -32
- package/backend/packages/gemini-core/src/index.ts +0 -35
- package/backend/packages/gemini-core/src/mcp/token-storage/base-token-storage.ts +0 -49
- package/backend/packages/gemini-core/src/mcp/token-storage/file-token-storage.ts +0 -184
- package/backend/packages/gemini-core/src/mcp/token-storage/hybrid-token-storage.ts +0 -97
- package/backend/packages/gemini-core/src/mcp/token-storage/index.ts +0 -14
- package/backend/packages/gemini-core/src/mcp/token-storage/keychain-token-storage.ts +0 -251
- package/backend/packages/gemini-core/src/mcp/token-storage/types.ts +0 -42
- package/backend/packages/gemini-core/src/utils/errors.ts +0 -112
- package/backend/packages/gemini-core/src/utils/partUtils.ts +0 -169
- package/backend/packages/gemini-core/src/utils/quotaErrorDetection.ts +0 -102
- package/backend/packages/gemini-core/src/utils/retry.ts +0 -265
- package/backend/packages/gemini-core/src/utils/session.ts +0 -9
- package/backend/packages/gemini-core/src/utils/userAccountManager.ts +0 -140
|
@@ -1,541 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright 2025 Google LLC
|
|
4
|
-
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { Credentials } from 'google-auth-library';
|
|
8
|
-
import {
|
|
9
|
-
OAuth2Client,
|
|
10
|
-
Compute,
|
|
11
|
-
CodeChallengeMethod,
|
|
12
|
-
} from 'google-auth-library';
|
|
13
|
-
import * as http from 'node:http';
|
|
14
|
-
import url from 'node:url';
|
|
15
|
-
import * as crypto from 'node:crypto';
|
|
16
|
-
import * as net from 'node:net';
|
|
17
|
-
import * as path from 'node:path';
|
|
18
|
-
import { promises as fs } from 'node:fs';
|
|
19
|
-
import type { Config } from '../config/config';
|
|
20
|
-
import { getErrorMessage, FatalAuthenticationError } from '../utils/errors';
|
|
21
|
-
import { UserAccountManager } from '../utils/userAccountManager';
|
|
22
|
-
import { AuthType } from '../core/contentGenerator';
|
|
23
|
-
import readline from 'node:readline';
|
|
24
|
-
import { Storage } from '../config/storage';
|
|
25
|
-
import { OAuthCredentialStorage } from './oauth-credential-storage';
|
|
26
|
-
import { FORCE_ENCRYPTED_FILE_ENV_VAR } from '../mcp/token-storage/index';
|
|
27
|
-
|
|
28
|
-
const userAccountManager = new UserAccountManager();
|
|
29
|
-
|
|
30
|
-
// OAuth Client ID used to initiate OAuth2Client class.
|
|
31
|
-
const OAUTH_CLIENT_ID =
|
|
32
|
-
'681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com';
|
|
33
|
-
|
|
34
|
-
// OAuth Secret value used to initiate OAuth2Client class.
|
|
35
|
-
// Note: It's ok to save this in git because this is an installed application
|
|
36
|
-
// as described here: https://developers.google.com/identity/protocols/oauth2#installed
|
|
37
|
-
// "The process results in a client ID and, in some cases, a client secret,
|
|
38
|
-
// which you embed in the source code of your application. (In this context,
|
|
39
|
-
// the client secret is obviously not treated as a secret.)"
|
|
40
|
-
const OAUTH_CLIENT_SECRET = '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';
|
|
41
|
-
|
|
42
|
-
// OAuth Scopes for Cloud Code authorization.
|
|
43
|
-
const OAUTH_SCOPE = [
|
|
44
|
-
'https://www.googleapis.com/auth/cloud-platform',
|
|
45
|
-
'https://www.googleapis.com/auth/userinfo.email',
|
|
46
|
-
'https://www.googleapis.com/auth/userinfo.profile',
|
|
47
|
-
];
|
|
48
|
-
|
|
49
|
-
const HTTP_REDIRECT = 301;
|
|
50
|
-
const SIGN_IN_SUCCESS_URL =
|
|
51
|
-
'https://developers.google.com/gemini-code-assist/auth_success_gemini';
|
|
52
|
-
const SIGN_IN_FAILURE_URL =
|
|
53
|
-
'https://developers.google.com/gemini-code-assist/auth_failure_gemini';
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* An Authentication URL for updating the credentials of a Oauth2Client
|
|
57
|
-
* as well as a promise that will resolve when the credentials have
|
|
58
|
-
* been refreshed (or which throws error when refreshing credentials failed).
|
|
59
|
-
*/
|
|
60
|
-
export interface OauthWebLogin {
|
|
61
|
-
authUrl: string;
|
|
62
|
-
loginCompletePromise: Promise<void>;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
const oauthClientPromises = new Map<AuthType, Promise<OAuth2Client>>();
|
|
66
|
-
|
|
67
|
-
function getUseEncryptedStorageFlag() {
|
|
68
|
-
return process.env[FORCE_ENCRYPTED_FILE_ENV_VAR] === 'true';
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function initOauthClient(
|
|
72
|
-
authType: AuthType,
|
|
73
|
-
config: Config,
|
|
74
|
-
): Promise<OAuth2Client> {
|
|
75
|
-
const client = new OAuth2Client({
|
|
76
|
-
clientId: OAUTH_CLIENT_ID,
|
|
77
|
-
clientSecret: OAUTH_CLIENT_SECRET,
|
|
78
|
-
});
|
|
79
|
-
const useEncryptedStorage = getUseEncryptedStorageFlag();
|
|
80
|
-
|
|
81
|
-
if (
|
|
82
|
-
process.env['GOOGLE_GENAI_USE_GCA'] &&
|
|
83
|
-
process.env['GOOGLE_CLOUD_ACCESS_TOKEN']
|
|
84
|
-
) {
|
|
85
|
-
client.setCredentials({
|
|
86
|
-
access_token: process.env['GOOGLE_CLOUD_ACCESS_TOKEN'],
|
|
87
|
-
});
|
|
88
|
-
await fetchAndCacheUserInfo(client);
|
|
89
|
-
return client;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
client.on('tokens', async (tokens: Credentials) => {
|
|
93
|
-
if (useEncryptedStorage) {
|
|
94
|
-
await OAuthCredentialStorage.saveCredentials(tokens);
|
|
95
|
-
} else {
|
|
96
|
-
await cacheCredentials(tokens);
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
// If there are cached creds on disk, they always take precedence
|
|
101
|
-
if (await loadCachedCredentials(client)) {
|
|
102
|
-
// Found valid cached credentials.
|
|
103
|
-
// Check if we need to retrieve Google Account ID or Email
|
|
104
|
-
if (!userAccountManager.getCachedGoogleAccount()) {
|
|
105
|
-
try {
|
|
106
|
-
await fetchAndCacheUserInfo(client);
|
|
107
|
-
} catch (error) {
|
|
108
|
-
// Non-fatal, continue with existing auth.
|
|
109
|
-
console.warn('Failed to fetch user info:', getErrorMessage(error));
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
console.log('Loaded cached credentials.');
|
|
113
|
-
return client;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
// In Google Cloud Shell, we can use Application Default Credentials (ADC)
|
|
117
|
-
// provided via its metadata server to authenticate non-interactively using
|
|
118
|
-
// the identity of the user logged into Cloud Shell.
|
|
119
|
-
if (authType === AuthType.CLOUD_SHELL) {
|
|
120
|
-
try {
|
|
121
|
-
console.log("Attempting to authenticate via Cloud Shell VM's ADC.");
|
|
122
|
-
const computeClient = new Compute({
|
|
123
|
-
// We can leave this empty, since the metadata server will provide
|
|
124
|
-
// the service account email.
|
|
125
|
-
});
|
|
126
|
-
await computeClient.getAccessToken();
|
|
127
|
-
console.log('Authentication successful.');
|
|
128
|
-
|
|
129
|
-
// Do not cache creds in this case; note that Compute client will handle its own refresh
|
|
130
|
-
return computeClient;
|
|
131
|
-
} catch (e) {
|
|
132
|
-
throw new Error(
|
|
133
|
-
`Could not authenticate using Cloud Shell credentials. Please select a different authentication method or ensure you are in a properly configured environment. Error: ${getErrorMessage(
|
|
134
|
-
e,
|
|
135
|
-
)}`,
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
if (config.isBrowserLaunchSuppressed()) {
|
|
141
|
-
let success = false;
|
|
142
|
-
const maxRetries = 2;
|
|
143
|
-
for (let i = 0; !success && i < maxRetries; i++) {
|
|
144
|
-
success = await authWithUserCode(client);
|
|
145
|
-
if (!success) {
|
|
146
|
-
console.error(
|
|
147
|
-
'\nFailed to authenticate with user code.',
|
|
148
|
-
i === maxRetries - 1 ? '' : 'Retrying...\n',
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
if (!success) {
|
|
153
|
-
throw new FatalAuthenticationError(
|
|
154
|
-
'Failed to authenticate with user code.',
|
|
155
|
-
);
|
|
156
|
-
}
|
|
157
|
-
} else {
|
|
158
|
-
const webLogin = await authWithWeb(client);
|
|
159
|
-
|
|
160
|
-
console.log(
|
|
161
|
-
`\n\nCode Assist login required.\n` +
|
|
162
|
-
`Attempting to open authentication page in your browser.\n` +
|
|
163
|
-
`Otherwise navigate to:\n\n${webLogin.authUrl}\n\n`,
|
|
164
|
-
);
|
|
165
|
-
try {
|
|
166
|
-
const { default: open } = await import('open');
|
|
167
|
-
// Attempt to open the authentication URL in the default browser.
|
|
168
|
-
// We do not use the `wait` option here because the main script's execution
|
|
169
|
-
// is already paused by `loginCompletePromise`, which awaits the server callback.
|
|
170
|
-
const childProcess = await open(webLogin.authUrl);
|
|
171
|
-
|
|
172
|
-
// IMPORTANT: Attach an error handler to the returned child process.
|
|
173
|
-
// Without this, if `open` fails to spawn a process (e.g., `xdg-open` is not found
|
|
174
|
-
// in a minimal Docker container), it will emit an unhandled 'error' event,
|
|
175
|
-
// causing the entire Node.js process to crash.
|
|
176
|
-
childProcess.on('error', (error) => {
|
|
177
|
-
console.error(
|
|
178
|
-
'Failed to open browser automatically. Please try running again with NO_BROWSER=true set.',
|
|
179
|
-
);
|
|
180
|
-
console.error('Browser error details:', getErrorMessage(error));
|
|
181
|
-
});
|
|
182
|
-
} catch (err) {
|
|
183
|
-
console.error(
|
|
184
|
-
'An unexpected error occurred while trying to open the browser:',
|
|
185
|
-
getErrorMessage(err),
|
|
186
|
-
'\nThis might be due to browser compatibility issues or system configuration.',
|
|
187
|
-
'\nPlease try running again with NO_BROWSER=true set for manual authentication.',
|
|
188
|
-
);
|
|
189
|
-
throw new FatalAuthenticationError(
|
|
190
|
-
`Failed to open browser: ${getErrorMessage(err)}`,
|
|
191
|
-
);
|
|
192
|
-
}
|
|
193
|
-
console.log('Waiting for authentication...');
|
|
194
|
-
|
|
195
|
-
// Add timeout to prevent infinite waiting when browser tab gets stuck
|
|
196
|
-
const authTimeout = 5 * 60 * 1000; // 5 minutes timeout
|
|
197
|
-
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
198
|
-
setTimeout(() => {
|
|
199
|
-
reject(
|
|
200
|
-
new FatalAuthenticationError(
|
|
201
|
-
'Authentication timed out after 5 minutes. The browser tab may have gotten stuck in a loading state. ' +
|
|
202
|
-
'Please try again or use NO_BROWSER=true for manual authentication.',
|
|
203
|
-
),
|
|
204
|
-
);
|
|
205
|
-
}, authTimeout);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
await Promise.race([webLogin.loginCompletePromise, timeoutPromise]);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return client;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export async function getOauthClient(
|
|
215
|
-
authType: AuthType,
|
|
216
|
-
config: Config,
|
|
217
|
-
): Promise<OAuth2Client> {
|
|
218
|
-
if (!oauthClientPromises.has(authType)) {
|
|
219
|
-
oauthClientPromises.set(authType, initOauthClient(authType, config));
|
|
220
|
-
}
|
|
221
|
-
return oauthClientPromises.get(authType)!;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
async function authWithUserCode(client: OAuth2Client): Promise<boolean> {
|
|
225
|
-
const redirectUri = 'https://codeassist.google.com/authcode';
|
|
226
|
-
const codeVerifier = await client.generateCodeVerifierAsync();
|
|
227
|
-
const state = crypto.randomBytes(32).toString('hex');
|
|
228
|
-
const authUrl: string = client.generateAuthUrl({
|
|
229
|
-
redirect_uri: redirectUri,
|
|
230
|
-
access_type: 'offline',
|
|
231
|
-
scope: OAUTH_SCOPE,
|
|
232
|
-
code_challenge_method: CodeChallengeMethod.S256,
|
|
233
|
-
code_challenge: codeVerifier.codeChallenge,
|
|
234
|
-
state,
|
|
235
|
-
});
|
|
236
|
-
console.log('Please visit the following URL to authorize the application:');
|
|
237
|
-
console.log('');
|
|
238
|
-
console.log(authUrl);
|
|
239
|
-
console.log('');
|
|
240
|
-
|
|
241
|
-
const code = await new Promise<string>((resolve) => {
|
|
242
|
-
const rl = readline.createInterface({
|
|
243
|
-
input: process.stdin,
|
|
244
|
-
output: process.stdout,
|
|
245
|
-
});
|
|
246
|
-
rl.question('Enter the authorization code: ', (code) => {
|
|
247
|
-
rl.close();
|
|
248
|
-
resolve(code.trim());
|
|
249
|
-
});
|
|
250
|
-
});
|
|
251
|
-
|
|
252
|
-
if (!code) {
|
|
253
|
-
console.error('Authorization code is required.');
|
|
254
|
-
return false;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
try {
|
|
258
|
-
const { tokens } = await client.getToken({
|
|
259
|
-
code,
|
|
260
|
-
codeVerifier: codeVerifier.codeVerifier,
|
|
261
|
-
redirect_uri: redirectUri,
|
|
262
|
-
});
|
|
263
|
-
client.setCredentials(tokens);
|
|
264
|
-
} catch (error) {
|
|
265
|
-
console.error(
|
|
266
|
-
'Failed to authenticate with authorization code:',
|
|
267
|
-
getErrorMessage(error),
|
|
268
|
-
);
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
async function authWithWeb(client: OAuth2Client): Promise<OauthWebLogin> {
|
|
275
|
-
const port = await getAvailablePort();
|
|
276
|
-
// The hostname used for the HTTP server binding (e.g., '0.0.0.0' in Docker).
|
|
277
|
-
const host = process.env['OAUTH_CALLBACK_HOST'] || 'localhost';
|
|
278
|
-
// The `redirectUri` sent to Google's authorization server MUST use a loopback IP literal
|
|
279
|
-
// (i.e., 'localhost' or '127.0.0.1'). This is a strict security policy for credentials of
|
|
280
|
-
// type 'Desktop app' or 'Web application' (when using loopback flow) to mitigate
|
|
281
|
-
// authorization code interception attacks.
|
|
282
|
-
const redirectUri = `http://localhost:${port}/oauth2callback`;
|
|
283
|
-
const state = crypto.randomBytes(32).toString('hex');
|
|
284
|
-
const authUrl = client.generateAuthUrl({
|
|
285
|
-
redirect_uri: redirectUri,
|
|
286
|
-
access_type: 'offline',
|
|
287
|
-
scope: OAUTH_SCOPE,
|
|
288
|
-
state,
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
const loginCompletePromise = new Promise<void>((resolve, reject) => {
|
|
292
|
-
const server = http.createServer(async (req, res) => {
|
|
293
|
-
try {
|
|
294
|
-
if (req.url!.indexOf('/oauth2callback') === -1) {
|
|
295
|
-
res.writeHead(HTTP_REDIRECT, { Location: SIGN_IN_FAILURE_URL });
|
|
296
|
-
res.end();
|
|
297
|
-
reject(
|
|
298
|
-
new FatalAuthenticationError(
|
|
299
|
-
'OAuth callback not received. Unexpected request: ' + req.url,
|
|
300
|
-
),
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
// acquire the code from the querystring, and close the web server.
|
|
304
|
-
const qs = new url.URL(req.url!, 'http://localhost:3000').searchParams;
|
|
305
|
-
if (qs.get('error')) {
|
|
306
|
-
res.writeHead(HTTP_REDIRECT, { Location: SIGN_IN_FAILURE_URL });
|
|
307
|
-
res.end();
|
|
308
|
-
|
|
309
|
-
const errorCode = qs.get('error');
|
|
310
|
-
const errorDescription =
|
|
311
|
-
qs.get('error_description') || 'No additional details provided';
|
|
312
|
-
reject(
|
|
313
|
-
new FatalAuthenticationError(
|
|
314
|
-
`Google OAuth error: ${errorCode}. ${errorDescription}`,
|
|
315
|
-
),
|
|
316
|
-
);
|
|
317
|
-
} else if (qs.get('state') !== state) {
|
|
318
|
-
res.end('State mismatch. Possible CSRF attack');
|
|
319
|
-
|
|
320
|
-
reject(
|
|
321
|
-
new FatalAuthenticationError(
|
|
322
|
-
'OAuth state mismatch. Possible CSRF attack or browser session issue.',
|
|
323
|
-
),
|
|
324
|
-
);
|
|
325
|
-
} else if (qs.get('code')) {
|
|
326
|
-
try {
|
|
327
|
-
const { tokens } = await client.getToken({
|
|
328
|
-
code: qs.get('code')!,
|
|
329
|
-
redirect_uri: redirectUri,
|
|
330
|
-
});
|
|
331
|
-
client.setCredentials(tokens);
|
|
332
|
-
|
|
333
|
-
// Retrieve and cache Google Account ID during authentication
|
|
334
|
-
try {
|
|
335
|
-
await fetchAndCacheUserInfo(client);
|
|
336
|
-
} catch (error) {
|
|
337
|
-
console.warn(
|
|
338
|
-
'Failed to retrieve Google Account ID during authentication:',
|
|
339
|
-
getErrorMessage(error),
|
|
340
|
-
);
|
|
341
|
-
// Don't fail the auth flow if Google Account ID retrieval fails
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
res.writeHead(HTTP_REDIRECT, { Location: SIGN_IN_SUCCESS_URL });
|
|
345
|
-
res.end();
|
|
346
|
-
resolve();
|
|
347
|
-
} catch (error) {
|
|
348
|
-
res.writeHead(HTTP_REDIRECT, { Location: SIGN_IN_FAILURE_URL });
|
|
349
|
-
res.end();
|
|
350
|
-
reject(
|
|
351
|
-
new FatalAuthenticationError(
|
|
352
|
-
`Failed to exchange authorization code for tokens: ${getErrorMessage(error)}`,
|
|
353
|
-
),
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
} else {
|
|
357
|
-
reject(
|
|
358
|
-
new FatalAuthenticationError(
|
|
359
|
-
'No authorization code received from Google OAuth. Please try authenticating again.',
|
|
360
|
-
),
|
|
361
|
-
);
|
|
362
|
-
}
|
|
363
|
-
} catch (e) {
|
|
364
|
-
// Provide more specific error message for unexpected errors during OAuth flow
|
|
365
|
-
if (e instanceof FatalAuthenticationError) {
|
|
366
|
-
reject(e);
|
|
367
|
-
} else {
|
|
368
|
-
reject(
|
|
369
|
-
new FatalAuthenticationError(
|
|
370
|
-
`Unexpected error during OAuth authentication: ${getErrorMessage(e)}`,
|
|
371
|
-
),
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
} finally {
|
|
375
|
-
server.close();
|
|
376
|
-
}
|
|
377
|
-
});
|
|
378
|
-
|
|
379
|
-
server.listen(port, host, () => {
|
|
380
|
-
// Server started successfully
|
|
381
|
-
});
|
|
382
|
-
|
|
383
|
-
server.on('error', (err) => {
|
|
384
|
-
reject(
|
|
385
|
-
new FatalAuthenticationError(
|
|
386
|
-
`OAuth callback server error: ${getErrorMessage(err)}`,
|
|
387
|
-
),
|
|
388
|
-
);
|
|
389
|
-
});
|
|
390
|
-
});
|
|
391
|
-
|
|
392
|
-
return {
|
|
393
|
-
authUrl,
|
|
394
|
-
loginCompletePromise,
|
|
395
|
-
};
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
export function getAvailablePort(): Promise<number> {
|
|
399
|
-
return new Promise((resolve, reject) => {
|
|
400
|
-
let port = 0;
|
|
401
|
-
try {
|
|
402
|
-
const portStr = process.env['OAUTH_CALLBACK_PORT'];
|
|
403
|
-
if (portStr) {
|
|
404
|
-
port = parseInt(portStr, 10);
|
|
405
|
-
if (isNaN(port) || port <= 0 || port > 65535) {
|
|
406
|
-
return reject(
|
|
407
|
-
new Error(`Invalid value for OAUTH_CALLBACK_PORT: "${portStr}"`),
|
|
408
|
-
);
|
|
409
|
-
}
|
|
410
|
-
return resolve(port);
|
|
411
|
-
}
|
|
412
|
-
const server = net.createServer();
|
|
413
|
-
server.listen(0, () => {
|
|
414
|
-
const address = server.address()! as net.AddressInfo;
|
|
415
|
-
port = address.port;
|
|
416
|
-
});
|
|
417
|
-
server.on('listening', () => {
|
|
418
|
-
server.close();
|
|
419
|
-
server.unref();
|
|
420
|
-
});
|
|
421
|
-
server.on('error', (e) => reject(e));
|
|
422
|
-
server.on('close', () => resolve(port));
|
|
423
|
-
} catch (e) {
|
|
424
|
-
reject(e);
|
|
425
|
-
}
|
|
426
|
-
});
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
async function loadCachedCredentials(client: OAuth2Client): Promise<boolean> {
|
|
430
|
-
const useEncryptedStorage = getUseEncryptedStorageFlag();
|
|
431
|
-
if (useEncryptedStorage) {
|
|
432
|
-
const credentials = await OAuthCredentialStorage.loadCredentials();
|
|
433
|
-
if (credentials) {
|
|
434
|
-
client.setCredentials(credentials);
|
|
435
|
-
return true;
|
|
436
|
-
}
|
|
437
|
-
return false;
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
const pathsToTry = [
|
|
441
|
-
Storage.getOAuthCredsPath(),
|
|
442
|
-
process.env['GOOGLE_APPLICATION_CREDENTIALS'],
|
|
443
|
-
].filter((p): p is string => !!p);
|
|
444
|
-
|
|
445
|
-
for (const keyFile of pathsToTry) {
|
|
446
|
-
try {
|
|
447
|
-
const creds = await fs.readFile(keyFile, 'utf-8');
|
|
448
|
-
client.setCredentials(JSON.parse(creds));
|
|
449
|
-
|
|
450
|
-
// This will verify locally that the credentials look good.
|
|
451
|
-
const { token } = await client.getAccessToken();
|
|
452
|
-
if (!token) {
|
|
453
|
-
continue;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
// This will check with the server to see if it hasn't been revoked.
|
|
457
|
-
await client.getTokenInfo(token);
|
|
458
|
-
|
|
459
|
-
return true;
|
|
460
|
-
} catch (error) {
|
|
461
|
-
// Log specific error for debugging, but continue trying other paths
|
|
462
|
-
console.debug(
|
|
463
|
-
`Failed to load credentials from ${keyFile}:`,
|
|
464
|
-
getErrorMessage(error),
|
|
465
|
-
);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
return false;
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
async function cacheCredentials(credentials: Credentials) {
|
|
473
|
-
const filePath = Storage.getOAuthCredsPath();
|
|
474
|
-
await fs.mkdir(path.dirname(filePath), { recursive: true });
|
|
475
|
-
|
|
476
|
-
const credString = JSON.stringify(credentials, null, 2);
|
|
477
|
-
await fs.writeFile(filePath, credString, { mode: 0o600 });
|
|
478
|
-
try {
|
|
479
|
-
await fs.chmod(filePath, 0o600);
|
|
480
|
-
} catch {
|
|
481
|
-
/* empty */
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
export function clearOauthClientCache() {
|
|
486
|
-
oauthClientPromises.clear();
|
|
487
|
-
}
|
|
488
|
-
|
|
489
|
-
export async function clearCachedCredentialFile() {
|
|
490
|
-
try {
|
|
491
|
-
const useEncryptedStorage = getUseEncryptedStorageFlag();
|
|
492
|
-
if (useEncryptedStorage) {
|
|
493
|
-
await OAuthCredentialStorage.clearCredentials();
|
|
494
|
-
} else {
|
|
495
|
-
await fs.rm(Storage.getOAuthCredsPath(), { force: true });
|
|
496
|
-
}
|
|
497
|
-
// Clear the Google Account ID cache when credentials are cleared
|
|
498
|
-
await userAccountManager.clearCachedGoogleAccount();
|
|
499
|
-
// Clear the in-memory OAuth client cache to force re-authentication
|
|
500
|
-
clearOauthClientCache();
|
|
501
|
-
} catch (e) {
|
|
502
|
-
console.error('Failed to clear cached credentials:', e);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
async function fetchAndCacheUserInfo(client: OAuth2Client): Promise<void> {
|
|
507
|
-
try {
|
|
508
|
-
const { token } = await client.getAccessToken();
|
|
509
|
-
if (!token) {
|
|
510
|
-
return;
|
|
511
|
-
}
|
|
512
|
-
|
|
513
|
-
const response = await fetch(
|
|
514
|
-
'https://www.googleapis.com/oauth2/v2/userinfo',
|
|
515
|
-
{
|
|
516
|
-
headers: {
|
|
517
|
-
Authorization: `Bearer ${token}`,
|
|
518
|
-
},
|
|
519
|
-
},
|
|
520
|
-
);
|
|
521
|
-
|
|
522
|
-
if (!response.ok) {
|
|
523
|
-
console.error(
|
|
524
|
-
'Failed to fetch user info:',
|
|
525
|
-
response.status,
|
|
526
|
-
response.statusText,
|
|
527
|
-
);
|
|
528
|
-
return;
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
const userInfo = await response.json();
|
|
532
|
-
await userAccountManager.cacheGoogleAccount(userInfo.email);
|
|
533
|
-
} catch (error) {
|
|
534
|
-
console.error('Error retrieving user info:', error);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
// Helper to ensure test isolation
|
|
539
|
-
export function resetOauthClientForTesting() {
|
|
540
|
-
oauthClientPromises.clear();
|
|
541
|
-
}
|