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

add third party github login for future updates in vscode-leetcode #34

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
Dec 24, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add third party--Linkedin and fix typo by review.
  • Loading branch information
yihong0618 committed Dec 17, 2019
commit 51ca691fe80a88dc55df594cfdd0f9d08f636feb
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ Great thanks to leetcode.com, a really awesome website!

## Quick Start

Read help first $ leetcode help
Login with your leetcode account $ leetcode user -l
Login with third party account--github $ leetcode user -g
Cookie login with cookie $ leetcode user -c
Browse all questions $ leetcode list
Choose one question $ leetcode show 1 -g -l cpp
Read help first $ leetcode help
Login with your leetcode account $ leetcode user -l
Login with third party account--Github $ leetcode user -g
Login with third party account--Linkedin $ leetcode user -i
Cookie login with cookie $ leetcode user -c
Browse all questions $ leetcode list
Choose one question $ leetcode show 1 -g -l cpp
Coding it!
Run test(s) and pray... $ leetcode test ./two-sum.cpp -t '[3,2,4]\n7'
Submit final solution! $ leetcode submit ./two-sum.cpp
Run test(s) and pray... $ leetcode test ./two-sum.cpp -t '[3,2,4]\n7'
Submit final solution! $ leetcode submit ./two-sum.cpp
100 changes: 59 additions & 41 deletions lib/commands/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,42 @@ const cmd = {
desc: 'Manage account',
builder: function(yargs) {
return yargs
.option('l', {
alias: 'login',
type: 'boolean',
default: false,
describe: 'Login'
})
.option('c', {
alias: 'cookie',
type: 'boolean',
default: false,
describe: 'cookieLogin'
})
.option('g', {
alias: 'github',
type: 'boolean',
default: false,
describe: 'githubLogin'
})
.option('L', {
alias: 'logout',
type: 'boolean',
default: false,
describe: 'Logout'
})
.example(chalk.yellow('leetcode user'), 'Show current user')
.example(chalk.yellow('leetcode user -l'), 'User login')
.example(chalk.yellow('leetcode user -c'), 'User Cookie login')
.example(chalk.yellow('leetcode user -g'), 'User Github login')
.example(chalk.yellow('leetcode user -L'), 'User logout');
.option('l', {
alias: 'login',
type: 'boolean',
default: false,
describe: 'Login'
})
.option('c', {
alias: 'cookie',
type: 'boolean',
default: false,
describe: 'cookieLogin'
})
.option('g', {
alias: 'github',
type: 'boolean',
default: false,
describe: 'githubLogin'
})
.option('i', {
alias: 'linkedin',
type: 'boolean',
default: false,
describe: 'linkinedLogin'
})
.option('L', {
alias: 'logout',
type: 'boolean',
default: false,
describe: 'Logout'
})
.example(chalk.yellow('leetcode user'), 'Show current user')
.example(chalk.yellow('leetcode user -l'), 'User login')
.example(chalk.yellow('leetcode user -c'), 'User Cookie login')
.example(chalk.yellow('leetcode user -g'), 'User Github login')
.example(chalk.yellow('leetcode user -i'), 'User Linkedin login')
.example(chalk.yellow('leetcode user -L'), 'User logout');
}
};

Expand Down Expand Up @@ -73,19 +80,30 @@ cmd.handler = function(argv) {
log.info('Successfully logout as', chalk.yellow(user.name));
else
log.fail('You are not login yet?');
} else if (argv.github) {
// github
// third parties
} else if (argv.github || argv.linkedin) {
// add future third parties here
const functionMap = new Map(
[
['g', core.githubLogin],
['github', core.githubLogin],
['i', core.linkinedLogin],
['linkedin', core.linkedinLogin],
]
);
const keyword = Object.entries(argv).filter((i) => (i[1] === true))[0][0];
const coreFunction = functionMap.get(keyword);
prompt.colors = false;
prompt.message = '';
prompt.start();
prompt.get([
{name: 'login', required: true},
{name: 'pass', required: true, hidden: true}
], function(e, user) {
if (e) return log.fail(e)
core.githubLogin(user, function(e, user) {
if (e) return log.fail(e);
log.info('Successfully github login as', chalk.yellow(user.name));
coreFunction(user, function(e, user) {
if (e) return log.fail(e);
log.info('Successfully third party login as', chalk.yellow(user.name));
});
});
} else if (argv.cookie) {
Expand All @@ -97,22 +115,22 @@ cmd.handler = function(argv) {
{name: 'login', required: true},
{name: 'cookie', required: true}
], function(e, user) {
if (e) return log.fail(e)
core.cookieLogin(user, function(e, user) {
if (e) return log.fail(e);
log.info('Successfully cookie login as', chalk.yellow(user.name));
core.cookieLogin(user, function(e, user) {
if (e) return log.fail(e);
log.info('Successfully cookie login as', chalk.yellow(user.name));
});
});
} else {
} else {
// show current user
user = session.getUser();
if (user) {
log.info(chalk.gray(sprintf(' %-9s %-20s %s', 'Premium', 'User', 'Host')));
log.info(chalk.gray('-'.repeat(60)));
log.printf(' %s %-20s %s',
h.prettyText('', user.paid || false),
chalk.yellow(user.name),
config.sys.urls.base);
h.prettyText('', user.paid || false),
chalk.yellow(user.name),
config.sys.urls.base);
} else
return log.fail('You are not login yet?');
}
Expand Down
8 changes: 5 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const DEFAULT_CONFIG = {
login: 'https://leetcode.com/accounts/login/',
// third part login base urls. TODO facebook linkin google
github_login: 'https://leetcode.com/accounts/github/login/?next=%2F',
facebook_login: 'https://leetcode.com/accounts/facebook/login/?next=%2F',
linkined_login: 'https://leetcode.com/accounts/linkedin_oauth2/login/?next=%2F',
problems: 'https://leetcode.com/api/problems/$category/',
problem: 'https://leetcode.com/problems/$slug/description/',
test: 'https://leetcode.com/problems/$slug/interpret_solution/',
Expand Down Expand Up @@ -81,15 +83,15 @@ function Config() {}

Config.prototype.init = function() {
nconf.file('local', file.configFile())
.add('global', {type: 'literal', store: DEFAULT_CONFIG})
.defaults({});
.add('global', {type: 'literal', store: DEFAULT_CONFIG})
.defaults({});

const cfg = nconf.get();
nconf.remove('local');
nconf.remove('global');

// HACK: remove old style configs
for (let x in cfg) {
for (const x in cfg) {
if (x === x.toUpperCase()) delete cfg[x];
}
delete DEFAULT_CONFIG.type;
Expand Down
60 changes: 55 additions & 5 deletions lib/plugins/leetcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,13 @@ function parseCookie(cookie, cb) {
};
}

function saveAndGetUser(user, cb, cookieData) {
user.sessionId = cookieData.sessionId;
user.sessionCSRF = cookieData.sessionCSRF;
session.saveUser(user);
plugin.getUser(user, cb);
}

plugin.cookieLogin = function(user, cb) {
const cookieData = parseCookie(user.cookie, cb);
user.sessionId = cookieData.sessionId;
Expand Down Expand Up @@ -590,13 +597,56 @@ plugin.githubLogin = function(user, cb) {
_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');
return cb('Github login failed or Github did not link to leetcode');
}
const cookieData = parseCookie(resp.request.headers.cookie, cb);
saveAndGetUser(user, cb, cookieData);
});
});
});
};

plugin.linkinedLogin = function(user, cb) {
const leetcodeUrl = config.sys.urls.linkined_login;
const _request = request.defaults({
jar: true,
headers: {
'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'
}
});
_request('https://www.linkedin.com', function(e, resp, body) {
if ( resp.statusCode !== 200) {
return cb('Get linkedin session failed');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linkedin -> LinkedIn

}
const authenticityToken = body.match(/input name="loginCsrfParam" value="(.*)" /);
if (authenticityToken === null) {
return cb('Get Linkined token failed');
}
const options = {
url: 'https://www.linkedin.com/uas/login-submit',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
followAllRedirects: true,
form: {
'session_key': user.login,
'session_password': user.pass,
'loginCsrfParam': authenticityToken[1],
'trk': 'guest_homepage-basic_sign-in-submit'
},
};
_request(options, function(e, resp, body) {
if (resp.statusCode !== 200) {
return cb('Facebook 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');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LinkedIn

LeetCode

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for that, I will correct them~
Thank you very much for your help ~

}
const cookieData = parseCookie(resp.request.headers.cookie, cb);
user.sessionId = cookieData.sessionId;
user.sessionCSRF = cookieData.sessionCSRF;
session.saveUser(user);
plugin.getUser(user, cb);
saveAndGetUser(user, cb, cookieData);
});
});
});
Expand Down