This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/server/auth/index.js
Tom Moor 1b6a986986 chore: Refactor authentication pass between subdomains (#1619)
* fix: Use get request instead of cookie to transfer token between domains

* Add domain to database
Add redirects to team domain when present

* 30s -> 1m

* fix: Avoid redirect loop if subdomain and domain set

* fix: Create a transfer specific token to prevent replay requests

* refactor: Move isCustomDomain out of shared as it won't work on the client
2020-11-04 19:54:04 -08:00

47 lines
1.2 KiB
JavaScript

// @flow
import addMonths from "date-fns/add_months";
import Koa from "koa";
import bodyParser from "koa-body";
import Router from "koa-router";
import { AuthenticationError } from "../errors";
import auth from "../middlewares/authentication";
import validation from "../middlewares/validation";
import { Team } from "../models";
import email from "./email";
import google from "./google";
import slack from "./slack";
const app = new Koa();
const router = new Router();
router.use("/", slack.routes());
router.use("/", google.routes());
router.use("/", email.routes());
router.get("/redirect", auth(), async (ctx) => {
const user = ctx.state.user;
const jwtToken = user.getJwtToken();
if (jwtToken === ctx.params.token) {
throw new AuthenticationError("Cannot extend token");
}
// ensure that the lastActiveAt on user is updated to prevent replay requests
await user.updateActiveAt(ctx.request.ip, true);
ctx.cookies.set("accessToken", jwtToken, {
httpOnly: false,
expires: addMonths(new Date(), 3),
});
const team = await Team.findByPk(user.teamId);
ctx.redirect(`${team.url}/home`);
});
app.use(bodyParser());
app.use(validation());
app.use(router.routes());
export default app;