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

Commit 7c350d9

Browse files
Refactoring authentication.js
1 parent 2f75f25 commit 7c350d9

File tree

2 files changed

+34
-74
lines changed

2 files changed

+34
-74
lines changed

octorun/src/authentication.js

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +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 lockedErrorMessage = "locked";
12-
var badCredentialsErrorMessage = "badcredentials";
9+
var scopes = ["user", "repo", "gist", "write:public_key"];
1310

14-
var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onFailure) {
11+
var handleAuthentication = function (username, password, onSuccess, onFailure, twoFactor) {
1512
var octokit = octokitWrapper.createOctokit();
1613

1714
octokit.authenticate({
@@ -20,56 +17,29 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
2017
password: password
2118
});
2219

20+
var headers;
21+
if (twoFactor) {
22+
headers = {
23+
"X-GitHub-OTP": twoFactor
24+
};
25+
}
26+
2327
octokit.authorization.create({
2428
scopes: scopes,
2529
note: config.appName,
2630
client_id: config.clientId,
27-
client_secret: config.clientSecret
31+
client_secret: config.clientSecret,
32+
headers: headers
2833
}, function (err, res) {
2934
if (err) {
3035
if (twoFactorRegex.test(err.message)) {
31-
onRequiresTwoFa();
36+
onSuccess(password, "2fa");
3237
}
3338
else if (lockedRegex.test(err.message)) {
34-
onFailure(lockedErrorMessage)
35-
}
36-
else if (badCredentialsRegex.test(err.message)) {
37-
onFailure(badCredentialsErrorMessage)
38-
}
39-
else {
40-
onFailure(err)
41-
}
42-
}
43-
else {
44-
onSuccess(res.data.token);
45-
}
46-
});
47-
}
48-
49-
var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onFailure) {
50-
var octokit = octokitWrapper.createOctokit();
51-
52-
octokit.authenticate({
53-
type: "basic",
54-
username: username,
55-
password: password
56-
});
57-
58-
octokit.authorization.create({
59-
scopes: scopes,
60-
note: config.appName,
61-
client_id: config.clientId,
62-
client_secret: config.clientSecret,
63-
headers: {
64-
"X-GitHub-OTP": twoFactor
65-
}
66-
}, function (err, res) {
67-
if (err) {
68-
if (lockedRegex.test(err.message)) {
69-
onFailure(lockedErrorMessage)
39+
onFailure("locked")
7040
}
7141
else if (badCredentialsRegex.test(err.message)) {
72-
onFailure(badCredentialsErrorMessage)
42+
onFailure("badcredentials")
7343
}
7444
else {
7545
onFailure(err)
@@ -82,6 +52,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
8252
}
8353

8454
module.exports = {
85-
handleBasicAuthentication: handleBasicAuthentication,
86-
handleTwoFactorAuthentication: handleTwoFactorAuthentication,
55+
handleAuthentication: handleAuthentication,
8756
};

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: ');
@@ -31,7 +36,7 @@ if (commander.twoFactor) {
3136

3237
var twoFactor = readlineSync.question('Two Factor: ');
3338

34-
handleTwoFactorAuthentication(username, password, twoFactor);
39+
handleAuthentication(username, password, twoFactor);
3540
}
3641
else {
3742
var data = '';
@@ -49,25 +54,11 @@ if (commander.twoFactor) {
4954
.split(/\r?\n/)
5055
.filter(function (item) { return item; });
5156

52-
handleTwoFactorAuthentication(items[0], items[1], items[2]);
57+
handleAuthentication(items[0], items[1], items[2]);
5358
});
5459
}
5560
}
5661
else {
57-
var handleBasicAuthentication = function (username, password) {
58-
authentication.handleBasicAuthentication(username, password,
59-
function (token) {
60-
output.success(token);
61-
process.exit();
62-
}, function () {
63-
output.custom("2fa", password);
64-
process.exit();
65-
}, function (error) {
66-
output.error(error);
67-
process.exit();
68-
});
69-
}
70-
7162
if (process.stdin.isTTY) {
7263
var readlineSync = require("readline-sync");
7364

@@ -76,7 +67,7 @@ else {
7667
hideEchoBack: true
7768
});
7869

79-
handleBasicAuthentication(username, password);
70+
handleAuthentication(username, password);
8071
}
8172
else {
8273
var data = '';
@@ -94,7 +85,7 @@ else {
9485
.split(/\r?\n/)
9586
.filter(function (item) { return item; });
9687

97-
handleBasicAuthentication(items[0], items[1]);
88+
handleAuthentication(items[0], items[1]);
9889
});
9990
}
10091
}

0 commit comments

Comments
 (0)