Skip to content
64 changes: 43 additions & 21 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ plugin.getSubmissions = function(problem, cb) {

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

return cb(null, submissions);
Expand Down Expand Up @@ -552,11 +552,18 @@ function parseCookie(cookie, cb) {
};
}

function saveAndGetUser(user, cb, cookieData) {
user.sessionId = cookieData.sessionId;
user.sessionCSRF = cookieData.sessionCSRF;
session.saveUser(user);
plugin.getUser(user, cb);
function requestLeetcodeAndSave(request, leetcodeUrl, user, cb, party) {
request.get({url: leetcodeUrl}, function(e, resp, body) {
const redirectUri = resp.request.uri.href;
if (redirectUri !== 'https://leetcode.com/') {
return cb(`${party} login failed or ${party} did not link to LeetCode`);
}
const cookieData = parseCookie(resp.request.headers.cookie, cb);
user.sessionId = cookieData.sessionId;
user.sessionCSRF = cookieData.sessionCSRF;
session.saveUser(user);
plugin.getUser(user, cb);
});
}

plugin.cookieLogin = function(user, cb) {
Expand Down Expand Up @@ -594,14 +601,36 @@ plugin.githubLogin = function(user, cb) {
if (resp.statusCode !== 200) {
return cb('GitHub login failed');
}
_request.get({url: leetcodeUrl}, function(e, resp, body) {
const redirectUri = resp.request.uri.href;
if (redirectUri !== 'https://leetcode.com/') {
return cb('GitHub login failed or GitHub did not link to LeetCode');
if (resp.request.uri.href == 'https://github.com/sessions/two-factor') {
cb('Your Github are using two-factor authentication');
// read two-factor code must be sync.
const twoFactorcode = require('prompt-sync')()('Please enter your two-factor code: ');
const authenticityTokenTwoFactor = body.match(/name="authenticity_token" value="(.*?)"/);
if (authenticityTokenTwoFactor === null) {
return cb('Get GitHub two-factor token failed');
}
const cookieData = parseCookie(resp.request.headers.cookie, cb);
saveAndGetUser(user, cb, cookieData);
});
const optionsTwoFactor = {
url: 'https://github.com/sessions/two-factor',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
followAllRedirects: true,
form: {
'otp': twoFactorcode,
'authenticity_token': authenticityTokenTwoFactor[1],
'utf8': encodeURIComponent('✓'),
},
};
_request(optionsTwoFactor, function(e, resp, body) {
if (resp.request.uri.href === 'https://github.com/sessions/two-factor') {
return cb('Wrong two-factor code please check');
}
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, 'GitHub');
});
} else {
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, 'GitHub');
}
});
});
};
Expand Down Expand Up @@ -640,14 +669,7 @@ plugin.linkedinLogin = function(user, cb) {
if (resp.statusCode !== 200) {
return cb('LinkedIn login failed');
}
_request.get({url: leetcodeUrl}, function(e, resp, body) {
const redirectUri = resp.request.uri.href;
if (redirectUri !== 'https://leetcode.com/') {
return cb('LinkedIn login failed or LinkedIn did not link to LeetCode');
}
const cookieData = parseCookie(resp.request.headers.cookie, cb);
saveAndGetUser(user, cb, cookieData);
});
requestLeetcodeAndSave(_request, leetcodeUrl, user, cb, 'LinkedIn');
});
});
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"nock": "10.0.2",
"nyc": "^13.3.0",
"pkg": "^4.3.4",
"prompt-sync": "^4.2.0",
"rewire": "4.0.1"
}
}