Use JWT expiry time for cookie duration

* Set default jwt expiry to 90 days.
This commit is contained in:
Philipinho
2025-06-18 12:46:43 -07:00
parent 080900610d
commit 583ce22d67
5 changed files with 26 additions and 9 deletions

View File

@ -1,11 +1,9 @@
import { import {
BadRequestException,
Body, Body,
Controller, Controller,
HttpCode, HttpCode,
HttpStatus, HttpStatus,
Post, Post,
Req,
Res, Res,
UseGuards, UseGuards,
} from '@nestjs/common'; } from '@nestjs/common';
@ -23,7 +21,6 @@ import { ForgotPasswordDto } from './dto/forgot-password.dto';
import { PasswordResetDto } from './dto/password-reset.dto'; import { PasswordResetDto } from './dto/password-reset.dto';
import { VerifyUserTokenDto } from './dto/verify-user-token.dto'; import { VerifyUserTokenDto } from './dto/verify-user-token.dto';
import { FastifyReply } from 'fastify'; import { FastifyReply } from 'fastify';
import { addDays } from 'date-fns';
import { validateSsoEnforcement } from './auth.util'; import { validateSsoEnforcement } from './auth.util';
@Controller('auth') @Controller('auth')
@ -125,7 +122,7 @@ export class AuthController {
res.setCookie('authToken', token, { res.setCookie('authToken', token, {
httpOnly: true, httpOnly: true,
path: '/', path: '/',
expires: addDays(new Date(), 30), expires: this.environmentService.getCookieExpiresIn(),
secure: this.environmentService.isHttps(), secure: this.environmentService.isHttps(),
}); });
} }

View File

@ -29,9 +29,7 @@ import WorkspaceAbilityFactory from '../../casl/abilities/workspace-ability.fact
import { import {
WorkspaceCaslAction, WorkspaceCaslAction,
WorkspaceCaslSubject, WorkspaceCaslSubject,
} from '../../casl/interfaces/workspace-ability.type'; } from '../../casl/interfaces/workspace-ability.type';import { FastifyReply } from 'fastify';
import { addDays } from 'date-fns';
import { FastifyReply } from 'fastify';
import { EnvironmentService } from '../../../integrations/environment/environment.service'; import { EnvironmentService } from '../../../integrations/environment/environment.service';
import { CheckHostnameDto } from '../dto/check-hostname.dto'; import { CheckHostnameDto } from '../dto/check-hostname.dto';
import { RemoveWorkspaceUserDto } from '../dto/remove-workspace-user.dto'; import { RemoveWorkspaceUserDto } from '../dto/remove-workspace-user.dto';
@ -267,7 +265,7 @@ export class WorkspaceController {
res.setCookie('authToken', authToken, { res.setCookie('authToken', authToken, {
httpOnly: true, httpOnly: true,
path: '/', path: '/',
expires: addDays(new Date(), 30), expires: this.environmentService.getCookieExpiresIn(),
secure: this.environmentService.isHttps(), secure: this.environmentService.isHttps(),
}); });
} }

View File

@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import ms, { StringValue } from 'ms';
@Injectable() @Injectable()
export class EnvironmentService { export class EnvironmentService {
@ -56,7 +57,18 @@ export class EnvironmentService {
} }
getJwtTokenExpiresIn(): string { getJwtTokenExpiresIn(): string {
return this.configService.get<string>('JWT_TOKEN_EXPIRES_IN', '30d'); return this.configService.get<string>('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 { getStorageDriver(): string {

View File

@ -69,6 +69,7 @@
"jszip": "^3.10.1", "jszip": "^3.10.1",
"linkifyjs": "^4.2.0", "linkifyjs": "^4.2.0",
"marked": "13.0.3", "marked": "13.0.3",
"ms": "3.0.0-canary.1",
"uuid": "^11.1.0", "uuid": "^11.1.0",
"y-indexeddb": "^9.0.12", "y-indexeddb": "^9.0.12",
"yjs": "^13.6.27" "yjs": "^13.6.27"

9
pnpm-lock.yaml generated
View File

@ -169,6 +169,9 @@ importers:
marked: marked:
specifier: 13.0.3 specifier: 13.0.3
version: 13.0.3 version: 13.0.3
ms:
specifier: 3.0.0-canary.1
version: 3.0.0-canary.1
uuid: uuid:
specifier: ^11.1.0 specifier: ^11.1.0
version: 11.1.0 version: 11.1.0
@ -7358,6 +7361,10 @@ packages:
ms@2.1.3: ms@2.1.3:
resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} 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: msgpackr-extract@3.0.2:
resolution: {integrity: sha512-SdzXp4kD/Qf8agZ9+iTu6eql0m3kWm1A2y1hkpTeVNENutaB0BwHlSvAIaMxwntmRUAUjon2V4L8Z/njd0Ct8A==} resolution: {integrity: sha512-SdzXp4kD/Qf8agZ9+iTu6eql0m3kWm1A2y1hkpTeVNENutaB0BwHlSvAIaMxwntmRUAUjon2V4L8Z/njd0Ct8A==}
hasBin: true hasBin: true
@ -17844,6 +17851,8 @@ snapshots:
ms@2.1.3: {} ms@2.1.3: {}
ms@3.0.0-canary.1: {}
msgpackr-extract@3.0.2: msgpackr-extract@3.0.2:
dependencies: dependencies:
node-gyp-build-optional-packages: 5.0.7 node-gyp-build-optional-packages: 5.0.7