Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 53b3446

Browse files
committed
format the code by review
1 parent f7a6b53 commit 53b3446

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

lib/config.js

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,42 @@ const DEFAULT_CONFIG = {
3131
'swift'
3232
],
3333
urls: {
34-
base: 'https://leetcode.com',
35-
graphql: 'https://leetcode.com/graphql',
36-
login: 'https://leetcode.com/accounts/login/',
37-
// third part login base urls. TODO facebook google
38-
github_login: 'https://leetcode.com/accounts/github/login/?next=%2F',
39-
facebook_login: 'https://leetcode.com/accounts/facebook/login/?next=%2F',
40-
linkedin_login: 'https://leetcode.com/accounts/linkedin_oauth2/login/?next=%2F',
41-
problems: 'https://leetcode.com/api/problems/$category/',
42-
problem: 'https://leetcode.com/problems/$slug/description/',
43-
test: 'https://leetcode.com/problems/$slug/interpret_solution/',
44-
session: 'https://leetcode.com/session/',
45-
submit: 'https://leetcode.com/problems/$slug/submit/',
46-
submissions: 'https://leetcode.com/api/submissions/$slug',
47-
submission: 'https://leetcode.com/submissions/detail/$id/',
48-
verify: 'https://leetcode.com/submissions/detail/$id/check/',
49-
favorites: 'https://leetcode.com/list/api/questions',
50-
favorite_delete: 'https://leetcode.com/list/api/questions/$hash/$id',
51-
plugin: 'https://raw.githubusercontent.com/leetcode-tools/leetcode-cli-plugins/master/plugins/$name.js'
34+
// base urls
35+
base: 'https://leetcode.com',
36+
graphql: 'https://leetcode.com/graphql',
37+
login: 'https://leetcode.com/accounts/login/',
38+
// third part login base urls. TODO facebook google
39+
github_login: 'https://leetcode.com/accounts/github/login/?next=%2F',
40+
facebook_login: 'https://leetcode.com/accounts/facebook/login/?next=%2F',
41+
linkedin_login: 'https://leetcode.com/accounts/linkedin_oauth2/login/?next=%2F',
42+
// redirect urls
43+
leetcode_redirect: 'https://leetcode.com/',
44+
github_tf_redirect: 'https://github.com/sessions/two-factor',
45+
// simulate login urls
46+
github_login_request: 'https://github.com/login',
47+
github_session_request: 'https://github.com/session',
48+
github_tf_session_request: 'https://github.com/sessions/two-factor',
49+
linkedin_login_request: 'https://www.linkedin.com',
50+
linkedin_session_request: 'https://www.linkedin.com/uas/login-submit',
51+
// questions urls
52+
problems: 'https://leetcode.com/api/problems/$category/',
53+
problem: 'https://leetcode.com/problems/$slug/description/',
54+
test: 'https://leetcode.com/problems/$slug/interpret_solution/',
55+
session: 'https://leetcode.com/session/',
56+
submit: 'https://leetcode.com/problems/$slug/submit/',
57+
submissions: 'https://leetcode.com/api/submissions/$slug',
58+
submission: 'https://leetcode.com/submissions/detail/$id/',
59+
verify: 'https://leetcode.com/submissions/detail/$id/check/',
60+
favorites: 'https://leetcode.com/list/api/questions',
61+
favorite_delete: 'https://leetcode.com/list/api/questions/$hash/$id',
62+
plugin: 'https://raw.githubusercontent.com/leetcode-tools/leetcode-cli-plugins/master/plugins/$name.js'
63+
},
64+
// login methods enum
65+
login_methods: {
66+
LeetCode: 'LeetCode',
67+
Cookie: 'Cookie',
68+
GitHub: 'Github',
69+
LinkedIn: 'LinkedIn'
5270
}
5371
},
5472

lib/plugins/leetcode.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ function parseCookie(cookie, cb) {
555555
function requestLeetcodeAndSave(request, leetcodeUrl, user, cb, party) {
556556
request.get({url: leetcodeUrl}, function(e, resp, body) {
557557
const redirectUri = resp.request.uri.href;
558-
if (redirectUri !== 'https://leetcode.com/') {
559-
return cb(`${party} login failed or ${party} did not link to LeetCode`);
558+
if (redirectUri !== config.sys.urls.leetcode_redirect) {
559+
return cb(`${party} login failed or ${party} did not connect to LeetCode`);
560560
}
561561
const cookieData = parseCookie(resp.request.headers.cookie, cb);
562562
user.sessionId = cookieData.sessionId;
@@ -575,15 +575,16 @@ plugin.cookieLogin = function(user, cb) {
575575
};
576576

577577
plugin.githubLogin = function(user, cb) {
578-
const leetcodeUrl = config.sys.urls.github_login;
578+
const urls = config.sys.urls
579+
const leetcodeUrl = urls.github_login;
579580
const _request = request.defaults({jar: true});
580-
_request('https://github.com/login', function(e, resp, body) {
581+
_request(urls.github_login_request, function(e, resp, body) {
581582
const authenticityToken = body.match(/name="authenticity_token" value="(.*?)"/);
582583
if (authenticityToken === null) {
583584
return cb('Get GitHub token failed');
584585
}
585586
const options = {
586-
url: 'https://github.com/session',
587+
url: urls.github_session_request,
587588
method: 'POST',
588589
headers: {
589590
'Content-Type': 'application/x-www-form-urlencoded',
@@ -601,16 +602,16 @@ plugin.githubLogin = function(user, cb) {
601602
if (resp.statusCode !== 200) {
602603
return cb('GitHub login failed');
603604
}
604-
if (resp.request.uri.href == 'https://github.com/sessions/two-factor') {
605-
cb('Your Github are using two-factor authentication');
605+
if (resp.request.uri.href === urls.github_tf_redirect) {
606+
cb('Your GitHub are using two-factor authentication');
606607
// read two-factor code must be sync.
607608
const twoFactorcode = require('prompt-sync')()('Please enter your two-factor code: ');
608609
const authenticityTokenTwoFactor = body.match(/name="authenticity_token" value="(.*?)"/);
609610
if (authenticityTokenTwoFactor === null) {
610611
return cb('Get GitHub two-factor token failed');
611612
}
612613
const optionsTwoFactor = {
613-
url: 'https://github.com/sessions/two-factor',
614+
url: urls.github_tf_session_request,
614615
method: 'POST',
615616
headers: {
616617
'Content-Type': 'application/x-www-form-urlencoded',
@@ -623,27 +624,28 @@ plugin.githubLogin = function(user, cb) {
623624
},
624625
};
625626
_request(optionsTwoFactor, function(e, resp, body) {
626-
if (resp.request.uri.href === 'https://github.com/sessions/two-factor') {
627-
return cb('Wrong two-factor code please check');
627+
if (resp.request.uri.href === urls.github_tf_session_request) {
628+
return cb('Invalid two-factor code please check');
628629
}
629-
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, 'GitHub');
630+
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, config.sys.login_methods.GitHub);
630631
});
631632
} else {
632-
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, 'GitHub');
633+
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, config.sys.login_methods.GitHub);
633634
}
634635
});
635636
});
636637
};
637638

638639
plugin.linkedinLogin = function(user, cb) {
639-
const leetcodeUrl = config.sys.urls.linkedin_login;
640+
const urls = config.sys.urls
641+
const leetcodeUrl = urls.linkedin_login;
640642
const _request = request.defaults({
641643
jar: true,
642644
headers: {
643645
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'
644646
}
645647
});
646-
_request('https://www.linkedin.com', function(e, resp, body) {
648+
_request(urls.linkedin_login_request, function(e, resp, body) {
647649
if ( resp.statusCode !== 200) {
648650
return cb('Get LinkedIn session failed');
649651
}
@@ -652,7 +654,7 @@ plugin.linkedinLogin = function(user, cb) {
652654
return cb('Get LinkedIn token failed');
653655
}
654656
const options = {
655-
url: 'https://www.linkedin.com/uas/login-submit',
657+
url: urls.linkedin_session_request,
656658
method: 'POST',
657659
headers: {
658660
'Content-Type': 'application/x-www-form-urlencoded',
@@ -669,7 +671,7 @@ plugin.linkedinLogin = function(user, cb) {
669671
if (resp.statusCode !== 200) {
670672
return cb('LinkedIn login failed');
671673
}
672-
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, 'LinkedIn');
674+
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, config.sys.login_methods.LinkedIn);
673675
});
674676
});
675677
};

0 commit comments

Comments
 (0)