@@ -2,13 +2,13 @@ var endOfLine = require('os').EOL;
22var config = require ( "./configuration" ) ;
33var octokitWrapper = require ( "./octokit" ) ;
44
5- var scopes = [ "user" , "repo" , "gist" , "write:public_key" ] ;
6-
75var lockedRegex = new RegExp ( "number of login attempts exceeded" , "gi" ) ;
86var twoFactorRegex = new RegExp ( "must specify two-factor authentication otp code" , "gi" ) ;
97var badCredentialsRegex = new RegExp ( "bad credentials" , "gi" ) ;
108
11- var handleBasicAuthentication = function ( username , password , onSuccess , onRequiresTwoFa , onFailure ) {
9+ var scopes = [ "user" , "repo" , "gist" , "write:public_key" ] ;
10+
11+ var handleAuthentication = function ( username , password , onSuccess , onFailure , twoFactor ) {
1212 if ( ! config . clientId || ! config . clientSecret ) {
1313 throw "clientId and/or clientSecret missing" ;
1414 }
@@ -25,64 +25,34 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
2525 password : password
2626 } ) ;
2727
28- octokit . authorization . create ( {
29- scopes : scopes ,
30- note : config . appName ,
31- client_id : config . clientId ,
32- client_secret : config . clientSecret
33- } , function ( err , res ) {
34- if ( err ) {
35- if ( twoFactorRegex . test ( err . message ) ) {
36- onRequiresTwoFa ( ) ;
37- }
38- else if ( lockedRegex . test ( err . message ) ) {
39- onFailure ( "Account locked." )
40- }
41- else if ( badCredentialsRegex . test ( err . message ) ) {
42- onFailure ( "Bad credentials." )
43- }
44- else {
45- onFailure ( err )
46- }
47- }
48- else {
49- onSuccess ( res . data . token ) ;
50- }
51- } ) ;
52- }
53-
54- var handleTwoFactorAuthentication = function ( username , password , twoFactor , onSuccess , onFailure ) {
55- if ( ! config . clientId || ! config . clientSecret ) {
56- throw "clientId and/or clientSecret missing" ;
57- }
58-
59- if ( ! config . appName ) {
60- throw "appName missing" ;
28+ var headers ;
29+ if ( twoFactor ) {
30+ headers = {
31+ "X-GitHub-OTP" : twoFactor ,
32+ "user-agent" : config . appName
33+ } ;
6134 }
62-
63- var octokit = octokitWrapper . createOctokit ( ) ;
64-
65- octokit . authenticate ( {
66- type : "basic" ,
67- username : username ,
68- password : password
69- } ) ;
7035
7136 octokit . authorization . create ( {
7237 scopes : scopes ,
38+ note : config . appName ,
7339 client_id : config . clientId ,
7440 client_secret : config . clientSecret ,
75- headers : {
76- "X-GitHub-OTP" : twoFactor ,
77- "user-agent" : config . appName
78- }
41+ headers : headers
7942 } , function ( err , res ) {
8043 if ( err ) {
81- if ( lockedRegex . test ( err . message ) ) {
82- onFailure ( "Account locked." )
44+ if ( twoFactor && err . code && err . code === 422 ) {
45+ //Two Factor Enterprise workaround
46+ onSuccess ( password ) ;
47+ }
48+ else if ( twoFactorRegex . test ( err . message ) ) {
49+ onSuccess ( password , "2fa" ) ;
50+ }
51+ else if ( lockedRegex . test ( err . message ) ) {
52+ onFailure ( "locked" )
8353 }
8454 else if ( badCredentialsRegex . test ( err . message ) ) {
85- onFailure ( "Bad credentials. " )
55+ onFailure ( "badcredentials " )
8656 }
8757 else {
8858 onFailure ( err )
@@ -95,6 +65,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
9565}
9666
9767module . exports = {
98- handleBasicAuthentication : handleBasicAuthentication ,
99- handleTwoFactorAuthentication : handleTwoFactorAuthentication ,
68+ handleAuthentication : handleAuthentication ,
10069} ;
0 commit comments