From c323de4807e93f673bad95af97721e98a059fad6 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 4 Nov 2018 15:02:27 -0800 Subject: [PATCH] Attempt to provision subdomain on team create --- app/components/Auth.js | 4 ++- server/auth/google.js | 12 +++++++-- server/auth/slack.js | 9 ++++++- server/models/Team.js | 2 +- {server => shared}/utils/domains.js | 31 ++++++++++++++++++++++++ {server => shared}/utils/domains.test.js | 0 6 files changed, 53 insertions(+), 5 deletions(-) rename {server => shared}/utils/domains.js (58%) rename {server => shared}/utils/domains.test.js (100%) diff --git a/app/components/Auth.js b/app/components/Auth.js index 7c8bc33f..1143ffe4 100644 --- a/app/components/Auth.js +++ b/app/components/Auth.js @@ -27,7 +27,9 @@ const Auth = observer(({ auth, children }: Props) => { // Check the current origin against the teams url, if they differ we need to // redirect to the canonical subdomain for this team if (window.location.origin !== team.url) { - window.location.href = `${team.url}${window.location.pathname}`; + const redirectTo = `${team.url}${window.location.pathname}`; + console.warn(`Redirecting to ${redirectTo}…`); + window.location.href = redirectTo; return ; } diff --git a/server/auth/google.js b/server/auth/google.js index 687cbd36..c4750d92 100644 --- a/server/auth/google.js +++ b/server/auth/google.js @@ -2,7 +2,7 @@ import crypto from 'crypto'; import Router from 'koa-router'; import addMonths from 'date-fns/add_months'; -import { stripSubdomain } from '../utils/domains'; +import { stripSubdomain } from '../../shared/utils/domains'; import { capitalize } from 'lodash'; import { OAuth2Client } from 'google-auth-library'; import { User, Team } from '../models'; @@ -53,7 +53,8 @@ router.get('google.callback', async ctx => { } const googleId = profile.data.hd; - const teamName = capitalize(profile.data.hd.split('.')[0]); + const hostname = profile.data.hd.split('.')[0]; + const teamName = capitalize(hostname); // attempt to get logo from Clearbit API. If one doesn't exist then // fall back to using tiley to generate a placeholder logo @@ -93,6 +94,13 @@ router.get('google.callback', async ctx => { if (isFirstUser) { await team.createFirstCollection(user.id); + + // attempt to give the new team a subdomain based on google hosted domain + try { + await team.update({ subdomain: hostname }); + } catch (err) { + // subdomain was invalid or already used + } } // not awaiting the promise here so that the request is not blocked diff --git a/server/auth/slack.js b/server/auth/slack.js index 93ebdcb8..5121e0de 100644 --- a/server/auth/slack.js +++ b/server/auth/slack.js @@ -4,8 +4,8 @@ import auth from '../middlewares/authentication'; import addHours from 'date-fns/add_hours'; import addMonths from 'date-fns/add_months'; import { slackAuth } from '../../shared/utils/routeHelpers'; +import { stripSubdomain } from '../../shared/utils/domains'; import { Authentication, Integration, User, Team } from '../models'; -import { stripSubdomain } from '../utils/domains'; import * as Slack from '../slack'; const router = new Router(); @@ -63,6 +63,13 @@ router.get('slack.callback', async ctx => { if (isFirstUser) { await team.createFirstCollection(user.id); + + // attempt to give the new team the same subdomain as they use on slack + try { + await team.update({ subdomain: data.team.domain }); + } catch (err) { + // subdomain was invalid or already used + } } // not awaiting the promise here so that the request is not blocked diff --git a/server/models/Team.js b/server/models/Team.js index bf8fe1fc..dc04fe78 100644 --- a/server/models/Team.js +++ b/server/models/Team.js @@ -3,7 +3,7 @@ import uuid from 'uuid'; import { URL } from 'url'; import { DataTypes, sequelize, Op } from '../sequelize'; import { publicS3Endpoint, uploadToS3FromUrl } from '../utils/s3'; -import { RESERVED_SUBDOMAINS } from '../utils/domains'; +import { RESERVED_SUBDOMAINS } from '../../shared/utils/domains'; import Collection from './Collection'; import User from './User'; diff --git a/server/utils/domains.js b/shared/utils/domains.js similarity index 58% rename from server/utils/domains.js rename to shared/utils/domains.js index 13133abd..c9c3629f 100644 --- a/server/utils/domains.js +++ b/shared/utils/domains.js @@ -10,26 +10,57 @@ export function stripSubdomain(hostname: string) { } export const RESERVED_SUBDOMAINS = [ + 'about', + 'account', 'admin', + 'advertising', 'api', + 'assets', + 'archive', 'beta', + 'billing', 'blog', + 'cache', 'cdn', + 'code', 'community', + 'dashboard', 'developer', + 'developers', 'forum', 'help', + 'home', + 'http', + 'https', 'imap', 'localhost', 'mail', + 'mobile', + 'news', + 'newsletter', 'ns1', 'ns2', 'ns3', 'ns4', + 'password', + 'profile', + 'sandbox', + 'script', + 'scripts', + 'setup', + 'signin', + 'signup', 'smtp', 'support', 'status', 'static', + 'stats', 'test', + 'update', + 'updates', 'www', + 'www1', + 'www2', + 'www3', + 'www4', ]; diff --git a/server/utils/domains.test.js b/shared/utils/domains.test.js similarity index 100% rename from server/utils/domains.test.js rename to shared/utils/domains.test.js