Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if 2FA is required and trigger SMS if needed #5

Merged
merged 1 commit into from
Jun 5, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions ghauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function createAuth (options, callback) {
}


function prompt (callback) {
function prompt (options, callback) {
read({ prompt: 'Your GitHub username:' }, function (err, user) {
if (err)
return callback(err)
Expand All @@ -58,12 +58,30 @@ function prompt (callback) {
if (err)
return callback(err)

read({ prompt: 'Your GitHub OTP/2FA Code (optional):' }, function (err, otp) {
if (err)
return callback(err)

callback(null, { user: user, pass: pass, otp: otp })
})
// Check for 2FA. This triggers an SMS if needed
var reqOptions = {
headers : {
'User-Agent' : options.userAgent || defaultUA
}
, method : 'post'
, auth : user + ':' + pass
}
var req = hyperquest(authUrl, reqOptions, function (err, response) {
if (err)
return callback(err)

var otp = response.headers['x-github-otp']
if (!otp || otp.indexOf('required') < 0)
return callback(null, { user: user, pass: pass, otp: null })

read({ prompt: 'Your GitHub OTP/2FA Code (optional):' }, function (err, otp) {
if (err)
return callback(err)

callback(null, { user: user, pass: pass, otp: otp })
})
})
req.end();
})
})
}
Expand All @@ -81,7 +99,7 @@ function auth (options, callback) {
if (authData && authData.user && authData.token)
return callback(null, authData)

prompt(function (err, data) {
prompt(options, function (err, data) {
if (err)
return callback(err)

Expand Down