Skip to content

Commit d9008e7

Browse files
committed
warn when api keys mismatch
1 parent 11b47eb commit d9008e7

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

src/options.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,39 +207,60 @@ export class Options {
207207
return this.cache.api_key;
208208
}
209209

210+
let from = '';
211+
210212
const keyFromSettings = this.getApiKeyFromEditor();
211213
if (!Utils.apiKeyInvalid(keyFromSettings)) {
212214
this.cache.api_key = keyFromSettings;
213-
return this.cache.api_key;
215+
from = 'settings.json editor';
214216
}
215217

216218
const keyFromEnv = this.getApiKeyFromEnv();
217219
if (!Utils.apiKeyInvalid(keyFromEnv)) {
220+
if (this.cache.api_key && this.cache.api_key !== keyFromEnv) {
221+
vscode.window.showErrorMessage(
222+
`WakaTime API Key conflict. Your env key doesn't match your ${from} key.`,
223+
);
224+
return this.cache.api_key;
225+
}
218226
this.cache.api_key = keyFromEnv;
219-
return this.cache.api_key;
227+
from = 'env var';
220228
}
221229

222230
try {
223231
const apiKeyFromVault = await this.getApiKeyFromVaultCmd();
224232
if (!Utils.apiKeyInvalid(apiKeyFromVault)) {
233+
if (this.cache.api_key && this.cache.api_key !== apiKeyFromVault) {
234+
vscode.window.showErrorMessage(
235+
`WakaTime API Key conflict. Your vault command key doesn't match your ${from} key.`,
236+
);
237+
return this.cache.api_key;
238+
}
225239
this.cache.api_key = apiKeyFromVault;
226-
return this.cache.api_key;
240+
from = 'vault command';
227241
}
228242
} catch (err) {}
229243

230244
try {
231245
const apiKey = await this.getSettingAsync<string>('settings', 'api_key');
232-
if (!Utils.apiKeyInvalid(apiKey)) this.cache.api_key = apiKey;
233-
return apiKey;
246+
if (!Utils.apiKeyInvalid(apiKey)) {
247+
if (this.cache.api_key && this.cache.api_key !== apiKey) {
248+
vscode.window.showErrorMessage(
249+
`WakaTime API Key conflict. Your ~/.wakatime.cfg key doesn't match your ${from} key.`,
250+
);
251+
}
252+
this.cache.api_key = apiKey;
253+
}
234254
} catch (err) {
235255
this.logger.debug(`Exception while reading API Key from config file: ${err}`);
236-
if (`${err}`.includes('spawn EPERM')) {
256+
if (!this.cache.api_key && `${err}`.includes('spawn EPERM')) {
237257
vscode.window.showErrorMessage(
238258
'Microsoft Defender is blocking WakaTime. Please allow WakaTime to run so it can upload code stats to your dashboard.',
239259
);
240260
}
241-
return '';
242261
}
262+
263+
return this.cache.api_key ?? '';
243264
}
244265

245266
public async getApiKeyFromVaultCmd(): Promise<string> {

0 commit comments

Comments
 (0)