From 838429ac898c9bd67e28a873770b187e8be69ace Mon Sep 17 00:00:00 2001 From: benjdlambert Date: Mon, 8 Sep 2025 10:52:05 +0200 Subject: [PATCH] chore: fix some more typescript errors Signed-off-by: benjdlambert --- .../src/database/TestDatabases.ts | 2 +- .../src/database/OidcDatabase.test.ts | 20 +++++++-------- .../auth-backend/src/database/OidcDatabase.ts | 25 +++++++++++++------ .../src/service/OidcService.test.ts | 2 ++ .../auth-backend/src/service/OidcService.ts | 16 ++++++------ 5 files changed, 38 insertions(+), 27 deletions(-) diff --git a/packages/backend-test-utils/src/database/TestDatabases.ts b/packages/backend-test-utils/src/database/TestDatabases.ts index cd20e2997b..00fae4130a 100644 --- a/packages/backend-test-utils/src/database/TestDatabases.ts +++ b/packages/backend-test-utils/src/database/TestDatabases.ts @@ -104,7 +104,7 @@ export class TestDatabases { if (supportedIds.length > 0) { afterAll(async () => { await databases.shutdown(); - }); + }, 30_000); } return databases; diff --git a/plugins/auth-backend/src/database/OidcDatabase.test.ts b/plugins/auth-backend/src/database/OidcDatabase.test.ts index 82064361a5..9965069acf 100644 --- a/plugins/auth-backend/src/database/OidcDatabase.test.ts +++ b/plugins/auth-backend/src/database/OidcDatabase.test.ts @@ -117,7 +117,7 @@ describe('Oidc Database', () => { codeChallenge: 'test-challenge', codeChallengeMethod: 'S256', nonce: 'test-nonce', - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); expect(session).toEqual( @@ -132,7 +132,7 @@ describe('Oidc Database', () => { codeChallenge: 'test-challenge', codeChallengeMethod: 'S256', nonce: 'test-nonce', - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), status: 'pending', }), ); @@ -155,7 +155,7 @@ describe('Oidc Database', () => { clientId: client.clientId, redirectUri: 'https://example.com/callback', responseType: 'code', - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); await expect( @@ -190,20 +190,20 @@ describe('Oidc Database', () => { clientId: client.clientId, redirectUri: 'https://example.com/callback', responseType: 'code', - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); const authCode = await oidc.createAuthorizationCode({ code: 'test-code', sessionId: session.id, - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); expect(authCode).toEqual( expect.objectContaining({ code: 'test-code', sessionId: session.id, - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }), ); }); @@ -230,13 +230,13 @@ describe('Oidc Database', () => { codeChallenge: 'test-challenge', codeChallengeMethod: 'S256', nonce: 'test-nonce', - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); const authCode = await oidc.createAuthorizationCode({ code: 'test-code', sessionId: session.id, - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); const authCodeFromDb = await oidc.getAuthorizationCode({ @@ -267,13 +267,13 @@ describe('Oidc Database', () => { clientId: client.clientId, redirectUri: 'https://example.com/callback', responseType: 'code', - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); const authCode = await oidc.createAuthorizationCode({ code: 'test-code', sessionId: session.id, - expiresAt: '2025-01-01T00:00:00Z', + expiresAt: new Date('2025-01-01T00:00:00Z'), }); const updatedAuthCode = await oidc.updateAuthorizationCode({ diff --git a/plugins/auth-backend/src/database/OidcDatabase.ts b/plugins/auth-backend/src/database/OidcDatabase.ts index f4f99ef6a6..ec9a879803 100644 --- a/plugins/auth-backend/src/database/OidcDatabase.ts +++ b/plugins/auth-backend/src/database/OidcDatabase.ts @@ -16,6 +16,15 @@ import { Knex } from 'knex'; import { AuthDatabase } from './AuthDatabase'; +function toDate(value?: Date | string | number): Date | undefined { + if (!value) { + return undefined; + } + + return typeof value === 'string' || typeof value === 'number' + ? new Date(value) + : value; +} type OidcClientRow = { client_id: string; client_secret: string; @@ -39,13 +48,13 @@ type OAuthAuthorizationSessionRow = { code_challenge_method: string | null; nonce: string | null; status: 'pending' | 'approved' | 'rejected' | 'expired'; - expires_at: string; + expires_at: Date | string; }; type OidcAuthorizationCodeRow = { code: string; session_id: string; - expires_at: string; + expires_at: Date | string; used: boolean; }; @@ -72,26 +81,26 @@ export type AuthorizationSession = { codeChallengeMethod?: string; nonce?: string; status: 'pending' | 'approved' | 'rejected' | 'expired'; - expiresAt: string; + expiresAt: Date; }; export type ConsentRequest = { id: string; sessionId: string; - expiresAt: string; + expiresAt: Date; }; export type AuthorizationCode = { code: string; sessionId: string; - expiresAt: string; + expiresAt: Date; used: boolean; }; export type AccessToken = { tokenId: string; sessionId: string; - expiresAt: string; + expiresAt: Date; }; /** @@ -342,7 +351,7 @@ export class OidcDatabase { codeChallengeMethod: row.code_challenge_method ?? undefined, nonce: row.nonce ?? undefined, status: row.status, - expiresAt: row.expires_at, + expiresAt: toDate(row.expires_at), }; } @@ -363,7 +372,7 @@ export class OidcDatabase { return { code: row.code, sessionId: row.session_id, - expiresAt: row.expires_at, + expiresAt: toDate(row.expires_at), used: Boolean(row.used), }; } diff --git a/plugins/auth-backend/src/service/OidcService.test.ts b/plugins/auth-backend/src/service/OidcService.test.ts index 017d3ab481..11a37e20bf 100644 --- a/plugins/auth-backend/src/service/OidcService.test.ts +++ b/plugins/auth-backend/src/service/OidcService.test.ts @@ -31,6 +31,8 @@ import { UserInfoDatabase } from '../database/UserInfoDatabase'; import crypto from 'crypto'; import { AnyJWK, TokenIssuer } from '../identity/types'; +jest.setTimeout(60_000); + describe('OidcService', () => { const databases = TestDatabases.create(); diff --git a/plugins/auth-backend/src/service/OidcService.ts b/plugins/auth-backend/src/service/OidcService.ts index 7809d2fd00..4b47d963c9 100644 --- a/plugins/auth-backend/src/service/OidcService.ts +++ b/plugins/auth-backend/src/service/OidcService.ts @@ -174,7 +174,7 @@ export class OidcService { } const sessionId = crypto.randomUUID(); - const sessionExpiresAt = DateTime.now().plus({ hours: 1 }).toISO(); + const sessionExpiresAt = DateTime.now().plus({ hours: 1 }).toJSDate(); await this.oidc.createAuthorizationSession({ id: sessionId, @@ -211,7 +211,7 @@ export class OidcService { throw new NotFoundError('Invalid authorization session'); } - if (DateTime.fromISO(session.expiresAt) < DateTime.now()) { + if (DateTime.fromJSDate(session.expiresAt) < DateTime.now()) { throw new InputError('Authorization session expired'); } @@ -226,7 +226,7 @@ export class OidcService { }); const authorizationCode = crypto.randomBytes(32).toString('base64url'); - const codeExpiresAt = DateTime.now().plus({ minutes: 10 }).toISO(); + const codeExpiresAt = DateTime.now().plus({ minutes: 10 }).toJSDate(); await this.oidc.createAuthorizationCode({ code: authorizationCode, @@ -255,7 +255,7 @@ export class OidcService { throw new NotFoundError('Invalid authorization session'); } - if (DateTime.fromISO(session.expiresAt) < DateTime.now()) { + if (DateTime.fromJSDate(session.expiresAt) < DateTime.now()) { throw new InputError('Authorization session expired'); } @@ -293,7 +293,7 @@ export class OidcService { throw new NotFoundError('Invalid authorization session'); } - if (DateTime.fromISO(session.expiresAt) < DateTime.now()) { + if (DateTime.fromJSDate(session.expiresAt) < DateTime.now()) { throw new InputError('Authorization session expired'); } @@ -353,7 +353,7 @@ export class OidcService { } const sessionId = crypto.randomUUID(); - const sessionExpiresAt = DateTime.now().plus({ hours: 1 }).toISO(); + const sessionExpiresAt = DateTime.now().plus({ hours: 1 }).toJSDate(); await this.oidc.createAuthorizationSession({ id: sessionId, @@ -375,7 +375,7 @@ export class OidcService { }); const authorizationCode = crypto.randomBytes(32).toString('base64url'); - const codeExpiresAt = DateTime.now().plus({ minutes: 10 }).toISO(); + const codeExpiresAt = DateTime.now().plus({ minutes: 10 }).toJSDate(); await this.oidc.createAuthorizationCode({ code: authorizationCode, @@ -429,7 +429,7 @@ export class OidcService { throw new AuthenticationError('Invalid authorization code'); } - if (DateTime.fromISO(authCode.expiresAt) < DateTime.now()) { + if (DateTime.fromJSDate(authCode.expiresAt) < DateTime.now()) { throw new AuthenticationError('Authorization code expired'); }