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

Commit eb8da48

Browse files
committed
fix: GihHub login for leetcode
1 parent 06f6569 commit eb8da48

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

lib/plugins/leetcode.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -540,30 +540,23 @@ plugin.login = function(user, cb) {
540540
};
541541

542542
function parseCookie(cookie, body, cb) {
543-
const isCN = config.app === 'leetcode.cn';
544543
const SessionPattern = /LEETCODE_SESSION=(.+?)(;|$)/;
545-
let csrfPattern;
546-
// leetcode-cn.com Cookie is not the same as leetcode.com in third parties
547-
if (isCN) {
548-
csrfPattern = /name="csrfmiddlewaretoken" value="(.*?)"/;
549-
} else {
550-
csrfPattern = /csrftoken=(.+?)(;|$)/;
551-
}
544+
const csrfPattern = /csrftoken=(.+?)(;|$)/;
552545
const reSessionResult = SessionPattern.exec(cookie);
553-
const reCsrfResult = csrfPattern.exec(isCN? body: cookie);
546+
const reCsrfResult = csrfPattern.exec(cookie);
554547
if (reSessionResult === null || reCsrfResult === null) {
555548
return cb('invalid cookie?');
556549
}
557550
return {
558-
sessionId: reSessionResult[1],
551+
// sessionId: SessionResult[1],
552+
sessionId: cookie,
559553
sessionCSRF: reCsrfResult[1],
560554
};
561555
}
562556

563557
function requestLeetcodeAndSave(request, leetcodeUrl, user, cb) {
564558
request.get({url: leetcodeUrl}, function(e, resp, body) {
565559
const redirectUri = resp.request.uri.href;
566-
console.log(redirectUri);
567560
if (redirectUri !== config.sys.urls.leetcode_redirect) {
568561
return cb('Login failed. Please make sure the credential is correct.');
569562
}
@@ -589,9 +582,18 @@ plugin.githubLogin = function(user, cb) {
589582
const _request = request.defaults({jar: true});
590583
_request(urls.github_login_request, function(e, resp, body) {
591584
const authenticityToken = body.match(/name="authenticity_token" value="(.*?)"/);
592-
if (authenticityToken === null) {
593-
return cb('Get GitHub token failed');
585+
let gaId = body.match(/name="ga_id" value="(.*?)"/);
586+
if (!gaId) {
587+
gaId = '';
588+
}
589+
let requiredField = body.match(/name="required_field_(.*?)"/);
590+
const timestamp = body.match(/name="timestamp" value="(.*?)"/);
591+
const timestampSecret = body.match(/name="timestamp_secret" value="(.*?)"/);
592+
593+
if (!(authenticityToken && timestamp && timestampSecret && requiredField)) {
594+
return cb('Get GitHub payload failed');
594595
}
596+
requiredField = 'required_field_' + requiredField[1];
595597
const options = {
596598
url: urls.github_session_request,
597599
method: 'POST',
@@ -600,11 +602,17 @@ plugin.githubLogin = function(user, cb) {
600602
},
601603
followAllRedirects: true,
602604
form: {
603-
'login': user.login,
604-
'password': user.pass,
605-
'authenticity_token': authenticityToken[1],
606-
'utf8': encodeURIComponent('✓'),
607-
'commit': encodeURIComponent('Sign in')
605+
'login': user.login,
606+
'password': user.pass,
607+
'authenticity_token': authenticityToken[1],
608+
'commit': encodeURIComponent('Sign in'),
609+
'ga_id': gaId,
610+
'webauthn-support': 'supported',
611+
'webauthn-iuvpaa-support': 'unsupported',
612+
'return_to': '',
613+
'requiredField': '',
614+
'timestamp': timestamp[1],
615+
'timestamp_secret': timestampSecret[1],
608616
},
609617
};
610618
_request(options, function(e, resp, body) {

0 commit comments

Comments
 (0)