Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit f09a668

Browse files
Completed functionality to use octorun js
1 parent abcdb4a commit f09a668

File tree

5 files changed

+246
-218
lines changed

5 files changed

+246
-218
lines changed

octorun/bin/octorun

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
#!/usr/bin/env node
22

3-
process.stdout.write("node:", process.argv[0]);
4-
53
require('../src/bin/app.js');

octorun/src/authentication.js

Lines changed: 53 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,66 @@
1+
var endOfLine = require('os').EOL;
12
var config = require("./configuration");
23
var octokitWrapper = require("./octokit");
34

45
var scopes = ["user", "repo", "gist", "write:public_key"];
56

6-
var stdIn = process.openStdin();
7-
8-
var awaiter = null;
9-
10-
stdIn.addListener("data", function (d) {
11-
var content = d.toString().trim();
12-
13-
if (awaiter) {
14-
var _awaiter = awaiter;
15-
awaiter = null;
16-
_awaiter(content);
17-
}
18-
});
19-
20-
var handleBasicAuthentication = function (onSuccess, onRequiresTwoFa, onFailure) {
21-
var username = null;
22-
var password = null;
23-
24-
var withPassword = function (input) {
25-
password = input;
26-
27-
var octokit = octokitWrapper.createOctokit();
28-
29-
octokit.authenticate({
30-
type: "basic",
31-
username: username,
32-
password: password
33-
});
34-
35-
octokit.authorization.create({
36-
scopes: scopes,
37-
note: config.appName,
38-
client_id: config.clientId,
39-
client_secret: config.clientSecret
40-
}, function (err, res) {
41-
if (err) {
42-
if (err.message === '{"message":"Must specify two-factor authentication OTP code.","documentation_url":"https://developer.github.com/v3/auth#working-with-two-factor-authentication"}') {
43-
onRequiresTwoFa();
44-
return;
45-
}
46-
else {
47-
onFailure(err)
48-
}
7+
var lockedRegex = new RegExp("number of login attempts exceeded", "gi");
8+
var twoFactorRegex = new RegExp("must specify two-factor authentication OTP code", "gi");
9+
10+
var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onLocked, onFailure) {
11+
var octokit = octokitWrapper.createOctokit();
12+
13+
octokit.authenticate({
14+
type: "basic",
15+
username: username,
16+
password: password
17+
});
18+
19+
octokit.authorization.create({
20+
scopes: scopes,
21+
note: config.appName,
22+
client_id: config.clientId,
23+
client_secret: config.clientSecret
24+
}, function (err, res) {
25+
if (err) {
26+
if (twoFactorRegex.test(err.message)) {
27+
onRequiresTwoFa();
4928
}
5029
else {
51-
onSuccess(res.data.token);
52-
}
53-
});
54-
}
55-
56-
var promptPassword = function () {
57-
process.stdout.write("Password: ");
58-
awaiter = withPassword;
59-
}
60-
61-
var withUser = function (input) {
62-
username = input;
63-
promptPassword();
64-
}
65-
66-
var promptUser = function () {
67-
process.stdout.write("Username: ");
68-
awaiter = withUser;
69-
}
70-
71-
promptUser();
72-
}
73-
74-
var handleTwoFactorAuthentication = function (onSuccess, onFailure) {
75-
var username = null;
76-
var password = null;
77-
var twoFactor = null;
78-
79-
var withTwoFactor = function (input) {
80-
twoFactor = input;
81-
82-
var octokit = octokitWrapper.createOctokit();
83-
84-
octokit.authenticate({
85-
type: "basic",
86-
username: username,
87-
password: password
88-
});
89-
90-
octokit.authorization.create({
91-
scopes: scopes,
92-
note: config.appName,
93-
client_id: config.clientId,
94-
client_secret: config.clientSecret,
95-
headers: {
96-
"X-GitHub-OTP": twoFactor
97-
}
98-
}, function (err, res) {
99-
if (err) {
10030
onFailure(err)
10131
}
102-
else {
103-
onSuccess(res.data.token);
104-
}
105-
});
106-
}
107-
108-
var promptTwoFactor = function () {
109-
process.stdout.write("Two Factor: ");
110-
awaiter = withTwoFactor;
111-
}
112-
113-
var withPassword = function (input) {
114-
password = input;
115-
promptTwoFactor();
116-
}
117-
118-
var promptPassword = function () {
119-
process.stdout.write("Password: ");
120-
awaiter = withPassword;
121-
}
122-
123-
var withUser = function (input) {
124-
username = input;
125-
promptPassword();
126-
}
127-
128-
var promptUser = function () {
129-
process.stdout.write("Username: ");
130-
awaiter = withUser;
131-
}
32+
}
33+
else {
34+
onSuccess(res.data.token);
35+
}
36+
});
37+
}
13238

133-
promptUser();
39+
var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onLocked, onFailure) {
40+
var octokit = octokitWrapper.createOctokit();
41+
42+
octokit.authenticate({
43+
type: "basic",
44+
username: username,
45+
password: password
46+
});
47+
48+
octokit.authorization.create({
49+
scopes: scopes,
50+
note: config.appName,
51+
client_id: config.clientId,
52+
client_secret: config.clientSecret,
53+
headers: {
54+
"X-GitHub-OTP": twoFactor
55+
}
56+
}, function (err, res) {
57+
if (err) {
58+
onFailure(err)
59+
}
60+
else {
61+
onSuccess(res.data.token);
62+
}
63+
});
13464
}
13565

13666
module.exports = {

octorun/src/bin/app-login.js

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,117 @@ var commander = require("commander");
22
var package = require('../../package.json')
33
var authentication = require('../authentication')
44

5+
var endOfLine = require('os').EOL;
6+
57
commander
68
.version(package.version)
79
.option('-t, --twoFactor')
810
.parse(process.argv);
911

12+
var encoding = 'utf-8';
13+
1014
if (commander.twoFactor) {
11-
authentication.handleTwoFactorAuthentication(function (token) {
12-
process.stdout.write(token);
13-
process.exit();
14-
}, function (err) {
15-
process.stdout.write(err);
16-
process.exit();
17-
});
15+
var handleTwoFactorAuthentication = function (username, password, token) {
16+
authentication.handleTwoFactorAuthentication(username, password, token, function (token) {
17+
process.stdout.write(token);
18+
process.stdout.write(endOfLine);
19+
process.exit();
20+
}, function () {
21+
process.stdout.write("Account locked.");
22+
process.stdout.write(endOfLine);
23+
process.exit();
24+
}, function (err) {
25+
process.stdout.write("Error");
26+
process.stdout.write(endOfLine);
27+
process.stdout.write(err);
28+
process.stdout.write(endOfLine);
29+
process.exit();
30+
});
31+
}
32+
33+
if (process.stdin.isTTY) {
34+
var readlineSync = require("readline-sync");
35+
var username = readlineSync.question('User: ');
36+
var password = readlineSync.question('Password: ', {
37+
hideEchoBack: true
38+
});
39+
40+
var twoFactor = readlineSync.question('Two Factor: ');
41+
42+
handleTwoFactorAuthentication(username, password, twoFactor);
43+
}
44+
else {
45+
var data = '';
46+
process.stdin.setEncoding(encoding);
47+
48+
process.stdin.on('readable', function () {
49+
var chunk;
50+
while (chunk = process.stdin.read()) {
51+
data += chunk;
52+
}
53+
});
54+
55+
process.stdin.on('end', function () {
56+
var items = data.toString()
57+
.split(/\r?\n/)
58+
.filter(function (item) { return item; });
59+
60+
handleTwoFactorAuthentication(items[0], items[1], items[2]);
61+
});
62+
}
1863
}
1964
else {
20-
authentication.handleBasicAuthentication(function (token) {
21-
process.stdout.write(token);
22-
process.exit();
23-
}, function () {
24-
process.stdout.write("Must specify two-factor authentication OTP code.");
25-
process.exit();
26-
}, function (err) {
27-
process.stdout.write(err);
28-
process.exit();
29-
});
65+
66+
var handleTwoFactorAuthentication = function (username, password) {
67+
authentication.handleBasicAuthentication(username, password,
68+
function (token) {
69+
process.stdout.write(token);
70+
process.stdout.write(endOfLine);
71+
process.exit();
72+
}, function () {
73+
process.stdout.write("Must specify two-factor authentication OTP code.");
74+
process.stdout.write(endOfLine);
75+
process.exit();
76+
}, function () {
77+
process.stdout.write("Account locked.");
78+
process.stdout.write(endOfLine);
79+
process.exit();
80+
}, function (err) {
81+
process.stdout.write("Error");
82+
process.stdout.write(endOfLine);
83+
process.stdout.write(err);
84+
process.stdout.write(endOfLine);
85+
process.exit();
86+
});
87+
}
88+
89+
if (process.stdin.isTTY) {
90+
var readlineSync = require("readline-sync");
91+
92+
var username = readlineSync.question('User: ');
93+
var password = readlineSync.question('Password: ', {
94+
hideEchoBack: true
95+
});
96+
97+
handleTwoFactorAuthentication(username, password);
98+
}
99+
else {
100+
var data = '';
101+
process.stdin.setEncoding(encoding);
102+
103+
process.stdin.on('readable', function () {
104+
var chunk;
105+
while (chunk = process.stdin.read()) {
106+
data += chunk;
107+
}
108+
});
109+
110+
process.stdin.on('end', function () {
111+
var items = data.toString()
112+
.split(/\r?\n/)
113+
.filter(function (item) { return item; });
114+
115+
handleTwoFactorAuthentication(items[0], items[1]);
116+
});
117+
}
30118
}

0 commit comments

Comments
 (0)