Skip to content

Commit

Permalink
Merge pull request #5 from jeppenejsum/force2fa
Browse files Browse the repository at this point in the history
Check if 2FA is required and trigger SMS if needed
  • Loading branch information
rvagg committed Jun 5, 2014
2 parents e6a2ef0 + 05f56c0 commit 6d22878
Showing 1 changed file with 26 additions and 8 deletions.
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

0 comments on commit 6d22878

Please sign in to comment.