@@ -2,16 +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 lockedErrorMessage = "locked" ;
12- var badCredentialsErrorMessage = "badcredentials" ;
9+ var scopes = [ "user" , "repo" , "gist" , "write:public_key" ] ;
1310
14- var handleBasicAuthentication = function ( username , password , onSuccess , onRequiresTwoFa , onFailure ) {
11+ var handleAuthentication = function ( username , password , onSuccess , onFailure , twoFactor ) {
1512 var octokit = octokitWrapper . createOctokit ( ) ;
1613
1714 octokit . authenticate ( {
@@ -20,56 +17,29 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
2017 password : password
2118 } ) ;
2219
20+ var headers ;
21+ if ( twoFactor ) {
22+ headers = {
23+ "X-GitHub-OTP" : twoFactor
24+ } ;
25+ }
26+
2327 octokit . authorization . create ( {
2428 scopes : scopes ,
2529 note : config . appName ,
2630 client_id : config . clientId ,
27- client_secret : config . clientSecret
31+ client_secret : config . clientSecret ,
32+ headers : headers
2833 } , function ( err , res ) {
2934 if ( err ) {
3035 if ( twoFactorRegex . test ( err . message ) ) {
31- onRequiresTwoFa ( ) ;
36+ onSuccess ( password , "2fa" ) ;
3237 }
3338 else if ( lockedRegex . test ( err . message ) ) {
34- onFailure ( lockedErrorMessage )
35- }
36- else if ( badCredentialsRegex . test ( err . message ) ) {
37- onFailure ( badCredentialsErrorMessage )
38- }
39- else {
40- onFailure ( err )
41- }
42- }
43- else {
44- onSuccess ( res . data . token ) ;
45- }
46- } ) ;
47- }
48-
49- var handleTwoFactorAuthentication = function ( username , password , twoFactor , onSuccess , onFailure ) {
50- var octokit = octokitWrapper . createOctokit ( ) ;
51-
52- octokit . authenticate ( {
53- type : "basic" ,
54- username : username ,
55- password : password
56- } ) ;
57-
58- octokit . authorization . create ( {
59- scopes : scopes ,
60- note : config . appName ,
61- client_id : config . clientId ,
62- client_secret : config . clientSecret ,
63- headers : {
64- "X-GitHub-OTP" : twoFactor
65- }
66- } , function ( err , res ) {
67- if ( err ) {
68- if ( lockedRegex . test ( err . message ) ) {
69- onFailure ( lockedErrorMessage )
39+ onFailure ( "locked" )
7040 }
7141 else if ( badCredentialsRegex . test ( err . message ) ) {
72- onFailure ( badCredentialsErrorMessage )
42+ onFailure ( "badcredentials" )
7343 }
7444 else {
7545 onFailure ( err )
@@ -82,6 +52,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
8252}
8353
8454module . exports = {
85- handleBasicAuthentication : handleBasicAuthentication ,
86- handleTwoFactorAuthentication : handleTwoFactorAuthentication ,
55+ handleAuthentication : handleAuthentication ,
8756} ;
0 commit comments