From 583ce22d672a21db97bb3c15d8136aa90b1441a9 Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Wed, 18 Jun 2025 12:46:43 -0700 Subject: [PATCH] Use JWT expiry time for cookie duration * Set default jwt expiry to 90 days. --- apps/server/src/core/auth/auth.controller.ts | 5 +---- .../workspace/controllers/workspace.controller.ts | 6 ++---- .../environment/environment.service.ts | 14 +++++++++++++- package.json | 1 + pnpm-lock.yaml | 9 +++++++++ 5 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/server/src/core/auth/auth.controller.ts b/apps/server/src/core/auth/auth.controller.ts index 20cc6ed5..fb98ed7f 100644 --- a/apps/server/src/core/auth/auth.controller.ts +++ b/apps/server/src/core/auth/auth.controller.ts @@ -1,11 +1,9 @@ import { - BadRequestException, Body, Controller, HttpCode, HttpStatus, Post, - Req, Res, UseGuards, } from '@nestjs/common'; @@ -23,7 +21,6 @@ import { ForgotPasswordDto } from './dto/forgot-password.dto'; import { PasswordResetDto } from './dto/password-reset.dto'; import { VerifyUserTokenDto } from './dto/verify-user-token.dto'; import { FastifyReply } from 'fastify'; -import { addDays } from 'date-fns'; import { validateSsoEnforcement } from './auth.util'; @Controller('auth') @@ -125,7 +122,7 @@ export class AuthController { res.setCookie('authToken', token, { httpOnly: true, path: '/', - expires: addDays(new Date(), 30), + expires: this.environmentService.getCookieExpiresIn(), secure: this.environmentService.isHttps(), }); } diff --git a/apps/server/src/core/workspace/controllers/workspace.controller.ts b/apps/server/src/core/workspace/controllers/workspace.controller.ts index 0a8e3b0c..47a78480 100644 --- a/apps/server/src/core/workspace/controllers/workspace.controller.ts +++ b/apps/server/src/core/workspace/controllers/workspace.controller.ts @@ -29,9 +29,7 @@ import WorkspaceAbilityFactory from '../../casl/abilities/workspace-ability.fact import { WorkspaceCaslAction, WorkspaceCaslSubject, -} from '../../casl/interfaces/workspace-ability.type'; -import { addDays } from 'date-fns'; -import { FastifyReply } from 'fastify'; +} from '../../casl/interfaces/workspace-ability.type';import { FastifyReply } from 'fastify'; import { EnvironmentService } from '../../../integrations/environment/environment.service'; import { CheckHostnameDto } from '../dto/check-hostname.dto'; import { RemoveWorkspaceUserDto } from '../dto/remove-workspace-user.dto'; @@ -267,7 +265,7 @@ export class WorkspaceController { res.setCookie('authToken', authToken, { httpOnly: true, path: '/', - expires: addDays(new Date(), 30), + expires: this.environmentService.getCookieExpiresIn(), secure: this.environmentService.isHttps(), }); } diff --git a/apps/server/src/integrations/environment/environment.service.ts b/apps/server/src/integrations/environment/environment.service.ts index d6336993..639113a6 100644 --- a/apps/server/src/integrations/environment/environment.service.ts +++ b/apps/server/src/integrations/environment/environment.service.ts @@ -1,5 +1,6 @@ import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; +import ms, { StringValue } from 'ms'; @Injectable() export class EnvironmentService { @@ -56,7 +57,18 @@ export class EnvironmentService { } getJwtTokenExpiresIn(): string { - return this.configService.get('JWT_TOKEN_EXPIRES_IN', '30d'); + return this.configService.get('JWT_TOKEN_EXPIRES_IN', '90d'); + } + + getCookieExpiresIn(): Date { + const expiresInStr = this.getJwtTokenExpiresIn(); + let msUntilExpiry: number; + try { + msUntilExpiry = ms(expiresInStr as StringValue); + } catch (err) { + msUntilExpiry = ms('90d'); + } + return new Date(Date.now() + msUntilExpiry); } getStorageDriver(): string { diff --git a/package.json b/package.json index 68093bba..50587933 100644 --- a/package.json +++ b/package.json @@ -69,6 +69,7 @@ "jszip": "^3.10.1", "linkifyjs": "^4.2.0", "marked": "13.0.3", + "ms": "3.0.0-canary.1", "uuid": "^11.1.0", "y-indexeddb": "^9.0.12", "yjs": "^13.6.27" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 029ba89a..6e078f47 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -169,6 +169,9 @@ importers: marked: specifier: 13.0.3 version: 13.0.3 + ms: + specifier: 3.0.0-canary.1 + version: 3.0.0-canary.1 uuid: specifier: ^11.1.0 version: 11.1.0 @@ -7358,6 +7361,10 @@ packages: ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + ms@3.0.0-canary.1: + resolution: {integrity: sha512-kh8ARjh8rMN7Du2igDRO9QJnqCb2xYTJxyQYK7vJJS4TvLLmsbyhiKpSW+t+y26gyOyMd0riphX0GeWKU3ky5g==} + engines: {node: '>=12.13'} + msgpackr-extract@3.0.2: resolution: {integrity: sha512-SdzXp4kD/Qf8agZ9+iTu6eql0m3kWm1A2y1hkpTeVNENutaB0BwHlSvAIaMxwntmRUAUjon2V4L8Z/njd0Ct8A==} hasBin: true @@ -17844,6 +17851,8 @@ snapshots: ms@2.1.3: {} + ms@3.0.0-canary.1: {} + msgpackr-extract@3.0.2: dependencies: node-gyp-build-optional-packages: 5.0.7