2018-05-28 18:36:37 +00:00
|
|
|
// @flow
|
2018-06-02 22:43:44 +00:00
|
|
|
import crypto from 'crypto';
|
2018-05-28 18:36:37 +00:00
|
|
|
import Router from 'koa-router';
|
|
|
|
import addMonths from 'date-fns/add_months';
|
2018-11-04 03:27:57 +00:00
|
|
|
import { stripSubdomain } from '../utils/domains';
|
2018-05-29 03:31:53 +00:00
|
|
|
import { capitalize } from 'lodash';
|
2018-05-28 18:36:37 +00:00
|
|
|
import { OAuth2Client } from 'google-auth-library';
|
|
|
|
import { User, Team } from '../models';
|
|
|
|
|
|
|
|
const router = new Router();
|
|
|
|
const client = new OAuth2Client(
|
|
|
|
process.env.GOOGLE_CLIENT_ID,
|
|
|
|
process.env.GOOGLE_CLIENT_SECRET,
|
|
|
|
`${process.env.URL}/auth/google.callback`
|
|
|
|
);
|
2018-06-16 19:36:02 +00:00
|
|
|
const allowedDomainsEnv = process.env.GOOGLE_ALLOWED_DOMAINS;
|
2018-05-28 18:36:37 +00:00
|
|
|
|
|
|
|
// start the oauth process and redirect user to Google
|
|
|
|
router.get('google', async ctx => {
|
|
|
|
// Generate the url that will be used for the consent dialog.
|
|
|
|
const authorizeUrl = client.generateAuthUrl({
|
|
|
|
access_type: 'offline',
|
|
|
|
scope: [
|
|
|
|
'https://www.googleapis.com/auth/userinfo.profile',
|
|
|
|
'https://www.googleapis.com/auth/userinfo.email',
|
|
|
|
],
|
|
|
|
prompt: 'consent',
|
|
|
|
});
|
|
|
|
ctx.redirect(authorizeUrl);
|
|
|
|
});
|
|
|
|
|
|
|
|
// signin callback from Google
|
|
|
|
router.get('google.callback', async ctx => {
|
|
|
|
const { code } = ctx.request.query;
|
|
|
|
ctx.assertPresent(code, 'code is required');
|
|
|
|
const response = await client.getToken(code);
|
|
|
|
client.setCredentials(response.tokens);
|
|
|
|
|
|
|
|
const profile = await client.request({
|
|
|
|
url: 'https://www.googleapis.com/oauth2/v1/userinfo',
|
|
|
|
});
|
|
|
|
|
2018-05-29 05:32:36 +00:00
|
|
|
if (!profile.data.hd) {
|
|
|
|
ctx.redirect('/?notice=google-hd');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-16 19:36:02 +00:00
|
|
|
// allow all domains by default if the env is not set
|
|
|
|
const allowedDomains = allowedDomainsEnv && allowedDomainsEnv.split(',');
|
|
|
|
if (allowedDomains && !allowedDomains.includes(profile.data.hd)) {
|
|
|
|
ctx.redirect('/?notice=hd-not-allowed');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-02 22:43:44 +00:00
|
|
|
const googleId = profile.data.hd;
|
2018-05-29 03:31:53 +00:00
|
|
|
const teamName = capitalize(profile.data.hd.split('.')[0]);
|
2018-06-02 22:43:44 +00:00
|
|
|
|
|
|
|
// attempt to get logo from Clearbit API. If one doesn't exist then
|
|
|
|
// fall back to using tiley to generate a placeholder logo
|
|
|
|
const hash = crypto.createHash('sha256');
|
|
|
|
hash.update(googleId);
|
|
|
|
const hashedGoogleId = hash.digest('hex');
|
|
|
|
const cbUrl = `https://logo.clearbit.com/${profile.data.hd}`;
|
|
|
|
const tileyUrl = `https://tiley.herokuapp.com/avatar/${hashedGoogleId}/${
|
|
|
|
teamName[0]
|
|
|
|
}.png`;
|
|
|
|
const cbResponse = await fetch(cbUrl);
|
|
|
|
const avatarUrl = cbResponse.status === 200 ? cbUrl : tileyUrl;
|
|
|
|
|
2018-05-28 18:36:37 +00:00
|
|
|
const [team, isFirstUser] = await Team.findOrCreate({
|
|
|
|
where: {
|
2018-06-02 22:43:44 +00:00
|
|
|
googleId,
|
2018-05-28 18:36:37 +00:00
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
name: teamName,
|
2018-06-02 22:43:44 +00:00
|
|
|
avatarUrl,
|
2018-05-28 18:36:37 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-05-29 03:31:53 +00:00
|
|
|
const [user] = await User.findOrCreate({
|
2018-05-28 18:36:37 +00:00
|
|
|
where: {
|
2018-05-29 03:31:53 +00:00
|
|
|
service: 'google',
|
|
|
|
serviceId: profile.data.id,
|
2018-05-28 18:36:37 +00:00
|
|
|
teamId: team.id,
|
|
|
|
},
|
|
|
|
defaults: {
|
|
|
|
name: profile.data.name,
|
|
|
|
email: profile.data.email,
|
|
|
|
isAdmin: isFirstUser,
|
|
|
|
avatarUrl: profile.data.picture,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (isFirstUser) {
|
|
|
|
await team.createFirstCollection(user.id);
|
|
|
|
}
|
|
|
|
|
2018-06-05 02:07:56 +00:00
|
|
|
// not awaiting the promise here so that the request is not blocked
|
|
|
|
user.updateSignedIn(ctx.request.ip);
|
|
|
|
|
2018-05-29 04:14:43 +00:00
|
|
|
ctx.cookies.set('lastSignedIn', 'google', {
|
2018-05-28 18:36:37 +00:00
|
|
|
httpOnly: false,
|
|
|
|
expires: new Date('2100'),
|
2018-11-04 03:27:57 +00:00
|
|
|
domain: stripSubdomain(ctx.request.hostname),
|
2018-05-28 18:36:37 +00:00
|
|
|
});
|
|
|
|
ctx.cookies.set('accessToken', user.getJwtToken(), {
|
|
|
|
httpOnly: false,
|
2018-05-29 03:31:53 +00:00
|
|
|
expires: addMonths(new Date(), 1),
|
2018-11-04 03:27:57 +00:00
|
|
|
domain: stripSubdomain(ctx.request.hostname),
|
2018-05-28 18:36:37 +00:00
|
|
|
});
|
|
|
|
|
2018-11-04 03:27:57 +00:00
|
|
|
ctx.redirect(team.url);
|
2018-05-28 18:36:37 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|