Skip to content

Commit af6d923

Browse files
authored
Merge pull request #20 from GitGuardian/salomevoltz/fix-multiple-instance-loggin
fix(auth-login): FIx auth login with multiple instances
2 parents d8e42c7 + 7d272bf commit af6d923

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/extension.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,14 @@ export function activate(context: ExtensionContext) {
173173
.then(() => {
174174
// Check if ggshield is authenticated
175175
authStatus = ggshieldAuthStatus(configuration);
176-
if (!authStatus) {
177-
updateStatusBarItem(StatusBarStatus.unauthenticated, statusBar);
178-
} else {
176+
const ggshieldApi = ggshieldApiKey(configuration);
177+
if (authStatus && ggshieldApi) {
179178
commands.executeCommand('setContext', 'isAuthenticated', true);
180179
updateStatusBarItem(StatusBarStatus.ready, statusBar);
181-
const ggshieldApi = ggshieldApiKey(configuration);
182180
setApiKey(configuration, ggshieldApi);
181+
} else {
182+
updateStatusBarItem(StatusBarStatus.unauthenticated, statusBar);
183+
authStatus = false;
183184
}
184185
})
185186
.then(async () => {

src/lib/ggshield-api.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ export function runGGShieldCommand(
2727
let env: {
2828
GITGUARDIAN_API_URL: string;
2929
GG_USER_AGENT: string;
30+
GITGUARDIAN_DONT_LOAD_ENV: string;
3031
GITGUARDIAN_API_KEY?: string; // Note the ? to indicate this property is optional
31-
} = {
32+
} = {
3233
GITGUARDIAN_API_URL: apiUrl,
3334
GG_USER_AGENT: "gitguardian-vscode",
35+
GITGUARDIAN_DONT_LOAD_ENV: "true",
3436
};
3537

3638
if (apiKey) {
@@ -43,6 +45,7 @@ export function runGGShieldCommand(
4345

4446
let options: SpawnSyncOptionsWithStringEncoding = {
4547
cwd: os.tmpdir(),
48+
env: env,
4649
encoding: "utf-8",
4750
windowsHide: true,
4851
};
@@ -273,6 +276,11 @@ export function ggshieldAuthStatus(
273276
}
274277
}
275278

279+
/**
280+
* Get ggshield API key from ggshield config list
281+
*
282+
* Search for the correct instance section and return the token
283+
* */
276284
export function ggshieldApiKey(
277285
configuration: GGShieldConfiguration,
278286
): string | undefined {
@@ -292,8 +300,13 @@ export function ggshieldApiKey(
292300
const instanceSection = instanceSectionMatch[0];
293301
const regexToken = /token:\s([a-zA-Z0-9]+)/;
294302
const matchToken = instanceSection.match(regexToken);
303+
304+
// if the token is not found, or is not a valid token, return undefined
305+
if (!matchToken || matchToken[1].trim().length !== 71 ) {
306+
return undefined;
307+
}
295308

296-
return matchToken ? matchToken[1].trim() : undefined;
309+
return matchToken[1].trim();
297310
}
298311
}
299312
}

0 commit comments

Comments
 (0)