This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathkeytar-strategy.js
262 lines (226 loc) · 6.69 KB
/
keytar-strategy.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* eslint comma-dangle: ["error", {
"arrays": "never",
"objects": "never",
"imports": "never",
"exports": "never",
"functions": "never"
}] */
const {execFile} = require('child_process');
const fs = require('fs');
if (typeof atom === 'undefined') {
global.atom = {
inSpecMode() { return !!process.env.ATOM_GITHUB_SPEC_MODE; },
inDevMode() { return false; }
};
}
// No token available in your OS keychain.
const UNAUTHENTICATED = Symbol('UNAUTHENTICATED');
// The token in your keychain isn't granted all of the required OAuth scopes.
const INSUFFICIENT = Symbol('INSUFFICIENT');
// The token in your keychain is not accepted by GitHub.
const UNAUTHORIZED = Symbol('UNAUTHORIZED');
class KeytarStrategy {
static get keytar() {
return require('keytar');
}
static async isValid() {
// Allow for disabling Keytar on problematic CI environments
if (process.env.ATOM_GITHUB_DISABLE_KEYTAR) {
return false;
}
const keytar = this.keytar;
try {
const rand = Math.floor(Math.random() * 10e20).toString(16);
await keytar.setPassword('atom-test-service', rand, rand);
const pass = await keytar.getPassword('atom-test-service', rand);
const success = pass === rand;
keytar.deletePassword('atom-test-service', rand);
return success;
} catch (err) {
return false;
}
}
async getPassword(service, account) {
const password = await this.constructor.keytar.getPassword(service, account);
return password !== null ? password : UNAUTHENTICATED;
}
replacePassword(service, account, password) {
return this.constructor.keytar.setPassword(service, account, password);
}
deletePassword(service, account) {
return this.constructor.keytar.deletePassword(service, account);
}
}
class SecurityBinaryStrategy {
static isValid() {
return process.platform === 'darwin';
}
async getPassword(service, account) {
try {
const password = await this.exec(['find-generic-password', '-s', service, '-a', account, '-w']);
return password.trim() || UNAUTHENTICATED;
} catch (err) {
return UNAUTHENTICATED;
}
}
replacePassword(service, account, newPassword) {
return this.exec(['add-generic-password', '-s', service, '-a', account, '-w', newPassword, '-U']);
}
deletePassword(service, account) {
return this.exec(['delete-generic-password', '-s', service, '-a', account]);
}
exec(securityArgs, {binary} = {binary: 'security'}) {
return new Promise((resolve, reject) => {
execFile(binary, securityArgs, (error, stdout) => {
if (error) { return reject(error); }
return resolve(stdout);
});
});
}
}
class InMemoryStrategy {
static isValid() {
return true;
}
constructor() {
if (!atom.inSpecMode()) {
// eslint-disable-next-line no-console
console.warn(
'Using an InMemoryStrategy strategy for storing tokens. ' +
'The tokens will only be stored for the current window.'
);
}
this.passwordsByService = new Map();
}
getPassword(service, account) {
const passwords = this.passwordsByService.get(service) || new Map();
const password = passwords.get(account);
return password || UNAUTHENTICATED;
}
replacePassword(service, account, newPassword) {
const passwords = this.passwordsByService.get(service) || new Map();
passwords.set(account, newPassword);
this.passwordsByService.set(service, passwords);
}
deletePassword(service, account) {
const passwords = this.passwordsByService.get(service);
if (passwords) {
passwords.delete(account);
}
}
}
class FileStrategy {
static isValid() {
if (!atom.inSpecMode() && !atom.inDevMode()) {
return false;
}
return Boolean(process.env.ATOM_GITHUB_KEYTAR_FILE);
}
constructor() {
this.filePath = process.env.ATOM_GITHUB_KEYTAR_FILE;
if (!atom.inSpecMode()) {
// eslint-disable-next-line no-console
console.warn(
'Using a FileStrategy strategy for storing tokens. ' +
'The tokens will be stored %cin the clear%c in a file at %s. ' +
"You probably shouldn't use real credentials while this strategy is in use. " +
'Unset ATOM_GITHUB_KEYTAR_FILE_STRATEGY to disable it.',
'color: red; font-weight: bold; font-style: italic',
'color: black; font-weight: normal; font-style: normal',
this.filePath
);
}
}
async getPassword(service, account) {
const payload = await this.load();
const forService = payload[service];
if (forService === undefined) {
return UNAUTHENTICATED;
}
const passwd = forService[account];
if (passwd === undefined) {
return UNAUTHENTICATED;
}
return passwd;
}
replacePassword(service, account, password) {
return this.modify(payload => {
let forService = payload[service];
if (forService === undefined) {
forService = {};
payload[service] = forService;
}
forService[account] = password;
});
}
deletePassword(service, account) {
return this.modify(payload => {
const forService = payload[service];
if (forService === undefined) {
return;
}
delete forService[account];
if (Object.keys(forService).length === 0) {
delete payload[service];
}
});
}
load() {
return new Promise((resolve, reject) => {
fs.readFile(this.filePath, 'utf8', (err, content) => {
if (err && err.code === 'ENOENT') {
return resolve({});
}
if (err) {
return reject(err);
}
return resolve(JSON.parse(content));
});
});
}
save(payload) {
return new Promise((resolve, reject) => {
fs.writeFile(this.filePath, JSON.stringify(payload), 'utf8', err => {
if (err) {
reject(err);
} else {
resolve();
}
});
});
}
async modify(callback) {
const payload = await this.load();
callback(payload);
await this.save(payload);
}
}
const strategies = [FileStrategy, KeytarStrategy, SecurityBinaryStrategy, InMemoryStrategy];
let ValidStrategy = null;
async function createStrategy() {
if (ValidStrategy) {
return new ValidStrategy();
}
for (let i = 0; i < strategies.length; i++) {
const strat = strategies[i];
const isValid = await strat.isValid();
if (isValid) {
ValidStrategy = strat;
break;
}
}
if (!ValidStrategy) {
throw new Error('None of the listed keytar strategies returned true for `isValid`');
}
return new ValidStrategy();
}
module.exports = {
UNAUTHENTICATED,
INSUFFICIENT,
UNAUTHORIZED,
KeytarStrategy,
SecurityBinaryStrategy,
InMemoryStrategy,
FileStrategy,
createStrategy
};