-
Notifications
You must be signed in to change notification settings - Fork 17
/
keychain.service.js
110 lines (98 loc) · 2.94 KB
/
keychain.service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const keytar = require('keytar');
const SERVICE_NAME = 'ReQLPro';
function getAccountNameForPassword(host, port, user) {
if (!host) {
throw new Error('host is required');
}
if (!port) {
throw new Error('port is required');
}
if (!user) {
throw new Error('user is required');
}
return user + '@' + host + ':' + port;
}
function getAccountNameForCert(host, port) {
if (!host) {
throw new Error('host is required');
}
if (!port) {
throw new Error('port is required');
}
return host + ':' + port + '_cert';
}
function updatePassword(host, port, user, password) {
if (!user) {
return Promise.resolve('');
}
const account = getAccountNameForPassword(host, port, user);
if (password) {
return keytar.setPassword(SERVICE_NAME, account, password);
} else {
return keytar.deletePassword(SERVICE_NAME, account);
}
}
function updateCert(host, port, cert) {
const account = getAccountNameForCert(host, port);
if (cert) {
return keytar.setPassword(SERVICE_NAME, account, cert);
} else {
return keytar.deletePassword(SERVICE_NAME, account);
}
}
function updateKeysForConnection(connection) {
if (!connection) {
throw new Error('connection is required');
}
return new Promise((resolve, reject) => {
// Set default host and port if not supplied
const host = connection.host || 'localhost';
const port = connection.port || '28015';
updatePassword(host, port, connection.user, connection.pass).then((pass) => {
updateCert(host, port, connection.ca).then((ca) => {
resolve({
pass: pass || '',
ca: ca || ''
});
});
});
});
}
function getPassword(host, port, user) {
if (!user) {
return Promise.resolve('');
}
const account = getAccountNameForPassword(host, port, user);
return keytar.getPassword(SERVICE_NAME, account);
}
function getCert(host, port) {
const account = getAccountNameForCert(host, port);
return keytar.getPassword(SERVICE_NAME, account);
}
function getKeysForConnection(connection) {
if (!connection) {
throw new Error('connection is required');
}
return new Promise((resolve, reject) => {
// Set default host and port if not supplied
const host = connection.host || 'localhost';
const port = connection.port || '28015';
getPassword(host, port, connection.user).then((pass) => {
getCert(host, port).then((ca) => {
resolve({
pass: pass || '',
ca: ca || ''
});
});
});
});
}
module.exports.SERVICE_NAME = SERVICE_NAME;
module.exports.getAccountNameForPassword = getAccountNameForPassword;
module.exports.getAccountNameForCert = getAccountNameForCert;
module.exports.updatePassword = updatePassword;
module.exports.updateCert = updateCert;
module.exports.updateKeysForConnection = updateKeysForConnection;
module.exports.getPassword = getPassword;
module.exports.getCert = getCert;
module.exports.getKeysForConnection = getKeysForConnection;