From f90309e7816a8e27fe8aa89718f74cc5c396c750 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 7 Jul 2022 12:16:54 +0200 Subject: [PATCH 1/4] fix: Unneccessary restrictive avatarUrl length --- server/models/User.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/server/models/User.ts b/server/models/User.ts index 87e446083c..cd89693c8f 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -84,17 +84,21 @@ export enum UserFlag { @Fix class User extends ParanoidModel { @IsEmail - @Length({ min: 0, max: 255, msg: "Must be less than 255 characters" }) + @Length({ min: 0, max: 255, msg: "email must be less than 255 characters" }) @Column email: string | null; @NotContainsUrl - @Length({ min: 0, max: 255, msg: "Must be less than 255 characters" }) + @Length({ + min: 0, + max: 255, + msg: "username must be less than 255 characters", + }) @Column username: string | null; @NotContainsUrl - @Length({ min: 0, max: 255, msg: "Must be less than 255 characters" }) + @Length({ min: 0, max: 255, msg: "name must be less than 255 characters" }) @Column name: string; @@ -144,8 +148,12 @@ class User extends ParanoidModel { @Column language: string; - @Length({ min: 0, max: 255, msg: "Must be less than 255 characters" }) @Column(DataType.STRING) + @Length({ + min: 0, + max: 1000, + msg: "avatarUrl must be less than 1000 characters", + }) get avatarUrl() { const original = this.getDataValue("avatarUrl"); From ec8c0645ba58276f84ceddd35f039676814814fa Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Thu, 7 Jul 2022 12:23:27 +0200 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20Correct=20annotation=20order=20?= =?UTF-8?q?=F0=9F=99=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/models/User.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/models/User.ts b/server/models/User.ts index cd89693c8f..727483200f 100644 --- a/server/models/User.ts +++ b/server/models/User.ts @@ -148,12 +148,12 @@ class User extends ParanoidModel { @Column language: string; - @Column(DataType.STRING) @Length({ min: 0, max: 1000, msg: "avatarUrl must be less than 1000 characters", }) + @Column(DataType.STRING) get avatarUrl() { const original = this.getDataValue("avatarUrl"); From 1e808fc52c84d308622b555808adc44e0d1fbf6b Mon Sep 17 00:00:00 2001 From: Nan Yu Date: Fri, 8 Jul 2022 00:24:46 -0700 Subject: [PATCH 3/4] Feat: add auth provider to users on sign in (#3739) * feat: merge a new authentication method onto existing user records when emails match * adds test for invite acceptance and auth provider creation * addresses comments - test existing user and invites in different test cases - update lastActiveAt syncronously when an invite is accepted * sort arrays in test to prevent nondeterministic test behaivior when doing array compare --- server/commands/userCreator.test.ts | 72 ++++++++++++++++++++++++ server/commands/userCreator.ts | 85 +++++++++++++++++------------ server/routes/api/team.test.ts | 4 +- server/test/factories.ts | 1 + 4 files changed, 125 insertions(+), 37 deletions(-) diff --git a/server/commands/userCreator.test.ts b/server/commands/userCreator.test.ts index d22d7bf43f..6969ba7f61 100644 --- a/server/commands/userCreator.test.ts +++ b/server/commands/userCreator.test.ts @@ -1,3 +1,4 @@ +import { v4 as uuidv4 } from "uuid"; import { TeamDomain } from "@server/models"; import { buildUser, buildTeam, buildInvite } from "@server/test/factories"; import { flushdb, seed } from "@server/test/support"; @@ -37,6 +38,77 @@ describe("userCreator", () => { expect(isNewUser).toEqual(false); }); + it("should add authentication provider to existing users", async () => { + const team = await buildTeam({ inviteRequired: true }); + const teamAuthProviders = await team.$get("authenticationProviders"); + const authenticationProvider = teamAuthProviders[0]; + + const email = "mynam@email.com"; + const existing = await buildUser({ + email, + teamId: team.id, + authentications: [], + }); + + const result = await userCreator({ + name: existing.name, + email, + username: "new-username", + avatarUrl: existing.avatarUrl, + teamId: existing.teamId, + ip, + authentication: { + authenticationProviderId: authenticationProvider.id, + providerId: uuidv4(), + accessToken: "123", + scopes: ["read"], + }, + }); + const { user, authentication, isNewUser } = result; + expect(authentication.accessToken).toEqual("123"); + expect(authentication.scopes.length).toEqual(1); + expect(authentication.scopes[0]).toEqual("read"); + + const authentications = await user.$get("authentications"); + expect(authentications.length).toEqual(1); + expect(isNewUser).toEqual(false); + }); + + it("should add authentication provider to invited users", async () => { + const team = await buildTeam({ inviteRequired: true }); + const teamAuthProviders = await team.$get("authenticationProviders"); + const authenticationProvider = teamAuthProviders[0]; + + const email = "mynam@email.com"; + const existing = await buildInvite({ + email, + teamId: team.id, + }); + + const result = await userCreator({ + name: existing.name, + email, + username: "new-username", + avatarUrl: existing.avatarUrl, + teamId: existing.teamId, + ip, + authentication: { + authenticationProviderId: authenticationProvider.id, + providerId: uuidv4(), + accessToken: "123", + scopes: ["read"], + }, + }); + const { user, authentication, isNewUser } = result; + expect(authentication.accessToken).toEqual("123"); + expect(authentication.scopes.length).toEqual(1); + expect(authentication.scopes[0]).toEqual("read"); + + const authentications = await user.$get("authentications"); + expect(authentications.length).toEqual(1); + expect(isNewUser).toEqual(true); + }); + it("should create user with deleted user matching providerId", async () => { const existing = await buildUser(); const authentications = await existing.$get("authentications"); diff --git a/server/commands/userCreator.ts b/server/commands/userCreator.ts index ac485cdf2b..59448f67b1 100644 --- a/server/commands/userCreator.ts +++ b/server/commands/userCreator.ts @@ -1,4 +1,3 @@ -import { Op } from "sequelize"; import { sequelize } from "@server/database/sequelize"; import InviteAcceptedEmail from "@server/emails/templates/InviteAcceptedEmail"; import { DomainNotAllowedError, InviteRequiredError } from "@server/errors"; @@ -89,48 +88,62 @@ export default async function userCreator({ // A `user` record might exist in the form of an invite even if there is no // existing authentication record that matches. In Outline an invite is a // shell user record. - const invite = await User.scope(["withAuthentications", "withTeam"]).findOne({ + const existingUser = await User.scope([ + "withAuthentications", + "withTeam", + ]).findOne({ where: { // Email from auth providers may be capitalized and we should respect that // however any existing invites will always be lowercased. email: email.toLowerCase(), teamId, - lastActiveAt: { - [Op.is]: null, - }, }, }); // We have an existing invite for his user, so we need to update it with our // new details, link up the authentication method, and count this as a new // user creation. - if (invite && !invite.authentications.length) { + if (existingUser) { + // A `user` record might exist in the form of an invite. + // In Outline an invite is a shell user record with no authentication method + // that's never been active before. + const isInvite = existingUser.isInvited; + const auth = await sequelize.transaction(async (transaction) => { - await invite.update( + if (isInvite) { + await Event.create( + { + name: "users.create", + actorId: existingUser.id, + userId: existingUser.id, + teamId: existingUser.teamId, + data: { + name, + }, + ip, + }, + { + transaction, + } + ); + } + + // Regardless, create a new authentication record + // against the existing user (user can auth with multiple SSO providers) + // Update user's name and avatar based on the most recently added provider + await existingUser.update( { name, avatarUrl, + lastActiveAt: new Date(), + lastActiveIp: ip, }, { transaction, } ); - await Event.create( - { - name: "users.create", - actorId: invite.id, - userId: invite.id, - teamId: invite.teamId, - data: { - name, - }, - ip, - }, - { - transaction, - } - ); - return await invite.$create( + + return await existingUser.$create( "authentication", authentication, { @@ -139,20 +152,22 @@ export default async function userCreator({ ); }); - const inviter = await invite.$get("invitedBy"); - if (inviter) { - await InviteAcceptedEmail.schedule({ - to: inviter.email, - inviterId: inviter.id, - invitedName: invite.name, - teamUrl: invite.team.url, - }); + if (isInvite) { + const inviter = await existingUser.$get("invitedBy"); + if (inviter) { + await InviteAcceptedEmail.schedule({ + to: inviter.email, + inviterId: inviter.id, + invitedName: existingUser.name, + teamUrl: existingUser.team.url, + }); + } } return { - user: invite, + user: existingUser, authentication: auth, - isNewUser: true, + isNewUser: isInvite, }; } @@ -165,9 +180,9 @@ export default async function userCreator({ transaction, }); - // If the team settings are set to require invites, and the user is not already invited, + // If the team settings are set to require invites, and there's no existing user record, // throw an error and fail user creation. - if (team?.inviteRequired && !invite) { + if (team?.inviteRequired) { throw InviteRequiredError(); } diff --git a/server/routes/api/team.test.ts b/server/routes/api/team.test.ts index 44db902dcf..b613cf6893 100644 --- a/server/routes/api/team.test.ts +++ b/server/routes/api/team.test.ts @@ -39,7 +39,7 @@ describe("#team.update", () => { }); const body = await res.json(); expect(res.status).toEqual(200); - expect(body.data.allowedDomains).toEqual([ + expect(body.data.allowedDomains.sort()).toEqual([ "example-company.com", "example-company.org", ]); @@ -47,7 +47,7 @@ describe("#team.update", () => { const teamDomains: TeamDomain[] = await TeamDomain.findAll({ where: { teamId: team.id }, }); - expect(teamDomains.map((d) => d.name)).toEqual([ + expect(teamDomains.map((d) => d.name).sort()).toEqual([ "example-company.com", "example-company.org", ]); diff --git a/server/test/factories.ts b/server/test/factories.ts index d9ff3cc3ce..086ed4d74b 100644 --- a/server/test/factories.ts +++ b/server/test/factories.ts @@ -179,6 +179,7 @@ export async function buildInvite(overrides: Partial = {}) { name: `User ${count}`, createdAt: new Date("2018-01-01T00:00:00.000Z"), invitedById: actor.id, + authentications: [], ...overrides, lastActiveAt: null, }); From 98106e7f6feca7d54546d3c87338c5b6540a4847 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Fri, 8 Jul 2022 02:22:48 -0700 Subject: [PATCH 4/4] Update 20220702132722-add-webhooks-deleted-at.js --- server/migrations/20220702132722-add-webhooks-deleted-at.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/migrations/20220702132722-add-webhooks-deleted-at.js b/server/migrations/20220702132722-add-webhooks-deleted-at.js index 5416698042..13f530e075 100644 --- a/server/migrations/20220702132722-add-webhooks-deleted-at.js +++ b/server/migrations/20220702132722-add-webhooks-deleted-at.js @@ -7,7 +7,7 @@ module.exports = { allowNull: true }); }, - down: async () => { + down: async (queryInterface) => { await queryInterface.removeColumn("webhook_subscriptions", "deletedAt"); }, };