Skip to content

Fix: LinkedIn login problem. #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ const DEFAULT_CONFIG = {
github_login_request: 'https://github.com/login',
github_session_request: 'https://github.com/session',
github_tf_session_request: 'https://github.com/sessions/two-factor',
linkedin_login_request: 'https://www.linkedin.com',
linkedin_session_request: 'https://www.linkedin.com/uas/login-submit',
linkedin_login_request: 'https://www.linkedin.com/login',
linkedin_session_request: 'https://www.linkedin.com/checkpoint/lg/login-submit',
// questions urls
problems: 'https://leetcode.com/api/problems/$category/',
problem: 'https://leetcode.com/problems/$slug/description/',
Expand All @@ -60,7 +60,7 @@ const DEFAULT_CONFIG = {
verify: 'https://leetcode.com/submissions/detail/$id/check/',
favorites: 'https://leetcode.com/list/api/questions',
favorite_delete: 'https://leetcode.com/list/api/questions/$hash/$id',
plugin: 'https://raw.githubusercontent.com/leetcode-tools/leetcode-cli-plugins/master/plugins/$name.js'
plugin: 'https://raw.githubusercontent.com/leetcode-tools/leetcode-cli/master/lib/plugins/$name.js'
},
},

Expand Down
69 changes: 46 additions & 23 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,17 +540,10 @@ plugin.login = function(user, cb) {
};

function parseCookie(cookie, body, cb) {
const isCN = config.app === 'leetcode.cn';
const SessionPattern = /LEETCODE_SESSION=(.+?)(;|$)/;
let csrfPattern;
// leetcode-cn.com Cookie is not the same as leetcode.com in third parties
if (isCN) {
csrfPattern = /name="csrfmiddlewaretoken" value="(.*?)"/;
} else {
csrfPattern = /csrftoken=(.+?)(;|$)/;
}
const csrfPattern = /csrftoken=(.+?)(;|$)/;
const reSessionResult = SessionPattern.exec(cookie);
const reCsrfResult = csrfPattern.exec(isCN? body: cookie);
const reCsrfResult = csrfPattern.exec(cookie);
if (reSessionResult === null || reCsrfResult === null) {
return cb('invalid cookie?');
}
Expand Down Expand Up @@ -588,9 +581,18 @@ plugin.githubLogin = function(user, cb) {
const _request = request.defaults({jar: true});
_request(urls.github_login_request, function(e, resp, body) {
const authenticityToken = body.match(/name="authenticity_token" value="(.*?)"/);
if (authenticityToken === null) {
return cb('Get GitHub token failed');
let gaId = body.match(/name="ga_id" value="(.*?)"/);
if (!gaId) {
gaId = '';
}
let requiredField = body.match(/name="required_field_(.*?)"/);
const timestamp = body.match(/name="timestamp" value="(.*?)"/);
const timestampSecret = body.match(/name="timestamp_secret" value="(.*?)"/);

if (!(authenticityToken && timestamp && timestampSecret && requiredField)) {
return cb('Get GitHub payload failed');
}
requiredField = 'required_field_' + requiredField[1];
const options = {
url: urls.github_session_request,
method: 'POST',
Expand All @@ -599,11 +601,17 @@ plugin.githubLogin = function(user, cb) {
},
followAllRedirects: true,
form: {
'login': user.login,
'password': user.pass,
'authenticity_token': authenticityToken[1],
'utf8': encodeURIComponent('✓'),
'commit': encodeURIComponent('Sign in')
'login': user.login,
'password': user.pass,
'authenticity_token': authenticityToken[1],
'commit': encodeURIComponent('Sign in'),
'ga_id': gaId,
'webauthn-support': 'supported',
'webauthn-iuvpaa-support': 'unsupported',
'return_to': '',
'requiredField': '',
'timestamp': timestamp[1],
'timestamp_secret': timestampSecret[1],
},
};
_request(options, function(e, resp, body) {
Expand Down Expand Up @@ -664,9 +672,12 @@ plugin.linkedinLogin = function(user, cb) {
if ( resp.statusCode !== 200) {
return cb('Get LinkedIn session failed');
}
const authenticityToken = body.match(/input name="loginCsrfParam" value="(.*)" /);
if (authenticityToken === null) {
return cb('Get LinkedIn token failed');
const csrfToken = body.match(/input type="hidden" name="csrfToken" value="(.*?)"/);
const loginCsrfToken = body.match(/input type="hidden" name="loginCsrfParam" value="(.*?)"/);
const sIdString = body.match(/input type="hidden" name="sIdString" value="(.*?)"/);
const pageInstance = body.match(/input type="hidden" name="pageInstance" value="(.*?)"/);
if (!(csrfToken && loginCsrfToken && sIdString && pageInstance)) {
return cb('Get LinkedIn payload failed');
}
const options = {
url: urls.linkedin_session_request,
Expand All @@ -676,10 +687,22 @@ plugin.linkedinLogin = function(user, cb) {
},
followAllRedirects: true,
form: {
'session_key': user.login,
'session_password': user.pass,
'loginCsrfParam': authenticityToken[1],
'trk': 'guest_homepage-basic_sign-in-submit'
'csrfToken': csrfToken[1],
'session_key': user.login,
'ac': 2,
'sIdString': sIdString[1],
'parentPageKey': 'd_checkpoint_lg_consumerLogin',
'pageInstance': pageInstance[1],
'trk': 'public_profile_nav-header-signin',
'authUUID': '',
'session_redirect': 'https://www.linkedin.com/feed/',
'loginCsrfParam': loginCsrfToken[1],
'fp_data': 'default',
'_d': 'd',
'showGoogleOneTapLogin': true,
'controlId': 'd_checkpoint_lg_consumerLogin-login_submit_button',
'session_password': user.pass,
'loginFlow': 'REMEMBER_ME_OPTIN'
},
};
_request(options, function(e, resp, body) {
Expand Down