-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials.js
More file actions
329 lines (282 loc) · 10.6 KB
/
Copy pathcredentials.js
File metadata and controls
329 lines (282 loc) · 10.6 KB
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
'use strict';
const { execFileSync } = require('child_process');
const crypto = require('crypto');
const fs = require('fs');
const os = require('os');
const path = require('path');
const readline = require('readline');
const SERVICE = 'tecli';
const CONFIG_PATH = path.join(os.homedir(), '.tecli.json');
const CRED_PATH = path.join(os.homedir(), '.tecli-credentials');
const DEFAULT_URL = 'https://te.leidos.com/cpweb/cploginform.htm?system=LEIDOS';
// ─── Config file (non-secret settings) ──────────────────────────
function readConfig() {
try {
return JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf8'));
} catch {
return {};
}
}
function writeConfig(config) {
fs.writeFileSync(CONFIG_PATH, JSON.stringify(config, null, 2) + '\n', { mode: 0o600 });
}
function deleteConfig() {
try { fs.unlinkSync(CONFIG_PATH); } catch { /* ignore */ }
}
// ─── Keychain backends ──────────────────────────────────────────
const platform = os.platform();
function keychainGet(account) {
if (platform === 'darwin') return _macGet(account);
if (platform === 'linux') return _linuxGet(account);
if (platform === 'win32') return _winGet(account);
return _fallbackGet(account);
}
function keychainSet(account, password) {
if (platform === 'darwin') return _macSet(account, password);
if (platform === 'linux') return _linuxSet(account, password);
if (platform === 'win32') return _winSet(account, password);
return _fallbackSet(account, password);
}
function keychainDelete(account) {
if (platform === 'darwin') return _macDelete(account);
if (platform === 'linux') return _linuxDelete(account);
if (platform === 'win32') return _winDelete();
return _fallbackDelete();
}
// ── macOS: security command ─────────────────────────────────────
function _macGet(account) {
try {
return execFileSync('security', [
'find-generic-password', '-s', SERVICE, '-a', account, '-w'
], { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
} catch {
return null;
}
}
function _macSet(account, password) {
// Delete first — add-generic-password fails if entry already exists
_macDelete(account);
execFileSync('security', [
'add-generic-password', '-s', SERVICE, '-a', account, '-w', password
], { stdio: ['pipe', 'pipe', 'pipe'] });
}
function _macDelete(account) {
try {
execFileSync('security', [
'delete-generic-password', '-s', SERVICE, '-a', account
], { stdio: ['pipe', 'pipe', 'pipe'] });
} catch { /* may not exist */ }
}
// ── Linux: secret-tool (libsecret) ─────────────────────────────
function _linuxGet(account) {
try {
return execFileSync('secret-tool', [
'lookup', 'service', SERVICE, 'username', account
], { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
} catch {
return null;
}
}
function _linuxSet(account, password) {
try {
execFileSync('secret-tool', [
'store', '--label=Time Entry CLI', 'service', SERVICE, 'username', account
], { input: password, stdio: ['pipe', 'pipe', 'pipe'] });
} catch {
// secret-tool not available — fall back to encrypted file
_fallbackSet(account, password);
}
}
function _linuxDelete(account) {
try {
execFileSync('secret-tool', [
'clear', 'service', SERVICE, 'username', account
], { stdio: ['pipe', 'pipe', 'pipe'] });
} catch { /* ignore */ }
_fallbackDelete(); // clean up any fallback file too
}
// ── Windows: DPAPI via PowerShell ────────────────────────────────
// ConvertTo/From-SecureString uses Windows Data Protection API,
// which ties encryption to the current user + machine — similar
// security model to macOS Keychain. Encrypted blob is stored in
// ~/.tecli-credentials alongside the account name.
function _winGet(account) {
try {
const data = JSON.parse(fs.readFileSync(CRED_PATH, 'utf8'));
if (data.account !== account || !data.dpapi) return null;
return execFileSync('powershell.exe', ['-NoProfile', '-Command',
'$secure = ConvertTo-SecureString \'' + data.dpapi + '\'; ' +
'$ptr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure); ' +
'[Runtime.InteropServices.Marshal]::PtrToStringAuto($ptr)'
], { encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim() || null;
} catch {
return null;
}
}
function _winSet(account, password) {
try {
const encrypted = execFileSync('powershell.exe', ['-NoProfile', '-Command',
'$pw = ($input | Out-String).TrimEnd(); ' +
'$secure = ConvertTo-SecureString $pw -AsPlainText -Force; ' +
'ConvertFrom-SecureString $secure'
], { input: password, encoding: 'utf8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
fs.writeFileSync(CRED_PATH, JSON.stringify({ account, dpapi: encrypted }, null, 2) + '\n');
} catch {
// PowerShell unavailable — fall back to AES
_fallbackSet(account, password);
}
}
function _winDelete() {
try { fs.unlinkSync(CRED_PATH); } catch { /* ignore */ }
}
// ── Fallback: AES-256-GCM encrypted file ────────────────────────
// For Windows or Linux without secret-tool. File is chmod 600.
// Key is derived from machine-local data — not a password, but keeps
// the credential from sitting in plaintext on disk.
function _deriveKey() {
const material = `${SERVICE}:${os.hostname()}:${os.userInfo().username}`;
return crypto.scryptSync(material, 'tecli-v1', 32);
}
function _fallbackGet(account) {
try {
const data = JSON.parse(fs.readFileSync(CRED_PATH, 'utf8'));
if (data.account !== account) return null;
const key = _deriveKey();
const decipher = crypto.createDecipheriv('aes-256-gcm', key, Buffer.from(data.iv, 'hex'));
decipher.setAuthTag(Buffer.from(data.tag, 'hex'));
let decrypted = decipher.update(data.encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
} catch {
return null;
}
}
function _fallbackSet(account, password) {
const key = _deriveKey();
const iv = crypto.randomBytes(16);
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
let encrypted = cipher.update(password, 'utf8', 'hex');
encrypted += cipher.final('hex');
const tag = cipher.getAuthTag().toString('hex');
fs.writeFileSync(CRED_PATH, JSON.stringify({
account, iv: iv.toString('hex'), encrypted, tag
}, null, 2) + '\n', { mode: 0o600 });
}
function _fallbackDelete() {
try { fs.unlinkSync(CRED_PATH); } catch { /* ignore */ }
}
// ─── Interactive prompts ────────────────────────────────────────
function prompt(text) {
const rl = readline.createInterface({ input: process.stdin, output: process.stderr });
return new Promise(resolve => {
rl.question(text, answer => { rl.close(); resolve(answer.trim()); });
});
}
function promptSecret(text) {
process.stderr.write(text);
return new Promise(resolve => {
const stdin = process.stdin;
if (!stdin.isTTY) {
// Non-interactive — read a line from stdin
const rl = readline.createInterface({ input: stdin });
rl.once('line', line => { rl.close(); resolve(line.trim()); });
return;
}
const wasRaw = stdin.isRaw;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
let input = '';
const onData = (ch) => {
if (ch === '\r' || ch === '\n') {
stdin.setRawMode(wasRaw || false);
stdin.pause();
stdin.removeListener('data', onData);
process.stderr.write('\n');
resolve(input);
} else if (ch === '\u0003') { // Ctrl+C
stdin.setRawMode(wasRaw || false);
process.stderr.write('\n');
process.exit(1);
} else if (ch === '\u007f' || ch === '\b') { // Backspace
input = input.slice(0, -1);
} else {
input += ch;
}
};
stdin.on('data', onData);
});
}
// ─── Public API ─────────────────────────────────────────────────
/**
* Get stored credentials. Priority:
* 1. Environment variables (COSTPOINT_URL, COSTPOINT_USERNAME, COSTPOINT_PASSWORD)
* 2. Config file (~/.tecli.json) + OS keychain
* Returns { url, username, password, system, useDirect } or null.
*/
function getCredentials() {
// 1. Env vars (preserves .env / CI compatibility)
const envUrl = process.env.COSTPOINT_URL;
const envUser = process.env.COSTPOINT_USERNAME;
const envPass = process.env.COSTPOINT_PASSWORD;
if (envUrl && envUser && envPass) {
return {
url: envUrl,
username: envUser,
password: envPass,
system: process.env.COSTPOINT_SYSTEM || '',
useDirect: process.env.COSTPOINT_DIRECT === 'true',
};
}
// 2. Config + keychain
const config = readConfig();
if (config.url && config.username) {
const password = keychainGet(config.username);
if (password) {
return {
url: config.url,
username: config.username,
password,
system: config.system || '',
useDirect: config.direct !== false, // default true
};
}
}
return null;
}
/**
* Interactive login — prompts for credentials and stores them.
* @param {object} [defaults] - pre-fill values (e.g. from env)
*/
async function login(defaults = {}) {
const config = readConfig();
const defaultUrl = defaults.url || config.url || DEFAULT_URL;
const defaultUser = defaults.username || config.username || '';
const username = (await prompt(`Username${defaultUser ? ` [${defaultUser}]` : ''}: `)) || defaultUser;
const password = await promptSecret('Password: ');
const url = defaultUrl;
if (!url || !username || !password) {
throw new Error('URL, username, and password are all required.');
}
// Extract system from URL query string if present
let system = '';
try {
system = new URL(url).searchParams.get('system') || '';
} catch { /* ignore */ }
// Save
keychainSet(username, password);
writeConfig({ url, username, system, direct: true });
return { url, username, password, system, useDirect: true };
}
/**
* Remove stored credentials from keychain and config file.
*/
function logout() {
const config = readConfig();
if (config.username) {
keychainDelete(config.username);
}
deleteConfig();
_fallbackDelete();
}
module.exports = { getCredentials, login, logout, prompt, promptSecret };