@@ -51,7 +51,7 @@ plugin.checkError = function(e, resp, expectedStatus) {
51
51
52
52
plugin . init = function ( ) {
53
53
config . app = 'leetcode' ;
54
- }
54
+ } ;
55
55
56
56
plugin . getProblems = function ( cb ) {
57
57
log . debug ( 'running leetcode.getProblems' ) ;
@@ -95,7 +95,7 @@ plugin.getCategoryProblems = function(category, cb) {
95
95
}
96
96
97
97
const problems = json . stat_status_pairs
98
- . filter ( p => ! p . stat . question__hide )
98
+ . filter ( ( p ) => ! p . stat . question__hide )
99
99
. map ( function ( p ) {
100
100
return {
101
101
state : p . status || 'None' ,
@@ -167,7 +167,7 @@ plugin.getProblem = function(problem, cb) {
167
167
problem . testable = q . enableRunCode ;
168
168
problem . templateMeta = JSON . parse ( q . metaData ) ;
169
169
// @si -yao: seems below property is never used.
170
- //problem.discuss = q.discussCategoryId;
170
+ // problem.discuss = q.discussCategoryId;
171
171
172
172
return cb ( null , problem ) ;
173
173
} ) ;
@@ -254,9 +254,9 @@ function formatResult(result) {
254
254
} ;
255
255
256
256
x . error = _ . chain ( result )
257
- . pick ( ( v , k ) => / _ e r r o r $ / . test ( k ) && v . length > 0 )
258
- . values ( )
259
- . value ( ) ;
257
+ . pick ( ( v , k ) => / _ e r r o r $ / . test ( k ) && v . length > 0 )
258
+ . values ( )
259
+ . value ( ) ;
260
260
261
261
if ( / [ r u n c o d e | i n t e r p r e t ] .* / . test ( result . submission_id ) ) {
262
262
// It's testing
@@ -374,8 +374,8 @@ plugin.starProblem = function(problem, starred, cb) {
374
374
} ;
375
375
} else {
376
376
opts . url = config . sys . urls . favorite_delete
377
- . replace ( '$hash' , user . hash )
378
- . replace ( '$id' , problem . id ) ;
377
+ . replace ( '$hash' , user . hash )
378
+ . replace ( '$id' , problem . id ) ;
379
379
opts . method = 'DELETE' ;
380
380
}
381
381
@@ -508,7 +508,7 @@ plugin.signin = function(user, cb) {
508
508
plugin . getUser = function ( user , cb ) {
509
509
plugin . getFavorites ( function ( e , favorites ) {
510
510
if ( ! e ) {
511
- const f = favorites . favorites . private_favorites . find ( f => f . name === 'Favorite' ) ;
511
+ const f = favorites . favorites . private_favorites . find ( ( f ) => f . name === 'Favorite' ) ;
512
512
if ( f ) {
513
513
user . hash = f . id_hash ;
514
514
user . name = favorites . user_name ;
@@ -538,19 +538,68 @@ plugin.login = function(user, cb) {
538
538
} ) ;
539
539
} ;
540
540
541
- plugin . cookieLogin = function ( user , cb ) {
542
- // re pattern for cookie chrome or firefox
541
+ function parseCookie ( cookie , cb ) {
543
542
const SessionPattern = / L E E T C O D E _ S E S S I O N = ( .+ ?) ( ; | $ ) / ;
544
543
const csrfPattern = / c s r f t o k e n = ( .+ ?) ( ; | $ ) / ;
545
- const reSessionResult = SessionPattern . exec ( user . cookie ) ;
546
- const reCsrfResult = csrfPattern . exec ( user . cookie ) ;
544
+ const reSessionResult = SessionPattern . exec ( cookie ) ;
545
+ const reCsrfResult = csrfPattern . exec ( cookie ) ;
547
546
if ( reSessionResult === null || reCsrfResult === null ) {
548
- return cb ( 'invalid cookie?' )
547
+ return cb ( 'invalid cookie?' ) ;
549
548
}
550
- user . sessionId = reSessionResult [ 1 ] ;
551
- user . sessionCSRF = reCsrfResult [ 1 ] ;
549
+ return {
550
+ sessionId : reSessionResult [ 1 ] ,
551
+ sessionCSRF : reCsrfResult [ 1 ] ,
552
+ } ;
553
+ }
554
+
555
+ plugin . cookieLogin = function ( user , cb ) {
556
+ const cookieData = parseCookie ( user . cookie , cb ) ;
557
+ user . sessionId = cookieData . sessionId ;
558
+ user . sessionCSRF = cookieData . sessionCSRF ;
552
559
session . saveUser ( user ) ;
553
560
plugin . getUser ( user , cb ) ;
554
- }
561
+ } ;
562
+
563
+ plugin . githubLogin = function ( user , cb ) {
564
+ const leetcodeUrl = config . sys . urls . github_login ;
565
+ const _request = request . defaults ( { jar : true } ) ;
566
+ _request ( 'https://github.com/login' , function ( e , resp , body ) {
567
+ const authenticityToken = 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 = " ( .* ?) " / ) ;
568
+ if ( authenticityToken === null ) {
569
+ return cb ( 'Get github token failed' ) ;
570
+ }
571
+ const options = {
572
+ url : 'https://github.com/session' ,
573
+ method : 'POST' ,
574
+ headers : {
575
+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
576
+ } ,
577
+ followAllRedirects : true ,
578
+ form : {
579
+ 'login' : user . login ,
580
+ 'password' : user . pass ,
581
+ 'authenticity_token' : authenticityToken [ 1 ] ,
582
+ 'utf8' : encodeURIComponent ( '✓' ) ,
583
+ 'commit' : encodeURIComponent ( 'Sign in' )
584
+ } ,
585
+ } ;
586
+ _request ( options , function ( e , resp , body ) {
587
+ if ( resp . statusCode !== 200 ) {
588
+ return cb ( 'Github login failed' ) ;
589
+ }
590
+ _request . get ( { url : leetcodeUrl } , function ( e , resp , body ) {
591
+ const redirectUri = resp . request . uri . href ;
592
+ if ( redirectUri !== 'https://leetcode.com/' ) {
593
+ return cb ( 'Github login failed or github did not link to leetcode' ) ;
594
+ }
595
+ const cookieData = parseCookie ( resp . request . headers . cookie , cb ) ;
596
+ user . sessionId = cookieData . sessionId ;
597
+ user . sessionCSRF = cookieData . sessionCSRF ;
598
+ session . saveUser ( user ) ;
599
+ plugin . getUser ( user , cb ) ;
600
+ } ) ;
601
+ } ) ;
602
+ } ) ;
603
+ } ;
555
604
556
605
module . exports = plugin ;
0 commit comments