diff --git a/server/routes/api/auth/auth.test.ts b/server/routes/api/auth/auth.test.ts index 5466361d4b..46d616528c 100644 --- a/server/routes/api/auth/auth.test.ts +++ b/server/routes/api/auth/auth.test.ts @@ -1,6 +1,13 @@ import { faker } from "@faker-js/faker"; import { randomUUID } from "node:crypto"; -import { buildUser, buildTeam, buildUserPasskey } from "@server/test/factories"; +import { Scope } from "@shared/types"; +import { + buildApiKey, + buildOAuthAuthentication, + buildUser, + buildTeam, + buildUserPasskey, +} from "@server/test/factories"; import { getTestServer, setSelfHosted } from "@server/test/support"; const mockTeamInSessionId = randomUUID(); @@ -44,6 +51,43 @@ describe("#auth.info", () => { expect(body.data.team.allowedDomains).toEqual([]); }); + it("should return a collaboration token for a session", async () => { + const user = await buildUser(); + const res = await server.post("/api/auth.info", user); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.collaborationToken).toBeTruthy(); + }); + + it("should not return a collaboration token for an API key", async () => { + const user = await buildUser(); + const key = await buildApiKey({ userId: user.id }); + const res = await server.post("/api/auth.info", { + headers: { + Authorization: `Bearer ${key.value}`, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.collaborationToken).toBeUndefined(); + }); + + it("should not return a collaboration token for an OAuth access token", async () => { + const user = await buildUser(); + const authentication = await buildOAuthAuthentication({ + user, + scope: [Scope.Read], + }); + const res = await server.post("/api/auth.info", { + headers: { + Authorization: `Bearer ${authentication.accessToken}`, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.collaborationToken).toBeUndefined(); + }); + it("should require the team to not be deleted", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); diff --git a/server/routes/api/auth/auth.ts b/server/routes/api/auth/auth.ts index 15f3d9dbdb..c5838f31d4 100644 --- a/server/routes/api/auth/auth.ts +++ b/server/routes/api/auth/auth.ts @@ -19,6 +19,7 @@ import { } from "@server/presenters"; import ValidateSSOAccessTask from "@server/queues/tasks/ValidateSSOAccessTask"; import type { APIContext } from "@server/types"; +import { AuthenticationType } from "@server/types"; import { getSessionsInCookie } from "@server/utils/authentication"; import RateLimiter from "@server/utils/RateLimiter"; import type * as T from "./schema"; @@ -119,7 +120,7 @@ router.post("auth.config", async (ctx: APIContext) => { const NON_SSO_SERVICES = ["email", "passkeys"]; router.post("auth.info", auth(), async (ctx: APIContext) => { - const { user, service } = ctx.state.auth; + const { user, service, type } = ctx.state.auth; const sessions = getSessionsInCookie(ctx); const signedInTeamIds = Object.keys(sessions); @@ -140,9 +141,9 @@ router.post("auth.info", auth(), async (ctx: APIContext) => { // to have access to the workspace they are signed into. This only applies // to SSO sessions - email and passkey logins don't have associated // UserAuthentication records that need validation. - const isOAuthSession = !service || !NON_SSO_SERVICES.includes(service); + const requiresSSOValidation = !service || !NON_SSO_SERVICES.includes(service); if ( - isOAuthSession && + requiresSSOValidation && user.lastSignedInAt && user.lastSignedInAt < subHours(new Date(), 1) ) { @@ -168,7 +169,12 @@ router.post("auth.info", auth(), async (ctx: APIContext) => { team: presentTeam(team), groups: await Promise.all(groups.map(presentGroup)), groupUsers: groups.map((group) => presentGroupUser(group.groupUsers[0])), - collaborationToken: user.getCollaborationToken(), + // The collaboration token is only for the client and should not be issued + // to API or OAuth consumers + collaborationToken: + type === AuthenticationType.APP + ? user.getCollaborationToken() + : undefined, availableTeams: uniqBy([...signedInTeams, ...availableTeams], "id").map( (availableTeam) => presentAvailableTeam(