-
Notifications
You must be signed in to change notification settings - Fork 5
/
login-module.js
109 lines (85 loc) · 3.28 KB
/
login-module.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
var clc = require('cli-color');
var username, password, twoFactorCode;
var read = require('read');
var request = require('request-json');
var Configstore = require('configstore');
var pkg = require('./package.json');
var handle_success = function (err, res, body) {
var conf = new Configstore(pkg.name);
conf.set('username', username);
conf.set('oauth_token', body.token);
console.log(clc.green('Authenticated!') + ' We have stored your OAuth Token in the ~/.config directory.');
if (conf.get('gist_id')) {
var ghGistOwner = require('gh-gist-owner');
var owner_username = ghGistOwner(conf.get('gist_id'));
if (username !== owner_username) {
console.log('Gist ID: ' + conf.get('gist_id') + ' was stored on file.');
console.log('It is owned by ' + owner_username + '. We are removing this from local storage.');
conf.del('gist_id');
} else {
console.log('Gist ID: ' + conf.get('gist_id') + ' will be used for pushing solves.');
}
}
}
var handle_error = function (err, res, body) {
console.log('HTTP Status Code: ' + res.statusCode);
console.log('Headers: ', require('util').inspect(res.headers));
console.log('We encountered an error!');
};
module.exports = function () {
console.log(clc.blue('Welcome to the one-time-authentication module.'));
console.log('This module will take your github username and password\nand exchange it for an OAuth token');
read({
prompt: 'Enter your GitHub username: '
}, function (err, result, isDef) {
if (err) {
console.log(require('util').inspect(err, { depth: null }));
}
username = result;
read({
prompt: 'Enter your GitHub password: ',
silent: true
}, function (err, result, isDef) {
if (err) {
console.log(require('util').inspect(err, { depth: null }));
return;
}
password = result;
var client = request.createClient('https://api.github.com/');
client.setBasicAuth(username, password);
var data = {
'scopes': ['gist'],
'note': 'OAuth token for node-cube-cli-timer.',
'client_id': '678a9606a01d79c24046',
'client_secret': '266c833862498ac55856cf276a26c8b680515e91'
};
client.post('authorizations', data, function (err, res, body) {
if (res.statusCode === 401 && !err && res.headers['x-github-otp']) {
console.log(clc.green("2FA enabled! I see you care about security."));
let twoFacPrompt = "Please enter your 2FA code ( " + res.headers['x-github-otp'] + " ): ";
read({
prompt: twoFacPrompt
}, function (err, result, isDef) {
if (err) {
console.log(require('util').inspect(err, { depth: null }));
return;
}
twoFactorCode = result;
client.headers['x-github-otp'] = twoFactorCode;
client.post('authorizations', data, function (err, res, body) {
if (res.statusCode === 201 && !err) {
handle_success(err, res, body);
} else {
handle_error(err, res, body);
}
});
});
} else if (res.statusCode === 201 && !err) {
handle_success(err, res, body);
} else {
handle_error(err, res, body);
}
});
});
});
};