@@ -2,16 +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 lockedErrorMessage = "locked" ;
12
- var badCredentialsErrorMessage = "badcredentials" ;
9
+ var scopes = [ "user" , "repo" , "gist" , "write:public_key" ] ;
13
10
14
- var handleBasicAuthentication = function ( username , password , onSuccess , onRequiresTwoFa , onFailure ) {
11
+ var handleAuthentication = function ( username , password , onSuccess , onFailure , twoFactor ) {
15
12
var octokit = octokitWrapper . createOctokit ( ) ;
16
13
17
14
octokit . authenticate ( {
@@ -20,56 +17,29 @@ var handleBasicAuthentication = function (username, password, onSuccess, onRequi
20
17
password : password
21
18
} ) ;
22
19
20
+ var headers ;
21
+ if ( twoFactor ) {
22
+ headers = {
23
+ "X-GitHub-OTP" : twoFactor
24
+ } ;
25
+ }
26
+
23
27
octokit . authorization . create ( {
24
28
scopes : scopes ,
25
29
note : config . appName ,
26
30
client_id : config . clientId ,
27
- client_secret : config . clientSecret
31
+ client_secret : config . clientSecret ,
32
+ headers : headers
28
33
} , function ( err , res ) {
29
34
if ( err ) {
30
35
if ( twoFactorRegex . test ( err . message ) ) {
31
- onRequiresTwoFa ( ) ;
36
+ onSuccess ( password , "2fa" ) ;
32
37
}
33
38
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" )
70
40
}
71
41
else if ( badCredentialsRegex . test ( err . message ) ) {
72
- onFailure ( badCredentialsErrorMessage )
42
+ onFailure ( "badcredentials" )
73
43
}
74
44
else {
75
45
onFailure ( err )
@@ -82,6 +52,5 @@ var handleTwoFactorAuthentication = function (username, password, twoFactor, onS
82
52
}
83
53
84
54
module . exports = {
85
- handleBasicAuthentication : handleBasicAuthentication ,
86
- handleTwoFactorAuthentication : handleTwoFactorAuthentication ,
55
+ handleAuthentication : handleAuthentication ,
87
56
} ;
0 commit comments