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

Commit 694dab1

Browse files
authored
Merge pull request #624 from github-for-unity/fixes/octorun-refactor-authentication
Refactoring Octorun output and authentication.js
2 parents c5d6949 + 76a10c5 commit 694dab1

File tree

4 files changed

+93
-159
lines changed

4 files changed

+93
-159
lines changed

octorun/src/authentication.js

Lines changed: 22 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ var endOfLine = require('os').EOL;
22
var config = require("./configuration");
33
var octokitWrapper = require("./octokit");
44

5-
var scopes = ["user", "repo", "gist", "write:public_key"];
6-
75
var lockedRegex = new RegExp("number of login attempts exceeded", "gi");
86
var twoFactorRegex = new RegExp("must specify two-factor authentication otp code", "gi");
97
var badCredentialsRegex = new RegExp("bad credentials", "gi");
108

11-
var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onFailure) {
9+
var scopes = ["user", "repo", "gist", "write:public_key"];
10+
11+
var handleAuthentication = function (username, password, onSuccess, onFailure, twoFactor) {
1212
if (!config.clientId || !config.clientSecret) {
1313
throw "clientId and/or clientSecret missing";
1414
}
@@ -25,64 +25,34 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
2525
password: password
2626
});
2727

28-
octokit.authorization.create({
29-
scopes: scopes,
30-
note: config.appName,
31-
client_id: config.clientId,
32-
client_secret: config.clientSecret
33-
}, function (err, res) {
34-
if (err) {
35-
if (twoFactorRegex.test(err.message)) {
36-
onRequiresTwoFa();
37-
}
38-
else if (lockedRegex.test(err.message)) {
39-
onFailure("Account locked.")
40-
}
41-
else if (badCredentialsRegex.test(err.message)) {
42-
onFailure("Bad credentials.")
43-
}
44-
else {
45-
onFailure(err)
46-
}
47-
}
48-
else {
49-
onSuccess(res.data.token);
50-
}
51-
});
52-
}
53-
54-
var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onFailure) {
55-
if (!config.clientId || !config.clientSecret) {
56-
throw "clientId and/or clientSecret missing";
57-
}
58-
59-
if (!config.appName) {
60-
throw "appName missing";
28+
var headers;
29+
if (twoFactor) {
30+
headers = {
31+
"X-GitHub-OTP": twoFactor,
32+
"user-agent": config.appName
33+
};
6134
}
62-
63-
var octokit = octokitWrapper.createOctokit();
64-
65-
octokit.authenticate({
66-
type: "basic",
67-
username: username,
68-
password: password
69-
});
7035

7136
octokit.authorization.create({
7237
scopes: scopes,
38+
note: config.appName,
7339
client_id: config.clientId,
7440
client_secret: config.clientSecret,
75-
headers: {
76-
"X-GitHub-OTP": twoFactor,
77-
"user-agent": config.appName
78-
}
41+
headers: headers
7942
}, function (err, res) {
8043
if (err) {
81-
if (lockedRegex.test(err.message)) {
82-
onFailure("Account locked.")
44+
if (twoFactor && err.code && err.code === 422) {
45+
//Two Factor Enterprise workaround
46+
onSuccess(password);
47+
}
48+
else if (twoFactorRegex.test(err.message)) {
49+
onSuccess(password, "2fa");
50+
}
51+
else if (lockedRegex.test(err.message)) {
52+
onFailure("locked")
8353
}
8454
else if (badCredentialsRegex.test(err.message)) {
85-
onFailure("Bad credentials.")
55+
onFailure("badcredentials")
8656
}
8757
else {
8858
onFailure(err)
@@ -95,6 +65,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
9565
}
9666

9767
module.exports = {
98-
handleBasicAuthentication: handleBasicAuthentication,
99-
handleTwoFactorAuthentication: handleTwoFactorAuthentication,
68+
handleAuthentication: handleAuthentication,
10069
};

octorun/src/bin/app-login.js

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,24 @@ commander
99
.option('-t, --twoFactor')
1010
.parse(process.argv);
1111

12-
var encoding = 'utf-8';
13-
14-
if (commander.twoFactor) {
15-
var handleTwoFactorAuthentication = function (username, password, token) {
16-
authentication.handleTwoFactorAuthentication(username, password, token, function (token) {
17-
output.success(token);
12+
var handleAuthentication = function (username, password, twoFactor) {
13+
authentication.handleAuthentication(username, password, function (token, status) {
14+
if (status) {
15+
output.custom(status, token);
1816
process.exit();
19-
}, function (error) {
20-
output.error(error);
17+
}
18+
else {
19+
output.success(token);
2120
process.exit();
22-
});
23-
}
21+
}
22+
}, function (error) {
23+
output.error(error);
24+
process.exit();
25+
}, twoFactor);
26+
}
2427

28+
var encoding = 'utf-8';
29+
if (commander.twoFactor) {
2530
if (process.stdin.isTTY) {
2631
var readlineSync = require("readline-sync");
2732
var username = readlineSync.question('User: ');
@@ -32,7 +37,7 @@ if (commander.twoFactor) {
3237
var twoFactor = readlineSync.question('Two Factor: ');
3338

3439
try {
35-
handleTwoFactorAuthentication(username, password, twoFactor);
40+
handleAuthentication(username, password, twoFactor);
3641
}
3742
catch (error) {
3843
output.error(error);
@@ -56,7 +61,7 @@ if (commander.twoFactor) {
5661
.filter(function (item) { return item; });
5762

5863
try {
59-
handleTwoFactorAuthentication(items[0], items[1], items[2]);
64+
handleAuthentication(items[0], items[1], items[2]);
6065
}
6166
catch (error) {
6267
output.error(error);
@@ -66,20 +71,6 @@ if (commander.twoFactor) {
6671
}
6772
}
6873
else {
69-
var handleBasicAuthentication = function (username, password) {
70-
authentication.handleBasicAuthentication(username, password,
71-
function (token) {
72-
output.success(token);
73-
process.exit();
74-
}, function () {
75-
output.custom("2fa", password);
76-
process.exit();
77-
}, function (error) {
78-
output.error(error);
79-
process.exit();
80-
});
81-
}
82-
8374
if (process.stdin.isTTY) {
8475
var readlineSync = require("readline-sync");
8576

@@ -89,7 +80,7 @@ else {
8980
});
9081

9182
try {
92-
handleBasicAuthentication(username, password);
83+
handleAuthentication(username, password);
9384
}
9485
catch (error) {
9586
output.error(error);
@@ -113,7 +104,7 @@ else {
113104
.filter(function (item) { return item; });
114105

115106
try {
116-
handleBasicAuthentication(items[0], items[1]);
107+
handleAuthentication(items[0], items[1]);
117108
}
118109
catch (error) {
119110
output.error(error);

0 commit comments

Comments
 (0)