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

Commit 5ef6c1b

Browse files
committed
solve GitHub login two-factor auth problem and add new node package prompt-sync
1 parent 1f26fbd commit 5ef6c1b

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

lib/plugins/leetcode.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ plugin.getSubmissions = function(problem, cb) {
333333

334334
// FIXME: this only return the 1st 20 submissions, we should get next if necessary.
335335
const submissions = JSON.parse(body).submissions_dump;
336-
for (let submission of submissions)
336+
for (const submission of submissions)
337337
submission.id = _.last(_.compact(submission.url.split('/')));
338338

339339
return cb(null, submissions);
@@ -559,6 +559,20 @@ function saveAndGetUser(user, cb, cookieData) {
559559
plugin.getUser(user, cb);
560560
}
561561

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+
562576
plugin.cookieLogin = function(user, cb) {
563577
const cookieData = parseCookie(user.cookie, cb);
564578
user.sessionId = cookieData.sessionId;
@@ -594,14 +608,36 @@ plugin.githubLogin = function(user, cb) {
594608
if (resp.statusCode !== 200) {
595609
return cb('GitHub login failed');
596610
}
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(/name="authenticity_token" value="(.*?)"/);
616+
if (authenticityTokenTwoFactor === null) {
617+
return cb('Get GitHub two-factor token failed');
601618
}
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+
}
605641
});
606642
});
607643
};
@@ -640,14 +676,7 @@ plugin.linkedinLogin = function(user, cb) {
640676
if (resp.statusCode !== 200) {
641677
return cb('LinkedIn login failed');
642678
}
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);
651680
});
652681
});
653682
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"nock": "10.0.2",
7373
"nyc": "^13.3.0",
7474
"pkg": "^4.3.4",
75+
"prompt-sync": "^4.2.0",
7576
"rewire": "4.0.1"
7677
}
7778
}

0 commit comments

Comments
 (0)