Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Look for keytar in the CLI dependency tree first #36

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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