Skip to content

Commit

Permalink
changed the location of calling keytar
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Nov 23, 2023
1 parent aef6429 commit 07b0695
Showing 1 changed file with 28 additions and 23 deletions.
51 changes: 28 additions & 23 deletions lib/authentication/secureStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ const GlobalConfig = require('../global_config');
const Logger = require('../logger');
const os = require('os');
const fs = require('fs/promises');
let keytar;
if(!isLinuxMachine()){
keytar = require('keytar');
}



function createCredentialCacheDir() {
Expand Down Expand Up @@ -41,13 +38,15 @@ async function writeCredential(host, user, credType, token){
Logger.getInstance().trace('The os is ', os.platform());
if (!token || token === '') {
Logger.getInstance().debug('Token is not provided');
return;
} else {
try {
if (isLinuxMachine()) {
try {
const keytar = require('keytar');
await keytar.setPassword(host, buildTemporaryCredentialName(host, user, credType), token);
} catch(err) {
const dir = createCredentialCacheDir();
await fs.writeFile(dir, token, 'utf8');
} else {
await keytar.setPassword(host, buildTemporaryCredentialName(host, user, credType), token);
}
} catch (err){
Logger.getInstance().error('Failed to save Credential: ', err.message);
Expand All @@ -56,25 +55,31 @@ async function writeCredential(host, user, credType, token){
}

async function readCredential(host, user, credType) {
Logger.getInstance().trace('The os is ', os.platform());

if (isLinuxMachine()) {
const dir = createCredentialCacheDir();
return await fs.readFile(dir, 'utf8');
} else {
return await keytar.getPassword(host, buildTemporaryCredentialName(host, user, credType));
}
try{
try{
const keytar = require('keytar');
return await keytar.getPassword(host, buildTemporaryCredentialName(host, user, credType));
}catch(err) {
const dir = createCredentialCacheDir();
return await fs.readFile(dir, 'utf8');
}
}catch (err){
Logger.getInstance().error('Failed to save Credential: ', err.message);
}
}

async function deleteCredential(host, user, credType) {
Logger.getInstance().trace('The os is ', os.platform());

if (isLinuxMachine()) {
const dir = createCredentialCacheDir();
return await fs.unlink(dir);
} else {
await keytar.deletePassword(host, buildTemporaryCredentialName(host, user, credType));
}
try{
try{
const keytar = require('keytar');
return await keytar.deletePassword(host, buildTemporaryCredentialName(host, user, credType));
}catch(err) {
const dir = createCredentialCacheDir();
return await fs.unlink(dir);
}
}catch (err){
Logger.getInstance().error('Failed to save Credential: ', err.message);
}
}

module.exports = {
Expand Down

0 comments on commit 07b0695

Please sign in to comment.