@@ -18,7 +18,7 @@ import { Account, User } from "../../src/types/auth";
18
18
import { Options } from "../../src/options" ;
19
19
import { currentOptions , getCommandOptions } from "./options" ;
20
20
import { setInquirerOptions } from "./stubs/inquirer-stub" ;
21
- import { ServiceAccount , ServiceAccountUser } from "../common/types" ;
21
+ import { ServiceAccount } from "../common/types" ;
22
22
import { listChannels } from "../../src/hosting/api" ;
23
23
import { ChannelWithId } from "../common/messaging/types" ;
24
24
import { pluginLogger } from "./logger-wrapper" ;
@@ -33,11 +33,26 @@ import { setAccessToken } from "../../src/apiv2";
33
33
async function getServiceAccount ( ) {
34
34
let email = null ;
35
35
try {
36
+ // Empty to make sure no oauth user/token is sent to requireAuth
37
+ // which would prevent autoAuth() from being reached
36
38
email = ( await requireAuth ( { } ) ) || null ;
37
39
} catch ( e ) {
38
40
pluginLogger . debug ( 'No service account found (this may be normal), requireAuth error output:' ,
39
41
e . original || e ) ;
42
+ return null ;
40
43
}
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 } ` ) ;
41
56
return email ;
42
57
}
43
58
@@ -50,7 +65,6 @@ async function requireAuthWrapper(showError: boolean = true): Promise<boolean> {
50
65
// Try to get global default from configstore. For some reason this is
51
66
// often overwritten when restarting the extension.
52
67
pluginLogger . debug ( 'requireAuthWrapper' ) ;
53
- let authFound = false ;
54
68
let account = getGlobalDefaultAccount ( ) ;
55
69
if ( ! account ) {
56
70
// If nothing in configstore top level, grab the first "additionalAccount"
@@ -62,9 +76,6 @@ async function requireAuthWrapper(showError: boolean = true): Promise<boolean> {
62
76
}
63
77
}
64
78
}
65
- if ( account ) {
66
- authFound = true ;
67
- }
68
79
const commandOptions = await getCommandOptions ( undefined , {
69
80
...currentOptions
70
81
} ) ;
@@ -77,34 +88,38 @@ async function requireAuthWrapper(showError: boolean = true): Promise<boolean> {
77
88
const serviceAccountEmail = await getServiceAccount ( ) ;
78
89
// Priority 1: Service account exists and is the current selected user
79
90
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 ;
81
94
} else if ( account ) {
82
95
// Priority 2: Google login account exists and is the currently selected
83
96
// user
84
97
// Priority 3: Google login account exists and there is no selected user
85
98
// Clear service account access token from memory in apiv2.
86
99
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 ;
88
107
}
108
+ pluginLogger . debug ( 'No user found (this may be normal)' ) ;
109
+ return false ;
89
110
} catch ( e ) {
90
- // No service account or google login found.
91
111
if ( showError ) {
92
- pluginLogger . error ( 'requireAuth error' , e . original || e ) ;
112
+ pluginLogger . error ( 'requireAuth error: ' , e . original || e ) ;
93
113
vscode . window . showErrorMessage ( "Not logged in" , {
94
114
modal : true ,
95
115
detail : `Log in by clicking "Sign in with Google" in the sidebar.` ,
96
116
} ) ;
97
117
} 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: ' ,
101
119
e . original || e ) ;
102
120
}
103
121
return false ;
104
122
}
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 ;
108
123
}
109
124
110
125
export async function getAccounts ( ) : Promise < Array < Account | ServiceAccount > > {
0 commit comments