@@ -333,7 +333,7 @@ plugin.getSubmissions = function(problem, cb) {
333
333
334
334
// FIXME: this only return the 1st 20 submissions, we should get next if necessary.
335
335
const submissions = JSON . parse ( body ) . submissions_dump ;
336
- for ( let submission of submissions )
336
+ for ( const submission of submissions )
337
337
submission . id = _ . last ( _ . compact ( submission . url . split ( '/' ) ) ) ;
338
338
339
339
return cb ( null , submissions ) ;
@@ -559,6 +559,20 @@ function saveAndGetUser(user, cb, cookieData) {
559
559
plugin . getUser ( user , cb ) ;
560
560
}
561
561
562
+ function requestLeetcodeAndSave ( request , leetcodeUrl , user , cb ) {
563
+ request . get ( { url : leetcodeUrl } , function ( e , resp , body ) {
564
+ const redirectUri = resp . request . uri . href ;
565
+ if ( redirectUri !== 'https://leetcode.com/' ) {
566
+ return cb ( 'LinkedIn login failed or LinkedIn did not link to LeetCode' ) ;
567
+ }
568
+ const cookieData = parseCookie ( resp . request . headers . cookie , cb ) ;
569
+ user . sessionId = cookieData . sessionId ;
570
+ user . sessionCSRF = cookieData . sessionCSRF ;
571
+ session . saveUser ( user ) ;
572
+ plugin . getUser ( user , cb ) ;
573
+ } ) ;
574
+ }
575
+
562
576
plugin . cookieLogin = function ( user , cb ) {
563
577
const cookieData = parseCookie ( user . cookie , cb ) ;
564
578
user . sessionId = cookieData . sessionId ;
@@ -594,14 +608,36 @@ plugin.githubLogin = function(user, cb) {
594
608
if ( resp . statusCode !== 200 ) {
595
609
return cb ( 'GitHub login failed' ) ;
596
610
}
597
- _request . get ( { url : leetcodeUrl } , function ( e , resp , body ) {
598
- const redirectUri = resp . request . uri . href ;
599
- if ( redirectUri !== 'https://leetcode.com/' ) {
600
- return cb ( 'GitHub login failed or GitHub did not link to LeetCode' ) ;
611
+ if ( resp . request . uri . href == 'https://github.com/sessions/two-factor' ) {
612
+ cb ( 'Your Github are using two-factor authentication' ) ;
613
+ // read two-factor code must be sync.
614
+ const twoFactorcode = require ( 'prompt-sync' ) ( ) ( 'Please enter your two-factor code: ' ) ;
615
+ const authenticityTokenTwoFactor = body . match ( / n a m e = " a u t h e n t i c i t y _ t o k e n " v a l u e = " ( .* ?) " / ) ;
616
+ if ( authenticityTokenTwoFactor === null ) {
617
+ return cb ( 'Get GitHub two-factor token failed' ) ;
601
618
}
602
- const cookieData = parseCookie ( resp . request . headers . cookie , cb ) ;
603
- saveAndGetUser ( user , cb , cookieData ) ;
604
- } ) ;
619
+ const optionsTwoFactor = {
620
+ url : 'https://github.com/sessions/two-factor' ,
621
+ method : 'POST' ,
622
+ headers : {
623
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
624
+ } ,
625
+ followAllRedirects : true ,
626
+ form : {
627
+ 'otp' : twoFactorcode ,
628
+ 'authenticity_token' : authenticityTokenTwoFactor [ 1 ] ,
629
+ 'utf8' : encodeURIComponent ( '✓' ) ,
630
+ } ,
631
+ } ;
632
+ _request ( optionsTwoFactor , function ( e , resp , body ) {
633
+ if ( resp . request . uri . href === 'https://github.com/sessions/two-factor' ) {
634
+ return cb ( 'Wrong two-factor code please check' ) ;
635
+ }
636
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
637
+ } ) ;
638
+ } else {
639
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
640
+ }
605
641
} ) ;
606
642
} ) ;
607
643
} ;
@@ -640,14 +676,7 @@ plugin.linkedinLogin = function(user, cb) {
640
676
if ( resp . statusCode !== 200 ) {
641
677
return cb ( 'LinkedIn login failed' ) ;
642
678
}
643
- _request . get ( { url : leetcodeUrl } , function ( e , resp , body ) {
644
- const redirectUri = resp . request . uri . href ;
645
- if ( redirectUri !== 'https://leetcode.com/' ) {
646
- return cb ( 'LinkedIn login failed or LinkedIn did not link to LeetCode' ) ;
647
- }
648
- const cookieData = parseCookie ( resp . request . headers . cookie , cb ) ;
649
- saveAndGetUser ( user , cb , cookieData ) ;
650
- } ) ;
679
+ requestLeetcodeAndSave ( _request , leetcodeUrl , user , cb ) ;
651
680
} ) ;
652
681
} ) ;
653
682
} ;
0 commit comments