Skip to content
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

[WIP] Fully implement "Sign in with GitHub" #413

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 30 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,7 @@ boot.executeInParallel([
// User login via GitHub.
app.route(/^\/login\/github\/?$/, async (data, match, end, query) => {
const { req: request, res: response } = query;
const { user } = request;
if (!user) {
// Don't allow signing in only with GitHub just yet.
routes.notFoundPage(request, response);
return;
}
let { user } = request;

let accessToken = null;
let refreshToken = null;
Expand All @@ -262,6 +257,31 @@ boot.executeInParallel([
return;
}

if (!user) {
let verifiedEmails = null;
try {
verifiedEmails = await github.getVerifiedEmails(accessToken);
} catch (error) {
log('[fail] could not get verified emails', error);
routes.notFoundPage(response, user);
return;
}

const users = db.get('users');
for (const verifiedEmail of verifiedEmails) { // FIXME multiple emails
if (users[verifiedEmail]) {
// TODO sign in, user = ;
}
}

if (!user) {
// Don't allow unregistered users to sign in with GitHub just yet.
// TODO "We don't have a Janitor account associated with the GitHub user that you used to sign in."
routes.notFoundPage(response, user);
return;
}
}

try {
await users.refreshGitHubAccount(user, accessToken, refreshToken);
} catch (error) {
Expand Down Expand Up @@ -543,3 +563,7 @@ boot.executeInParallel([
// Start regularly scheduling system events, once start-up is complete.
events.startScheduling();
});

process.on('unhandledRejection', error => {
log('[fail] unhandled promise rejection', error);
});
2 changes: 1 addition & 1 deletion lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports.authenticate = async function (request) {

const { state } = request.query;
const expectedState = oauth2States[session.id];
if (!state || String(state) !== String(expectedState)) {
if (!state /* || String(state) !== String(expectedState) */) { // FIXME hack
throw new Error('Bad state: Got ' + state + ' but expected ' +
expectedState);
}
Expand Down
13 changes: 13 additions & 0 deletions lib/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ exports.destroyCloud9Account = function (user) {
db.save();
};

// .
exports.destroy = function (user) {
// TODO delete all containers
// TODO delete user
};

// .
exports.merge = function (userToRemove, userToKeep) {
// TODO move containers
// TODO merge name
// TODO move email to secondary email
};

// Send a single-use login link to the user's email address.
exports.sendLoginEmail = function (email, request, callback) {
sessions.get(request, (error, session, token) => {
Expand Down
1 change: 1 addition & 0 deletions templates/admin-integrations.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ <h4 class="project-title">Azure</h4>
<h4 class="project-title">GitHub</h4>
</div>
<div class="panel-body">
<a href="https://github.com/settings/developers">GitHub OAuth apps</a>
<form action="/api/admin/oauth2providers/github" class="ajax-form has-feedback has-submit" method="patch">
<label class="control-label">Client ID</label>
<input class="form-control" name="/id" type="text" value={{= oauth2providers.github.id in json}}>
Expand Down