@@ -2,13 +2,13 @@ var endOfLine = require('os').EOL;
2
2
var config = require ( "./configuration" ) ;
3
3
var octokitWrapper = require ( "./octokit" ) ;
4
4
5
- var scopes = [ "user" , "repo" , "gist" , "write:public_key" ] ;
6
-
7
5
var lockedRegex = new RegExp ( "number of login attempts exceeded" , "gi" ) ;
8
6
var twoFactorRegex = new RegExp ( "must specify two-factor authentication otp code" , "gi" ) ;
9
7
var badCredentialsRegex = new RegExp ( "bad credentials" , "gi" ) ;
10
8
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 ) {
12
12
if ( ! config . clientId || ! config . clientSecret ) {
13
13
throw "clientId and/or clientSecret missing" ;
14
14
}
@@ -25,64 +25,34 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
25
25
password : password
26
26
} ) ;
27
27
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
+ } ;
61
34
}
62
-
63
- var octokit = octokitWrapper . createOctokit ( ) ;
64
-
65
- octokit . authenticate ( {
66
- type : "basic" ,
67
- username : username ,
68
- password : password
69
- } ) ;
70
35
71
36
octokit . authorization . create ( {
72
37
scopes : scopes ,
38
+ note : config . appName ,
73
39
client_id : config . clientId ,
74
40
client_secret : config . clientSecret ,
75
- headers : {
76
- "X-GitHub-OTP" : twoFactor ,
77
- "user-agent" : config . appName
78
- }
41
+ headers : headers
79
42
} , function ( err , res ) {
80
43
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" )
83
53
}
84
54
else if ( badCredentialsRegex . test ( err . message ) ) {
85
- onFailure ( "Bad credentials. " )
55
+ onFailure ( "badcredentials " )
86
56
}
87
57
else {
88
58
onFailure ( err )
@@ -95,6 +65,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
95
65
}
96
66
97
67
module . exports = {
98
- handleBasicAuthentication : handleBasicAuthentication ,
99
- handleTwoFactorAuthentication : handleTwoFactorAuthentication ,
68
+ handleAuthentication : handleAuthentication ,
100
69
} ;
0 commit comments