chore: Move to prettier standard double quotes (#1309)
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
// @flow
|
||||
import type { Context } from 'koa';
|
||||
import type { Context } from "koa";
|
||||
|
||||
export default function apexRedirect() {
|
||||
return async function apexRedirectMiddleware(
|
||||
ctx: Context,
|
||||
next: () => Promise<*>
|
||||
) {
|
||||
if (ctx.headers.host === 'getoutline.com') {
|
||||
if (ctx.headers.host === "getoutline.com") {
|
||||
ctx.redirect(`https://www.${ctx.headers.host}${ctx.path}`);
|
||||
} else {
|
||||
return next();
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
// @flow
|
||||
import JWT from 'jsonwebtoken';
|
||||
import { type Context } from 'koa';
|
||||
import { User, ApiKey } from '../models';
|
||||
import { getUserForJWT } from '../utils/jwt';
|
||||
import { AuthenticationError, UserSuspendedError } from '../errors';
|
||||
import addMonths from 'date-fns/add_months';
|
||||
import addMinutes from 'date-fns/add_minutes';
|
||||
import { getCookieDomain } from '../../shared/utils/domains';
|
||||
import JWT from "jsonwebtoken";
|
||||
import { type Context } from "koa";
|
||||
import { User, ApiKey } from "../models";
|
||||
import { getUserForJWT } from "../utils/jwt";
|
||||
import { AuthenticationError, UserSuspendedError } from "../errors";
|
||||
import addMonths from "date-fns/add_months";
|
||||
import addMinutes from "date-fns/add_minutes";
|
||||
import { getCookieDomain } from "../../shared/utils/domains";
|
||||
|
||||
export default function auth(options?: { required?: boolean } = {}) {
|
||||
return async function authMiddleware(ctx: Context, next: () => Promise<*>) {
|
||||
let token;
|
||||
|
||||
const authorizationHeader = ctx.request.get('authorization');
|
||||
const authorizationHeader = ctx.request.get("authorization");
|
||||
if (authorizationHeader) {
|
||||
const parts = authorizationHeader.split(' ');
|
||||
const parts = authorizationHeader.split(" ");
|
||||
if (parts.length === 2) {
|
||||
const scheme = parts[0];
|
||||
const credentials = parts[1];
|
||||
@@ -33,11 +33,11 @@ export default function auth(options?: { required?: boolean } = {}) {
|
||||
} else if (ctx.request.query.token) {
|
||||
token = ctx.request.query.token;
|
||||
} else {
|
||||
token = ctx.cookies.get('accessToken');
|
||||
token = ctx.cookies.get("accessToken");
|
||||
}
|
||||
|
||||
if (!token && options.required !== false) {
|
||||
throw new AuthenticationError('Authentication required');
|
||||
throw new AuthenticationError("Authentication required");
|
||||
}
|
||||
|
||||
let user;
|
||||
@@ -52,13 +52,13 @@ export default function auth(options?: { required?: boolean } = {}) {
|
||||
},
|
||||
});
|
||||
} catch (e) {
|
||||
throw new AuthenticationError('Invalid API key');
|
||||
throw new AuthenticationError("Invalid API key");
|
||||
}
|
||||
|
||||
if (!apiKey) throw new AuthenticationError('Invalid API key');
|
||||
if (!apiKey) throw new AuthenticationError("Invalid API key");
|
||||
|
||||
user = await User.findByPk(apiKey.userId);
|
||||
if (!user) throw new AuthenticationError('Invalid API key');
|
||||
if (!user) throw new AuthenticationError("Invalid API key");
|
||||
} else {
|
||||
// JWT
|
||||
user = await getUserForJWT(token);
|
||||
@@ -83,7 +83,7 @@ export default function auth(options?: { required?: boolean } = {}) {
|
||||
|
||||
ctx.signIn = async (user, team, service, isFirstSignin = false) => {
|
||||
if (user.isSuspended) {
|
||||
return ctx.redirect('/?notice=suspended');
|
||||
return ctx.redirect("/?notice=suspended");
|
||||
}
|
||||
|
||||
// update the database when the user last signed in
|
||||
@@ -94,18 +94,18 @@ export default function auth(options?: { required?: boolean } = {}) {
|
||||
|
||||
// set a cookie for which service we last signed in with. This is
|
||||
// only used to display a UI hint for the user for next time
|
||||
ctx.cookies.set('lastSignedIn', service, {
|
||||
ctx.cookies.set("lastSignedIn", service, {
|
||||
httpOnly: false,
|
||||
expires: new Date('2100'),
|
||||
expires: new Date("2100"),
|
||||
domain,
|
||||
});
|
||||
|
||||
// set a transfer cookie for the access token itself and redirect
|
||||
// to the teams subdomain if subdomains are enabled
|
||||
if (process.env.SUBDOMAINS_ENABLED === 'true' && team.subdomain) {
|
||||
if (process.env.SUBDOMAINS_ENABLED === "true" && team.subdomain) {
|
||||
// get any existing sessions (teams signed in) and add this team
|
||||
const existing = JSON.parse(
|
||||
decodeURIComponent(ctx.cookies.get('sessions') || '') || '{}'
|
||||
decodeURIComponent(ctx.cookies.get("sessions") || "") || "{}"
|
||||
);
|
||||
const sessions = encodeURIComponent(
|
||||
JSON.stringify({
|
||||
@@ -117,24 +117,24 @@ export default function auth(options?: { required?: boolean } = {}) {
|
||||
},
|
||||
})
|
||||
);
|
||||
ctx.cookies.set('sessions', sessions, {
|
||||
ctx.cookies.set("sessions", sessions, {
|
||||
httpOnly: false,
|
||||
expires,
|
||||
domain,
|
||||
});
|
||||
|
||||
ctx.cookies.set('accessToken', user.getJwtToken(), {
|
||||
ctx.cookies.set("accessToken", user.getJwtToken(), {
|
||||
httpOnly: true,
|
||||
expires: addMinutes(new Date(), 1),
|
||||
domain,
|
||||
});
|
||||
ctx.redirect(`${team.url}/auth/redirect`);
|
||||
} else {
|
||||
ctx.cookies.set('accessToken', user.getJwtToken(), {
|
||||
ctx.cookies.set("accessToken", user.getJwtToken(), {
|
||||
httpOnly: false,
|
||||
expires,
|
||||
});
|
||||
ctx.redirect(`${team.url}/home${isFirstSignin ? '?welcome' : ''}`);
|
||||
ctx.redirect(`${team.url}/home${isFirstSignin ? "?welcome" : ""}`);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
/* eslint-disable flowtype/require-valid-file-annotation */
|
||||
import { flushdb, seed } from '../test/support';
|
||||
import { buildUser } from '../test/factories';
|
||||
import { ApiKey } from '../models';
|
||||
import randomstring from 'randomstring';
|
||||
import auth from './authentication';
|
||||
import { flushdb, seed } from "../test/support";
|
||||
import { buildUser } from "../test/factories";
|
||||
import { ApiKey } from "../models";
|
||||
import randomstring from "randomstring";
|
||||
import auth from "./authentication";
|
||||
|
||||
beforeEach(flushdb);
|
||||
|
||||
describe('Authentication middleware', async () => {
|
||||
describe('with JWT', () => {
|
||||
it('should authenticate with correct token', async () => {
|
||||
describe("Authentication middleware", async () => {
|
||||
describe("with JWT", () => {
|
||||
it("should authenticate with correct token", async () => {
|
||||
const state = {};
|
||||
const { user } = await seed();
|
||||
const authMiddleware = auth();
|
||||
@@ -27,7 +27,7 @@ describe('Authentication middleware', async () => {
|
||||
expect(state.user.id).toEqual(user.id);
|
||||
});
|
||||
|
||||
it('should return error with invalid token', async () => {
|
||||
it("should return error with invalid token", async () => {
|
||||
const state = {};
|
||||
const { user } = await seed();
|
||||
const authMiddleware = auth();
|
||||
@@ -44,13 +44,13 @@ describe('Authentication middleware', async () => {
|
||||
jest.fn()
|
||||
);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe('Invalid token');
|
||||
expect(e.message).toBe("Invalid token");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('with API key', () => {
|
||||
it('should authenticate user with valid API key', async () => {
|
||||
describe("with API key", () => {
|
||||
it("should authenticate user with valid API key", async () => {
|
||||
const state = {};
|
||||
const { user } = await seed();
|
||||
const authMiddleware = auth();
|
||||
@@ -71,7 +71,7 @@ describe('Authentication middleware', async () => {
|
||||
expect(state.user.id).toEqual(user.id);
|
||||
});
|
||||
|
||||
it('should return error with invalid API key', async () => {
|
||||
it("should return error with invalid API key", async () => {
|
||||
const state = {};
|
||||
const authMiddleware = auth();
|
||||
|
||||
@@ -87,12 +87,12 @@ describe('Authentication middleware', async () => {
|
||||
jest.fn()
|
||||
);
|
||||
} catch (e) {
|
||||
expect(e.message).toBe('Invalid API key');
|
||||
expect(e.message).toBe("Invalid API key");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should return error message if no auth token is available', async () => {
|
||||
it("should return error message if no auth token is available", async () => {
|
||||
const state = {};
|
||||
const authMiddleware = auth();
|
||||
|
||||
@@ -100,7 +100,7 @@ describe('Authentication middleware', async () => {
|
||||
await authMiddleware(
|
||||
{
|
||||
request: {
|
||||
get: jest.fn(() => 'error'),
|
||||
get: jest.fn(() => "error"),
|
||||
},
|
||||
state,
|
||||
cache: {},
|
||||
@@ -114,7 +114,7 @@ describe('Authentication middleware', async () => {
|
||||
}
|
||||
});
|
||||
|
||||
it('should allow passing auth token as a GET param', async () => {
|
||||
it("should allow passing auth token as a GET param", async () => {
|
||||
const state = {};
|
||||
const { user } = await seed();
|
||||
const authMiddleware = auth();
|
||||
@@ -136,7 +136,7 @@ describe('Authentication middleware', async () => {
|
||||
expect(state.user.id).toEqual(user.id);
|
||||
});
|
||||
|
||||
it('should allow passing auth token in body params', async () => {
|
||||
it("should allow passing auth token in body params", async () => {
|
||||
const state = {};
|
||||
const { user } = await seed();
|
||||
const authMiddleware = auth();
|
||||
@@ -157,7 +157,7 @@ describe('Authentication middleware', async () => {
|
||||
expect(state.user.id).toEqual(user.id);
|
||||
});
|
||||
|
||||
it('should return an error for suspended users', async () => {
|
||||
it("should return an error for suspended users", async () => {
|
||||
const state = {};
|
||||
const admin = await buildUser({});
|
||||
const user = await buildUser({
|
||||
@@ -179,7 +179,7 @@ describe('Authentication middleware', async () => {
|
||||
);
|
||||
} catch (e) {
|
||||
expect(e.message).toEqual(
|
||||
'Your access has been suspended by the team admin'
|
||||
"Your access has been suspended by the team admin"
|
||||
);
|
||||
expect(e.errorData.adminEmail).toEqual(admin.email);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
import Sequelize from 'sequelize';
|
||||
import { snakeCase } from 'lodash';
|
||||
import { type Context } from 'koa';
|
||||
import Sequelize from "sequelize";
|
||||
import { snakeCase } from "lodash";
|
||||
import { type Context } from "koa";
|
||||
|
||||
export default function errorHandling() {
|
||||
return async function errorHandlingMiddleware(
|
||||
@@ -25,18 +25,18 @@ export default function errorHandling() {
|
||||
|
||||
if (message.match(/Not found/i)) {
|
||||
ctx.status = 404;
|
||||
error = 'not_found';
|
||||
error = "not_found";
|
||||
}
|
||||
|
||||
if (message.match(/Authorization error/i)) {
|
||||
ctx.status = 403;
|
||||
error = 'authorization_error';
|
||||
error = "authorization_error";
|
||||
}
|
||||
|
||||
if (ctx.status === 500) {
|
||||
message = 'Internal Server Error';
|
||||
error = 'internal_server_error';
|
||||
ctx.app.emit('error', err, ctx);
|
||||
message = "Internal Server Error";
|
||||
error = "internal_server_error";
|
||||
ctx.app.emit("error", err, ctx);
|
||||
}
|
||||
|
||||
ctx.body = {
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
// @flow
|
||||
import queryString from 'query-string';
|
||||
import { type Context } from 'koa';
|
||||
import queryString from "query-string";
|
||||
import { type Context } from "koa";
|
||||
|
||||
export default function methodOverride() {
|
||||
return async function methodOverrideMiddleware(
|
||||
ctx: Context,
|
||||
next: () => Promise<*>
|
||||
) {
|
||||
if (ctx.method === 'POST') {
|
||||
if (ctx.method === "POST") {
|
||||
// $FlowFixMe
|
||||
ctx.body = ctx.request.body;
|
||||
} else if (ctx.method === 'GET') {
|
||||
} else if (ctx.method === "GET") {
|
||||
ctx.method = 'POST'; // eslint-disable-line
|
||||
ctx.body = queryString.parse(ctx.querystring);
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
// @flow
|
||||
import validator from 'validator';
|
||||
import { type Context } from 'koa';
|
||||
import { ParamRequiredError, ValidationError } from '../errors';
|
||||
import { validateColorHex } from '../../shared/utils/color';
|
||||
import validator from "validator";
|
||||
import { type Context } from "koa";
|
||||
import { ParamRequiredError, ValidationError } from "../errors";
|
||||
import { validateColorHex } from "../../shared/utils/color";
|
||||
|
||||
export default function validation() {
|
||||
return function validationMiddleware(ctx: Context, next: () => Promise<*>) {
|
||||
ctx.assertPresent = (value, message) => {
|
||||
if (value === undefined || value === null || value === '') {
|
||||
if (value === undefined || value === null || value === "") {
|
||||
throw new ParamRequiredError(message);
|
||||
}
|
||||
};
|
||||
@@ -19,18 +19,18 @@ export default function validation() {
|
||||
};
|
||||
|
||||
ctx.assertNotEmpty = (value, message) => {
|
||||
if (value === '') {
|
||||
if (value === "") {
|
||||
throw new ValidationError(message);
|
||||
}
|
||||
};
|
||||
|
||||
ctx.assertEmail = (value = '', message) => {
|
||||
ctx.assertEmail = (value = "", message) => {
|
||||
if (!validator.isEmail(value)) {
|
||||
throw new ValidationError(message);
|
||||
}
|
||||
};
|
||||
|
||||
ctx.assertUuid = (value = '', message) => {
|
||||
ctx.assertUuid = (value = "", message) => {
|
||||
if (!validator.isUUID(value)) {
|
||||
throw new ValidationError(message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user