Skip to content

Commit 473deff

Browse files
authored
Fix requireAuthWrapper logic and service account email detection (#6115)
1 parent 6b27c62 commit 473deff

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

firebase-vscode/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

firebase-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"displayName": "firebase-vscode",
44
"publisher": "firebase",
55
"description": "VSCode Extension for Firebase",
6-
"version": "0.0.23-alpha.3",
6+
"version": "0.0.23-alpha.4",
77
"engines": {
88
"vscode": "^1.69.0"
99
},

firebase-vscode/src/cli.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { Account, User } from "../../src/types/auth";
1818
import { Options } from "../../src/options";
1919
import { currentOptions, getCommandOptions } from "./options";
2020
import { setInquirerOptions } from "./stubs/inquirer-stub";
21-
import { ServiceAccount, ServiceAccountUser } from "../common/types";
21+
import { ServiceAccount } from "../common/types";
2222
import { listChannels } from "../../src/hosting/api";
2323
import { ChannelWithId } from "../common/messaging/types";
2424
import { pluginLogger } from "./logger-wrapper";
@@ -33,11 +33,26 @@ import { setAccessToken } from "../../src/apiv2";
3333
async function getServiceAccount() {
3434
let email = null;
3535
try {
36+
// Empty to make sure no oauth user/token is sent to requireAuth
37+
// which would prevent autoAuth() from being reached
3638
email = (await requireAuth({})) || null;
3739
} catch (e) {
3840
pluginLogger.debug('No service account found (this may be normal), requireAuth error output:',
3941
e.original || e);
42+
return null;
4043
}
44+
if (process.env.WORKSPACE_SERVICE_ACCOUNT_EMAIL) {
45+
// If Monospace, get service account email using env variable as
46+
// the metadata server doesn't currently return the credentials
47+
// for the workspace service account. Remove when Monospace is
48+
// updated to return credentials through the metadata server.
49+
pluginLogger.debug(`Using WORKSPACE_SERVICE_ACCOUNT_EMAIL env `
50+
+ `variable to get service account email: `
51+
+ `${process.env.WORKSPACE_SERVICE_ACCOUNT_EMAIL}`);
52+
return process.env.WORKSPACE_SERVICE_ACCOUNT_EMAIL;
53+
}
54+
pluginLogger.debug(`Got service account email through credentials:`
55+
+ ` ${email}`);
4156
return email;
4257
}
4358

@@ -50,7 +65,6 @@ async function requireAuthWrapper(showError: boolean = true): Promise<boolean> {
5065
// Try to get global default from configstore. For some reason this is
5166
// often overwritten when restarting the extension.
5267
pluginLogger.debug('requireAuthWrapper');
53-
let authFound = false;
5468
let account = getGlobalDefaultAccount();
5569
if (!account) {
5670
// If nothing in configstore top level, grab the first "additionalAccount"
@@ -62,9 +76,6 @@ async function requireAuthWrapper(showError: boolean = true): Promise<boolean> {
6276
}
6377
}
6478
}
65-
if (account) {
66-
authFound = true;
67-
}
6879
const commandOptions = await getCommandOptions(undefined, {
6980
...currentOptions
7081
});
@@ -77,34 +88,38 @@ async function requireAuthWrapper(showError: boolean = true): Promise<boolean> {
7788
const serviceAccountEmail = await getServiceAccount();
7889
// Priority 1: Service account exists and is the current selected user
7990
if (serviceAccountEmail && currentUser.email === serviceAccountEmail) {
80-
await requireAuth(commandOptions);
91+
// requireAuth should have been run and apiv2 token should be stored
92+
// already due to getServiceAccount() call above.
93+
return true;
8194
} else if (account) {
8295
// Priority 2: Google login account exists and is the currently selected
8396
// user
8497
// Priority 3: Google login account exists and there is no selected user
8598
// Clear service account access token from memory in apiv2.
8699
setAccessToken();
87-
await requireAuth({...commandOptions, ...account});
100+
await requireAuth({ ...commandOptions, ...account });
101+
return true;
102+
} else if (serviceAccountEmail) {
103+
// Priority 4: There is a service account but it's not set as
104+
// currentUser for some reason, but there also isn't an oauth account.
105+
// requireAuth was already run as part of getServiceAccount() above
106+
return true;
88107
}
108+
pluginLogger.debug('No user found (this may be normal)');
109+
return false;
89110
} catch (e) {
90-
// No service account or google login found.
91111
if (showError) {
92-
pluginLogger.error('requireAuth error', e.original || e);
112+
pluginLogger.error('requireAuth error: ', e.original || e);
93113
vscode.window.showErrorMessage("Not logged in", {
94114
modal: true,
95115
detail: `Log in by clicking "Sign in with Google" in the sidebar.`,
96116
});
97117
} else {
98-
// If "showError" is false, this may not be an error, just an indication
99-
// no one is logged in. Log to "debug".
100-
pluginLogger.debug('No user found (this may be normal), requireAuth error output:',
118+
pluginLogger.debug('requireAuth error output: ',
101119
e.original || e);
102120
}
103121
return false;
104122
}
105-
// If we reach here, there is either a google account or no error on
106-
// requireAuth (which means there is a service account or glogin)
107-
return authFound;
108123
}
109124

110125
export async function getAccounts(): Promise<Array<Account | ServiceAccount>> {

0 commit comments

Comments
 (0)