Skip to content

Commit

Permalink
Look for keytar in the CLI dependency tree first (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
childish-sambino authored Jul 10, 2019
1 parent 85ec23c commit e938997
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/services/secure-storage.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const keytar = require('keytar');
let keytar; // Lazy-loaded below.
const { CLI_NAME } = require('./config');
const { logger } = require('../services/messaging/logging');

const STORAGE_LOCATIONS = {
KEYCHAIN: 'keychain',
Expand All @@ -18,18 +19,44 @@ class SecureStorage {
this.platform = platform || process.platform;
}

get keytar() {
if (!keytar) {
// Since keytar is a native module (compiled for the specific version of
// Node it was installed with), we don't want plugins that depend on us
// to depend on it. Otherwise, they'd need to reinstall each time Node
// changes since it requires keytar be reinstalled. For this reason, we
// don't expect to find keytar under the normal dependency tree. It
// should exist with the CLI's dependencies. We'll attempt to find out
// where that is based on the location of the script that kicked off the
// process. Resolve any symlinks along the way to find the "real" path
// and then load up the module. If this fails for whatever reason,
// fallback to the standard location (and log some debug).
try {
const realAppPath = require('fs').realpathSync(process.argv[1]);
const realKeytarPath = require.resolve('keytar', { paths: [realAppPath] });

keytar = require(realKeytarPath);
} catch (error) {
logger.debug('Failed to find the keytar module with the CLI: ' + error.message);

keytar = require('keytar');
}
}
return keytar;
}

async saveCredentials(projectId, username, password) {
await keytar.setPassword(CLI_NAME, projectId, username + '|' + password);
await this.keytar.setPassword(CLI_NAME, projectId, username + '|' + password);
}

async removeCredentials(projectId) {
return keytar.deletePassword(CLI_NAME, projectId);
return this.keytar.deletePassword(CLI_NAME, projectId);
}

async getCredentials(projectId) {
let credentials = null;
try {
credentials = await keytar.getPassword(CLI_NAME, projectId);
credentials = await this.keytar.getPassword(CLI_NAME, projectId);
} catch (e) {
return { apiKey: 'error', apiSecret: e.message };
}
Expand Down

0 comments on commit e938997

Please sign in to comment.