-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathauth.js
More file actions
87 lines (78 loc) · 2.72 KB
/
auth.js
File metadata and controls
87 lines (78 loc) · 2.72 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
'use strict';
const assert = require('assert');
const fs = require('fs');
const { ClientRequest } = require('http');
const { homedir, EOL } = require('os');
const path = require('path');
const util = require('util');
const ghauth = util.promisify(require('ghauth'));
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
let authFile; // ncurc file in $XDG_CONFIG_HOME or ~.
module.exports = lazy(auth);
function check(username, token) {
assert(typeof username === 'string' && /^[a-zA-Z0-9]*/.test(username));
assert(typeof token === 'string' && /^[0-9a-f]*/.test(token));
}
async function auth(getCredentials = ghauth) {
let username, token;
// Try reading from $XDG_CONFIG_HOME/ncurc, fall back to ~/.ncurc.
try {
authFile = path.join(process.env.XDG_CONFIG_HOME, 'ncurc');
({ username, token } = JSON.parse(await readFile(authFile, 'utf8')));
} catch (e) {
try {
authFile = path.join(homedir(), '.ncurc');
({ username, token } = JSON.parse(await readFile(authFile, 'utf8')));
} catch (e) {
process.stdout.write('If this is your first time running this command, ' +
'follow the instructions to create an access token' +
'. If you prefer to create it yourself on Github, ' +
'see https://github.com/nodejs/node-core-utils/blob/master/README.md.' +
EOL);
}
}
// If that worked, yay
if (username && token) {
check(username, token);
return Buffer.from(`${username}:${token}`).toString('base64');
}
// Ask the user for input, create a token via github v3 API
// then write to ~/.ncurc and try auth() again
let credentials;
try {
credentials = await getCredentials({
noSave: true,
scopes: ['user:email', 'read:org'],
note: 'node-core-utils CLI tools'
});
} catch (e) {
process.stderr.write(`Could not get token: ${e.message}${EOL}`);
process.exit(1);
}
const json = JSON.stringify({
username: credentials.user,
token: credentials.token
}, null, ' ');
await writeFile(authFile, json, { mode:
0o600 /* owner read/write */
});
return auth(getCredentials);
}
function lazy(fn) {
let cachedValue;
return function(...args) {
if (cachedValue !== undefined) {
return cachedValue;
}
cachedValue = fn(...args);
return cachedValue;
};
}
// This is an ugly hack to get around a bug in hyperquest & ghauth
// which are not currently maintained
const originalSetTimeout = ClientRequest.prototype.setTimeout;
ClientRequest.prototype.setTimeout = function(msecs, ...args) {
msecs = Math.min(msecs, Math.pow(2, 31) - 1);
return originalSetTimeout.call(this, msecs, ...args);
};