diff --git a/server/api/hooks.js b/server/api/hooks.js index d3b088c46..716b0f5f3 100644 --- a/server/api/hooks.js +++ b/server/api/hooks.js @@ -5,13 +5,13 @@ import { AuthenticationError, InvalidRequestError } from "../errors"; import { UserAuthentication, AuthenticationProvider, - Authentication, Document, User, Team, Collection, SearchQuery, Integration, + IntegrationAuthentication, } from "../models"; import { presentSlackAttachment } from "../presenters"; import * as Slack from "../slack"; @@ -38,7 +38,7 @@ router.post("hooks.unfurl", async (ctx) => { }); if (!user) return; - const auth = await Authentication.findOne({ + const auth = await IntegrationAuthentication.findOne({ where: { service: "slack", teamId: user.teamId }, }); if (!auth) return; diff --git a/server/api/hooks.test.js b/server/api/hooks.test.js index bf284397a..196cb2397 100644 --- a/server/api/hooks.test.js +++ b/server/api/hooks.test.js @@ -1,7 +1,7 @@ /* eslint-disable flowtype/require-valid-file-annotation */ import TestServer from "fetch-test-server"; import app from "../app"; -import { Authentication, SearchQuery } from "../models"; +import { IntegrationAuthentication, SearchQuery } from "../models"; import * as Slack from "../slack"; import { buildDocument, buildIntegration } from "../test/factories"; import { flushdb, seed } from "../test/support"; @@ -18,7 +18,7 @@ jest.mock("../slack", () => ({ describe("#hooks.unfurl", () => { it("should return documents", async () => { const { user, document } = await seed(); - await Authentication.create({ + await IntegrationAuthentication.create({ service: "slack", userId: user.id, teamId: user.teamId, diff --git a/server/auth/providers/slack.js b/server/auth/providers/slack.js index bedec9bb4..104f9202b 100644 --- a/server/auth/providers/slack.js +++ b/server/auth/providers/slack.js @@ -6,7 +6,12 @@ import accountProvisioner from "../../commands/accountProvisioner"; import env from "../../env"; import auth from "../../middlewares/authentication"; import passportMiddleware from "../../middlewares/passport"; -import { Authentication, Collection, Integration, Team } from "../../models"; +import { + IntegrationAuthentication, + Collection, + Integration, + Team, +} from "../../models"; import * as Slack from "../../slack"; import { StateStore } from "../../utils/passport"; @@ -113,7 +118,7 @@ if (SLACK_CLIENT_ID) { const endpoint = `${process.env.URL || ""}/auth/slack.commands`; const data = await Slack.oauthAccess(code, endpoint); - const authentication = await Authentication.create({ + const authentication = await IntegrationAuthentication.create({ service: "slack", userId: user.id, teamId: user.teamId, @@ -168,7 +173,7 @@ if (SLACK_CLIENT_ID) { const endpoint = `${process.env.URL || ""}/auth/slack.post`; const data = await Slack.oauthAccess(code, endpoint); - const authentication = await Authentication.create({ + const authentication = await IntegrationAuthentication.create({ service: "slack", userId: user.id, teamId: user.teamId, diff --git a/server/models/Integration.js b/server/models/Integration.js index a21365265..bbd9da17c 100644 --- a/server/models/Integration.js +++ b/server/models/Integration.js @@ -26,7 +26,7 @@ Integration.associate = (models) => { as: "collection", foreignKey: "collectionId", }); - Integration.belongsTo(models.Authentication, { + Integration.belongsTo(models.IntegrationAuthentication, { as: "authentication", foreignKey: "authenticationId", }); diff --git a/server/models/Authentication.js b/server/models/IntegrationAuthentication.js similarity index 59% rename from server/models/Authentication.js rename to server/models/IntegrationAuthentication.js index 0b32d9767..4b8886411 100644 --- a/server/models/Authentication.js +++ b/server/models/IntegrationAuthentication.js @@ -1,7 +1,7 @@ // @flow import { DataTypes, sequelize, encryptedFields } from "../sequelize"; -const Authentication = sequelize.define("authentication", { +const IntegrationAuthentication = sequelize.define("authentication", { id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, @@ -12,15 +12,15 @@ const Authentication = sequelize.define("authentication", { token: encryptedFields().vault("token"), }); -Authentication.associate = (models) => { - Authentication.belongsTo(models.User, { +IntegrationAuthentication.associate = (models) => { + IntegrationAuthentication.belongsTo(models.User, { as: "user", foreignKey: "userId", }); - Authentication.belongsTo(models.Team, { + IntegrationAuthentication.belongsTo(models.Team, { as: "team", foreignKey: "teamId", }); }; -export default Authentication; +export default IntegrationAuthentication; diff --git a/server/models/index.js b/server/models/index.js index 6d7ff844c..3bc02ba12 100644 --- a/server/models/index.js +++ b/server/models/index.js @@ -1,7 +1,6 @@ // @flow import ApiKey from "./ApiKey"; import Attachment from "./Attachment"; -import Authentication from "./Authentication"; import AuthenticationProvider from "./AuthenticationProvider"; import Backlink from "./Backlink"; import Collection from "./Collection"; @@ -12,6 +11,7 @@ import Event from "./Event"; import Group from "./Group"; import GroupUser from "./GroupUser"; import Integration from "./Integration"; +import IntegrationAuthentication from "./IntegrationAuthentication"; import Notification from "./Notification"; import NotificationSetting from "./NotificationSetting"; import Revision from "./Revision"; @@ -26,7 +26,6 @@ import View from "./View"; const models = { ApiKey, Attachment, - Authentication, AuthenticationProvider, Backlink, Collection, @@ -37,6 +36,7 @@ const models = { Group, GroupUser, Integration, + IntegrationAuthentication, Notification, NotificationSetting, Revision, @@ -59,7 +59,6 @@ Object.keys(models).forEach((modelName) => { export { ApiKey, Attachment, - Authentication, AuthenticationProvider, Backlink, Collection, @@ -70,6 +69,7 @@ export { Group, GroupUser, Integration, + IntegrationAuthentication, Notification, NotificationSetting, Revision, diff --git a/server/test/factories.js b/server/test/factories.js index 6d00be315..3a0ac0314 100644 --- a/server/test/factories.js +++ b/server/test/factories.js @@ -10,7 +10,7 @@ import { Group, GroupUser, Attachment, - Authentication, + IntegrationAuthentication, Integration, AuthenticationProvider, } from "../models"; @@ -123,7 +123,7 @@ export async function buildIntegration(overrides: Object = {}) { const user = await buildUser({ teamId: overrides.teamId }); - const authentication = await Authentication.create({ + const authentication = await IntegrationAuthentication.create({ service: "slack", userId: user.id, teamId: user.teamId,