Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.
75 changes: 22 additions & 53 deletions octorun/src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ var endOfLine = require('os').EOL;
var config = require("./configuration");
var octokitWrapper = require("./octokit");

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

var lockedRegex = new RegExp("number of login attempts exceeded", "gi");
var twoFactorRegex = new RegExp("must specify two-factor authentication otp code", "gi");
var badCredentialsRegex = new RegExp("bad credentials", "gi");

var handleBasicAuthentication = function (username, password, onSuccess, onRequiresTwoFa, onFailure) {
var scopes = ["user", "repo", "gist", "write:public_key"];

var handleAuthentication = function (username, password, onSuccess, onFailure, twoFactor) {
if (!config.clientId || !config.clientSecret) {
throw "clientId and/or clientSecret missing";
}
Expand All @@ -25,64 +25,34 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
password: password
});

octokit.authorization.create({
scopes: scopes,
note: config.appName,
client_id: config.clientId,
client_secret: config.clientSecret
}, function (err, res) {
if (err) {
if (twoFactorRegex.test(err.message)) {
onRequiresTwoFa();
}
else if (lockedRegex.test(err.message)) {
onFailure("Account locked.")
}
else if (badCredentialsRegex.test(err.message)) {
onFailure("Bad credentials.")
}
else {
onFailure(err)
}
}
else {
onSuccess(res.data.token);
}
});
}

var handleTwoFactorAuthentication = function (username, password, twoFactor, onSuccess, onFailure) {
if (!config.clientId || !config.clientSecret) {
throw "clientId and/or clientSecret missing";
}

if (!config.appName) {
throw "appName missing";
var headers;
if (twoFactor) {
headers = {
"X-GitHub-OTP": twoFactor,
"user-agent": config.appName
};
}

var octokit = octokitWrapper.createOctokit();

octokit.authenticate({
type: "basic",
username: username,
password: password
});

octokit.authorization.create({
scopes: scopes,
note: config.appName,
client_id: config.clientId,
client_secret: config.clientSecret,
headers: {
"X-GitHub-OTP": twoFactor,
"user-agent": config.appName
}
headers: headers
}, function (err, res) {
if (err) {
if (lockedRegex.test(err.message)) {
onFailure("Account locked.")
if (twoFactor && err.code && err.code === 422) {
//Two Factor Enterprise workaround
onSuccess(password);
}
else if (twoFactorRegex.test(err.message)) {
onSuccess(password, "2fa");
}
else if (lockedRegex.test(err.message)) {
onFailure("locked")
}
else if (badCredentialsRegex.test(err.message)) {
onFailure("Bad credentials.")
onFailure("badcredentials")
}
else {
onFailure(err)
Expand All @@ -95,6 +65,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
}

module.exports = {
handleBasicAuthentication: handleBasicAuthentication,
handleTwoFactorAuthentication: handleTwoFactorAuthentication,
handleAuthentication: handleAuthentication,
};
47 changes: 19 additions & 28 deletions octorun/src/bin/app-login.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ commander
.option('-t, --twoFactor')
.parse(process.argv);

var encoding = 'utf-8';

if (commander.twoFactor) {
var handleTwoFactorAuthentication = function (username, password, token) {
authentication.handleTwoFactorAuthentication(username, password, token, function (token) {
output.success(token);
var handleAuthentication = function (username, password, twoFactor) {
authentication.handleAuthentication(username, password, function (token, status) {
if (status) {
output.custom(status, token);
process.exit();
}, function (error) {
output.error(error);
}
else {
output.success(token);
process.exit();
});
}
}
}, function (error) {
output.error(error);
process.exit();
}, twoFactor);
}

var encoding = 'utf-8';
if (commander.twoFactor) {
if (process.stdin.isTTY) {
var readlineSync = require("readline-sync");
var username = readlineSync.question('User: ');
Expand All @@ -32,7 +37,7 @@ if (commander.twoFactor) {
var twoFactor = readlineSync.question('Two Factor: ');

try {
handleTwoFactorAuthentication(username, password, twoFactor);
handleAuthentication(username, password, twoFactor);
}
catch (error) {
output.error(error);
Expand All @@ -56,7 +61,7 @@ if (commander.twoFactor) {
.filter(function (item) { return item; });

try {
handleTwoFactorAuthentication(items[0], items[1], items[2]);
handleAuthentication(items[0], items[1], items[2]);
}
catch (error) {
output.error(error);
Expand All @@ -66,20 +71,6 @@ if (commander.twoFactor) {
}
}
else {
var handleBasicAuthentication = function (username, password) {
authentication.handleBasicAuthentication(username, password,
function (token) {
output.success(token);
process.exit();
}, function () {
output.custom("2fa", password);
process.exit();
}, function (error) {
output.error(error);
process.exit();
});
}

if (process.stdin.isTTY) {
var readlineSync = require("readline-sync");

Expand All @@ -89,7 +80,7 @@ else {
});

try {
handleBasicAuthentication(username, password);
handleAuthentication(username, password);
}
catch (error) {
output.error(error);
Expand All @@ -113,7 +104,7 @@ else {
.filter(function (item) { return item; });

try {
handleBasicAuthentication(items[0], items[1]);
handleAuthentication(items[0], items[1]);
}
catch (error) {
output.error(error);
Expand Down
Loading