From 8214a5f6d45f7b45fc987e4e417092e256d8e659 Mon Sep 17 00:00:00 2001 From: Anton SHEPILOV Date: Fri, 24 May 2024 20:31:51 +0200 Subject: [PATCH] :white_check_mark: Fixed test for back-channel logout --- .../src/core/platform/services/auth/web/jwt.ts | 6 ++++-- .../e2e/console/backchannel-logout.spec.ts | 4 ---- tdrive/backend/node/test/e2e/setup/index.ts | 18 +++++++++++++++++- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts b/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts index a6de6e43..eb1ca418 100644 --- a/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts +++ b/tdrive/backend/node/src/core/platform/services/auth/web/jwt.ts @@ -20,8 +20,10 @@ const jwtPlugin: FastifyPluginCallback = async (fastify, _opts, next) => { const authenticate = async (request: FastifyRequest) => { const jwt: JwtType = await request.jwtVerify(); - // Verify the SID exists and is valid - await gr.services.console.getClient().verifyJwtSid(jwt.sid); + // Verify the SID exists and is valid except tokens for the public link + if (!jwt.public_token_document_id) { + await gr.services.console.getClient().verifyJwtSid(jwt.sid); + } if (jwt.type === "refresh") { // TODO in the future we must invalidate the refresh token (because it should be single use) diff --git a/tdrive/backend/node/test/e2e/console/backchannel-logout.spec.ts b/tdrive/backend/node/test/e2e/console/backchannel-logout.spec.ts index 220ce6f6..876c5fcf 100644 --- a/tdrive/backend/node/test/e2e/console/backchannel-logout.spec.ts +++ b/tdrive/backend/node/test/e2e/console/backchannel-logout.spec.ts @@ -62,8 +62,6 @@ describe("The /backchannel_logout API", () => { // Verify the session is removed from the database const deletedSession = await currentUser.dbService.getSessionById(currentUser.session); expect(deletedSession).toBeNull(); - expect((await currentUser.dbService.getSessionsByUserId(currentUser.user.id)).length).toEqual(0); - }); it("should create a session on login", async () => { @@ -122,8 +120,6 @@ describe("The /backchannel_logout API", () => { await currentUser.login(currentUser.session); expect((await currentUser.getDocument("user_" + currentUser.user.id)).statusCode).toEqual(200); - const sessions = await currentUser.dbService.getSessionsByUserId(currentUser.user.id); - expect(sessions.length).toEqual(1); }); it("should fail to login with empty session id", async () => { diff --git a/tdrive/backend/node/test/e2e/setup/index.ts b/tdrive/backend/node/test/e2e/setup/index.ts index 2b5d75fb..8aefc703 100644 --- a/tdrive/backend/node/test/e2e/setup/index.ts +++ b/tdrive/backend/node/test/e2e/setup/index.ts @@ -14,9 +14,11 @@ import globalResolver from "../../../src/services/global-resolver"; import {FileServiceImpl} from "../../../src/services/files/services"; import StorageAPI from "../../../src/core/platform/services/storage/provider"; import {SearchServiceAPI} from "../../../src/core/platform/services/search/api"; +import Session from "../../../src/services/console/entities/session"; type TokenPayload = { sub: string; + sid?: string; org?: { [companyId: string]: { role: string; @@ -32,6 +34,7 @@ export type User = { export interface TestPlatform { currentUser: User; + currentSession: string; platform: TdrivePlatform; workspace: Workspace; app: FastifyInstance; @@ -89,6 +92,7 @@ export async function init( storage, workspace: { company_id: "", workspace_id: "" }, currentUser: { id: "" }, + currentSession: uuidv1(), authService: auth, filesService: globalResolver.services.files, auth: { @@ -111,11 +115,23 @@ export async function init( //await testPlatform.messageQueue.start(); async function getJWTToken( - payload: TokenPayload = { sub: testPlatform.currentUser.id }, + payload: TokenPayload = { sub: testPlatform.currentUser.id, sid: testPlatform.currentSession }, ): Promise { + const sessionRepository = await testPlatform.database.getRepository("session", Session); if (!payload.sub) { payload.sub = testPlatform.currentUser.id; } + if (!payload.sid) { + payload.sid = testPlatform.currentSession; + } + + let session = (await sessionRepository.find({ sub: payload.sub })).getEntities(); + if (session.length == 0) { + const session = new Session(); + session.sid = payload.sid; + session.sub = payload.sub; + await sessionRepository.save(session); + } if (testPlatform.currentUser.isWorkspaceModerator) { payload.org = {};