From 0d9dfc4076b4d57d91e81fb2c2c598b85f37c335 Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sun, 9 May 2021 22:05:47 +0200 Subject: [PATCH] AuthTokenEntity: Make validUntil not nullable As all tokens are valid for a maximum of 2 years, the validUntil attribute is always populated. This updates the database schema and the DTO to reflect that. Fixes #1256 Signed-off-by: David Mehren --- src/auth/auth-token.dto.ts | 3 +-- src/auth/auth-token.entity.ts | 7 ++----- src/auth/auth.service.spec.ts | 5 ++++- src/auth/auth.service.ts | 6 +----- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/auth/auth-token.dto.ts b/src/auth/auth-token.dto.ts index bd3ef8970..793a3b9c9 100644 --- a/src/auth/auth-token.dto.ts +++ b/src/auth/auth-token.dto.ts @@ -14,8 +14,7 @@ export class AuthTokenDto { @IsDate() createdAt: Date; @IsDate() - @IsOptional() - validUntil: Date | null; + validUntil: Date; @IsDate() @IsOptional() lastUsed: Date | null; diff --git a/src/auth/auth-token.entity.ts b/src/auth/auth-token.entity.ts index 996199fed..f91f16b6a 100644 --- a/src/auth/auth-token.entity.ts +++ b/src/auth/auth-token.entity.ts @@ -35,11 +35,8 @@ export class AuthToken { @Column({ unique: true }) accessTokenHash: string; - @Column({ - nullable: true, - type: 'date', - }) - validUntil: Date | null; + @Column() + validUntil: Date; @Column({ nullable: true, diff --git a/src/auth/auth.service.spec.ts b/src/auth/auth.service.spec.ts index ab13f1141..c3a255d3d 100644 --- a/src/auth/auth.service.spec.ts +++ b/src/auth/auth.service.spec.ts @@ -309,11 +309,14 @@ describe('AuthService', () => { authToken.keyId = 'testKeyId'; authToken.label = 'testLabel'; authToken.createdAt = new Date(); + authToken.validUntil = new Date(); const tokenDto = service.toAuthTokenDto(authToken); expect(tokenDto.keyId).toEqual(authToken.keyId); expect(tokenDto.lastUsed).toBeNull(); expect(tokenDto.label).toEqual(authToken.label); - expect(tokenDto.validUntil).toBeNull(); + expect(tokenDto.validUntil.getTime()).toEqual( + authToken.createdAt.getTime(), + ); expect(tokenDto.createdAt.getTime()).toEqual( authToken.createdAt.getTime(), ); diff --git a/src/auth/auth.service.ts b/src/auth/auth.service.ts index c018f4d6c..1c4d50ae8 100644 --- a/src/auth/auth.service.ts +++ b/src/auth/auth.service.ts @@ -184,14 +184,10 @@ export class AuthService { label: authToken.label, keyId: authToken.keyId, createdAt: authToken.createdAt, - validUntil: null, + validUntil: authToken.validUntil, lastUsed: null, }; - if (authToken.validUntil) { - tokenDto.validUntil = new Date(authToken.validUntil); - } - if (authToken.lastUsed) { tokenDto.lastUsed = new Date(authToken.lastUsed); }