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

Commit 879e869

Browse files
Making the success/failure status more explicit
1 parent 46ccb91 commit 879e869

File tree

6 files changed

+127
-67
lines changed

6 files changed

+127
-67
lines changed

octorun/src/api.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,25 @@ ApiWrapper.prototype.getOrgs = function (callback) {
3232
var that = this;
3333
var getOrgsAtPosition = function () {
3434
that.octokit.users.getOrgs(position, function (error, result) {
35-
for (var index = 0; index < result.data.length; index++) {
36-
var element = result.data[index];
37-
organizations.push(element);
38-
}
35+
if (error) {
36+
callback(error, null);
37+
} else {
38+
for (var index = 0; index < result.data.length; index++) {
39+
var element = result.data[index];
40+
organizations.push(element);
41+
}
3942

40-
if (result.data.length == perPageCount) {
41-
position.page = position.page + 1;
42-
getOrgsAtPosition();
43-
}
44-
else {
45-
callback(error, organizations.map(function (item) {
46-
return item.login;
47-
}));
43+
if (result.data.length == perPageCount) {
44+
position.page = position.page + 1;
45+
getOrgsAtPosition();
46+
}
47+
else {
48+
var organizationLogins = organizations.map(function (item) {
49+
return item.login;
50+
});
51+
52+
callback(null, organizationLogins);
53+
}
4854
}
4955
});
5056
}

octorun/src/authentication.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ var scopes = ["user", "repo", "gist", "write:public_key"];
66

77
var lockedRegex = new RegExp("number of login attempts exceeded", "gi");
88
var twoFactorRegex = new RegExp("must specify two-factor authentication OTP code", "gi");
9+
var badCredentialsRegex = new RegExp("bad credentials", "gi");
910

10-
var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onLocked, onFailure) {
11+
var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onFailure) {
1112
var octokit = octokitWrapper.createOctokit();
1213

1314
octokit.authenticate({
@@ -26,6 +27,12 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
2627
if (twoFactorRegex.test(err.message)) {
2728
onRequiresTwoFa();
2829
}
30+
else if (lockedRegex.test(err.message)) {
31+
onFailure("Account locked.")
32+
}
33+
else if (badCredentialsRegex.test(err.message)) {
34+
onFailure("Bad credentials.")
35+
}
2936
else {
3037
onFailure(err)
3138
}
@@ -36,7 +43,7 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
3643
});
3744
}
3845

39-
var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onLocked, onFailure) {
46+
var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onFailure) {
4047
var octokit = octokitWrapper.createOctokit();
4148

4249
octokit.authenticate({
@@ -55,7 +62,15 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
5562
}
5663
}, function (err, res) {
5764
if (err) {
58-
onFailure(err)
65+
if (lockedRegex.test(err.message)) {
66+
onFailure("Account locked.")
67+
}
68+
else if (badCredentialsRegex.test(err.message)) {
69+
onFailure("Bad credentials.")
70+
}
71+
else {
72+
onFailure(err)
73+
}
5974
}
6075
else {
6176
onSuccess(res.data.token);

octorun/src/bin/app-login.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ var encoding = 'utf-8';
1313
if (commander.twoFactor) {
1414
var handleTwoFactorAuthentication = function (username, password, token) {
1515
authentication.handleTwoFactorAuthentication(username, password, token, function (token) {
16-
process.stdout.write(token);
16+
process.stdout.write("Success");
1717
process.stdout.write(endOfLine);
18-
process.exit();
19-
}, function () {
20-
process.stdout.write("Account locked.");
18+
process.stdout.write(token);
2119
process.stdout.write(endOfLine);
2220
process.exit();
23-
}, function (err) {
21+
}, function (error) {
2422
process.stdout.write("Error");
2523
process.stdout.write(endOfLine);
26-
process.stdout.write(err);
27-
process.stdout.write(endOfLine);
24+
25+
if (error) {
26+
process.stdout.write(error.toString());
27+
process.stdout.write(endOfLine);
28+
}
29+
2830
process.exit();
2931
});
3032
}
@@ -61,39 +63,41 @@ if (commander.twoFactor) {
6163
}
6264
}
6365
else {
64-
65-
var handleTwoFactorAuthentication = function (username, password) {
66+
var handleBasicAuthentication = function (username, password) {
6667
authentication.handleBasicAuthentication(username, password,
6768
function (token) {
69+
process.stdout.write("Success");
70+
process.stdout.write(endOfLine);
6871
process.stdout.write(token);
6972
process.stdout.write(endOfLine);
7073
process.exit();
7174
}, function () {
72-
process.stdout.write("Must specify two-factor authentication OTP code.");
75+
process.stdout.write("Error");
7376
process.stdout.write(endOfLine);
74-
process.exit();
75-
}, function () {
76-
process.stdout.write("Account locked.");
77+
process.stdout.write("Must specify two-factor authentication OTP code.");
7778
process.stdout.write(endOfLine);
7879
process.exit();
79-
}, function (err) {
80+
}, function (error) {
8081
process.stdout.write("Error");
8182
process.stdout.write(endOfLine);
82-
process.stdout.write(err);
83-
process.stdout.write(endOfLine);
83+
84+
if (error) {
85+
process.stdout.write(error.toString());
86+
process.stdout.write(endOfLine);
87+
}
8488
process.exit();
8589
});
8690
}
8791

8892
if (process.stdin.isTTY) {
8993
var readlineSync = require("readline-sync");
90-
94+
9195
var username = readlineSync.question('User: ');
9296
var password = readlineSync.question('Password: ', {
9397
hideEchoBack: true
9498
});
9599

96-
handleTwoFactorAuthentication(username, password);
100+
handleBasicAuthentication(username, password);
97101
}
98102
else {
99103
var data = '';
@@ -111,7 +115,7 @@ else {
111115
.split(/\r?\n/)
112116
.filter(function (item) { return item; });
113117

114-
handleTwoFactorAuthentication(items[0], items[1]);
118+
handleBasicAuthentication(items[0], items[1]);
115119
});
116120
}
117121
}

octorun/src/bin/app-organizations.js

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,42 @@ commander
77
.version(package.version)
88
.parse(process.argv);
99

10-
var apiWrapper = new ApiWrapper();
11-
apiWrapper.getOrgs(function (error, result) {
10+
try {
11+
12+
var apiWrapper = new ApiWrapper();
13+
apiWrapper.getOrgs(function (error, result) {
14+
if (error) {
15+
process.stdout.write("Error");
16+
process.stdout.write(endOfLine);
17+
18+
if (error) {
19+
process.stdout.write(error.toString());
20+
process.stdout.write(endOfLine);
21+
}
22+
23+
process.exit();
24+
}
25+
else {
26+
process.stdout.write("Success");
27+
process.stdout.write(endOfLine);
28+
29+
for (var i = 0; i < result.length; i++) {
30+
process.stdout.write(result[i]);
31+
process.stdout.write(endOfLine);
32+
}
33+
34+
process.exit();
35+
}
36+
});
37+
}
38+
catch (error) {
39+
process.stdout.write("Error");
40+
process.stdout.write(endOfLine);
41+
1242
if (error) {
13-
process.stdout.write(error);
14-
process.stdout.write(endOfLine);
15-
process.exit(-1);
16-
}
17-
else {
18-
process.stdout.write(result);
43+
process.stdout.write(error.toString());
1944
process.stdout.write(endOfLine);
20-
process.exit();
2145
}
22-
});
46+
47+
process.exit();
48+
}

octorun/src/bin/app-validate.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ try {
1313
apiWrapper.verifyUser(function (error, result) {
1414
if (error) {
1515
process.stdout.write("Error");
16-
process.stdout.write(error);
16+
process.stdout.write(endOfLine);
17+
18+
if (error) {
19+
process.stdout.write(error.toString());
20+
process.stdout.write(endOfLine);
21+
}
22+
1723
process.exit();
1824
}
1925
else {

src/GitHub.Api/Authentication/LoginManager.cs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ public async Task<LoginResultData> ContinueLogin(LoginResultData loginResultData
156156
{
157157
var newAuth = loginResultData.NewAuth;
158158
var host = loginResultData.Host;
159-
var k = keychain.Connect(host);
160-
var username = k.Credential.Username;
161-
var password = k.Credential.Token;
159+
var keychainAdapter = keychain.Connect(host);
160+
var username = keychainAdapter.Credential.Username;
161+
var password = keychainAdapter.Credential.Token;
162162
try
163163
{
164164
logger.Trace("2FA Continue");
@@ -271,28 +271,30 @@ string password
271271
throw new Exception("Authentication failed");
272272
}
273273

274-
if (ret.Count == 1)
274+
if (ret[0] == "Success")
275275
{
276-
if (ret[0] == "Must specify two-factor authentication OTP code.")
276+
auth = new ApplicationAuthorization(ret[1]);
277+
return auth;
278+
}
279+
280+
if (ret.Count > 1)
281+
{
282+
if (ret[1] == "Must specify two-factor authentication OTP code.")
277283
{
278284
keychain.SetToken(host, ret[0]);
279285
await keychain.Save(host);
280286
throw new TwoFactorRequiredException(TwoFactorType.Unknown);
281287
}
282288

283-
if (ret[0] == "Account locked.")
289+
if (ret[1] == "Account locked.")
284290
{
285291
throw new LoginAttemptsExceededException(null, null);
286292
}
287293

288-
auth = new ApplicationAuthorization(ret[0]);
289-
}
290-
else
291-
{
292-
throw new Exception("Authentication failed");
294+
throw new Exception(ret[1]);
293295
}
294296

295-
return auth;
297+
throw new Exception("Authentication failed");
296298
}
297299

298300
private async Task<ApplicationAuthorization> TryContinueLogin(
@@ -321,23 +323,24 @@ string code
321323
{
322324
throw new Exception("Authentication failed");
323325
}
324-
325-
// success
326-
if (ret.Count == 1)
326+
327+
if (ret[0] == "Success")
327328
{
328-
if (ret[0] == "Account locked.")
329+
auth = new ApplicationAuthorization(ret[1]);
330+
return auth;
331+
}
332+
333+
if (ret.Count > 1)
334+
{
335+
if (ret[1] == "Account locked.")
329336
{
330337
throw new LoginAttemptsExceededException(null, null);
331338
}
332339

333-
auth = new ApplicationAuthorization(ret[0]);
334-
}
335-
else
336-
{
337-
throw new Exception("Authentication failed");
340+
throw new Exception(ret[1]);
338341
}
339342

340-
return auth;
343+
throw new Exception("Authentication failed");
341344
}
342345

343346
ApplicationAuthorization EnsureNonNullAuthorization(ApplicationAuthorization auth)

0 commit comments

Comments
 (0)