From 539f32e96002ba927eece99ee6cba66ba87f785f Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 15 Oct 2024 14:42:04 +0200 Subject: [PATCH 01/42] Switch to asymmetric signing --- .../src/infra/auth-guard/config/auth-config.ts | 5 ++++- .../infra/auth-guard/mapper/authConfig.factory.ts | 13 ++++++++----- .../src/infra/auth-guard/strategy/jwt.strategy.ts | 2 +- .../infra/auth-guard/strategy/ws-jwt.strategy.ts | 2 +- .../modules/authentication/authentication.module.ts | 3 ++- config/default.schema.json | 8 ++++++++ config/development.json | 2 ++ config/test.json | 2 ++ src/services/authentication/configuration.js | 4 ++-- 9 files changed, 30 insertions(+), 11 deletions(-) diff --git a/apps/server/src/infra/auth-guard/config/auth-config.ts b/apps/server/src/infra/auth-guard/config/auth-config.ts index 02dc8c6fdfa..9082f6faada 100644 --- a/apps/server/src/infra/auth-guard/config/auth-config.ts +++ b/apps/server/src/infra/auth-guard/config/auth-config.ts @@ -7,4 +7,7 @@ const jwtOptions = { expiresIn: Configuration.get('JWT_LIFETIME') as string, }; -export const authConfig = AuthConfigFactory.build(Configuration.get('AUTHENTICATION'), jwtOptions); +const privateKey = Configuration.get('JWT_PRIVATE_KEY') as string; +const publicKey = Configuration.get('JWT_PUBLIC_KEY') as string; + +export const authConfig = AuthConfigFactory.build(privateKey, publicKey, jwtOptions); diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts index 7c9f3d84aba..941fc58630b 100644 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts +++ b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts @@ -45,14 +45,16 @@ export interface JwtOptions { } export interface JwtConstants { - secret: string; + privateKey: string; + publicKey: string; jwtOptions: JwtOptions; } // Check if it not more a jwt factory and should be renamed and moved export class AuthConfigFactory { - public static build(secretInput: unknown, jwtOptionsInput: unknown): JwtConstants { - const secret = TypeGuard.checkString(secretInput); + public static build(privateKeyInput: unknown, publicKeyInput: unknown, jwtOptionsInput: unknown): JwtConstants { + const privateKey = TypeGuard.checkString(privateKeyInput); + const publicKey = TypeGuard.checkString(publicKeyInput); // Should we add length check for secrets and that it is NOT secrets like for local? const jwtOptions = TypeGuard.checkKeysInObject(jwtOptionsInput, ['audience', 'issuer', 'expiresIn']); @@ -61,12 +63,13 @@ export class AuthConfigFactory { const expiresIn = TypeGuard.checkString(jwtOptions.expiresIn); const jwtConstants = { - secret, + privateKey, + publicKey, jwtOptions: { header: { typ: 'access' }, // or should it be typ: 'JWT' ? alg ? audience, issuer, - algorithm: Algorithms.HS256, + algorithm: Algorithms.RS256, expiresIn, }, }; diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index 4722ab94c5e..3c60eb5ebbd 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -13,7 +13,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) { super({ jwtFromRequest: extractJwtFromHeader, ignoreExpiration: false, - secretOrKey: authConfig.secret, + secretOrKey: authConfig.publicKey, ...authConfig.jwtOptions, }); } diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index 4136e9616fe..a0f31ed0aca 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -14,7 +14,7 @@ export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JW super({ jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), ignoreExpiration: false, - secretOrKey: authConfig.secret, + secretOrKey: authConfig.publicKey, ...authConfig.jwtOptions, }); } diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index b608ed84c2d..a538688ad92 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -27,7 +27,8 @@ const signOptions: SignOptions = { header: { ...header, alg: algorithm }, }; const jwtModuleOptions: JwtModuleOptions = { - secret: authConfig.secret, + privateKey: authConfig.privateKey, + publicKey: authConfig.publicKey, signOptions, verifyOptions: signOptions, }; diff --git a/config/default.schema.json b/config/default.schema.json index d13131bf0e9..5c9f276f449 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -123,6 +123,14 @@ "type": "string", "description": "The secret that is used for create and validate the jwt." }, + "JWT_PRIVATE_KEY": { + "type": "string", + "description": "The private key used to sign JWTs." + }, + "JWT_PUBLIC_KEY": { + "type": "string", + "description": "The public key used to verify JWTs." + }, "JWT_LIFETIME": { "type": "string", "default": "30d", diff --git a/config/development.json b/config/development.json index e3d6d3d5602..9a02d67eb86 100644 --- a/config/development.json +++ b/config/development.json @@ -1,6 +1,8 @@ { "$schema": "./default.schema.json", "AUTHENTICATION": "secrets", + "JWT_PRIVATE_KEY": "-----BEGIN RSA PRIVATE KEY-----\nMIIJKAIBAAKCAgEA0/oW2sIZWvVt0AEgQ8PS80/udJzfWXu6t2QWjUcQA2THGvDS\nXXMH6YMMY2czyBgf6L7hHV/9p1Trfpe7YgxYhOoGsxhXG1keAYQ4+mdveaUAa3ui\nACdEodsB0OFjVUdgOHCyUIXFfhSsp2p2tmZeFi/bE2v/05kYO+ExgQuzUDbB8bCr\n1sc7gMS/2dC2iE/BVw/I0F14oZkZn0fshojg4qoaLbLVKB7Iw53IXF2878zXp81J\ndnnvHdwVbGWqoII6sHZFQs8ob5S/WGMl4QnBHN98x0KmORUFyTv5kK4cdcC8LJ1H\npoVWNC6js84iF9yFRhYXY2RHqh7BwaZZ4XZym/MetTdQTBDaSvhXe0A3WdahNG+D\nGriehd6doWk98Adb49InaodH64ZRkurxiX61GEtzjMRq9EfGS5R/IfcWyPQbiir6\nymKXfOUtywRjcm3F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ZRkurxiX61GEtzjMRq9EfGS5R/IfcWyPQbiir6ymKX\nfOUtywRjcm3FZzmT7j3c0UHzQVEH0NBfTMj+QKz5NILNP230j0DcjNImDbHHcVH1\nquSb6e0WXjKANTkf4gaTOw7jdQDFw0Ou3aEmwPg+Xk1cwCwSHOOmPSSssZwgjpzG\nodPO3vsMGfRYTwcGbzgdQFFj0qTmvgnM5MHtEy8qCyvM4OsAPnE0zQWn48p7PVdJ\nm6j0H/1BYgVw1KxecIVk/HryoTOkgS9lhLu8iEIyrpAlWascIK7Uw58CAwEAAQ==\n-----END RSA PUBLIC KEY-----\n", "SC_DOMAIN": "localhost", "PUBLIC_BACKEND_URL": "http://localhost:3030/api", "FEATURE_API_VALIDATION_ENABLED": true, diff --git a/config/test.json b/config/test.json index 4205d3f29d2..e4f08093fcc 100644 --- a/config/test.json +++ b/config/test.json @@ -1,6 +1,8 @@ { "$schema": "./default.schema.json", "AUTHENTICATION": "secrets", + "JWT_PRIVATE_KEY": "-----BEGIN RSA PRIVATE KEY-----\nMIIJKAIBAAKCAgEA0/oW2sIZWvVt0AEgQ8PS80/udJzfWXu6t2QWjUcQA2THGvDS\nXXMH6YMMY2czyBgf6L7hHV/9p1Trfpe7YgxYhOoGsxhXG1keAYQ4+mdveaUAa3ui\nACdEodsB0OFjVUdgOHCyUIXFfhSsp2p2tmZeFi/bE2v/05kYO+ExgQuzUDbB8bCr\n1sc7gMS/2dC2iE/BVw/I0F14oZkZn0fshojg4qoaLbLVKB7Iw53IXF2878zXp81J\ndnnvHdwVbGWqoII6sHZFQs8ob5S/WGMl4QnBHN98x0KmORUFyTv5kK4cdcC8LJ1H\npoVWNC6js84iF9yFRhYXY2RHqh7BwaZZ4XZym/MetTdQTBDaSvhXe0A3WdahNG+D\nGriehd6doWk98Adb49InaodH64ZRkurxiX61GEtzjMRq9EfGS5R/IfcWyPQbiir6\nymKXfOUtywRjcm3FZzmT7j3c0UHzQVEH0NBfTMj+QKz5NILNP230j0DcjNImDbHH\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ywRjcm3FZzmT7j3c0UHzQVEH0NBfTMj+QKz5NILNP230j0DcjNImDbHHcVH1\nquSb6e0WXjKANTkf4gaTOw7jdQDFw0Ou3aEmwPg+Xk1cwCwSHOOmPSSssZwgjpzG\nodPO3vsMGfRYTwcGbzgdQFFj0qTmvgnM5MHtEy8qCyvM4OsAPnE0zQWn48p7PVdJ\nm6j0H/1BYgVw1KxecIVk/HryoTOkgS9lhLu8iEIyrpAlWascIK7Uw58CAwEAAQ==\n-----END RSA PUBLIC KEY-----\n", "host": "localhost", "port": 3030, "SC_DOMAIN": "localhost", diff --git a/src/services/authentication/configuration.js b/src/services/authentication/configuration.js index 3ea49809981..ec00d27f051 100644 --- a/src/services/authentication/configuration.js +++ b/src/services/authentication/configuration.js @@ -7,13 +7,13 @@ module.exports = { // entityId and service are never queried, but need to be provided otherwise the server doesn't start entityId: 'id', service: 'emptyService', // This service is registered in 'index.js' - secret: Configuration.get('AUTHENTICATION'), + secret: Configuration.get('JWT_PUBLIC_KEY'), authStrategies: ['jwt', 'tsp', 'api-key'], jwtOptions: { header: { typ: 'access' }, audience: Configuration.get('JWT_AUD'), issuer: 'feathers', - algorithm: 'HS256', + algorithm: 'RS256', expiresIn: Configuration.get('JWT_LIFETIME'), }, tsp: {}, From 92dbf9d589ee3361920bb9ec6060af3acc1fad00 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 15 Oct 2024 14:47:57 +0200 Subject: [PATCH 02/42] Adjust configs --- apps/server/src/infra/auth-guard/auth-guard.config.ts | 2 +- .../src/modules/authentication/authentication-config.ts | 3 ++- apps/server/src/modules/h5p-editor/h5p-editor.config.ts | 2 +- apps/server/src/modules/server/admin-api-server.config.ts | 2 +- apps/server/src/modules/server/server.config.ts | 3 ++- config/default.schema.json | 4 ---- config/development.json | 1 - config/test.json | 1 - 8 files changed, 7 insertions(+), 11 deletions(-) diff --git a/apps/server/src/infra/auth-guard/auth-guard.config.ts b/apps/server/src/infra/auth-guard/auth-guard.config.ts index 4b6fc411410..7ec81d5e2a0 100644 --- a/apps/server/src/infra/auth-guard/auth-guard.config.ts +++ b/apps/server/src/infra/auth-guard/auth-guard.config.ts @@ -2,5 +2,5 @@ export interface AuthGuardConfig { ADMIN_API__ALLOWED_API_KEYS: string[]; JWT_AUD: string; JWT_LIFETIME: string; - AUTHENTICATION: string; + JWT_PUBLIC_KEY: string; } diff --git a/apps/server/src/modules/authentication/authentication-config.ts b/apps/server/src/modules/authentication/authentication-config.ts index d86aa80d822..840695856df 100644 --- a/apps/server/src/modules/authentication/authentication-config.ts +++ b/apps/server/src/modules/authentication/authentication-config.ts @@ -2,9 +2,10 @@ import { XApiKeyConfig } from '@infra/auth-guard'; import { AccountConfig } from '@modules/account'; export interface AuthenticationConfig extends AccountConfig, XApiKeyConfig { - AUTHENTICATION: string; DISABLED_BRUTE_FORCE_CHECK: boolean; FEATURE_JWT_EXTENDED_TIMEOUT_ENABLED: boolean; + JWT_PRIVATE_KEY: string; + JWT_PUBLIC_KEY: string; JWT_LIFETIME: string; JWT_TIMEOUT_SECONDS: number; JWT_LIFETIME_SUPPORT_SECONDS: number; diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index 19c9bf7467f..ebf25139ba8 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -22,7 +22,7 @@ const h5pEditorConfig: H5PEditorConfig = { ADMIN_API__ALLOWED_API_KEYS: [], JWT_AUD: Configuration.get('JWT_AUD') as string, JWT_LIFETIME: Configuration.get('JWT_LIFETIME') as string, - AUTHENTICATION: Configuration.get('AUTHENTICATION') as string, + JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, }; export const translatorConfig = { diff --git a/apps/server/src/modules/server/admin-api-server.config.ts b/apps/server/src/modules/server/admin-api-server.config.ts index 336d4deba6a..a6d6c289ab0 100644 --- a/apps/server/src/modules/server/admin-api-server.config.ts +++ b/apps/server/src/modules/server/admin-api-server.config.ts @@ -58,7 +58,7 @@ const config: AdminApiServerConfig = { .map((part) => (part.split(':').pop() ?? '').trim()), JWT_AUD: Configuration.get('JWT_AUD') as string, JWT_LIFETIME: Configuration.get('JWT_LIFETIME') as string, - AUTHENTICATION: Configuration.get('AUTHENTICATION') as string, + JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, LOGIN_BLOCK_TIME: 0, TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE: Configuration.get( 'TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE' diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index c967ba2a5de..17b05e718bd 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -137,7 +137,6 @@ export interface ServerConfig } const config: ServerConfig = { - AUTHENTICATION: Configuration.get('AUTHENTICATION') as string, ACCESSIBILITY_REPORT_EMAIL: Configuration.get('ACCESSIBILITY_REPORT_EMAIL') as string, ADMIN_TABLES_DISPLAY_CONSENT_COLUMN: Configuration.get('ADMIN_TABLES_DISPLAY_CONSENT_COLUMN') as boolean, ALERT_STATUS_URL: @@ -189,6 +188,8 @@ const config: ServerConfig = { JWT_TIMEOUT_SECONDS: Configuration.get('JWT_TIMEOUT_SECONDS') as number, JWT_LIFETIME_SUPPORT_SECONDS: Configuration.get('JWT_LIFETIME_SUPPORT_SECONDS') as number, JWT_EXTENDED_TIMEOUT_SECONDS: Configuration.get('JWT_EXTENDED_TIMEOUT_SECONDS') as number, + JWT_PRIVATE_KEY: Configuration.get('JWT_PRIVATE_KEY') as string, + JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, NOT_AUTHENTICATED_REDIRECT_URL: Configuration.get('NOT_AUTHENTICATED_REDIRECT_URL') as string, DOCUMENT_BASE_DIR: Configuration.get('DOCUMENT_BASE_DIR') as string, SC_THEME: Configuration.get('SC_THEME') as SchulcloudTheme, diff --git a/config/default.schema.json b/config/default.schema.json index 5c9f276f449..622c8b74eaa 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -119,10 +119,6 @@ "default": 86400, "description": "Maximum time in seconds a generated pin is handled as valid." }, - "AUTHENTICATION": { - "type": "string", - "description": "The secret that is used for create and validate the jwt." - }, "JWT_PRIVATE_KEY": { "type": "string", "description": "The private key used to sign JWTs." diff --git a/config/development.json b/config/development.json index 9a02d67eb86..991020bb033 100644 --- a/config/development.json +++ b/config/development.json @@ -1,6 +1,5 @@ { "$schema": "./default.schema.json", - "AUTHENTICATION": "secrets", "JWT_PRIVATE_KEY": "-----BEGIN RSA PRIVATE KEY-----\nMIIJKAIBAAKCAgEA0/oW2sIZWvVt0AEgQ8PS80/udJzfWXu6t2QWjUcQA2THGvDS\nXXMH6YMMY2czyBgf6L7hHV/9p1Trfpe7YgxYhOoGsxhXG1keAYQ4+mdveaUAa3ui\nACdEodsB0OFjVUdgOHCyUIXFfhSsp2p2tmZeFi/bE2v/05kYO+ExgQuzUDbB8bCr\n1sc7gMS/2dC2iE/BVw/I0F14oZkZn0fshojg4qoaLbLVKB7Iw53IXF2878zXp81J\ndnnvHdwVbGWqoII6sHZFQs8ob5S/WGMl4QnBHN98x0KmORUFyTv5kK4cdcC8LJ1H\npoVWNC6js84iF9yFRhYXY2RHqh7BwaZZ4XZym/MetTdQTBDaSvhXe0A3WdahNG+D\nGriehd6doWk98Adb49InaodH64ZRkurxiX61GEtzjMRq9EfGS5R/IfcWyPQbiir6\nymKXfOUtywRjcm3FZzmT7j3c0UHzQVEH0NBfTMj+QKz5NILNP230j0DcjNImDbHH\ncVH1quSb6e0WXjKANTkf4gaTOw7jdQDFw0Ou3aEmwPg+Xk1cwCwSHOOmPSSssZwg\njpzGodPO3vsMGfRYTwcGbzgdQFFj0qTmvgnM5MHtEy8qCyvM4OsAPnE0zQWn48p7\nPVdJm6j0H/1BYgVw1KxecIVk/HryoTOkgS9lhLu8iE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PnE0zQWn48p7PVdJ\nm6j0H/1BYgVw1KxecIVk/HryoTOkgS9lhLu8iEIyrpAlWascIK7Uw58CAwEAAQ==\n-----END RSA PUBLIC KEY-----\n", "SC_DOMAIN": "localhost", diff --git a/config/test.json b/config/test.json index e4f08093fcc..c46f050adee 100644 --- a/config/test.json +++ b/config/test.json @@ -1,6 +1,5 @@ { "$schema": "./default.schema.json", - "AUTHENTICATION": "secrets", "JWT_PRIVATE_KEY": "-----BEGIN RSA PRIVATE KEY-----\nMIIJKAIBAAKCAgEA0/oW2sIZWvVt0AEgQ8PS80/udJzfWXu6t2QWjUcQA2THGvDS\nXXMH6YMMY2czyBgf6L7hHV/9p1Trfpe7YgxYhOoGsxhXG1keAYQ4+mdveaUAa3ui\nACdEodsB0OFjVUdgOHCyUIXFfhSsp2p2tmZeFi/bE2v/05kYO+ExgQuzUDbB8bCr\n1sc7gMS/2dC2iE/BVw/I0F14oZkZn0fshojg4qoaLbLVKB7Iw53IXF2878zXp81J\ndnnvHdwVbGWqoII6sHZFQs8ob5S/WGMl4QnBHN98x0KmORUFyTv5kK4cdcC8LJ1H\npoVWNC6js84iF9yFRhYXY2RHqh7BwaZZ4XZym/MetTdQTBDaSvhXe0A3WdahNG+D\nGriehd6doWk98Adb49InaodH64ZRkurxiX61GEtzjMRq9EfGS5R/IfcWyPQbiir6\nymKXfOUtywRjcm3FZzmT7j3c0UHzQVEH0NBfTMj+QKz5NILNP230j0DcjNImDbHH\ncVH1quSb6e0WXjKANTkf4gaTOw7jdQDFw0Ou3aEmwPg+Xk1cwCwSHOOmPSSssZwg\njpzGodPO3vsMGfRYTwcGbzgdQFFj0qTmvgnM5MHtEy8qCyvM4OsAPnE0zQWn48p7\nPVdJm6j0H/1BYgVw1KxecIVk/HryoTOkgS9lhLu8iEIyrpAlWascIK7Uw58CAwEA\nAQKCAgAA0/lC4X83272SEm8N1LX+PVGxIuu8bb9M+BcediiZ2srsUASCWPCu+NQT\nj1OkdHOrdRNsCfPzs2E4H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"localhost", From 2f0b22117ca1f51b219c7ca04642c93e608885a7 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 15 Oct 2024 16:26:29 +0200 Subject: [PATCH 03/42] Fix tests --- .../mapper/authConfig.factory.spec.ts | 135 ++++++++---------- .../auth-guard/strategy/jwt.strategy.spec.ts | 4 +- .../strategy/ws-jwt.strategy.spec.ts | 4 +- 3 files changed, 61 insertions(+), 82 deletions(-) diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts index 1c9caf86b37..4c1fdc4114a 100644 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts +++ b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts @@ -6,70 +6,41 @@ const buildNotAnObjectError = () => new Error(`Type is not an object.`); describe('AuthConfigFactory.build', () => { describe('when input is valid', () => { const setup = () => { - const secret = 'mysecret'; - const input = { + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; + const jwtOptions = { audience: 'myaudience', issuer: 'myissuer', expiresIn: '1h', }; const expectedResult: JwtConstants = { - secret: 'mysecret', + privateKey: 'myprivatekey', + publicKey: 'mypublickey', jwtOptions: { header: { typ: 'access' }, audience: 'myaudience', issuer: 'myissuer', - algorithm: Algorithms.HS256, + algorithm: Algorithms.RS256, expiresIn: '1h', }, }; - return { secret, input, expectedResult }; + return { privateKey, publicKey, jwtOptions, expectedResult }; }; it('should map input to JwtConstants', () => { - const { secret, input, expectedResult } = setup(); + const { privateKey, publicKey, jwtOptions, expectedResult } = setup(); - const result: JwtConstants = AuthConfigFactory.build(secret, input); + const result: JwtConstants = AuthConfigFactory.build(privateKey, publicKey, jwtOptions); expect(result).toEqual(expectedResult); }); }); - describe('when input is null', () => { - const setup = () => { - const secret = 'mysecret'; - const input = null; - const error = buildNotAnObjectError(); - - return { secret, input, error }; - }; - - it('should throw', () => { - const { secret, input, error } = setup(); - - expect(() => AuthConfigFactory.build(secret, input)).toThrow(error); - }); - }); - - describe('when input is undefined', () => { - const setup = () => { - const secret = 'mysecret'; - const input = undefined; - const error = buildNotAnObjectError(); - - return { secret, input, error }; - }; - - it('should throw', () => { - const { secret, input, error } = setup(); - - expect(() => AuthConfigFactory.build(secret, input)).toThrow(error); - }); - }); - - describe('when secret prop is unedfined', () => { + describe('when privateKey is undefined', () => { const setup = () => { - const secret = undefined; + const privateKey = undefined; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 'myaudience', issuer: 'myissuer', @@ -77,19 +48,20 @@ describe('AuthConfigFactory.build', () => { }; const error = buildNotAStringError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); - describe('when secret prop is number', () => { + describe('when privateKey is number', () => { const setup = () => { - const secret = 123; + const privateKey = 123; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 'myaudience', issuer: 'myissuer', @@ -97,35 +69,37 @@ describe('AuthConfigFactory.build', () => { }; const error = buildNotAStringError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); - describe('when jwtOptions is unedfined', () => { + describe('when jwtOptions is undefined', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = undefined; const error = buildNotAnObjectError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); describe('when audience prop is undefined', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = { issuer: 'myissuer', expiresIn: '1h', @@ -134,19 +108,20 @@ describe('AuthConfigFactory.build', () => { 'Object has missing key. Required are: ["audience","issuer","expiresIn"]. Get object keys: ["issuer","expiresIn"]' ); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); describe('when audience prop is number', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 123, issuer: 'myissuer', @@ -154,19 +129,20 @@ describe('AuthConfigFactory.build', () => { }; const error = buildNotAStringError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); describe('when issuer prop is undefined', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 'myaudience', issuer: undefined, @@ -174,19 +150,20 @@ describe('AuthConfigFactory.build', () => { }; const error = buildNotAStringError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); describe('when issuer prop is number', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 'myaudience', issuer: 123, @@ -194,19 +171,20 @@ describe('AuthConfigFactory.build', () => { }; const error = buildNotAStringError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); describe('when expiresIn prop is undefined', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 'myaudience', issuer: 'myissuer', @@ -215,19 +193,20 @@ describe('AuthConfigFactory.build', () => { 'Object has missing key. Required are: ["audience","issuer","expiresIn"]. Get object keys: ["audience","issuer"]' ); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); describe('when expiresIn prop is number', () => { const setup = () => { - const secret = 'mysecret'; + const privateKey = 'myprivatekey'; + const publicKey = 'mypublickey'; const jwtOptions = { audience: 'myaudience', issuer: 'myissuer', @@ -235,13 +214,13 @@ describe('AuthConfigFactory.build', () => { }; const error = buildNotAStringError(); - return { secret, jwtOptions, error }; + return { privateKey, publicKey, jwtOptions, error }; }; it('should throw', () => { - const { secret, jwtOptions, error } = setup(); + const { privateKey, publicKey, jwtOptions, error } = setup(); - expect(() => AuthConfigFactory.build(secret, jwtOptions)).toThrow(error); + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); }); }); }); diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts index eff14de3ca4..34a7e45bbdc 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts @@ -11,7 +11,7 @@ import { JwtStrategy } from './jwt.strategy'; jest.mock('../config', () => { const authConfig = { - secret: 'mysecret', + publicKey: 'mypublickey', jwtOptions: { header: { typ: 'JWT' }, audience: 'myaudience', @@ -28,7 +28,7 @@ jest.mock('../config', () => { const buildAuthConfig = () => { return { - secret: 'mysecret', + publicKey: 'mypublickey', jwtOptions: { header: { typ: 'JWT' }, audience: 'myaudience', diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts index 685710a922b..0b987162660 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts @@ -9,7 +9,7 @@ import { WsJwtStrategy } from './ws-jwt.strategy'; jest.mock('../config', () => { const authConfig = { - secret: 'mysecret', + publicKey: 'mypublickey', jwtOptions: { header: { typ: 'JWT' }, audience: 'myaudience', @@ -26,7 +26,7 @@ jest.mock('../config', () => { const buildAuthConfig = () => { return { - secret: 'mysecret', + publicKey: 'mypublickey', jwtOptions: { header: { typ: 'JWT' }, audience: 'myaudience', From 35d13fb0eddd67f6c8d3e650da2d5e507f3ca566 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Wed, 16 Oct 2024 11:14:09 +0200 Subject: [PATCH 04/42] Make secret combination of private and public key in feathers --- src/services/authentication/configuration.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/services/authentication/configuration.js b/src/services/authentication/configuration.js index ec00d27f051..d15221b380f 100644 --- a/src/services/authentication/configuration.js +++ b/src/services/authentication/configuration.js @@ -7,7 +7,8 @@ module.exports = { // entityId and service are never queried, but need to be provided otherwise the server doesn't start entityId: 'id', service: 'emptyService', // This service is registered in 'index.js' - secret: Configuration.get('JWT_PUBLIC_KEY'), + // The idea to concatenate the keys is from this feathers issue: https://github.com/feathersjs/feathers/issues/1251 + secret: Configuration.get('JWT_PRIVATE_KEY') + Configuration.get('JWT_PUBLIC_KEY'), authStrategies: ['jwt', 'tsp', 'api-key'], jwtOptions: { header: { typ: 'access' }, From 4f906dad1091dbe8ba13271fdb7d9fd0f7447f34 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Wed, 16 Oct 2024 11:26:42 +0200 Subject: [PATCH 05/42] Add tests --- .../mapper/authConfig.factory.spec.ts | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts index 4c1fdc4114a..265aa0c5242 100644 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts +++ b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts @@ -79,6 +79,48 @@ describe('AuthConfigFactory.build', () => { }); }); + describe('when publicKey is undefined', () => { + const setup = () => { + const privateKey = 'myprivatekey'; + const publicKey = undefined; + const jwtOptions = { + audience: 'myaudience', + issuer: 'myissuer', + expiresIn: '1h', + }; + const error = buildNotAStringError(); + + return { privateKey, publicKey, jwtOptions, error }; + }; + + it('should throw', () => { + const { privateKey, publicKey, jwtOptions, error } = setup(); + + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); + }); + }); + + describe('when publicKey is number', () => { + const setup = () => { + const privateKey = 'myprivatekey'; + const publicKey = 123; + const jwtOptions = { + audience: 'myaudience', + issuer: 'myissuer', + expiresIn: '1h', + }; + const error = buildNotAStringError(); + + return { privateKey, publicKey, jwtOptions, error }; + }; + + it('should throw', () => { + const { privateKey, publicKey, jwtOptions, error } = setup(); + + expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); + }); + }); + describe('when jwtOptions is undefined', () => { const setup = () => { const privateKey = 'myprivatekey'; From c50292b49d6b69e8f31a54fd750b82ebfad0de0b Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Wed, 16 Oct 2024 11:30:08 +0200 Subject: [PATCH 06/42] Update typ of JWT --- .../src/infra/auth-guard/mapper/authConfig.factory.spec.ts | 2 +- apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts | 2 +- src/services/authentication/configuration.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts index 265aa0c5242..5678a544271 100644 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts +++ b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts @@ -17,7 +17,7 @@ describe('AuthConfigFactory.build', () => { privateKey: 'myprivatekey', publicKey: 'mypublickey', jwtOptions: { - header: { typ: 'access' }, + header: { typ: 'JWT' }, audience: 'myaudience', issuer: 'myissuer', algorithm: Algorithms.RS256, diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts index 941fc58630b..5db437efa41 100644 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts +++ b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts @@ -66,7 +66,7 @@ export class AuthConfigFactory { privateKey, publicKey, jwtOptions: { - header: { typ: 'access' }, // or should it be typ: 'JWT' ? alg ? + header: { typ: 'JWT' }, audience, issuer, algorithm: Algorithms.RS256, diff --git a/src/services/authentication/configuration.js b/src/services/authentication/configuration.js index d15221b380f..2e26e5dd88e 100644 --- a/src/services/authentication/configuration.js +++ b/src/services/authentication/configuration.js @@ -11,7 +11,7 @@ module.exports = { secret: Configuration.get('JWT_PRIVATE_KEY') + Configuration.get('JWT_PUBLIC_KEY'), authStrategies: ['jwt', 'tsp', 'api-key'], jwtOptions: { - header: { typ: 'access' }, + header: { typ: 'JWT' }, audience: Configuration.get('JWT_AUD'), issuer: 'feathers', algorithm: 'RS256', From d14c39a2ceaca565a9c0d1a49c191f723ff1445f Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Wed, 16 Oct 2024 11:49:59 +0200 Subject: [PATCH 07/42] Update authConfig in tests --- .../infra/auth-guard/strategy/jwt.strategy.spec.ts | 14 ++++++-------- .../auth-guard/strategy/ws-jwt.strategy.spec.ts | 8 ++++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts index 34a7e45bbdc..c431ec85578 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts @@ -1,11 +1,9 @@ +import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { UnauthorizedException } from '@nestjs/common'; import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { Test, TestingModule } from '@nestjs/testing'; - -import { createMock, DeepMocked } from '@golevelup/ts-jest'; -import { UnauthorizedException } from '@nestjs/common'; import { jwtPayloadFactory, setupEntities } from '@shared/testing'; - import { JwtValidationAdapter } from '../adapter/jwt-validation.adapter'; import { JwtStrategy } from './jwt.strategy'; @@ -13,10 +11,10 @@ jest.mock('../config', () => { const authConfig = { publicKey: 'mypublickey', jwtOptions: { - header: { typ: 'JWT' }, + header: { typ: 'mytyp' }, audience: 'myaudience', issuer: 'myissuer', - algorithm: 'HS256', + algorithm: 'myalgorithm', expiresIn: '1h', }, }; @@ -30,10 +28,10 @@ const buildAuthConfig = () => { return { publicKey: 'mypublickey', jwtOptions: { - header: { typ: 'JWT' }, + header: { typ: 'mytyp' }, audience: 'myaudience', issuer: 'myissuer', - algorithm: 'HS256', + algorithm: 'myalgorithm', expiresIn: '1h', }, }; diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts index 0b987162660..becb6dc35fc 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts @@ -11,10 +11,10 @@ jest.mock('../config', () => { const authConfig = { publicKey: 'mypublickey', jwtOptions: { - header: { typ: 'JWT' }, + header: { typ: 'mytyp' }, audience: 'myaudience', issuer: 'myissuer', - algorithm: 'HS256', + algorithm: 'myalgorithm', expiresIn: '1h', }, }; @@ -28,10 +28,10 @@ const buildAuthConfig = () => { return { publicKey: 'mypublickey', jwtOptions: { - header: { typ: 'JWT' }, + header: { typ: 'mytyp' }, audience: 'myaudience', issuer: 'myissuer', - algorithm: 'HS256', + algorithm: 'myalgorithm', expiresIn: '1h', }, }; From 44821a8340cb966b34d76191398315122d03048d Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Thu, 17 Oct 2024 15:58:20 +0200 Subject: [PATCH 08/42] Handle new lines in keys --- apps/server/src/infra/auth-guard/config/auth-config.ts | 7 ++++++- apps/server/src/modules/server/server.config.ts | 7 +++++-- src/services/authentication/configuration.js | 6 +++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/apps/server/src/infra/auth-guard/config/auth-config.ts b/apps/server/src/infra/auth-guard/config/auth-config.ts index 9082f6faada..00ef0db0de8 100644 --- a/apps/server/src/infra/auth-guard/config/auth-config.ts +++ b/apps/server/src/infra/auth-guard/config/auth-config.ts @@ -10,4 +10,9 @@ const jwtOptions = { const privateKey = Configuration.get('JWT_PRIVATE_KEY') as string; const publicKey = Configuration.get('JWT_PUBLIC_KEY') as string; -export const authConfig = AuthConfigFactory.build(privateKey, publicKey, jwtOptions); +// Node's process.env escapes newlines. We need to reverse it for the keys to work. +// See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node +const formattedPrivateKey = privateKey.replace(/\\n/g, '\n'); +const formattedPublicKey = publicKey.replace(/\\n/g, '\n'); + +export const authConfig = AuthConfigFactory.build(formattedPrivateKey, formattedPublicKey, jwtOptions); diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 17b05e718bd..8cff856f6ef 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -188,8 +188,11 @@ const config: ServerConfig = { JWT_TIMEOUT_SECONDS: Configuration.get('JWT_TIMEOUT_SECONDS') as number, JWT_LIFETIME_SUPPORT_SECONDS: Configuration.get('JWT_LIFETIME_SUPPORT_SECONDS') as number, JWT_EXTENDED_TIMEOUT_SECONDS: Configuration.get('JWT_EXTENDED_TIMEOUT_SECONDS') as number, - JWT_PRIVATE_KEY: Configuration.get('JWT_PRIVATE_KEY') as string, - JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PRIVATE_KEY: (Configuration.get('JWT_PRIVATE_KEY') as string).replace(/\\n/g, '\n'), + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), NOT_AUTHENTICATED_REDIRECT_URL: Configuration.get('NOT_AUTHENTICATED_REDIRECT_URL') as string, DOCUMENT_BASE_DIR: Configuration.get('DOCUMENT_BASE_DIR') as string, SC_THEME: Configuration.get('SC_THEME') as SchulcloudTheme, diff --git a/src/services/authentication/configuration.js b/src/services/authentication/configuration.js index 2e26e5dd88e..a23c9b39c61 100644 --- a/src/services/authentication/configuration.js +++ b/src/services/authentication/configuration.js @@ -7,8 +7,12 @@ module.exports = { // entityId and service are never queried, but need to be provided otherwise the server doesn't start entityId: 'id', service: 'emptyService', // This service is registered in 'index.js' + // The idea to concatenate the keys is from this feathers issue: https://github.com/feathersjs/feathers/issues/1251 - secret: Configuration.get('JWT_PRIVATE_KEY') + Configuration.get('JWT_PUBLIC_KEY'), + // Furthermore: Node's process.env escapes newlines. We need to reverse it for the keys to work. + secret: + Configuration.get('JWT_PRIVATE_KEY').replace(/\\n/g, '\n') + + Configuration.get('JWT_PUBLIC_KEY').replace(/\\n/g, '\n'), authStrategies: ['jwt', 'tsp', 'api-key'], jwtOptions: { header: { typ: 'JWT' }, From b3bbb7021713c4c9be5e40c6ee77f8813dfa801f Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 18 Oct 2024 10:54:48 +0200 Subject: [PATCH 09/42] Clean up strategy tests --- .../auth-guard/strategy/jwt.strategy.spec.ts | 37 +------------------ .../strategy/ws-jwt.strategy.spec.ts | 37 +------------------ 2 files changed, 2 insertions(+), 72 deletions(-) diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts index c431ec85578..d38d719c402 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts @@ -1,52 +1,17 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { UnauthorizedException } from '@nestjs/common'; -import { JwtModule } from '@nestjs/jwt'; -import { PassportModule } from '@nestjs/passport'; import { Test, TestingModule } from '@nestjs/testing'; -import { jwtPayloadFactory, setupEntities } from '@shared/testing'; +import { jwtPayloadFactory } from '@shared/testing'; import { JwtValidationAdapter } from '../adapter/jwt-validation.adapter'; import { JwtStrategy } from './jwt.strategy'; -jest.mock('../config', () => { - const authConfig = { - publicKey: 'mypublickey', - jwtOptions: { - header: { typ: 'mytyp' }, - audience: 'myaudience', - issuer: 'myissuer', - algorithm: 'myalgorithm', - expiresIn: '1h', - }, - }; - - return { - authConfig, - }; -}); - -const buildAuthConfig = () => { - return { - publicKey: 'mypublickey', - jwtOptions: { - header: { typ: 'mytyp' }, - audience: 'myaudience', - issuer: 'myissuer', - algorithm: 'myalgorithm', - expiresIn: '1h', - }, - }; -}; - describe('jwt strategy', () => { let validationAdapter: DeepMocked; let strategy: JwtStrategy; let module: TestingModule; beforeAll(async () => { - await setupEntities(); - module = await Test.createTestingModule({ - imports: [PassportModule, JwtModule.register(buildAuthConfig())], providers: [ JwtStrategy, { diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts index becb6dc35fc..dc05c751f29 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts @@ -1,52 +1,17 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; -import { JwtModule } from '@nestjs/jwt'; -import { PassportModule } from '@nestjs/passport'; import { Test, TestingModule } from '@nestjs/testing'; import { WsException } from '@nestjs/websockets'; -import { jwtPayloadFactory, setupEntities } from '@shared/testing'; +import { jwtPayloadFactory } from '@shared/testing'; import { JwtValidationAdapter } from '../adapter'; import { WsJwtStrategy } from './ws-jwt.strategy'; -jest.mock('../config', () => { - const authConfig = { - publicKey: 'mypublickey', - jwtOptions: { - header: { typ: 'mytyp' }, - audience: 'myaudience', - issuer: 'myissuer', - algorithm: 'myalgorithm', - expiresIn: '1h', - }, - }; - - return { - authConfig, - }; -}); - -const buildAuthConfig = () => { - return { - publicKey: 'mypublickey', - jwtOptions: { - header: { typ: 'mytyp' }, - audience: 'myaudience', - issuer: 'myissuer', - algorithm: 'myalgorithm', - expiresIn: '1h', - }, - }; -}; - describe('jwt strategy', () => { let validationAdapter: DeepMocked; let strategy: WsJwtStrategy; let module: TestingModule; beforeAll(async () => { - await setupEntities(); - module = await Test.createTestingModule({ - imports: [PassportModule, JwtModule.register(buildAuthConfig())], providers: [ WsJwtStrategy, { From d7e04f87ef96b3ebb70a9ec7c49df8bc9884e699 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 18 Oct 2024 11:56:36 +0200 Subject: [PATCH 10/42] Use JwtModuleOptionsFactory --- .../authentication/authentication.module.ts | 23 ++------ .../jwt-module-options.factory.ts | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 apps/server/src/modules/authentication/jwt-module-options.factory.ts diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index a538688ad92..408040f8ebf 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -1,4 +1,4 @@ -import { authConfig, AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule } from '@infra/auth-guard'; import { CacheWrapperModule } from '@infra/cache'; import { IdentityManagementModule } from '@infra/identity-management'; import { AccountModule } from '@modules/account'; @@ -6,38 +6,23 @@ import { OauthModule } from '@modules/oauth/oauth.module'; import { RoleModule } from '@modules/role'; import { SystemModule } from '@modules/system'; import { Module } from '@nestjs/common'; -import { JwtModule, JwtModuleOptions } from '@nestjs/jwt'; +import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { LegacySchoolRepo, UserRepo } from '@shared/repo'; import { LoggerModule } from '@src/core/logger'; -import { SignOptions } from 'jsonwebtoken'; import { JwtWhitelistAdapter } from './helper/jwt-whitelist.adapter'; import { AuthenticationService } from './services/authentication.service'; import { LdapService } from './services/ldap.service'; import { LdapStrategy } from './strategy/ldap.strategy'; import { LocalStrategy } from './strategy/local.strategy'; import { Oauth2Strategy } from './strategy/oauth2.strategy'; - -const { algorithm, audience, expiresIn, issuer, header } = authConfig.jwtOptions; -const signOptions: SignOptions = { - algorithm, - audience, - expiresIn, - issuer, - header: { ...header, alg: algorithm }, -}; -const jwtModuleOptions: JwtModuleOptions = { - privateKey: authConfig.privateKey, - publicKey: authConfig.publicKey, - signOptions, - verifyOptions: signOptions, -}; +import { JwtModuleOptionsFactory } from './jwt-module-options.factory'; @Module({ imports: [ LoggerModule, PassportModule, - JwtModule.register(jwtModuleOptions), + JwtModule.registerAsync({ useClass: JwtModuleOptionsFactory }), AccountModule, SystemModule, OauthModule, diff --git a/apps/server/src/modules/authentication/jwt-module-options.factory.ts b/apps/server/src/modules/authentication/jwt-module-options.factory.ts new file mode 100644 index 00000000000..6160fbc02e5 --- /dev/null +++ b/apps/server/src/modules/authentication/jwt-module-options.factory.ts @@ -0,0 +1,57 @@ +import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { ConfigService } from '@nestjs/config'; +import { JwtModuleOptions, JwtOptionsFactory } from '@nestjs/jwt'; +import { SignOptions } from 'jsonwebtoken'; + +/* + TODO: look at existing keys, vs implemented keys + support: true, + supportUserId, + accountId, + userId, + iat, + exp, + aud: this.aud, + iss: 'feathers', + sub: accountId, + jti: `support_${ObjectId()}`, +*/ + +export class JwtModuleOptionsFactory implements JwtOptionsFactory { + constructor(private readonly configService: ConfigService) {} + + public createJwtOptions(): JwtModuleOptions { + // const privateKey = TypeGuard.checkString(privateKeyInput); + // const publicKey = TypeGuard.checkString(publicKeyInput); + // // Should we add length check for secrets and that it is NOT secrets like for local? + + // const jwtOptions = TypeGuard.checkKeysInObject(jwtOptionsInput, ['audience', 'issuer', 'expiresIn']); + // const audience = TypeGuard.checkString(jwtOptions.audience); + // const issuer = TypeGuard.checkString(jwtOptions.issuer); + // const expiresIn = TypeGuard.checkString(jwtOptions.expiresIn); + + const algorithm = 'RS256'; + + const signOptions: SignOptions = { + algorithm, + audience: Configuration.get('JWT_AUD') as string, + expiresIn: Configuration.get('JWT_LIFETIME') as string, + issuer: 'feathers', + header: { typ: 'JWT', alg: algorithm }, + }; + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + const privateKey = (Configuration.get('JWT_PRIVATE_KEY') as string).replace(/\\n/g, '\n'); + const publicKey = (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'); + + const options = { + privateKey, + publicKey, + signOptions, + verifyOptions: signOptions, + }; + + return options; + } +} From a8353fd541ddd62293196a0aa37d465e0ecee684 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 18 Oct 2024 12:07:21 +0200 Subject: [PATCH 11/42] Use factory method instead of class --- .../authentication/authentication-config.ts | 1 + .../authentication/authentication.module.ts | 33 ++++++++++- .../jwt-module-options.factory.ts | 57 ------------------- .../src/modules/server/server.config.ts | 1 + 4 files changed, 33 insertions(+), 59 deletions(-) delete mode 100644 apps/server/src/modules/authentication/jwt-module-options.factory.ts diff --git a/apps/server/src/modules/authentication/authentication-config.ts b/apps/server/src/modules/authentication/authentication-config.ts index 840695856df..d14ebd3c17d 100644 --- a/apps/server/src/modules/authentication/authentication-config.ts +++ b/apps/server/src/modules/authentication/authentication-config.ts @@ -10,5 +10,6 @@ export interface AuthenticationConfig extends AccountConfig, XApiKeyConfig { JWT_TIMEOUT_SECONDS: number; JWT_LIFETIME_SUPPORT_SECONDS: number; JWT_EXTENDED_TIMEOUT_SECONDS: number; + JWT_AUD: string; LOGIN_BLOCK_TIME: number; } diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index 408040f8ebf..ab9ad296ab6 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -6,23 +6,52 @@ import { OauthModule } from '@modules/oauth/oauth.module'; import { RoleModule } from '@modules/role'; import { SystemModule } from '@modules/system'; import { Module } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { LegacySchoolRepo, UserRepo } from '@shared/repo'; import { LoggerModule } from '@src/core/logger'; +import { SignOptions } from 'jsonwebtoken'; +import { AuthenticationConfig } from './authentication-config'; import { JwtWhitelistAdapter } from './helper/jwt-whitelist.adapter'; import { AuthenticationService } from './services/authentication.service'; import { LdapService } from './services/ldap.service'; import { LdapStrategy } from './strategy/ldap.strategy'; import { LocalStrategy } from './strategy/local.strategy'; import { Oauth2Strategy } from './strategy/oauth2.strategy'; -import { JwtModuleOptionsFactory } from './jwt-module-options.factory'; + +const createJwtOptions = (configService: ConfigService) => { + const algorithm = 'RS256'; + + const signOptions: SignOptions = { + algorithm, + audience: configService.get('JWT_AUD'), + expiresIn: configService.get('JWT_LIFETIME'), + issuer: 'feathers', + header: { typ: 'JWT', alg: algorithm }, + }; + + const privateKey = configService.get('JWT_PRIVATE_KEY'); + const publicKey = configService.get('JWT_PUBLIC_KEY'); + + const options = { + privateKey, + publicKey, + signOptions, + verifyOptions: signOptions, + }; + + return options; +}; @Module({ imports: [ LoggerModule, PassportModule, - JwtModule.registerAsync({ useClass: JwtModuleOptionsFactory }), + JwtModule.registerAsync({ + useFactory: createJwtOptions, + inject: [ConfigService], + }), AccountModule, SystemModule, OauthModule, diff --git a/apps/server/src/modules/authentication/jwt-module-options.factory.ts b/apps/server/src/modules/authentication/jwt-module-options.factory.ts deleted file mode 100644 index 6160fbc02e5..00000000000 --- a/apps/server/src/modules/authentication/jwt-module-options.factory.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { ConfigService } from '@nestjs/config'; -import { JwtModuleOptions, JwtOptionsFactory } from '@nestjs/jwt'; -import { SignOptions } from 'jsonwebtoken'; - -/* - TODO: look at existing keys, vs implemented keys - support: true, - supportUserId, - accountId, - userId, - iat, - exp, - aud: this.aud, - iss: 'feathers', - sub: accountId, - jti: `support_${ObjectId()}`, -*/ - -export class JwtModuleOptionsFactory implements JwtOptionsFactory { - constructor(private readonly configService: ConfigService) {} - - public createJwtOptions(): JwtModuleOptions { - // const privateKey = TypeGuard.checkString(privateKeyInput); - // const publicKey = TypeGuard.checkString(publicKeyInput); - // // Should we add length check for secrets and that it is NOT secrets like for local? - - // const jwtOptions = TypeGuard.checkKeysInObject(jwtOptionsInput, ['audience', 'issuer', 'expiresIn']); - // const audience = TypeGuard.checkString(jwtOptions.audience); - // const issuer = TypeGuard.checkString(jwtOptions.issuer); - // const expiresIn = TypeGuard.checkString(jwtOptions.expiresIn); - - const algorithm = 'RS256'; - - const signOptions: SignOptions = { - algorithm, - audience: Configuration.get('JWT_AUD') as string, - expiresIn: Configuration.get('JWT_LIFETIME') as string, - issuer: 'feathers', - header: { typ: 'JWT', alg: algorithm }, - }; - - // Node's process.env escapes newlines. We need to reverse it for the keys to work. - // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node - const privateKey = (Configuration.get('JWT_PRIVATE_KEY') as string).replace(/\\n/g, '\n'); - const publicKey = (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'); - - const options = { - privateKey, - publicKey, - signOptions, - verifyOptions: signOptions, - }; - - return options; - } -} diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 8cff856f6ef..8a6b818f0ba 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -188,6 +188,7 @@ const config: ServerConfig = { JWT_TIMEOUT_SECONDS: Configuration.get('JWT_TIMEOUT_SECONDS') as number, JWT_LIFETIME_SUPPORT_SECONDS: Configuration.get('JWT_LIFETIME_SUPPORT_SECONDS') as number, JWT_EXTENDED_TIMEOUT_SECONDS: Configuration.get('JWT_EXTENDED_TIMEOUT_SECONDS') as number, + JWT_AUD: Configuration.get('JWT_AUD') as string, // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node From 1376f581ace22b2485a3a084be824125eaf4622e Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 18 Oct 2024 16:02:57 +0200 Subject: [PATCH 12/42] Remove authConfig --- .../infra/auth-guard/config/auth-config.ts | 18 -- .../src/infra/auth-guard/config/index.ts | 1 - apps/server/src/infra/auth-guard/index.ts | 2 +- .../mapper/authConfig.factory.spec.ts | 268 ------------------ .../auth-guard/mapper/authConfig.factory.ts | 79 ------ .../src/infra/auth-guard/mapper/index.ts | 1 - .../auth-guard/strategy/jwt.strategy.spec.ts | 5 + .../infra/auth-guard/strategy/jwt.strategy.ts | 13 +- .../strategy/ws-jwt.strategy.spec.ts | 5 + .../auth-guard/strategy/ws-jwt.strategy.ts | 13 +- 10 files changed, 29 insertions(+), 376 deletions(-) delete mode 100644 apps/server/src/infra/auth-guard/config/auth-config.ts delete mode 100644 apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts delete mode 100644 apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts diff --git a/apps/server/src/infra/auth-guard/config/auth-config.ts b/apps/server/src/infra/auth-guard/config/auth-config.ts deleted file mode 100644 index 00ef0db0de8..00000000000 --- a/apps/server/src/infra/auth-guard/config/auth-config.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Configuration } from '@hpi-schul-cloud/commons'; -import { AuthConfigFactory } from '../mapper'; - -const jwtOptions = { - audience: Configuration.get('JWT_AUD') as string, - issuer: 'feathers', // should be in our case need to be changed in part 3 of this ticket Configuration.get('JWT_AUD') as string, - expiresIn: Configuration.get('JWT_LIFETIME') as string, -}; - -const privateKey = Configuration.get('JWT_PRIVATE_KEY') as string; -const publicKey = Configuration.get('JWT_PUBLIC_KEY') as string; - -// Node's process.env escapes newlines. We need to reverse it for the keys to work. -// See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node -const formattedPrivateKey = privateKey.replace(/\\n/g, '\n'); -const formattedPublicKey = publicKey.replace(/\\n/g, '\n'); - -export const authConfig = AuthConfigFactory.build(formattedPrivateKey, formattedPublicKey, jwtOptions); diff --git a/apps/server/src/infra/auth-guard/config/index.ts b/apps/server/src/infra/auth-guard/config/index.ts index 80219784f77..cd9c0d28b96 100644 --- a/apps/server/src/infra/auth-guard/config/index.ts +++ b/apps/server/src/infra/auth-guard/config/index.ts @@ -1,2 +1 @@ -export * from './auth-config'; export * from './x-api-key.config'; diff --git a/apps/server/src/infra/auth-guard/index.ts b/apps/server/src/infra/auth-guard/index.ts index 53ca333e40d..92fa2f36347 100644 --- a/apps/server/src/infra/auth-guard/index.ts +++ b/apps/server/src/infra/auth-guard/index.ts @@ -1,7 +1,7 @@ export { JwtValidationAdapter } from './adapter'; export { AuthGuardModule } from './auth-guard.module'; export { AuthGuardConfig } from './auth-guard.config'; -export { XApiKeyConfig, authConfig } from './config'; +export { XApiKeyConfig } from './config'; export { CurrentUser, JWT, JwtAuthentication } from './decorator'; // JwtAuthGuard only exported because api tests still overried this guard. // Use JwtAuthentication decorator for request validation diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts deleted file mode 100644 index 5678a544271..00000000000 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.spec.ts +++ /dev/null @@ -1,268 +0,0 @@ -import { Algorithms, AuthConfigFactory, JwtConstants } from './authConfig.factory'; - -const buildNotAStringError = () => new Error(`Type is not a string`); -const buildNotAnObjectError = () => new Error(`Type is not an object.`); - -describe('AuthConfigFactory.build', () => { - describe('when input is valid', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - expiresIn: '1h', - }; - const expectedResult: JwtConstants = { - privateKey: 'myprivatekey', - publicKey: 'mypublickey', - jwtOptions: { - header: { typ: 'JWT' }, - audience: 'myaudience', - issuer: 'myissuer', - algorithm: Algorithms.RS256, - expiresIn: '1h', - }, - }; - - return { privateKey, publicKey, jwtOptions, expectedResult }; - }; - - it('should map input to JwtConstants', () => { - const { privateKey, publicKey, jwtOptions, expectedResult } = setup(); - - const result: JwtConstants = AuthConfigFactory.build(privateKey, publicKey, jwtOptions); - - expect(result).toEqual(expectedResult); - }); - }); - - describe('when privateKey is undefined', () => { - const setup = () => { - const privateKey = undefined; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when privateKey is number', () => { - const setup = () => { - const privateKey = 123; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when publicKey is undefined', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = undefined; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when publicKey is number', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 123; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when jwtOptions is undefined', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = undefined; - const error = buildNotAnObjectError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when audience prop is undefined', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - issuer: 'myissuer', - expiresIn: '1h', - }; - const error = new Error( - 'Object has missing key. Required are: ["audience","issuer","expiresIn"]. Get object keys: ["issuer","expiresIn"]' - ); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when audience prop is number', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 123, - issuer: 'myissuer', - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when issuer prop is undefined', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: undefined, - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when issuer prop is number', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: 123, - expiresIn: '1h', - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when expiresIn prop is undefined', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - }; - const error = new Error( - 'Object has missing key. Required are: ["audience","issuer","expiresIn"]. Get object keys: ["audience","issuer"]' - ); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); - - describe('when expiresIn prop is number', () => { - const setup = () => { - const privateKey = 'myprivatekey'; - const publicKey = 'mypublickey'; - const jwtOptions = { - audience: 'myaudience', - issuer: 'myissuer', - expiresIn: 123, - }; - const error = buildNotAStringError(); - - return { privateKey, publicKey, jwtOptions, error }; - }; - - it('should throw', () => { - const { privateKey, publicKey, jwtOptions, error } = setup(); - - expect(() => AuthConfigFactory.build(privateKey, publicKey, jwtOptions)).toThrow(error); - }); - }); -}); diff --git a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts b/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts deleted file mode 100644 index 5db437efa41..00000000000 --- a/apps/server/src/infra/auth-guard/mapper/authConfig.factory.ts +++ /dev/null @@ -1,79 +0,0 @@ -import { TypeGuard } from '@shared/common'; - -export enum Algorithms { - HS256 = 'HS256', - HS384 = 'HS384', - HS512 = 'HS512', - RS256 = 'RS256', - RS384 = 'RS384', - RS512 = 'RS512', - ES256 = 'ES256', - ES384 = 'ES384', - ES512 = 'ES512', - PS256 = 'PS256', - PS384 = 'PS384', - PS512 = 'PS512', - none = 'none', -} - -/* - TODO: look at existing keys, vs implemented keys - support: true, - supportUserId, - accountId, - userId, - iat, - exp, - aud: this.aud, - iss: 'feathers', - sub: accountId, - jti: `support_${ObjectId()}`, -*/ - -interface JwtOptionsHeader { - typ: string; - alg?: Algorithms; -} - -export interface JwtOptions { - header: JwtOptionsHeader; - audience: string; - issuer: string; - algorithm: Algorithms; - expiresIn: string; - // keys are missing! -} - -export interface JwtConstants { - privateKey: string; - publicKey: string; - jwtOptions: JwtOptions; -} - -// Check if it not more a jwt factory and should be renamed and moved -export class AuthConfigFactory { - public static build(privateKeyInput: unknown, publicKeyInput: unknown, jwtOptionsInput: unknown): JwtConstants { - const privateKey = TypeGuard.checkString(privateKeyInput); - const publicKey = TypeGuard.checkString(publicKeyInput); - // Should we add length check for secrets and that it is NOT secrets like for local? - - const jwtOptions = TypeGuard.checkKeysInObject(jwtOptionsInput, ['audience', 'issuer', 'expiresIn']); - const audience = TypeGuard.checkString(jwtOptions.audience); - const issuer = TypeGuard.checkString(jwtOptions.issuer); - const expiresIn = TypeGuard.checkString(jwtOptions.expiresIn); - - const jwtConstants = { - privateKey, - publicKey, - jwtOptions: { - header: { typ: 'JWT' }, - audience, - issuer, - algorithm: Algorithms.RS256, - expiresIn, - }, - }; - - return jwtConstants; - } -} diff --git a/apps/server/src/infra/auth-guard/mapper/index.ts b/apps/server/src/infra/auth-guard/mapper/index.ts index 94ae9f21978..e5e72b12686 100644 --- a/apps/server/src/infra/auth-guard/mapper/index.ts +++ b/apps/server/src/infra/auth-guard/mapper/index.ts @@ -1,3 +1,2 @@ -export * from './authConfig.factory'; export * from './current-user.factory'; export * from './jwt.factory'; diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts index d38d719c402..3b142b3e0cd 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.spec.ts @@ -1,5 +1,6 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; import { UnauthorizedException } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { jwtPayloadFactory } from '@shared/testing'; import { JwtValidationAdapter } from '../adapter/jwt-validation.adapter'; @@ -18,6 +19,10 @@ describe('jwt strategy', () => { provide: JwtValidationAdapter, useValue: createMock(), }, + { + provide: ConfigService, + useValue: createMock(), + }, ], }).compile(); diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index 3c60eb5ebbd..f1e5dbd8d63 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -1,20 +1,25 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { extractJwtFromHeader } from '@shared/common'; import { Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; -import { authConfig } from '../config'; +import { AuthGuardConfig } from '../auth-guard.config'; import { ICurrentUser, JwtPayload } from '../interface'; import { CurrentUserBuilder } from '../mapper'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { - constructor(private readonly jwtValidationAdapter: JwtValidationAdapter) { + constructor( + private readonly jwtValidationAdapter: JwtValidationAdapter, + configService: ConfigService + ) { + const publicKey = configService.get('JWT_PUBLIC_KEY'); + super({ jwtFromRequest: extractJwtFromHeader, ignoreExpiration: false, - secretOrKey: authConfig.publicKey, - ...authConfig.jwtOptions, + secretOrKey: publicKey, }); } diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts index dc05c751f29..5b5ecf068a1 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.spec.ts @@ -1,4 +1,5 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest'; +import { ConfigService } from '@nestjs/config'; import { Test, TestingModule } from '@nestjs/testing'; import { WsException } from '@nestjs/websockets'; import { jwtPayloadFactory } from '@shared/testing'; @@ -18,6 +19,10 @@ describe('jwt strategy', () => { provide: JwtValidationAdapter, useValue: createMock(), }, + { + provide: ConfigService, + useValue: createMock(), + }, ], }).compile(); diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index a0f31ed0aca..477500feaad 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -1,21 +1,26 @@ import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { WsException } from '@nestjs/websockets'; import { JwtExtractor } from '@shared/common'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; -import { authConfig } from '../config'; +import { AuthGuardConfig } from '../auth-guard.config'; import { ICurrentUser, JwtPayload, StrategyType } from '../interface'; import { CurrentUserBuilder } from '../mapper'; @Injectable() export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JWT) { - constructor(private readonly jwtValidationAdapter: JwtValidationAdapter) { + constructor( + private readonly jwtValidationAdapter: JwtValidationAdapter, + configService: ConfigService + ) { + const publicKey = configService.get('JWT_PUBLIC_KEY'); + super({ jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), ignoreExpiration: false, - secretOrKey: authConfig.publicKey, - ...authConfig.jwtOptions, + secretOrKey: publicKey, }); } From 8f49fe297f791654fbb26c0775a52a442bddd883 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 18 Oct 2024 16:10:11 +0200 Subject: [PATCH 13/42] Remove unnecessary props in AuthGuardConfig --- apps/server/src/infra/auth-guard/auth-guard.config.ts | 2 -- apps/server/src/modules/h5p-editor/h5p-editor.config.ts | 2 -- apps/server/src/modules/server/admin-api-server.config.ts | 2 -- 3 files changed, 6 deletions(-) diff --git a/apps/server/src/infra/auth-guard/auth-guard.config.ts b/apps/server/src/infra/auth-guard/auth-guard.config.ts index 7ec81d5e2a0..a3c048313cd 100644 --- a/apps/server/src/infra/auth-guard/auth-guard.config.ts +++ b/apps/server/src/infra/auth-guard/auth-guard.config.ts @@ -1,6 +1,4 @@ export interface AuthGuardConfig { ADMIN_API__ALLOWED_API_KEYS: string[]; - JWT_AUD: string; - JWT_LIFETIME: string; JWT_PUBLIC_KEY: string; } diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index ebf25139ba8..8be8b022135 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -20,8 +20,6 @@ const h5pEditorConfig: H5PEditorConfig = { ...authorizationClientConfig, EXIT_ON_ERROR: Configuration.get('EXIT_ON_ERROR') as boolean, ADMIN_API__ALLOWED_API_KEYS: [], - JWT_AUD: Configuration.get('JWT_AUD') as string, - JWT_LIFETIME: Configuration.get('JWT_LIFETIME') as string, JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, }; diff --git a/apps/server/src/modules/server/admin-api-server.config.ts b/apps/server/src/modules/server/admin-api-server.config.ts index a6d6c289ab0..74a4ae696f6 100644 --- a/apps/server/src/modules/server/admin-api-server.config.ts +++ b/apps/server/src/modules/server/admin-api-server.config.ts @@ -56,8 +56,6 @@ const config: AdminApiServerConfig = { ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string) .split(',') .map((part) => (part.split(':').pop() ?? '').trim()), - JWT_AUD: Configuration.get('JWT_AUD') as string, - JWT_LIFETIME: Configuration.get('JWT_LIFETIME') as string, JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, LOGIN_BLOCK_TIME: 0, TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE: Configuration.get( From 6bbb8b1698f6b9b81c5b9212d06f2d1c5b6280eb Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 18 Oct 2024 16:10:38 +0200 Subject: [PATCH 14/42] Add AuthGuardConfig to files-storage --- .../src/modules/files-storage/files-storage.config.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apps/server/src/modules/files-storage/files-storage.config.ts b/apps/server/src/modules/files-storage/files-storage.config.ts index a5e90d90b3e..b9ebb83536e 100644 --- a/apps/server/src/modules/files-storage/files-storage.config.ts +++ b/apps/server/src/modules/files-storage/files-storage.config.ts @@ -2,9 +2,10 @@ import { Configuration } from '@hpi-schul-cloud/commons'; import { AuthorizationClientConfig } from '@infra/authorization-client'; import { S3Config } from '@infra/s3-client'; import { CoreModuleConfig } from '@src/core'; +import { AuthGuardConfig } from '@src/infra/auth-guard'; export const FILES_STORAGE_S3_CONNECTION = 'FILES_STORAGE_S3_CONNECTION'; -export interface FileStorageConfig extends CoreModuleConfig, AuthorizationClientConfig { +export interface FileStorageConfig extends CoreModuleConfig, AuthorizationClientConfig, AuthGuardConfig { MAX_FILE_SIZE: number; MAX_SECURITY_CHECK_FILE_SIZE: number; USE_STREAM_TO_ANTIVIRUS: boolean; @@ -19,12 +20,18 @@ export const authorizationClientConfig: AuthorizationClientConfig = { basePath: `${Configuration.get('API_HOST') as string}/v3/`, }; +const authGuardConfig: AuthGuardConfig = { + ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string).split(','), + JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, +}; + const fileStorageConfig: FileStorageConfig = { MAX_FILE_SIZE: Configuration.get('FILES_STORAGE__MAX_FILE_SIZE') as number, MAX_SECURITY_CHECK_FILE_SIZE: Configuration.get('FILES_STORAGE__MAX_FILE_SIZE') as number, USE_STREAM_TO_ANTIVIRUS: Configuration.get('FILES_STORAGE__USE_STREAM_TO_ANTIVIRUS') as boolean, ...authorizationClientConfig, ...defaultConfig, + ...authGuardConfig, EXIT_ON_ERROR: Configuration.get('EXIT_ON_ERROR') as boolean, }; From 75daefffffeb0edfceb543f7abc5856b7c578981 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 21 Oct 2024 11:14:44 +0200 Subject: [PATCH 15/42] Change setting of options in authn service --- .../services/authentication.service.ts | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/apps/server/src/modules/authentication/services/authentication.service.ts b/apps/server/src/modules/authentication/services/authentication.service.ts index dd1864c575c..69336942b7a 100644 --- a/apps/server/src/modules/authentication/services/authentication.service.ts +++ b/apps/server/src/modules/authentication/services/authentication.service.ts @@ -2,7 +2,7 @@ import { CreateJwtPayload, ICurrentUser, JwtPayloadFactory } from '@infra/auth-g import { Account, AccountService } from '@modules/account'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; -import { JwtService } from '@nestjs/jwt'; +import { JwtService, JwtSignOptions } from '@nestjs/jwt'; import { User } from '@shared/domain/entity'; import { Logger } from '@src/core/logger'; import { randomUUID } from 'crypto'; @@ -46,13 +46,22 @@ export class AuthenticationService { return account; } - private async generateJwt(createJwtPayload: CreateJwtPayload, expiresIn?: number | string): Promise { + private async generateJwtAndAddToWhitelist( + createJwtPayload: CreateJwtPayload, + expiresIn?: number | string + ): Promise { const jti = randomUUID(); - const accessToken = this.jwtService.sign(createJwtPayload, { + const options: JwtSignOptions = { subject: createJwtPayload.accountId, jwtid: jti, - expiresIn, - }); + }; + + // It is necessary to set expiresIn conditionally like this, because setting it to undefined in the JwtSignOptions overwrites the value from the JwtModuleOptions. + if (expiresIn) { + options.expiresIn = expiresIn; + } + + const accessToken = this.jwtService.sign(createJwtPayload, options); await this.jwtWhitelistAdapter.addToWhitelist(createJwtPayload.accountId, jti); @@ -61,8 +70,7 @@ export class AuthenticationService { public async generateCurrentUserJwt(currentUser: ICurrentUser): Promise { const createJwtPayload = JwtPayloadFactory.buildFromCurrentUser(currentUser); - const expiresIn = this.configService.get('JWT_LIFETIME'); - const jwtToken = await this.generateJwt(createJwtPayload, expiresIn); + const jwtToken = await this.generateJwtAndAddToWhitelist(createJwtPayload); return jwtToken; } @@ -78,7 +86,7 @@ export class AuthenticationService { const createJwtPayload = JwtPayloadFactory.buildFromSupportUser(currentUser, supportUser.id); const expiresIn = this.configService.get('JWT_LIFETIME_SUPPORT_SECONDS'); - const jwtToken = await this.generateJwt(createJwtPayload, expiresIn); + const jwtToken = await this.generateJwtAndAddToWhitelist(createJwtPayload, expiresIn); this.logger.info(new ShdUserCreateTokenLoggable(supportUser.id, targetUser.id, expiresIn)); From b665f02f98d350daaa1cffceed54942a076ff8c7 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 21 Oct 2024 11:14:57 +0200 Subject: [PATCH 16/42] Add authn test modules --- .../authentication-api-test.module.ts | 12 +++ .../authentication-test.module.ts | 75 +++++++++++++++++++ .../h5p-editor/h5p-editor-test.module.ts | 4 +- 3 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 apps/server/src/modules/authentication/authentication-api-test.module.ts create mode 100644 apps/server/src/modules/authentication/authentication-test.module.ts diff --git a/apps/server/src/modules/authentication/authentication-api-test.module.ts b/apps/server/src/modules/authentication/authentication-api-test.module.ts new file mode 100644 index 00000000000..a83a4f8825c --- /dev/null +++ b/apps/server/src/modules/authentication/authentication-api-test.module.ts @@ -0,0 +1,12 @@ +import { Module } from '@nestjs/common'; +import { AuthenticationTestModule } from './authentication-test.module'; +import { LoginController, LogoutController } from './controllers'; +import { LoginUc, LogoutUc } from './uc'; + +// This module is for use in api tests of other apps than the core server. +@Module({ + imports: [AuthenticationTestModule], + providers: [LoginUc, LogoutUc], + controllers: [LoginController, LogoutController], +}) +export class AuthenticationApiTestModule {} diff --git a/apps/server/src/modules/authentication/authentication-test.module.ts b/apps/server/src/modules/authentication/authentication-test.module.ts new file mode 100644 index 00000000000..e2583f88eca --- /dev/null +++ b/apps/server/src/modules/authentication/authentication-test.module.ts @@ -0,0 +1,75 @@ +import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { AuthGuardModule } from '@infra/auth-guard'; +import { CacheWrapperModule } from '@infra/cache'; +import { IdentityManagementModule } from '@infra/identity-management'; +import { AccountModule } from '@modules/account'; +import { OauthModule } from '@modules/oauth/oauth.module'; +import { RoleModule } from '@modules/role'; +import { SystemModule } from '@modules/system'; +import { Module } from '@nestjs/common'; +import { JwtModule } from '@nestjs/jwt'; +import { PassportModule } from '@nestjs/passport'; +import { LegacySchoolRepo, UserRepo } from '@shared/repo'; +import { LoggerModule } from '@src/core/logger'; +import { SignOptions } from 'jsonwebtoken'; +import { JwtWhitelistAdapter } from './helper/jwt-whitelist.adapter'; +import { AuthenticationService } from './services/authentication.service'; +import { LdapService } from './services/ldap.service'; +import { LdapStrategy } from './strategy/ldap.strategy'; +import { LocalStrategy } from './strategy/local.strategy'; +import { Oauth2Strategy } from './strategy/oauth2.strategy'; + +const createJwtOptions = () => { + const algorithm = 'RS256'; + + const signOptions: SignOptions = { + algorithm, + audience: Configuration.get('JWT_AUD') as string, + expiresIn: Configuration.get('JWT_LIFETIME') as string, + issuer: 'feathers', + header: { typ: 'JWT', alg: algorithm }, + }; + + const privateKey = Configuration.get('JWT_PRIVATE_KEY') as string; + const publicKey = Configuration.get('JWT_PUBLIC_KEY') as string; + + const options = { + privateKey, + publicKey, + signOptions, + verifyOptions: signOptions, + }; + + return options; +}; + +// This module is for use by the AuthenticationApiTestModule, i.e. for use in api tests of other apps than the core server. +// In those tests the configService can't be used, because the core server's config is not available. +@Module({ + imports: [ + LoggerModule, + PassportModule, + JwtModule.registerAsync({ + useFactory: createJwtOptions, + }), + AccountModule, + SystemModule, + OauthModule, + RoleModule, + IdentityManagementModule, + CacheWrapperModule, + AuthGuardModule, + ], + providers: [ + UserRepo, + LegacySchoolRepo, + LocalStrategy, + AuthenticationService, + LdapService, + LdapStrategy, + Oauth2Strategy, + JwtWhitelistAdapter, + ], + exports: [AuthenticationService], +}) +export class AuthenticationTestModule {} diff --git a/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts b/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts index 0248552c576..d64eeba53eb 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor-test.module.ts @@ -2,7 +2,7 @@ import { AuthorizationClientModule } from '@infra/authorization-client'; import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@infra/database'; import { RabbitMQWrapperTestModule } from '@infra/rabbitmq'; import { S3ClientModule } from '@infra/s3-client'; -import { AuthenticationApiModule } from '@modules/authentication/authentication-api.module'; +import { AuthenticationApiTestModule } from '@modules/authentication/authentication-api-test.module'; import { UserModule } from '@modules/user'; import { DynamicModule, Module } from '@nestjs/common'; import { ALL_ENTITIES } from '@shared/domain/entity'; @@ -20,7 +20,7 @@ import { H5PEditorUc } from './uc/h5p.uc'; const imports = [ H5PEditorModule, MongoMemoryDatabaseModule.forRoot({ entities: [...ALL_ENTITIES, H5PContent] }), - AuthenticationApiModule, + AuthenticationApiTestModule, AuthorizationClientModule.register(authorizationClientConfig), UserModule, CoreModule, From f6188ffbe212a6df1ffa019c083edb0d32ec294a Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 21 Oct 2024 11:44:10 +0200 Subject: [PATCH 17/42] Fix authn service test --- .../authentication/services/authentication.service.spec.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/server/src/modules/authentication/services/authentication.service.spec.ts b/apps/server/src/modules/authentication/services/authentication.service.spec.ts index 31db4ac0e28..786e71420d2 100644 --- a/apps/server/src/modules/authentication/services/authentication.service.spec.ts +++ b/apps/server/src/modules/authentication/services/authentication.service.spec.ts @@ -185,14 +185,13 @@ describe('AuthenticationService', () => { }; it('should pass the correct parameters', async () => { - const { mockCurrentUser, expectedPayload, expiresIn } = setup(); + const { mockCurrentUser, expectedPayload } = setup(); await authenticationService.generateCurrentUserJwt(mockCurrentUser); expect(jwtService.sign).toBeCalledWith( expectedPayload, expect.objectContaining({ subject: mockCurrentUser.accountId, jwtid: expect.any(String), - expiresIn, }) ); }); From 41fe72f604be7a4869764e0b8fafc62d3ef99240 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 21 Oct 2024 12:27:15 +0200 Subject: [PATCH 18/42] Change iss and aud of JWT --- .../src/modules/authentication/authentication-config.ts | 2 +- .../modules/authentication/authentication-test.module.ts | 4 ++-- .../src/modules/authentication/authentication.module.ts | 4 ++-- apps/server/src/modules/server/server.config.ts | 1 - config/default.schema.json | 5 ----- src/services/authentication/configuration.js | 6 +++--- 6 files changed, 8 insertions(+), 14 deletions(-) diff --git a/apps/server/src/modules/authentication/authentication-config.ts b/apps/server/src/modules/authentication/authentication-config.ts index d14ebd3c17d..c346416fa1a 100644 --- a/apps/server/src/modules/authentication/authentication-config.ts +++ b/apps/server/src/modules/authentication/authentication-config.ts @@ -10,6 +10,6 @@ export interface AuthenticationConfig extends AccountConfig, XApiKeyConfig { JWT_TIMEOUT_SECONDS: number; JWT_LIFETIME_SUPPORT_SECONDS: number; JWT_EXTENDED_TIMEOUT_SECONDS: number; - JWT_AUD: string; + SC_DOMAIN: string; LOGIN_BLOCK_TIME: number; } diff --git a/apps/server/src/modules/authentication/authentication-test.module.ts b/apps/server/src/modules/authentication/authentication-test.module.ts index e2583f88eca..63a197cddc5 100644 --- a/apps/server/src/modules/authentication/authentication-test.module.ts +++ b/apps/server/src/modules/authentication/authentication-test.module.ts @@ -24,9 +24,9 @@ const createJwtOptions = () => { const signOptions: SignOptions = { algorithm, - audience: Configuration.get('JWT_AUD') as string, expiresIn: Configuration.get('JWT_LIFETIME') as string, - issuer: 'feathers', + issuer: Configuration.get('SC_DOMAIN') as string, + audience: Configuration.get('SC_DOMAIN') as string, header: { typ: 'JWT', alg: algorithm }, }; diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index ab9ad296ab6..777ae6a7c33 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -25,9 +25,9 @@ const createJwtOptions = (configService: ConfigService) => const signOptions: SignOptions = { algorithm, - audience: configService.get('JWT_AUD'), expiresIn: configService.get('JWT_LIFETIME'), - issuer: 'feathers', + issuer: configService.get('SC_DOMAIN'), + audience: configService.get('SC_DOMAIN'), header: { typ: 'JWT', alg: algorithm }, }; diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 8a6b818f0ba..8cff856f6ef 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -188,7 +188,6 @@ const config: ServerConfig = { JWT_TIMEOUT_SECONDS: Configuration.get('JWT_TIMEOUT_SECONDS') as number, JWT_LIFETIME_SUPPORT_SECONDS: Configuration.get('JWT_LIFETIME_SUPPORT_SECONDS') as number, JWT_EXTENDED_TIMEOUT_SECONDS: Configuration.get('JWT_EXTENDED_TIMEOUT_SECONDS') as number, - JWT_AUD: Configuration.get('JWT_AUD') as string, // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node diff --git a/config/default.schema.json b/config/default.schema.json index 622c8b74eaa..24bc886d624 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -1119,11 +1119,6 @@ "default": false, "description": "Enabled that every request is logged with the information route and method" }, - "JWT_AUD": { - "type": "string", - "default": "https://dbildungscloud.de", - "description": "It is the organisation web point that is created the jwt and where questions can addressed." - }, "SUPPORT_PROBLEM_EMAIL_ADDRESS": { "type": "string", "default": "ticketsystem@dbildungscloud.de", diff --git a/src/services/authentication/configuration.js b/src/services/authentication/configuration.js index a23c9b39c61..266680cf2b8 100644 --- a/src/services/authentication/configuration.js +++ b/src/services/authentication/configuration.js @@ -1,7 +1,7 @@ const { Configuration } = require('@hpi-schul-cloud/commons'); module.exports = { - audience: Configuration.get('JWT_AUD'), + audience: Configuration.get('SC_DOMAIN'), authConfig: { entity: 'account', // name of the found user in the request context // entityId and service are never queried, but need to be provided otherwise the server doesn't start @@ -16,8 +16,8 @@ module.exports = { authStrategies: ['jwt', 'tsp', 'api-key'], jwtOptions: { header: { typ: 'JWT' }, - audience: Configuration.get('JWT_AUD'), - issuer: 'feathers', + audience: Configuration.get('SC_DOMAIN'), + issuer: Configuration.get('SC_DOMAIN'), algorithm: 'RS256', expiresIn: Configuration.get('JWT_LIFETIME'), }, From 6eb471bc7155828fe8e35e116320b94f67650a9b Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 21 Oct 2024 12:40:48 +0200 Subject: [PATCH 19/42] Fix reading of config for other apps --- .../server/src/modules/files-storage/files-storage.config.ts | 5 ++++- apps/server/src/modules/h5p-editor/h5p-editor.config.ts | 5 ++++- apps/server/src/modules/server/admin-api-server.config.ts | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/files-storage/files-storage.config.ts b/apps/server/src/modules/files-storage/files-storage.config.ts index b9ebb83536e..41983f90d0e 100644 --- a/apps/server/src/modules/files-storage/files-storage.config.ts +++ b/apps/server/src/modules/files-storage/files-storage.config.ts @@ -22,7 +22,10 @@ export const authorizationClientConfig: AuthorizationClientConfig = { const authGuardConfig: AuthGuardConfig = { ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string).split(','), - JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), }; const fileStorageConfig: FileStorageConfig = { diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index 8be8b022135..92e30a6d6b4 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -20,7 +20,10 @@ const h5pEditorConfig: H5PEditorConfig = { ...authorizationClientConfig, EXIT_ON_ERROR: Configuration.get('EXIT_ON_ERROR') as boolean, ADMIN_API__ALLOWED_API_KEYS: [], - JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), }; export const translatorConfig = { diff --git a/apps/server/src/modules/server/admin-api-server.config.ts b/apps/server/src/modules/server/admin-api-server.config.ts index 74a4ae696f6..b4b968eb676 100644 --- a/apps/server/src/modules/server/admin-api-server.config.ts +++ b/apps/server/src/modules/server/admin-api-server.config.ts @@ -56,7 +56,10 @@ const config: AdminApiServerConfig = { ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string) .split(',') .map((part) => (part.split(':').pop() ?? '').trim()), - JWT_PUBLIC_KEY: Configuration.get('JWT_PUBLIC_KEY') as string, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), LOGIN_BLOCK_TIME: 0, TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE: Configuration.get( 'TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE' From 9e82a8c73dfea53cc6264677ecad875f002aa147 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Tue, 22 Oct 2024 13:44:47 +0200 Subject: [PATCH 20/42] Add XApiKeyDecorator --- apps/server/src/infra/auth-guard/decorator/index.ts | 1 + .../src/infra/auth-guard/decorator/x-api-key.decorator.ts | 8 ++++++++ .../src/infra/auth-guard/guard/x-api-key-auth.guard.ts | 2 +- apps/server/src/infra/auth-guard/index.ts | 6 +++--- .../controller/api-test/deletion-executions.api.spec.ts | 4 ++-- .../api-test/deletion-request-create.api.spec.ts | 4 ++-- .../api-test/deletion-request-delete.api.spec.ts | 4 ++-- .../controller/api-test/deletion-request-find.api.spec.ts | 4 ++-- .../api/controller/deletion-executions.controller.ts | 6 +++--- .../api/controller/deletion-requests.controller.ts | 6 +++--- .../controller/admin-api-schools.controller.ts | 6 +++--- .../controller/api-test/school.administration.api.spec.ts | 4 ++-- .../api/admin-api-registration-pin.controller.ts | 6 +++--- .../controller/api-test/tldraw.controller.api.spec.ts | 4 ++-- .../src/modules/tldraw/controller/tldraw.controller.ts | 6 +++--- .../admin-api-context-external-tool.controller.ts | 6 +++--- .../controller/admin-api-external-tool.controller.ts | 6 +++--- .../admin-api-school-external-tool.controller.ts | 6 +++--- .../modules/user/controller/admin-api-user.controller.ts | 6 +++--- .../user/controller/api-test/admin-api-user.api.spec.ts | 4 ++-- apps/server/src/shared/testing/test-api-client.spec.ts | 4 ++-- 21 files changed, 56 insertions(+), 47 deletions(-) create mode 100644 apps/server/src/infra/auth-guard/decorator/x-api-key.decorator.ts diff --git a/apps/server/src/infra/auth-guard/decorator/index.ts b/apps/server/src/infra/auth-guard/decorator/index.ts index 73ec25ffece..235741e5e93 100644 --- a/apps/server/src/infra/auth-guard/decorator/index.ts +++ b/apps/server/src/infra/auth-guard/decorator/index.ts @@ -1 +1,2 @@ export * from './jwt-auth.decorator'; +export * from './x-api-key.decorator'; diff --git a/apps/server/src/infra/auth-guard/decorator/x-api-key.decorator.ts b/apps/server/src/infra/auth-guard/decorator/x-api-key.decorator.ts new file mode 100644 index 00000000000..b40ba777b4a --- /dev/null +++ b/apps/server/src/infra/auth-guard/decorator/x-api-key.decorator.ts @@ -0,0 +1,8 @@ +import { applyDecorators, UseGuards } from '@nestjs/common'; +import { XApiKeyGuard } from '../guard'; + +export const XApiKeyAuthentication = () => { + const decorator = applyDecorators(UseGuards(XApiKeyGuard)); + + return decorator; +}; diff --git a/apps/server/src/infra/auth-guard/guard/x-api-key-auth.guard.ts b/apps/server/src/infra/auth-guard/guard/x-api-key-auth.guard.ts index beafd01d1b1..05287f83d17 100644 --- a/apps/server/src/infra/auth-guard/guard/x-api-key-auth.guard.ts +++ b/apps/server/src/infra/auth-guard/guard/x-api-key-auth.guard.ts @@ -3,4 +3,4 @@ import { AuthGuard } from '@nestjs/passport'; import { StrategyType } from '../interface'; @Injectable() -export class ApiKeyGuard extends AuthGuard(StrategyType.API_KEY) {} +export class XApiKeyGuard extends AuthGuard(StrategyType.API_KEY) {} diff --git a/apps/server/src/infra/auth-guard/index.ts b/apps/server/src/infra/auth-guard/index.ts index 92fa2f36347..f07015b707c 100644 --- a/apps/server/src/infra/auth-guard/index.ts +++ b/apps/server/src/infra/auth-guard/index.ts @@ -1,10 +1,10 @@ export { JwtValidationAdapter } from './adapter'; -export { AuthGuardModule } from './auth-guard.module'; export { AuthGuardConfig } from './auth-guard.config'; +export { AuthGuardModule } from './auth-guard.module'; export { XApiKeyConfig } from './config'; -export { CurrentUser, JWT, JwtAuthentication } from './decorator'; +export { CurrentUser, JWT, JwtAuthentication, XApiKeyAuthentication } from './decorator'; // JwtAuthGuard only exported because api tests still overried this guard. // Use JwtAuthentication decorator for request validation -export { ApiKeyGuard, JwtAuthGuard, WsJwtAuthGuard } from './guard'; +export { JwtAuthGuard, WsJwtAuthGuard, XApiKeyGuard } from './guard'; export { CreateJwtPayload, ICurrentUser, JwtPayload, StrategyType } from './interface'; export { CurrentUserBuilder, JwtPayloadFactory } from './mapper'; diff --git a/apps/server/src/modules/deletion/api/controller/api-test/deletion-executions.api.spec.ts b/apps/server/src/modules/deletion/api/controller/api-test/deletion-executions.api.spec.ts index 75a9611a087..7dbfa9a5f9d 100644 --- a/apps/server/src/modules/deletion/api/controller/api-test/deletion-executions.api.spec.ts +++ b/apps/server/src/modules/deletion/api/controller/api-test/deletion-executions.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; @@ -15,7 +15,7 @@ describe(`deletionExecution (api)`, () => { const module: TestingModule = await Test.createTestingModule({ imports: [AdminApiServerTestModule], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-create.api.spec.ts b/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-create.api.spec.ts index 3a75d47f6b5..bf045d7f868 100644 --- a/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-create.api.spec.ts +++ b/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-create.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { ExecutionContext, INestApplication } from '@nestjs/common'; @@ -61,7 +61,7 @@ describe(`deletionRequest create (api)`, () => { const module: TestingModule = await Test.createTestingModule({ imports: [AdminApiServerTestModule], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-delete.api.spec.ts b/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-delete.api.spec.ts index 399df974353..bb7665eabd2 100644 --- a/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-delete.api.spec.ts +++ b/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-delete.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { ExecutionContext, INestApplication } from '@nestjs/common'; @@ -20,7 +20,7 @@ describe(`deletionRequest delete (api)`, () => { const module: TestingModule = await Test.createTestingModule({ imports: [AdminApiServerTestModule], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-find.api.spec.ts b/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-find.api.spec.ts index 476340743d7..298021a2a07 100644 --- a/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-find.api.spec.ts +++ b/apps/server/src/modules/deletion/api/controller/api-test/deletion-request-find.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { AdminApiServerTestModule } from '@modules/server/admin-api.server.module'; import { ExecutionContext, INestApplication } from '@nestjs/common'; @@ -20,7 +20,7 @@ describe(`deletionRequest find (api)`, () => { const module: TestingModule = await Test.createTestingModule({ imports: [AdminApiServerTestModule], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/modules/deletion/api/controller/deletion-executions.controller.ts b/apps/server/src/modules/deletion/api/controller/deletion-executions.controller.ts index e1ac9e1a500..4fad5858ccf 100644 --- a/apps/server/src/modules/deletion/api/controller/deletion-executions.controller.ts +++ b/apps/server/src/modules/deletion/api/controller/deletion-executions.controller.ts @@ -1,11 +1,11 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Controller, HttpCode, Post, Query, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Controller, HttpCode, Post, Query } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { DeletionRequestUc } from '../uc'; import { DeletionExecutionParams } from './dto'; @ApiTags('DeletionExecutions') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('deletionExecutions') export class DeletionExecutionsController { constructor(private readonly deletionRequestUc: DeletionRequestUc) {} diff --git a/apps/server/src/modules/deletion/api/controller/deletion-requests.controller.ts b/apps/server/src/modules/deletion/api/controller/deletion-requests.controller.ts index f47cbc5d1f6..daf3aca4ccd 100644 --- a/apps/server/src/modules/deletion/api/controller/deletion-requests.controller.ts +++ b/apps/server/src/modules/deletion/api/controller/deletion-requests.controller.ts @@ -1,11 +1,11 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Body, Controller, Delete, Get, HttpCode, Param, Post, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Body, Controller, Delete, Get, HttpCode, Param, Post } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { DeletionRequestUc } from '../uc'; import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from './dto'; @ApiTags('DeletionRequests') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('deletionRequests') export class DeletionRequestsController { constructor(private readonly deletionRequestUc: DeletionRequestUc) {} diff --git a/apps/server/src/modules/legacy-school/controller/admin-api-schools.controller.ts b/apps/server/src/modules/legacy-school/controller/admin-api-schools.controller.ts index 5fbbfce8ace..05809485a60 100644 --- a/apps/server/src/modules/legacy-school/controller/admin-api-schools.controller.ts +++ b/apps/server/src/modules/legacy-school/controller/admin-api-schools.controller.ts @@ -1,5 +1,5 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { AdminApiSchoolUc } from '../uc/admin-api-schools.uc'; import { AdminApiSchoolMapper } from './admin-api-schools.mapper'; @@ -7,7 +7,7 @@ import { AdminApiSchoolCreateBodyParams } from './dto/request/admin-api-school-c import { AdminApiSchoolCreateResponseDto } from './dto/response/admin-api-school-create.response.dto'; @ApiTags('AdminSchool') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('admin/schools') export class AdminApiSchoolsController { constructor(private readonly uc: AdminApiSchoolUc) {} diff --git a/apps/server/src/modules/legacy-school/controller/api-test/school.administration.api.spec.ts b/apps/server/src/modules/legacy-school/controller/api-test/school.administration.api.spec.ts index 402b69907af..9fe7319cab6 100644 --- a/apps/server/src/modules/legacy-school/controller/api-test/school.administration.api.spec.ts +++ b/apps/server/src/modules/legacy-school/controller/api-test/school.administration.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; @@ -19,7 +19,7 @@ describe('Admin API - Schools (API)', () => { const module: TestingModule = await Test.createTestingModule({ imports: [AdminApiServerTestModule], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/modules/registration-pin/api/admin-api-registration-pin.controller.ts b/apps/server/src/modules/registration-pin/api/admin-api-registration-pin.controller.ts index 6abb0ff6d7c..c69966373fb 100644 --- a/apps/server/src/modules/registration-pin/api/admin-api-registration-pin.controller.ts +++ b/apps/server/src/modules/registration-pin/api/admin-api-registration-pin.controller.ts @@ -1,11 +1,11 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Controller, Get, Param, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Controller, Get, Param } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; import { GetRegistrationPinResponse } from './dto'; import { RegistrationPinUc } from './registration-pin.uc'; @ApiTags('AdminRegistrationPin') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('registration-pin') export class AdminApiRegistrationPinController { constructor(private readonly uc: RegistrationPinUc) {} diff --git a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts index 2dde66d1b75..37cb1595aef 100644 --- a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts +++ b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { ServerTestModule } from '@modules/server'; import { ExecutionContext, INestApplication } from '@nestjs/common'; @@ -24,7 +24,7 @@ describe('tldraw controller (api)', () => { controllers: [TldrawController], providers: [Logger, TldrawService, TldrawRepo], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/modules/tldraw/controller/tldraw.controller.ts b/apps/server/src/modules/tldraw/controller/tldraw.controller.ts index dd32f289c2f..a7f5ed2bbc5 100644 --- a/apps/server/src/modules/tldraw/controller/tldraw.controller.ts +++ b/apps/server/src/modules/tldraw/controller/tldraw.controller.ts @@ -1,12 +1,12 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Controller, Delete, ForbiddenException, HttpCode, NotFoundException, Param, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Controller, Delete, ForbiddenException, HttpCode, NotFoundException, Param } from '@nestjs/common'; import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger'; import { ApiValidationError } from '@shared/common'; import { TldrawService } from '../service'; import { TldrawDeleteParams } from './tldraw.params'; @ApiTags('Tldraw Document') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('tldraw-document') export class TldrawController { constructor(private readonly tldrawService: TldrawService) {} diff --git a/apps/server/src/modules/tool/context-external-tool/controller/admin-api-context-external-tool.controller.ts b/apps/server/src/modules/tool/context-external-tool/controller/admin-api-context-external-tool.controller.ts index 580bebd8f3b..d44ddb4f2b1 100644 --- a/apps/server/src/modules/tool/context-external-tool/controller/admin-api-context-external-tool.controller.ts +++ b/apps/server/src/modules/tool/context-external-tool/controller/admin-api-context-external-tool.controller.ts @@ -1,12 +1,12 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { ContextExternalToolRequestMapper, ContextExternalToolResponseMapper } from '../mapper'; import { AdminApiContextExternalToolUc } from '../uc'; import { ContextExternalToolPostParams, ContextExternalToolResponse } from './dto'; @ApiTags('AdminApi: Context External Tool') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('admin/tools/context-external-tools') export class AdminApiContextExternalToolController { constructor(private readonly adminApiContextExternalToolUc: AdminApiContextExternalToolUc) {} diff --git a/apps/server/src/modules/tool/external-tool/controller/admin-api-external-tool.controller.ts b/apps/server/src/modules/tool/external-tool/controller/admin-api-external-tool.controller.ts index 883b2d2cf42..6e375de1f47 100644 --- a/apps/server/src/modules/tool/external-tool/controller/admin-api-external-tool.controller.ts +++ b/apps/server/src/modules/tool/external-tool/controller/admin-api-external-tool.controller.ts @@ -1,12 +1,12 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { ExternalToolRequestMapper, ExternalToolResponseMapper } from '../mapper'; import { AdminApiExternalToolUc } from '../uc'; import { ExternalToolCreateParams, ExternalToolResponse } from './dto'; @ApiTags('AdminApi: External Tools') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('admin/tools/external-tools') export class AdminApiExternalToolController { constructor( diff --git a/apps/server/src/modules/tool/school-external-tool/controller/admin-api-school-external-tool.controller.ts b/apps/server/src/modules/tool/school-external-tool/controller/admin-api-school-external-tool.controller.ts index 05e29a43d3d..2542c3c5af4 100644 --- a/apps/server/src/modules/tool/school-external-tool/controller/admin-api-school-external-tool.controller.ts +++ b/apps/server/src/modules/tool/school-external-tool/controller/admin-api-school-external-tool.controller.ts @@ -1,5 +1,5 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { SchoolExternalTool, SchoolExternalToolProps } from '../domain'; import { SchoolExternalToolRequestMapper, SchoolExternalToolResponseMapper } from '../mapper'; @@ -7,7 +7,7 @@ import { AdminApiSchoolExternalToolUc } from '../uc'; import { SchoolExternalToolPostParams, SchoolExternalToolResponse } from './dto'; @ApiTags('AdminApi: School External Tool') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('admin/tools/school-external-tools') export class AdminApiSchoolExternalToolController { constructor(private readonly adminApiSchoolExternalToolUc: AdminApiSchoolExternalToolUc) {} diff --git a/apps/server/src/modules/user/controller/admin-api-user.controller.ts b/apps/server/src/modules/user/controller/admin-api-user.controller.ts index 4268172e220..bd7aaafb785 100644 --- a/apps/server/src/modules/user/controller/admin-api-user.controller.ts +++ b/apps/server/src/modules/user/controller/admin-api-user.controller.ts @@ -1,12 +1,12 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; -import { Body, Controller, Post, UseGuards } from '@nestjs/common'; +import { XApiKeyAuthentication } from '@infra/auth-guard'; +import { Body, Controller, Post } from '@nestjs/common'; import { ApiOperation, ApiTags } from '@nestjs/swagger'; import { AdminApiUserUc } from '../uc'; import { AdminApiUserCreateBodyParams } from './dto/admin-api-user-create.body.params'; import { AdminApiUserCreateResponse } from './dto/admin-api-user-create.response.dto'; @ApiTags('AdminApiUsers') -@UseGuards(ApiKeyGuard) +@XApiKeyAuthentication() @Controller('/admin/users') export class AdminApiUsersController { constructor(private readonly uc: AdminApiUserUc) {} diff --git a/apps/server/src/modules/user/controller/api-test/admin-api-user.api.spec.ts b/apps/server/src/modules/user/controller/api-test/admin-api-user.api.spec.ts index dff8169da9c..3b89aa1eb06 100644 --- a/apps/server/src/modules/user/controller/api-test/admin-api-user.api.spec.ts +++ b/apps/server/src/modules/user/controller/api-test/admin-api-user.api.spec.ts @@ -1,4 +1,4 @@ -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; @@ -22,7 +22,7 @@ describe('Admin API - Users (API)', () => { const module: TestingModule = await Test.createTestingModule({ imports: [AdminApiServerTestModule], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); diff --git a/apps/server/src/shared/testing/test-api-client.spec.ts b/apps/server/src/shared/testing/test-api-client.spec.ts index e69bc683894..553aecbe1d8 100644 --- a/apps/server/src/shared/testing/test-api-client.spec.ts +++ b/apps/server/src/shared/testing/test-api-client.spec.ts @@ -14,7 +14,7 @@ import { } from '@nestjs/common'; import { Test } from '@nestjs/testing'; -import { ApiKeyGuard } from '@infra/auth-guard'; +import { XApiKeyGuard } from '@infra/auth-guard'; import { accountFactory } from '@src/modules/account/testing'; import { TestApiClient } from './test-api-client'; @@ -262,7 +262,7 @@ describe(TestApiClient.name, () => { const moduleFixture = await Test.createTestingModule({ controllers: [TestXApiKeyController], }) - .overrideGuard(ApiKeyGuard) + .overrideGuard(XApiKeyGuard) .useValue({ canActivate(context: ExecutionContext) { const req: Request = context.switchToHttp().getRequest(); From 9b81974a03b413eff1c35232e5fed146f56c34e8 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Tue, 22 Oct 2024 14:44:24 +0200 Subject: [PATCH 21/42] Add WSJwtAuthenticationDecorator --- apps/server/src/infra/auth-guard/decorator/index.ts | 1 + .../infra/auth-guard/decorator/ws-jwt-auth.decorator.ts | 8 ++++++++ apps/server/src/infra/auth-guard/index.ts | 2 +- .../modules/board/gateway/board-collaboration.gateway.ts | 6 +++--- 4 files changed, 13 insertions(+), 4 deletions(-) create mode 100644 apps/server/src/infra/auth-guard/decorator/ws-jwt-auth.decorator.ts diff --git a/apps/server/src/infra/auth-guard/decorator/index.ts b/apps/server/src/infra/auth-guard/decorator/index.ts index 235741e5e93..b33c36b11ff 100644 --- a/apps/server/src/infra/auth-guard/decorator/index.ts +++ b/apps/server/src/infra/auth-guard/decorator/index.ts @@ -1,2 +1,3 @@ export * from './jwt-auth.decorator'; +export * from './ws-jwt-auth.decorator'; export * from './x-api-key.decorator'; diff --git a/apps/server/src/infra/auth-guard/decorator/ws-jwt-auth.decorator.ts b/apps/server/src/infra/auth-guard/decorator/ws-jwt-auth.decorator.ts new file mode 100644 index 00000000000..486986c3664 --- /dev/null +++ b/apps/server/src/infra/auth-guard/decorator/ws-jwt-auth.decorator.ts @@ -0,0 +1,8 @@ +import { applyDecorators, UseGuards } from '@nestjs/common'; +import { WsJwtAuthGuard } from '../guard'; + +export const WsJwtAuthentication = () => { + const decorator = applyDecorators(UseGuards(WsJwtAuthGuard)); + + return decorator; +}; diff --git a/apps/server/src/infra/auth-guard/index.ts b/apps/server/src/infra/auth-guard/index.ts index f07015b707c..44ae59c3b51 100644 --- a/apps/server/src/infra/auth-guard/index.ts +++ b/apps/server/src/infra/auth-guard/index.ts @@ -2,7 +2,7 @@ export { JwtValidationAdapter } from './adapter'; export { AuthGuardConfig } from './auth-guard.config'; export { AuthGuardModule } from './auth-guard.module'; export { XApiKeyConfig } from './config'; -export { CurrentUser, JWT, JwtAuthentication, XApiKeyAuthentication } from './decorator'; +export { CurrentUser, JWT, JwtAuthentication, WsJwtAuthentication, XApiKeyAuthentication } from './decorator'; // JwtAuthGuard only exported because api tests still overried this guard. // Use JwtAuthentication decorator for request validation export { JwtAuthGuard, WsJwtAuthGuard, XApiKeyGuard } from './guard'; diff --git a/apps/server/src/modules/board/gateway/board-collaboration.gateway.ts b/apps/server/src/modules/board/gateway/board-collaboration.gateway.ts index 10d5c3cc287..ca0c00aecbc 100644 --- a/apps/server/src/modules/board/gateway/board-collaboration.gateway.ts +++ b/apps/server/src/modules/board/gateway/board-collaboration.gateway.ts @@ -1,7 +1,7 @@ -import { WsJwtAuthGuard } from '@infra/auth-guard'; +import { WsJwtAuthentication } from '@infra/auth-guard'; import { Socket, WsValidationPipe } from '@infra/socketio'; import { MikroORM, UseRequestContext } from '@mikro-orm/core'; -import { UseGuards, UsePipes } from '@nestjs/common'; +import { UsePipes } from '@nestjs/common'; import { OnGatewayDisconnect, SubscribeMessage, @@ -43,7 +43,7 @@ import { UpdateContentElementMessageParams } from './dto/update-content-element. @UsePipes(new WsValidationPipe()) @WebSocketGateway(BoardCollaborationConfiguration.websocket) -@UseGuards(WsJwtAuthGuard) +@WsJwtAuthentication() export class BoardCollaborationGateway implements OnGatewayDisconnect { @WebSocketServer() server!: Server; From ffc33be34f5419d24604a155851dab347996bed3 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Tue, 22 Oct 2024 16:04:20 +0200 Subject: [PATCH 22/42] Change AuthGuardModule to dynamic module --- .../src/infra/auth-guard/auth-guard.module.ts | 33 +++++++++++++++---- apps/server/src/infra/auth-guard/index.ts | 2 +- .../authentication-test.module.ts | 4 +-- .../authentication/authentication.module.ts | 4 +-- .../board/board-collaboration.module.ts | 4 +-- .../files-storage/files-storage-api.module.ts | 4 +-- .../fwu-learning-contents.module.ts | 4 +-- .../modules/h5p-editor/h5p-editor.module.ts | 4 +-- .../modules/server/admin-api.server.module.ts | 6 ++-- apps/server/src/modules/shd/shd.api.module.ts | 9 +++-- .../tldraw.controller.401.api.spec.ts | 11 ++++--- .../src/modules/tldraw/tldraw-api.module.ts | 4 +-- .../src/modules/tldraw/tldraw-ws.module.ts | 2 -- 13 files changed, 57 insertions(+), 34 deletions(-) diff --git a/apps/server/src/infra/auth-guard/auth-guard.module.ts b/apps/server/src/infra/auth-guard/auth-guard.module.ts index 4ba91b4658b..93e818ce303 100644 --- a/apps/server/src/infra/auth-guard/auth-guard.module.ts +++ b/apps/server/src/infra/auth-guard/auth-guard.module.ts @@ -1,11 +1,30 @@ -import { Module } from '@nestjs/common'; +import { DynamicModule, Module, Provider } from '@nestjs/common'; import { PassportModule } from '@nestjs/passport'; import { JwtValidationAdapter } from './adapter'; import { JwtStrategy, WsJwtStrategy, XApiKeyStrategy } from './strategy'; -@Module({ - imports: [PassportModule], - providers: [JwtStrategy, WsJwtStrategy, JwtValidationAdapter, XApiKeyStrategy], - exports: [JwtValidationAdapter], -}) -export class AuthGuardModule {} +export enum AuthGuardOptions { + JWT = 'jwt', + WS_JWT = 'ws-jwt', + X_API_KEY = 'x-api-key', +} + +@Module({}) +export class AuthGuardModule { + static register(options: AuthGuardOptions[]): DynamicModule { + const providers: Provider[] = [JwtValidationAdapter]; + + if (options.includes(AuthGuardOptions.JWT)) providers.push(JwtStrategy); + + if (options.includes(AuthGuardOptions.WS_JWT)) providers.push(WsJwtStrategy); + + if (options.includes(AuthGuardOptions.X_API_KEY)) providers.push(XApiKeyStrategy); + + return { + module: AuthGuardModule, + imports: [PassportModule], + providers, + exports: [JwtValidationAdapter], + }; + } +} diff --git a/apps/server/src/infra/auth-guard/index.ts b/apps/server/src/infra/auth-guard/index.ts index 44ae59c3b51..071f51377fd 100644 --- a/apps/server/src/infra/auth-guard/index.ts +++ b/apps/server/src/infra/auth-guard/index.ts @@ -1,6 +1,6 @@ export { JwtValidationAdapter } from './adapter'; export { AuthGuardConfig } from './auth-guard.config'; -export { AuthGuardModule } from './auth-guard.module'; +export { AuthGuardModule, AuthGuardOptions } from './auth-guard.module'; export { XApiKeyConfig } from './config'; export { CurrentUser, JWT, JwtAuthentication, WsJwtAuthentication, XApiKeyAuthentication } from './decorator'; // JwtAuthGuard only exported because api tests still overried this guard. diff --git a/apps/server/src/modules/authentication/authentication-test.module.ts b/apps/server/src/modules/authentication/authentication-test.module.ts index 63a197cddc5..00384b40067 100644 --- a/apps/server/src/modules/authentication/authentication-test.module.ts +++ b/apps/server/src/modules/authentication/authentication-test.module.ts @@ -1,5 +1,5 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { CacheWrapperModule } from '@infra/cache'; import { IdentityManagementModule } from '@infra/identity-management'; import { AccountModule } from '@modules/account'; @@ -58,7 +58,7 @@ const createJwtOptions = () => { RoleModule, IdentityManagementModule, CacheWrapperModule, - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.JWT]), ], providers: [ UserRepo, diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index 777ae6a7c33..6f79d5f3969 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { CacheWrapperModule } from '@infra/cache'; import { IdentityManagementModule } from '@infra/identity-management'; import { AccountModule } from '@modules/account'; @@ -58,7 +58,7 @@ const createJwtOptions = (configService: ConfigService) => RoleModule, IdentityManagementModule, CacheWrapperModule, - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.JWT, AuthGuardOptions.WS_JWT]), ], providers: [ UserRepo, diff --git a/apps/server/src/modules/board/board-collaboration.module.ts b/apps/server/src/modules/board/board-collaboration.module.ts index 46d19a462e0..4bdd0b0180e 100644 --- a/apps/server/src/modules/board/board-collaboration.module.ts +++ b/apps/server/src/modules/board/board-collaboration.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { MikroOrmModule } from '@mikro-orm/nestjs'; import { defaultMikroOrmOptions } from '@modules/server'; import { Module } from '@nestjs/common'; @@ -28,7 +28,7 @@ import { BoardModule } from './board.module'; BoardModule, AuthorizationModule, BoardWsApiModule, - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.WS_JWT]), ], providers: [], exports: [], diff --git a/apps/server/src/modules/files-storage/files-storage-api.module.ts b/apps/server/src/modules/files-storage/files-storage-api.module.ts index 1bd6407d345..0703231097e 100644 --- a/apps/server/src/modules/files-storage/files-storage-api.module.ts +++ b/apps/server/src/modules/files-storage/files-storage-api.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { AuthorizationClientModule } from '@infra/authorization-client'; import { HttpModule } from '@nestjs/axios'; import { Module } from '@nestjs/common'; @@ -17,7 +17,7 @@ import { FilesStorageUC } from './uc'; CoreModule, HttpModule, ConfigModule.forRoot(createConfigModuleOptions(config)), - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.JWT]), ], controllers: [FilesStorageController, FilesStorageConfigController, FileSecurityController], providers: [FilesStorageUC], diff --git a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.module.ts b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.module.ts index d6736208ecd..01f90028efd 100644 --- a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.module.ts +++ b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { RabbitMQWrapperModule } from '@infra/rabbitmq'; import { S3ClientModule } from '@infra/s3-client'; import { Dictionary, IPrimaryKey } from '@mikro-orm/core'; @@ -43,7 +43,7 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { }), ConfigModule.forRoot(createConfigModuleOptions(config)), S3ClientModule.register([s3Config]), - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.JWT]), ], controllers: [FwuLearningContentsController], providers: [FwuLearningContentsUc], diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.module.ts b/apps/server/src/modules/h5p-editor/h5p-editor.module.ts index 4fd634cdf94..4c2bcc32b00 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.module.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { AuthorizationClientModule } from '@infra/authorization-client'; import { RabbitMQWrapperModule } from '@infra/rabbitmq'; import { S3ClientModule } from '@infra/s3-client'; @@ -43,7 +43,7 @@ const imports = [ }), ConfigModule.forRoot(createConfigModuleOptions(config)), S3ClientModule.register([s3ConfigContent, s3ConfigLibraries]), - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.JWT]), ]; const controllers = [H5PEditorController]; diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 8f380ff96bb..ba6cb22eeec 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -1,5 +1,5 @@ import { Configuration } from '@hpi-schul-cloud/commons'; -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { MikroOrmModule } from '@mikro-orm/nestjs'; import { DeletionApiModule } from '@modules/deletion/deletion-api.module'; import { FileEntity } from '@modules/files/entity'; @@ -16,8 +16,8 @@ import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infr import { EtherpadClientModule } from '@src/infra/etherpad-client'; import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; import { AdminApiRegistrationPinModule } from '../registration-pin/admin-api-registration-pin.module'; -import { defaultMikroOrmOptions } from './server.module'; import { adminApiServerConfig } from './admin-api-server.config'; +import { defaultMikroOrmOptions } from './server.module'; const serverModules = [ ConfigModule.forRoot(createConfigModuleOptions(adminApiServerConfig)), @@ -30,7 +30,7 @@ const serverModules = [ apiKey: Configuration.has('ETHERPAD__API_KEY') ? (Configuration.get('ETHERPAD__API_KEY') as string) : undefined, basePath: Configuration.has('ETHERPAD__URI') ? (Configuration.get('ETHERPAD__URI') as string) : undefined, }), - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.X_API_KEY]), ]; @Module({ diff --git a/apps/server/src/modules/shd/shd.api.module.ts b/apps/server/src/modules/shd/shd.api.module.ts index b6624db1758..65ca833a348 100644 --- a/apps/server/src/modules/shd/shd.api.module.ts +++ b/apps/server/src/modules/shd/shd.api.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { AuthenticationModule } from '@modules/authentication'; import { AuthorizationModule } from '@modules/authorization/authorization.module'; import { InstanceModule } from '@modules/instance'; @@ -6,7 +6,12 @@ import { Module } from '@nestjs/common'; import { ShdController, ShdUc } from './api'; @Module({ - imports: [AuthorizationModule, AuthenticationModule, AuthGuardModule, InstanceModule], + imports: [ + AuthorizationModule, + AuthenticationModule, + AuthGuardModule.register([AuthGuardOptions.JWT]), + InstanceModule, + ], controllers: [ShdController], providers: [ShdUc], }) diff --git a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts index ef78734fba6..791ad810275 100644 --- a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts +++ b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts @@ -1,12 +1,13 @@ -import { INestApplication } from '@nestjs/common'; import { EntityManager } from '@mikro-orm/mongodb'; -import { courseFactory, TestApiClient, UserAndAccountTestFactory } from '@shared/testing'; -import { Test, TestingModule } from '@nestjs/testing'; import { ServerTestModule } from '@modules/server'; +import { INestApplication } from '@nestjs/common'; +import { Test, TestingModule } from '@nestjs/testing'; +import { courseFactory, TestApiClient, UserAndAccountTestFactory } from '@shared/testing'; import { Logger } from '@src/core/logger'; -import { TldrawService } from '../../service'; +import { AuthGuardModule, AuthGuardOptions } from '@src/infra/auth-guard'; import { TldrawController } from '..'; import { TldrawRepo } from '../../repo'; +import { TldrawService } from '../../service'; import { tldrawEntityFactory } from '../../testing'; const baseRouteName = '/tldraw-document'; @@ -18,7 +19,7 @@ describe('tldraw controller (api)', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [ServerTestModule], + imports: [ServerTestModule, AuthGuardModule.register([AuthGuardOptions.X_API_KEY])], controllers: [TldrawController], providers: [Logger, TldrawService, TldrawRepo], }).compile(); diff --git a/apps/server/src/modules/tldraw/tldraw-api.module.ts b/apps/server/src/modules/tldraw/tldraw-api.module.ts index 238a8c605b3..e5d85599267 100644 --- a/apps/server/src/modules/tldraw/tldraw-api.module.ts +++ b/apps/server/src/modules/tldraw/tldraw-api.module.ts @@ -1,4 +1,4 @@ -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { Dictionary, IPrimaryKey } from '@mikro-orm/core'; import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs'; import { Module, NotFoundException } from '@nestjs/common'; @@ -31,7 +31,7 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { entities: [TldrawDrawing], }), ConfigModule.forRoot(createConfigModuleOptions(config)), - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.X_API_KEY]), ], providers: [TldrawService, TldrawBoardRepo, TldrawRepo, YMongodb], controllers: [TldrawController], diff --git a/apps/server/src/modules/tldraw/tldraw-ws.module.ts b/apps/server/src/modules/tldraw/tldraw-ws.module.ts index 6f8702fe4b6..7273c14814c 100644 --- a/apps/server/src/modules/tldraw/tldraw-ws.module.ts +++ b/apps/server/src/modules/tldraw/tldraw-ws.module.ts @@ -1,4 +1,3 @@ -import { AuthGuardModule } from '@infra/auth-guard'; import { Dictionary, IPrimaryKey } from '@mikro-orm/core'; import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs'; import { HttpModule } from '@nestjs/axios'; @@ -35,7 +34,6 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { entities: [TldrawDrawing], }), ConfigModule.forRoot(createConfigModuleOptions(config)), - AuthGuardModule, ], providers: [ TldrawWs, From caae7559f60b566e2ba85fe244d073df28cb36e3 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Wed, 23 Oct 2024 10:50:04 +0200 Subject: [PATCH 23/42] Fix import --- .../controller/api-test/tldraw.controller.401.api.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts index 791ad810275..4a8b0bac325 100644 --- a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts +++ b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts @@ -1,10 +1,10 @@ +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; import { ServerTestModule } from '@modules/server'; import { INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; import { courseFactory, TestApiClient, UserAndAccountTestFactory } from '@shared/testing'; import { Logger } from '@src/core/logger'; -import { AuthGuardModule, AuthGuardOptions } from '@src/infra/auth-guard'; import { TldrawController } from '..'; import { TldrawRepo } from '../../repo'; import { TldrawService } from '../../service'; From f0a1c5ba66da53f5e84631376394cbbbd5f575dc Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Thu, 24 Oct 2024 13:47:46 +0200 Subject: [PATCH 24/42] Only import AuthGuardModul in ServerModul --- apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts | 2 +- .../src/infra/auth-guard/strategy/ws-jwt.strategy.ts | 2 +- .../src/infra/auth-guard/strategy/x-api-key.strategy.ts | 2 +- .../src/modules/authentication/authentication.module.ts | 2 -- apps/server/src/modules/server/server.module.ts | 4 ++-- apps/server/src/modules/shd/shd.api.module.ts | 8 +------- 6 files changed, 6 insertions(+), 14 deletions(-) diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index f1e5dbd8d63..aab271b5045 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -14,7 +14,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) { private readonly jwtValidationAdapter: JwtValidationAdapter, configService: ConfigService ) { - const publicKey = configService.get('JWT_PUBLIC_KEY'); + const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); super({ jwtFromRequest: extractJwtFromHeader, diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index 477500feaad..bc4f4aeb636 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -15,7 +15,7 @@ export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JW private readonly jwtValidationAdapter: JwtValidationAdapter, configService: ConfigService ) { - const publicKey = configService.get('JWT_PUBLIC_KEY'); + const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); super({ jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), diff --git a/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts b/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts index e7aa0939439..7d2645184d1 100644 --- a/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts @@ -11,7 +11,7 @@ export class XApiKeyStrategy extends PassportStrategy(Strategy, StrategyType.API constructor(private readonly configService: ConfigService) { super({ header: 'X-API-KEY' }, false); - this.allowedApiKeys = this.configService.get('ADMIN_API__ALLOWED_API_KEYS'); + this.allowedApiKeys = this.configService.getOrThrow('ADMIN_API__ALLOWED_API_KEYS'); } public validate = (apiKey: string, done: (error: Error | null, data: boolean | null) => void) => { diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index 6f79d5f3969..5cd31817edc 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -1,4 +1,3 @@ -import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { CacheWrapperModule } from '@infra/cache'; import { IdentityManagementModule } from '@infra/identity-management'; import { AccountModule } from '@modules/account'; @@ -58,7 +57,6 @@ const createJwtOptions = (configService: ConfigService) => RoleModule, IdentityManagementModule, CacheWrapperModule, - AuthGuardModule.register([AuthGuardOptions.JWT, AuthGuardOptions.WS_JWT]), ], providers: [ UserRepo, diff --git a/apps/server/src/modules/server/server.module.ts b/apps/server/src/modules/server/server.module.ts index af457631d66..59e4c19dc8c 100644 --- a/apps/server/src/modules/server/server.module.ts +++ b/apps/server/src/modules/server/server.module.ts @@ -1,5 +1,5 @@ import { Configuration } from '@hpi-schul-cloud/commons'; -import { AuthGuardModule } from '@infra/auth-guard'; +import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@infra/database'; import { MailModule } from '@infra/mail'; import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@infra/rabbitmq'; @@ -54,7 +54,7 @@ const serverModules = [ ConfigModule.forRoot(createConfigModuleOptions(serverConfig)), CoreModule, AuthenticationApiModule, - AuthGuardModule, + AuthGuardModule.register([AuthGuardOptions.JWT]), AuthorizationReferenceApiModule, AccountApiModule, CollaborativeStorageModule, diff --git a/apps/server/src/modules/shd/shd.api.module.ts b/apps/server/src/modules/shd/shd.api.module.ts index 65ca833a348..6e3686f4fd6 100644 --- a/apps/server/src/modules/shd/shd.api.module.ts +++ b/apps/server/src/modules/shd/shd.api.module.ts @@ -1,4 +1,3 @@ -import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { AuthenticationModule } from '@modules/authentication'; import { AuthorizationModule } from '@modules/authorization/authorization.module'; import { InstanceModule } from '@modules/instance'; @@ -6,12 +5,7 @@ import { Module } from '@nestjs/common'; import { ShdController, ShdUc } from './api'; @Module({ - imports: [ - AuthorizationModule, - AuthenticationModule, - AuthGuardModule.register([AuthGuardOptions.JWT]), - InstanceModule, - ], + imports: [AuthorizationModule, AuthenticationModule, InstanceModule], controllers: [ShdController], providers: [ShdUc], }) From 1f133c338d42e6ebc8e9fae0470e09f26e4a9e2d Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 25 Oct 2024 09:50:25 +0200 Subject: [PATCH 25/42] Add JWT_SIGNING_ALGORITHM as env var --- .../src/modules/authentication/authentication-config.ts | 2 ++ .../src/modules/authentication/authentication-test.module.ts | 4 ++-- .../src/modules/authentication/authentication.module.ts | 4 ++-- apps/server/src/modules/server/server.config.ts | 2 ++ config/default.schema.json | 5 +++++ src/services/authentication/configuration.js | 2 +- 6 files changed, 14 insertions(+), 5 deletions(-) diff --git a/apps/server/src/modules/authentication/authentication-config.ts b/apps/server/src/modules/authentication/authentication-config.ts index c346416fa1a..898a1802e7b 100644 --- a/apps/server/src/modules/authentication/authentication-config.ts +++ b/apps/server/src/modules/authentication/authentication-config.ts @@ -1,11 +1,13 @@ import { XApiKeyConfig } from '@infra/auth-guard'; import { AccountConfig } from '@modules/account'; +import { Algorithm } from 'jsonwebtoken'; export interface AuthenticationConfig extends AccountConfig, XApiKeyConfig { DISABLED_BRUTE_FORCE_CHECK: boolean; FEATURE_JWT_EXTENDED_TIMEOUT_ENABLED: boolean; JWT_PRIVATE_KEY: string; JWT_PUBLIC_KEY: string; + JWT_SIGNING_ALGORITHM: Algorithm; JWT_LIFETIME: string; JWT_TIMEOUT_SECONDS: number; JWT_LIFETIME_SUPPORT_SECONDS: number; diff --git a/apps/server/src/modules/authentication/authentication-test.module.ts b/apps/server/src/modules/authentication/authentication-test.module.ts index 63a197cddc5..11c9cd1380d 100644 --- a/apps/server/src/modules/authentication/authentication-test.module.ts +++ b/apps/server/src/modules/authentication/authentication-test.module.ts @@ -11,7 +11,7 @@ import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { LegacySchoolRepo, UserRepo } from '@shared/repo'; import { LoggerModule } from '@src/core/logger'; -import { SignOptions } from 'jsonwebtoken'; +import { Algorithm, SignOptions } from 'jsonwebtoken'; import { JwtWhitelistAdapter } from './helper/jwt-whitelist.adapter'; import { AuthenticationService } from './services/authentication.service'; import { LdapService } from './services/ldap.service'; @@ -20,7 +20,7 @@ import { LocalStrategy } from './strategy/local.strategy'; import { Oauth2Strategy } from './strategy/oauth2.strategy'; const createJwtOptions = () => { - const algorithm = 'RS256'; + const algorithm = Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm; const signOptions: SignOptions = { algorithm, diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index 777ae6a7c33..5e5a6552105 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -11,7 +11,7 @@ import { JwtModule } from '@nestjs/jwt'; import { PassportModule } from '@nestjs/passport'; import { LegacySchoolRepo, UserRepo } from '@shared/repo'; import { LoggerModule } from '@src/core/logger'; -import { SignOptions } from 'jsonwebtoken'; +import { Algorithm, SignOptions } from 'jsonwebtoken'; import { AuthenticationConfig } from './authentication-config'; import { JwtWhitelistAdapter } from './helper/jwt-whitelist.adapter'; import { AuthenticationService } from './services/authentication.service'; @@ -21,7 +21,7 @@ import { LocalStrategy } from './strategy/local.strategy'; import { Oauth2Strategy } from './strategy/oauth2.strategy'; const createJwtOptions = (configService: ConfigService) => { - const algorithm = 'RS256'; + const algorithm = configService.getOrThrow('JWT_SIGNING_ALGORITHM'); const signOptions: SignOptions = { algorithm, diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 8cff856f6ef..21c82d504f7 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -31,6 +31,7 @@ import type { BbbConfig } from '@modules/video-conference/bbb'; import type { LanguageType } from '@shared/domain/interface'; import type { SchulcloudTheme } from '@shared/domain/types'; import type { CoreModuleConfig } from '@src/core'; +import { Algorithm } from 'jsonwebtoken'; import type { Timezone } from './types/timezone.enum'; export enum NodeEnvType { @@ -193,6 +194,7 @@ const config: ServerConfig = { // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PRIVATE_KEY: (Configuration.get('JWT_PRIVATE_KEY') as string).replace(/\\n/g, '\n'), JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, NOT_AUTHENTICATED_REDIRECT_URL: Configuration.get('NOT_AUTHENTICATED_REDIRECT_URL') as string, DOCUMENT_BASE_DIR: Configuration.get('DOCUMENT_BASE_DIR') as string, SC_THEME: Configuration.get('SC_THEME') as SchulcloudTheme, diff --git a/config/default.schema.json b/config/default.schema.json index 24bc886d624..78c18ecd2c9 100644 --- a/config/default.schema.json +++ b/config/default.schema.json @@ -127,6 +127,11 @@ "type": "string", "description": "The public key used to verify JWTs." }, + "JWT_SIGNING_ALGORITHM": { + "type": "string", + "default": "RS256", + "description": "The algorithm used to sign JWTs. Must be an asymmetric algorithm." + }, "JWT_LIFETIME": { "type": "string", "default": "30d", diff --git a/src/services/authentication/configuration.js b/src/services/authentication/configuration.js index 266680cf2b8..d40335db846 100644 --- a/src/services/authentication/configuration.js +++ b/src/services/authentication/configuration.js @@ -18,7 +18,7 @@ module.exports = { header: { typ: 'JWT' }, audience: Configuration.get('SC_DOMAIN'), issuer: Configuration.get('SC_DOMAIN'), - algorithm: 'RS256', + algorithm: Configuration.get('JWT_SIGNING_ALGORITHM'), expiresIn: Configuration.get('JWT_LIFETIME'), }, tsp: {}, From 36185c67ae139bbee2d30745bd0f65352e71a9b5 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 25 Oct 2024 10:13:40 +0200 Subject: [PATCH 26/42] Add check of algorithm in JWT validation --- apps/server/src/infra/auth-guard/auth-guard.config.ts | 3 +++ apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts | 3 +++ .../src/infra/auth-guard/strategy/ws-jwt.strategy.ts | 2 ++ .../src/modules/files-storage/files-storage.config.ts | 2 ++ apps/server/src/modules/h5p-editor/h5p-editor.config.ts | 2 ++ apps/server/src/modules/server/admin-api-server.config.ts | 8 +++++--- 6 files changed, 17 insertions(+), 3 deletions(-) diff --git a/apps/server/src/infra/auth-guard/auth-guard.config.ts b/apps/server/src/infra/auth-guard/auth-guard.config.ts index a3c048313cd..2194a1f92f0 100644 --- a/apps/server/src/infra/auth-guard/auth-guard.config.ts +++ b/apps/server/src/infra/auth-guard/auth-guard.config.ts @@ -1,4 +1,7 @@ +import { Algorithm } from 'jsonwebtoken'; + export interface AuthGuardConfig { ADMIN_API__ALLOWED_API_KEYS: string[]; JWT_PUBLIC_KEY: string; + JWT_SIGNING_ALGORITHM: Algorithm; } diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index f1e5dbd8d63..98466237fa6 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -2,6 +2,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { extractJwtFromHeader } from '@shared/common'; +import { Algorithm } from 'jsonwebtoken'; import { Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; import { AuthGuardConfig } from '../auth-guard.config'; @@ -15,11 +16,13 @@ export class JwtStrategy extends PassportStrategy(Strategy) { configService: ConfigService ) { const publicKey = configService.get('JWT_PUBLIC_KEY'); + const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); super({ jwtFromRequest: extractJwtFromHeader, ignoreExpiration: false, secretOrKey: publicKey, + algorithms: [algorithm], }); } diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index 477500feaad..51e17b4de9c 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -16,11 +16,13 @@ export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JW configService: ConfigService ) { const publicKey = configService.get('JWT_PUBLIC_KEY'); + const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); super({ jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), ignoreExpiration: false, secretOrKey: publicKey, + algorithms: [algorithm], }); } diff --git a/apps/server/src/modules/files-storage/files-storage.config.ts b/apps/server/src/modules/files-storage/files-storage.config.ts index 41983f90d0e..106ff54d426 100644 --- a/apps/server/src/modules/files-storage/files-storage.config.ts +++ b/apps/server/src/modules/files-storage/files-storage.config.ts @@ -3,6 +3,7 @@ import { AuthorizationClientConfig } from '@infra/authorization-client'; import { S3Config } from '@infra/s3-client'; import { CoreModuleConfig } from '@src/core'; import { AuthGuardConfig } from '@src/infra/auth-guard'; +import { Algorithm } from 'jsonwebtoken'; export const FILES_STORAGE_S3_CONNECTION = 'FILES_STORAGE_S3_CONNECTION'; export interface FileStorageConfig extends CoreModuleConfig, AuthorizationClientConfig, AuthGuardConfig { @@ -26,6 +27,7 @@ const authGuardConfig: AuthGuardConfig = { // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, }; const fileStorageConfig: FileStorageConfig = { diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index 92e30a6d6b4..353f472b626 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -4,6 +4,7 @@ import { AuthorizationClientConfig } from '@infra/authorization-client'; import { S3Config } from '@infra/s3-client'; import { LanguageType } from '@shared/domain/interface'; import { CoreModuleConfig } from '@src/core'; +import { Algorithm } from 'jsonwebtoken'; export interface H5PEditorConfig extends CoreModuleConfig, AuthorizationClientConfig, AuthGuardConfig { NEST_LOG_LEVEL: string; @@ -24,6 +25,7 @@ const h5pEditorConfig: H5PEditorConfig = { // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, }; export const translatorConfig = { diff --git a/apps/server/src/modules/server/admin-api-server.config.ts b/apps/server/src/modules/server/admin-api-server.config.ts index b4b968eb676..c84a5e170f7 100644 --- a/apps/server/src/modules/server/admin-api-server.config.ts +++ b/apps/server/src/modules/server/admin-api-server.config.ts @@ -1,11 +1,12 @@ -import { DeletionConfig } from '@modules/deletion'; +import { Configuration } from '@hpi-schul-cloud/commons/lib'; import { AuthGuardConfig } from '@infra/auth-guard'; +import { DeletionConfig } from '@modules/deletion'; import { LegacySchoolConfig } from '@modules/legacy-school'; -import { UserConfig } from '@modules/user'; import { RegistrationPinConfig } from '@modules/registration-pin'; import { ToolConfig } from '@modules/tool'; -import { Configuration } from '@hpi-schul-cloud/commons/lib'; +import { UserConfig } from '@modules/user'; import { LanguageType } from '@shared/domain/interface'; +import { Algorithm } from 'jsonwebtoken'; export interface AdminApiServerConfig extends DeletionConfig, @@ -60,6 +61,7 @@ const config: AdminApiServerConfig = { // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, LOGIN_BLOCK_TIME: 0, TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE: Configuration.get( 'TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE' From 23daddad069b5b02dc99e85336d23fce25bb78a9 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 25 Oct 2024 10:25:56 +0200 Subject: [PATCH 27/42] Add check of issuer and audience in JWT validation --- apps/server/src/infra/auth-guard/auth-guard.config.ts | 1 + apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts | 4 ++++ apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts | 4 ++++ apps/server/src/modules/files-storage/files-storage.config.ts | 1 + apps/server/src/modules/h5p-editor/h5p-editor.config.ts | 1 + apps/server/src/modules/server/admin-api-server.config.ts | 1 + 6 files changed, 12 insertions(+) diff --git a/apps/server/src/infra/auth-guard/auth-guard.config.ts b/apps/server/src/infra/auth-guard/auth-guard.config.ts index 2194a1f92f0..a31c148cb48 100644 --- a/apps/server/src/infra/auth-guard/auth-guard.config.ts +++ b/apps/server/src/infra/auth-guard/auth-guard.config.ts @@ -4,4 +4,5 @@ export interface AuthGuardConfig { ADMIN_API__ALLOWED_API_KEYS: string[]; JWT_PUBLIC_KEY: string; JWT_SIGNING_ALGORITHM: Algorithm; + SC_DOMAIN: string; } diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index 98466237fa6..ea5b7ac9bef 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -17,12 +17,16 @@ export class JwtStrategy extends PassportStrategy(Strategy) { ) { const publicKey = configService.get('JWT_PUBLIC_KEY'); const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); + const issuer = configService.get('SC_DOMAIN'); + const audience = configService.get('SC_DOMAIN'); super({ jwtFromRequest: extractJwtFromHeader, ignoreExpiration: false, secretOrKey: publicKey, algorithms: [algorithm], + issuer, + audience, }); } diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index 51e17b4de9c..cf19076bbf0 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -17,12 +17,16 @@ export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JW ) { const publicKey = configService.get('JWT_PUBLIC_KEY'); const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); + const issuer = configService.get('SC_DOMAIN'); + const audience = configService.get('SC_DOMAIN'); super({ jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), ignoreExpiration: false, secretOrKey: publicKey, algorithms: [algorithm], + issuer, + audience, }); } diff --git a/apps/server/src/modules/files-storage/files-storage.config.ts b/apps/server/src/modules/files-storage/files-storage.config.ts index 106ff54d426..b1a06834bc3 100644 --- a/apps/server/src/modules/files-storage/files-storage.config.ts +++ b/apps/server/src/modules/files-storage/files-storage.config.ts @@ -28,6 +28,7 @@ const authGuardConfig: AuthGuardConfig = { // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, }; const fileStorageConfig: FileStorageConfig = { diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index 353f472b626..84509d1dd22 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -26,6 +26,7 @@ const h5pEditorConfig: H5PEditorConfig = { // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, }; export const translatorConfig = { diff --git a/apps/server/src/modules/server/admin-api-server.config.ts b/apps/server/src/modules/server/admin-api-server.config.ts index c84a5e170f7..424a35368fc 100644 --- a/apps/server/src/modules/server/admin-api-server.config.ts +++ b/apps/server/src/modules/server/admin-api-server.config.ts @@ -69,6 +69,7 @@ const config: AdminApiServerConfig = { FEATURE_IDENTITY_MANAGEMENT_LOGIN_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_LOGIN_ENABLED') as boolean, FEATURE_IDENTITY_MANAGEMENT_STORE_ENABLED: Configuration.get('FEATURE_IDENTITY_MANAGEMENT_STORE_ENABLED') as boolean, CTL_TOOLS__PREFERRED_TOOLS_LIMIT: Configuration.get('CTL_TOOLS__PREFERRED_TOOLS_LIMIT') as number, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, }; export const adminApiServerConfig = () => config; From 2d0b20a7d79b11b7e1751f0a3762c9406c330479 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 25 Oct 2024 10:28:55 +0200 Subject: [PATCH 28/42] Remove unnecessary consts --- apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts | 6 ++---- .../server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index ea5b7ac9bef..caf853d751a 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -17,16 +17,14 @@ export class JwtStrategy extends PassportStrategy(Strategy) { ) { const publicKey = configService.get('JWT_PUBLIC_KEY'); const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); - const issuer = configService.get('SC_DOMAIN'); - const audience = configService.get('SC_DOMAIN'); super({ jwtFromRequest: extractJwtFromHeader, ignoreExpiration: false, secretOrKey: publicKey, algorithms: [algorithm], - issuer, - audience, + issuer: configService.get('SC_DOMAIN'), + audience: configService.get('SC_DOMAIN'), }); } diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index cf19076bbf0..a8f1dabec5e 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -17,16 +17,14 @@ export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JW ) { const publicKey = configService.get('JWT_PUBLIC_KEY'); const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); - const issuer = configService.get('SC_DOMAIN'); - const audience = configService.get('SC_DOMAIN'); super({ jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), ignoreExpiration: false, secretOrKey: publicKey, algorithms: [algorithm], - issuer, - audience, + issuer: configService.get('SC_DOMAIN'), + audience: configService.get('SC_DOMAIN'), }); } From f44de4478462b3cb9d0c88353d0433c8d1baa37c Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 25 Oct 2024 11:59:17 +0200 Subject: [PATCH 29/42] Skip broken test --- .../infra/tsp-client/tsp-client-factory.integration.spec.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/apps/server/src/infra/tsp-client/tsp-client-factory.integration.spec.ts b/apps/server/src/infra/tsp-client/tsp-client-factory.integration.spec.ts index 90d798ea650..2db181b930b 100644 --- a/apps/server/src/infra/tsp-client/tsp-client-factory.integration.spec.ts +++ b/apps/server/src/infra/tsp-client/tsp-client-factory.integration.spec.ts @@ -7,7 +7,9 @@ import { TspClientModule } from './tsp-client.module'; // NOTE: This test is skipped because it requires a valid client id, secret and token endpoint. // It is meant to be used for manual testing only. -describe('TspClientFactory Integration', () => { +// This test expects that configService.getOrThrow is only used for the specified keys. This is not a reasonable expectation. +// In fact getOrThrow is used now at another place and the test is broken. +describe.skip('TspClientFactory Integration', () => { let module: TestingModule; let sut: TspClientFactory; From 41565fb20caad89b594df783d4dc7d01053b59c6 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Fri, 25 Oct 2024 15:17:16 +0200 Subject: [PATCH 30/42] Add SC_Domain to preview-generator-configmap --- .../templates/preview-generator-configmap.yml.j2 | 1 + 1 file changed, 1 insertion(+) diff --git a/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 b/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 index 7ef3e22d623..b28813d631d 100644 --- a/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 @@ -8,3 +8,4 @@ metadata: data: NEST_LOG_LEVEL: "{{ NEST_LOG_LEVEL }}" EXIT_ON_ERROR: "true" + SC_DOMAIN: "{{ DOMAIN }}" From 83f0acfe82199a3c3c985b6374b659cb715ed6fb Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Fri, 25 Oct 2024 15:17:42 +0200 Subject: [PATCH 31/42] Split configs --- .../src/infra/auth-guard/config/index.ts | 3 ++- .../jwt-auth-guard.config.ts} | 3 +-- ...nfig.ts => x-api-key-auth-guard.config.ts} | 2 +- apps/server/src/infra/auth-guard/index.ts | 3 +-- .../infra/auth-guard/strategy/jwt.strategy.ts | 4 ++-- .../auth-guard/strategy/ws-jwt.strategy.ts | 4 ++-- .../strategy/x-api-key.strategy.spec.ts | 12 +++++----- .../auth-guard/strategy/x-api-key.strategy.ts | 4 ++-- .../authentication/authentication-config.ts | 3 +-- .../board/board-collaboration.config.ts | 11 +++++++-- .../src/modules/deletion/deletion.config.ts | 12 +++++----- .../src/modules/deletion/deletion.module.ts | 4 ++-- .../files-storage/files-storage.config.ts | 10 ++++---- .../fwu-learning-contents.config.ts | 11 +++++++++ .../modules/h5p-editor/h5p-editor.config.ts | 5 ++-- .../modules/server/admin-api-server.config.ts | 11 ++------- .../src/modules/server/server.config.ts | 16 ++++--------- apps/server/src/modules/tldraw/config.ts | 23 ++++++++++++++----- 18 files changed, 75 insertions(+), 66 deletions(-) rename apps/server/src/infra/auth-guard/{auth-guard.config.ts => config/jwt-auth-guard.config.ts} (62%) rename apps/server/src/infra/auth-guard/config/{x-api-key.config.ts => x-api-key-auth-guard.config.ts} (50%) diff --git a/apps/server/src/infra/auth-guard/config/index.ts b/apps/server/src/infra/auth-guard/config/index.ts index cd9c0d28b96..5feee8c84be 100644 --- a/apps/server/src/infra/auth-guard/config/index.ts +++ b/apps/server/src/infra/auth-guard/config/index.ts @@ -1 +1,2 @@ -export * from './x-api-key.config'; +export * from './jwt-auth-guard.config'; +export * from './x-api-key-auth-guard.config'; diff --git a/apps/server/src/infra/auth-guard/auth-guard.config.ts b/apps/server/src/infra/auth-guard/config/jwt-auth-guard.config.ts similarity index 62% rename from apps/server/src/infra/auth-guard/auth-guard.config.ts rename to apps/server/src/infra/auth-guard/config/jwt-auth-guard.config.ts index a31c148cb48..98ee0839171 100644 --- a/apps/server/src/infra/auth-guard/auth-guard.config.ts +++ b/apps/server/src/infra/auth-guard/config/jwt-auth-guard.config.ts @@ -1,7 +1,6 @@ import { Algorithm } from 'jsonwebtoken'; -export interface AuthGuardConfig { - ADMIN_API__ALLOWED_API_KEYS: string[]; +export interface JwtAuthGuardConfig { JWT_PUBLIC_KEY: string; JWT_SIGNING_ALGORITHM: Algorithm; SC_DOMAIN: string; diff --git a/apps/server/src/infra/auth-guard/config/x-api-key.config.ts b/apps/server/src/infra/auth-guard/config/x-api-key-auth-guard.config.ts similarity index 50% rename from apps/server/src/infra/auth-guard/config/x-api-key.config.ts rename to apps/server/src/infra/auth-guard/config/x-api-key-auth-guard.config.ts index 97c189e2853..19a3758aef2 100644 --- a/apps/server/src/infra/auth-guard/config/x-api-key.config.ts +++ b/apps/server/src/infra/auth-guard/config/x-api-key-auth-guard.config.ts @@ -1,3 +1,3 @@ -export interface XApiKeyConfig { +export interface XApiKeyAuthGuardConfig { ADMIN_API__ALLOWED_API_KEYS: string[]; } diff --git a/apps/server/src/infra/auth-guard/index.ts b/apps/server/src/infra/auth-guard/index.ts index 071f51377fd..0d2a21e9103 100644 --- a/apps/server/src/infra/auth-guard/index.ts +++ b/apps/server/src/infra/auth-guard/index.ts @@ -1,7 +1,6 @@ export { JwtValidationAdapter } from './adapter'; -export { AuthGuardConfig } from './auth-guard.config'; export { AuthGuardModule, AuthGuardOptions } from './auth-guard.module'; -export { XApiKeyConfig } from './config'; +export { JwtAuthGuardConfig, XApiKeyAuthGuardConfig } from './config'; export { CurrentUser, JWT, JwtAuthentication, WsJwtAuthentication, XApiKeyAuthentication } from './decorator'; // JwtAuthGuard only exported because api tests still overried this guard. // Use JwtAuthentication decorator for request validation diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index 211d9d06f6f..1a48d0fa984 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -5,7 +5,7 @@ import { extractJwtFromHeader } from '@shared/common'; import { Algorithm } from 'jsonwebtoken'; import { Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; -import { AuthGuardConfig } from '../auth-guard.config'; +import { JwtAuthGuardConfig } from '../config'; import { ICurrentUser, JwtPayload } from '../interface'; import { CurrentUserBuilder } from '../mapper'; @@ -13,7 +13,7 @@ import { CurrentUserBuilder } from '../mapper'; export class JwtStrategy extends PassportStrategy(Strategy) { constructor( private readonly jwtValidationAdapter: JwtValidationAdapter, - configService: ConfigService + configService: ConfigService ) { const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); const algorithm = configService.getOrThrow('JWT_SIGNING_ALGORITHM'); diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index 024ce316da6..b59e5a13ba2 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -5,7 +5,7 @@ import { WsException } from '@nestjs/websockets'; import { JwtExtractor } from '@shared/common'; import { ExtractJwt, Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; -import { AuthGuardConfig } from '../auth-guard.config'; +import { JwtAuthGuardConfig } from '../config'; import { ICurrentUser, JwtPayload, StrategyType } from '../interface'; import { CurrentUserBuilder } from '../mapper'; @@ -13,7 +13,7 @@ import { CurrentUserBuilder } from '../mapper'; export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JWT) { constructor( private readonly jwtValidationAdapter: JwtValidationAdapter, - configService: ConfigService + configService: ConfigService ) { const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); const algorithm = configService.getOrThrow('JWT_SIGNING_ALGORITHM'); diff --git a/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts b/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts index fd59f5ad9b4..17c4d978b72 100644 --- a/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts +++ b/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.spec.ts @@ -1,14 +1,14 @@ +import { createMock } from '@golevelup/ts-jest'; import { UnauthorizedException } from '@nestjs/common'; -import { Test, TestingModule } from '@nestjs/testing'; import { ConfigService } from '@nestjs/config'; -import { createMock } from '@golevelup/ts-jest'; +import { Test, TestingModule } from '@nestjs/testing'; +import { XApiKeyAuthGuardConfig } from '../config/x-api-key-auth-guard.config'; import { XApiKeyStrategy } from './x-api-key.strategy'; -import { XApiKeyConfig } from '../config/x-api-key.config'; describe('XApiKeyStrategy', () => { let module: TestingModule; let strategy: XApiKeyStrategy; - let configService: ConfigService; + let configService: ConfigService; beforeAll(async () => { module = await Test.createTestingModule({ @@ -17,7 +17,7 @@ describe('XApiKeyStrategy', () => { XApiKeyStrategy, { provide: ConfigService, - useValue: createMock>({ + useValue: createMock>({ get: () => ['7ccd4e11-c6f6-48b0-81eb-cccf7922e7a4'], }), }, @@ -25,7 +25,7 @@ describe('XApiKeyStrategy', () => { }).compile(); strategy = module.get(XApiKeyStrategy); - configService = module.get(ConfigService); + configService = module.get(ConfigService); }); afterAll(async () => { diff --git a/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts b/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts index 7d2645184d1..39b2f33f721 100644 --- a/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/x-api-key.strategy.ts @@ -2,14 +2,14 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import Strategy from 'passport-headerapikey'; -import { XApiKeyConfig } from '../config'; +import { XApiKeyAuthGuardConfig } from '../config'; import { StrategyType } from '../interface'; @Injectable() export class XApiKeyStrategy extends PassportStrategy(Strategy, StrategyType.API_KEY) { private readonly allowedApiKeys: string[]; - constructor(private readonly configService: ConfigService) { + constructor(private readonly configService: ConfigService) { super({ header: 'X-API-KEY' }, false); this.allowedApiKeys = this.configService.getOrThrow('ADMIN_API__ALLOWED_API_KEYS'); } diff --git a/apps/server/src/modules/authentication/authentication-config.ts b/apps/server/src/modules/authentication/authentication-config.ts index 898a1802e7b..59b86ad5f19 100644 --- a/apps/server/src/modules/authentication/authentication-config.ts +++ b/apps/server/src/modules/authentication/authentication-config.ts @@ -1,8 +1,7 @@ -import { XApiKeyConfig } from '@infra/auth-guard'; import { AccountConfig } from '@modules/account'; import { Algorithm } from 'jsonwebtoken'; -export interface AuthenticationConfig extends AccountConfig, XApiKeyConfig { +export interface AuthenticationConfig extends AccountConfig { DISABLED_BRUTE_FORCE_CHECK: boolean; FEATURE_JWT_EXTENDED_TIMEOUT_ENABLED: boolean; JWT_PRIVATE_KEY: string; diff --git a/apps/server/src/modules/board/board-collaboration.config.ts b/apps/server/src/modules/board/board-collaboration.config.ts index b1fc78cfdc1..5a74844d2af 100644 --- a/apps/server/src/modules/board/board-collaboration.config.ts +++ b/apps/server/src/modules/board/board-collaboration.config.ts @@ -1,11 +1,18 @@ import { Configuration } from '@hpi-schul-cloud/commons'; +import { JwtAuthGuardConfig } from '@infra/auth-guard'; +import { Algorithm } from 'jsonwebtoken'; -export interface BoardCollaborationConfig { +export interface BoardCollaborationConfig extends JwtAuthGuardConfig { NEST_LOG_LEVEL: string; } -const boardCollaborationConfig = { +const boardCollaborationConfig: BoardCollaborationConfig = { NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string, + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, }; export const config = () => boardCollaborationConfig; diff --git a/apps/server/src/modules/deletion/deletion.config.ts b/apps/server/src/modules/deletion/deletion.config.ts index a205674e786..a8c78307587 100644 --- a/apps/server/src/modules/deletion/deletion.config.ts +++ b/apps/server/src/modules/deletion/deletion.config.ts @@ -1,12 +1,12 @@ -import { LoggerConfig } from '@src/core/logger'; -import { CalendarConfig } from '@src/infra/calendar'; +import { XApiKeyAuthGuardConfig } from '@infra/auth-guard'; import { ClassConfig } from '@modules/class'; -import { XApiKeyConfig } from '@infra/auth-guard'; +import { FilesConfig } from '@modules/files'; import { NewsConfig } from '@modules/news'; -import { TeamsConfig } from '@modules/teams'; import { PseudonymConfig } from '@modules/pseudonym'; -import { FilesConfig } from '@modules/files'; import { RocketChatUserConfig } from '@modules/rocketchat-user'; +import { TeamsConfig } from '@modules/teams'; +import { LoggerConfig } from '@src/core/logger'; +import { CalendarConfig } from '@src/infra/calendar'; export interface DeletionConfig extends LoggerConfig, @@ -17,7 +17,7 @@ export interface DeletionConfig PseudonymConfig, FilesConfig, RocketChatUserConfig, - XApiKeyConfig { + XApiKeyAuthGuardConfig { ADMIN_API__MODIFICATION_THRESHOLD_MS: number; ADMIN_API__MAX_CONCURRENT_DELETION_REQUESTS: number; ADMIN_API__DELETION_DELAY_MILLISECONDS: number; diff --git a/apps/server/src/modules/deletion/deletion.module.ts b/apps/server/src/modules/deletion/deletion.module.ts index 74dd1e60d4e..221664ca8af 100644 --- a/apps/server/src/modules/deletion/deletion.module.ts +++ b/apps/server/src/modules/deletion/deletion.module.ts @@ -1,4 +1,4 @@ -import { XApiKeyConfig } from '@infra/auth-guard'; +import { XApiKeyAuthGuardConfig } from '@infra/auth-guard'; import { Module } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { DeletionLogService, DeletionRequestService } from './domain/service'; @@ -8,7 +8,7 @@ import { DeletionLogRepo, DeletionRequestRepo } from './repo'; providers: [ DeletionRequestRepo, DeletionLogRepo, - ConfigService, + ConfigService, DeletionLogService, DeletionRequestService, ], diff --git a/apps/server/src/modules/files-storage/files-storage.config.ts b/apps/server/src/modules/files-storage/files-storage.config.ts index b1a06834bc3..0772476bb4d 100644 --- a/apps/server/src/modules/files-storage/files-storage.config.ts +++ b/apps/server/src/modules/files-storage/files-storage.config.ts @@ -1,12 +1,12 @@ import { Configuration } from '@hpi-schul-cloud/commons'; +import { JwtAuthGuardConfig } from '@infra/auth-guard'; import { AuthorizationClientConfig } from '@infra/authorization-client'; import { S3Config } from '@infra/s3-client'; import { CoreModuleConfig } from '@src/core'; -import { AuthGuardConfig } from '@src/infra/auth-guard'; import { Algorithm } from 'jsonwebtoken'; export const FILES_STORAGE_S3_CONNECTION = 'FILES_STORAGE_S3_CONNECTION'; -export interface FileStorageConfig extends CoreModuleConfig, AuthorizationClientConfig, AuthGuardConfig { +export interface FileStorageConfig extends CoreModuleConfig, AuthorizationClientConfig, JwtAuthGuardConfig { MAX_FILE_SIZE: number; MAX_SECURITY_CHECK_FILE_SIZE: number; USE_STREAM_TO_ANTIVIRUS: boolean; @@ -21,9 +21,7 @@ export const authorizationClientConfig: AuthorizationClientConfig = { basePath: `${Configuration.get('API_HOST') as string}/v3/`, }; -const authGuardConfig: AuthGuardConfig = { - ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string).split(','), - +const jwtAuthGuardConfig: JwtAuthGuardConfig = { // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), @@ -37,7 +35,7 @@ const fileStorageConfig: FileStorageConfig = { USE_STREAM_TO_ANTIVIRUS: Configuration.get('FILES_STORAGE__USE_STREAM_TO_ANTIVIRUS') as boolean, ...authorizationClientConfig, ...defaultConfig, - ...authGuardConfig, + ...jwtAuthGuardConfig, EXIT_ON_ERROR: Configuration.get('EXIT_ON_ERROR') as boolean, }; diff --git a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts index 6cfcb03b74f..a92554d3411 100644 --- a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts +++ b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts @@ -1,5 +1,7 @@ import { Configuration } from '@hpi-schul-cloud/commons'; +import { JwtAuthGuardConfig } from '@infra/auth-guard'; import { S3Config } from '@infra/s3-client'; +import { Algorithm } from 'jsonwebtoken'; export const FWU_CONTENT_S3_CONNECTION = 'FWU_CONTENT_S3_CONNECTION'; @@ -12,9 +14,18 @@ export const s3Config: S3Config = { secretAccessKey: Configuration.get('FWU_CONTENT__S3_SECRET_KEY') as string, }; +const jwtAuthGuardConfig: JwtAuthGuardConfig = { + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, +}; + const fwuLearningContentsConfig = { NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string, INCOMING_REQUEST_TIMEOUT: Configuration.get('FWU_CONTENT__INCOMING_REQUEST_TIMEOUT_MS') as number, + ...jwtAuthGuardConfig, }; export const config = () => fwuLearningContentsConfig; diff --git a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts index 84509d1dd22..81b8e4b555a 100644 --- a/apps/server/src/modules/h5p-editor/h5p-editor.config.ts +++ b/apps/server/src/modules/h5p-editor/h5p-editor.config.ts @@ -1,12 +1,12 @@ import { Configuration } from '@hpi-schul-cloud/commons'; -import { AuthGuardConfig } from '@infra/auth-guard'; +import { JwtAuthGuardConfig } from '@infra/auth-guard'; import { AuthorizationClientConfig } from '@infra/authorization-client'; import { S3Config } from '@infra/s3-client'; import { LanguageType } from '@shared/domain/interface'; import { CoreModuleConfig } from '@src/core'; import { Algorithm } from 'jsonwebtoken'; -export interface H5PEditorConfig extends CoreModuleConfig, AuthorizationClientConfig, AuthGuardConfig { +export interface H5PEditorConfig extends CoreModuleConfig, AuthorizationClientConfig, JwtAuthGuardConfig { NEST_LOG_LEVEL: string; INCOMING_REQUEST_TIMEOUT: number; } @@ -20,7 +20,6 @@ const h5pEditorConfig: H5PEditorConfig = { INCOMING_REQUEST_TIMEOUT: Configuration.get('H5P_EDITOR__INCOMING_REQUEST_TIMEOUT') as number, ...authorizationClientConfig, EXIT_ON_ERROR: Configuration.get('EXIT_ON_ERROR') as boolean, - ADMIN_API__ALLOWED_API_KEYS: [], // Node's process.env escapes newlines. We need to reverse it for the keys to work. // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node diff --git a/apps/server/src/modules/server/admin-api-server.config.ts b/apps/server/src/modules/server/admin-api-server.config.ts index b17e0706e71..fc2790c0a81 100644 --- a/apps/server/src/modules/server/admin-api-server.config.ts +++ b/apps/server/src/modules/server/admin-api-server.config.ts @@ -1,12 +1,11 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib'; -import { AuthGuardConfig } from '@infra/auth-guard'; +import { XApiKeyAuthGuardConfig } from '@infra/auth-guard'; import { DeletionConfig } from '@modules/deletion'; import { LegacySchoolConfig } from '@modules/legacy-school'; import { RegistrationPinConfig } from '@modules/registration-pin'; import { ToolConfig } from '@modules/tool'; import { UserConfig } from '@modules/user'; import { LanguageType } from '@shared/domain/interface'; -import { Algorithm } from 'jsonwebtoken'; export interface AdminApiServerConfig extends DeletionConfig, @@ -14,7 +13,7 @@ export interface AdminApiServerConfig UserConfig, RegistrationPinConfig, ToolConfig, - AuthGuardConfig { + XApiKeyAuthGuardConfig { ETHERPAD__API_KEY?: string; ETHERPAD__URI?: string; } @@ -57,11 +56,6 @@ const config: AdminApiServerConfig = { ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string) .split(',') .map((part) => (part.split(':').pop() ?? '').trim()), - - // Node's process.env escapes newlines. We need to reverse it for the keys to work. - // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node - JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), - JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, LOGIN_BLOCK_TIME: 0, TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE: Configuration.get( 'TEACHER_STUDENT_VISIBILITY__IS_CONFIGURABLE' @@ -72,7 +66,6 @@ const config: AdminApiServerConfig = { TEACHER_VISIBILITY_FOR_EXTERNAL_TEAM_INVITATION: Configuration.get( 'TEACHER_VISIBILITY_FOR_EXTERNAL_TEAM_INVITATION' ) as string, - SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, }; export const adminApiServerConfig = () => config; diff --git a/apps/server/src/modules/server/server.config.ts b/apps/server/src/modules/server/server.config.ts index 9b91d8d93da..b6f458092cc 100644 --- a/apps/server/src/modules/server/server.config.ts +++ b/apps/server/src/modules/server/server.config.ts @@ -1,5 +1,5 @@ import { Configuration } from '@hpi-schul-cloud/commons'; -import { XApiKeyConfig } from '@infra/auth-guard'; +import { JwtAuthGuardConfig } from '@infra/auth-guard'; import type { IdentityManagementConfig } from '@infra/identity-management'; import type { MailConfig } from '@infra/mail/interfaces/mail-config'; import type { SchulconnexClientConfig } from '@infra/schulconnex-client'; @@ -11,12 +11,12 @@ import type { AuthenticationConfig } from '@modules/authentication'; import type { BoardConfig } from '@modules/board'; import type { MediaBoardConfig } from '@modules/board/media-board.config'; import type { CollaborativeTextEditorConfig } from '@modules/collaborative-text-editor'; -import { DeletionConfig } from '@modules/deletion'; import type { FilesStorageClientConfig } from '@modules/files-storage-client'; import { SynchronizationConfig } from '@modules/idp-console'; import type { LearnroomConfig } from '@modules/learnroom'; import type { LessonConfig } from '@modules/lesson'; import { ProvisioningConfig } from '@modules/provisioning'; +import { RocketChatUserConfig } from '@modules/rocketchat-user'; import { RoomConfig } from '@modules/room'; import type { SchoolConfig } from '@modules/school'; import type { SharingConfig } from '@modules/sharing'; @@ -51,7 +51,8 @@ export interface ServerConfig IdentityManagementConfig, SchoolConfig, MailConfig, - XApiKeyConfig, + JwtAuthGuardConfig, + RocketChatUserConfig, LearnroomConfig, AuthenticationConfig, ToolConfig, @@ -64,7 +65,6 @@ export interface ServerConfig UserImportConfig, SchulconnexClientConfig, SynchronizationConfig, - DeletionConfig, CollaborativeTextEditorConfig, ProvisioningConfig, RoomConfig, @@ -226,14 +226,6 @@ const config: ServerConfig = { STUDENT_TEAM_CREATION: Configuration.get('STUDENT_TEAM_CREATION') as string, SYNCHRONIZATION_CHUNK: Configuration.get('SYNCHRONIZATION_CHUNK') as number, // parse [:],[:]... and discard description - ADMIN_API__MODIFICATION_THRESHOLD_MS: Configuration.get('ADMIN_API__MODIFICATION_THRESHOLD_MS') as number, - ADMIN_API__MAX_CONCURRENT_DELETION_REQUESTS: Configuration.get( - 'ADMIN_API__MAX_CONCURRENT_DELETION_REQUESTS' - ) as number, - ADMIN_API__DELETION_DELAY_MILLISECONDS: Configuration.get('ADMIN_API__DELETION_DELAY_MILLISECONDS') as number, - ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string) - .split(',') - .map((part) => (part.split(':').pop() ?? '').trim()), BLOCKLIST_OF_EMAIL_DOMAINS: (Configuration.get('BLOCKLIST_OF_EMAIL_DOMAINS') as string) .split(',') .map((domain) => domain.trim()), diff --git a/apps/server/src/modules/tldraw/config.ts b/apps/server/src/modules/tldraw/config.ts index bef12dde3c2..6803bff3101 100644 --- a/apps/server/src/modules/tldraw/config.ts +++ b/apps/server/src/modules/tldraw/config.ts @@ -1,21 +1,23 @@ import { Configuration } from '@hpi-schul-cloud/commons'; +import { JwtAuthGuardConfig } from '@infra/auth-guard'; +import { Algorithm } from 'jsonwebtoken'; import { env } from 'process'; -export interface TldrawConfig { +export interface TldrawConfig extends JwtAuthGuardConfig { TLDRAW_DB_URL: string; NEST_LOG_LEVEL: string; INCOMING_REQUEST_TIMEOUT: number; - TLDRAW_DB_COMPRESS_THRESHOLD: string; + TLDRAW_DB_COMPRESS_THRESHOLD: number; CONNECTION_STRING: string; FEATURE_TLDRAW_ENABLED: boolean; TLDRAW_PING_TIMEOUT: number; - TLDRAW_GC_ENABLED: number; - REDIS_URI: string; + TLDRAW_GC_ENABLED: boolean; + REDIS_URI: string | null; TLDRAW_ASSETS_ENABLED: boolean; TLDRAW_ASSETS_SYNC_ENABLED: boolean; TLDRAW_ASSETS_MAX_SIZE_BYTES: number; ASSETS_ALLOWED_MIME_TYPES_LIST: string; - API_HOST: number; + API_HOST: string; TLDRAW_MAX_DOCUMENT_SIZE: number; TLDRAW_FINALIZE_DELAY: number; PERFORMANCE_MEASURE_ENABLED: boolean; @@ -38,7 +40,15 @@ export const tldrawS3Config = { secretAccessKey: env.S3_SECRET_KEY as string, }; -const tldrawConfig = { +const jwtAuthGuardConfig: JwtAuthGuardConfig = { + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, +}; + +const tldrawConfig: TldrawConfig = { TLDRAW_DB_URL, NEST_LOG_LEVEL: Configuration.get('TLDRAW__LOG_LEVEL') as string, INCOMING_REQUEST_TIMEOUT: Configuration.get('INCOMING_REQUEST_TIMEOUT_API') as number, @@ -56,6 +66,7 @@ const tldrawConfig = { TLDRAW_MAX_DOCUMENT_SIZE: Configuration.get('TLDRAW__MAX_DOCUMENT_SIZE') as number, TLDRAW_FINALIZE_DELAY: Configuration.get('TLDRAW__FINALIZE_DELAY') as number, PERFORMANCE_MEASURE_ENABLED: Configuration.get('TLDRAW__PERFORMANCE_MEASURE_ENABLED') as boolean, + ...jwtAuthGuardConfig, }; export const config = () => tldrawConfig; From 6400acfb41cf33032bfe79f30eeb6ceff3c78513 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Mon, 28 Oct 2024 08:03:05 +0100 Subject: [PATCH 32/42] Add interface FwuLearningContentsConfig --- .../fwu-learning-contents/fwu-learning-contents.config.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts index a92554d3411..3ea158070fc 100644 --- a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts +++ b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts @@ -3,6 +3,11 @@ import { JwtAuthGuardConfig } from '@infra/auth-guard'; import { S3Config } from '@infra/s3-client'; import { Algorithm } from 'jsonwebtoken'; +export interface FwuLearningContentsConfig extends JwtAuthGuardConfig { + NEST_LOG_LEVEL: string; + INCOMING_REQUEST_TIMEOUT: number; +} + export const FWU_CONTENT_S3_CONNECTION = 'FWU_CONTENT_S3_CONNECTION'; export const s3Config: S3Config = { @@ -22,7 +27,7 @@ const jwtAuthGuardConfig: JwtAuthGuardConfig = { SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, }; -const fwuLearningContentsConfig = { +const fwuLearningContentsConfig: FwuLearningContentsConfig = { NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string, INCOMING_REQUEST_TIMEOUT: Configuration.get('FWU_CONTENT__INCOMING_REQUEST_TIMEOUT_MS') as number, ...jwtAuthGuardConfig, From c66aff6c5c5746826d313e0b300b2068ac7996fa Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Mon, 28 Oct 2024 10:10:29 +0100 Subject: [PATCH 33/42] Add values to top-level configs where forgotten --- .../server/src/modules/board/board-collaboration.config.ts | 7 +++++++ .../fwu-learning-contents/fwu-learning-contents.config.ts | 7 +++++++ apps/server/src/modules/tldraw/config.ts | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/apps/server/src/modules/board/board-collaboration.config.ts b/apps/server/src/modules/board/board-collaboration.config.ts index b1fc78cfdc1..18316af5729 100644 --- a/apps/server/src/modules/board/board-collaboration.config.ts +++ b/apps/server/src/modules/board/board-collaboration.config.ts @@ -6,6 +6,13 @@ export interface BoardCollaborationConfig { const boardCollaborationConfig = { NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, + ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string).split(','), }; export const config = () => boardCollaborationConfig; diff --git a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts index 6cfcb03b74f..8c2b6a55f72 100644 --- a/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts +++ b/apps/server/src/modules/fwu-learning-contents/fwu-learning-contents.config.ts @@ -15,6 +15,13 @@ export const s3Config: S3Config = { const fwuLearningContentsConfig = { NEST_LOG_LEVEL: Configuration.get('NEST_LOG_LEVEL') as string, INCOMING_REQUEST_TIMEOUT: Configuration.get('FWU_CONTENT__INCOMING_REQUEST_TIMEOUT_MS') as number, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, + ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string).split(','), }; export const config = () => fwuLearningContentsConfig; diff --git a/apps/server/src/modules/tldraw/config.ts b/apps/server/src/modules/tldraw/config.ts index bef12dde3c2..550cbd3609f 100644 --- a/apps/server/src/modules/tldraw/config.ts +++ b/apps/server/src/modules/tldraw/config.ts @@ -56,6 +56,13 @@ const tldrawConfig = { TLDRAW_MAX_DOCUMENT_SIZE: Configuration.get('TLDRAW__MAX_DOCUMENT_SIZE') as number, TLDRAW_FINALIZE_DELAY: Configuration.get('TLDRAW__FINALIZE_DELAY') as number, PERFORMANCE_MEASURE_ENABLED: Configuration.get('TLDRAW__PERFORMANCE_MEASURE_ENABLED') as boolean, + + // Node's process.env escapes newlines. We need to reverse it for the keys to work. + // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node + JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), + JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, + SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, + ADMIN_API__ALLOWED_API_KEYS: (Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string).split(','), }; export const config = () => tldrawConfig; From cc7ff5bc5e1e4bb461067d8a05594ea6c7bd4017 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 29 Oct 2024 09:48:53 +0100 Subject: [PATCH 34/42] Add factory for strategy options to satisfy SonarCloud --- .../src/infra/auth-guard/mapper/index.ts | 1 + .../mapper/jwt-strategy-options.factory.ts | 24 +++++++++++++++++++ .../infra/auth-guard/strategy/jwt.strategy.ts | 15 +++--------- .../auth-guard/strategy/ws-jwt.strategy.ts | 16 ++++--------- 4 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts diff --git a/apps/server/src/infra/auth-guard/mapper/index.ts b/apps/server/src/infra/auth-guard/mapper/index.ts index e5e72b12686..4832027d67a 100644 --- a/apps/server/src/infra/auth-guard/mapper/index.ts +++ b/apps/server/src/infra/auth-guard/mapper/index.ts @@ -1,2 +1,3 @@ export * from './current-user.factory'; export * from './jwt.factory'; +export * from './jwt-strategy-options.factory'; diff --git a/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts b/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts new file mode 100644 index 00000000000..231ef140d92 --- /dev/null +++ b/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts @@ -0,0 +1,24 @@ +import { ConfigService } from '@nestjs/config'; +import { JwtFromRequestFunction, StrategyOptions } from 'passport-jwt'; +import { AuthGuardConfig } from '../auth-guard.config'; + +export class JwtStrategyOptionsFactory { + static build( + jwtFromRequestFunction: JwtFromRequestFunction, + configService: ConfigService + ): StrategyOptions { + const publicKey = configService.get('JWT_PUBLIC_KEY'); + const algorithm = configService.getOrThrow('JWT_SIGNING_ALGORITHM'); + + const options = { + jwtFromRequest: jwtFromRequestFunction, + secretOrKey: publicKey, + ignoreExpiration: false, + algorithms: [algorithm], + issuer: configService.get('SC_DOMAIN'), + audience: configService.get('SC_DOMAIN'), + }; + + return options; + } +} diff --git a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts index caf853d751a..4579b29115b 100644 --- a/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/jwt.strategy.ts @@ -2,12 +2,11 @@ import { Injectable, UnauthorizedException } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { extractJwtFromHeader } from '@shared/common'; -import { Algorithm } from 'jsonwebtoken'; import { Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; import { AuthGuardConfig } from '../auth-guard.config'; import { ICurrentUser, JwtPayload } from '../interface'; -import { CurrentUserBuilder } from '../mapper'; +import { CurrentUserBuilder, JwtStrategyOptionsFactory } from '../mapper'; @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { @@ -15,17 +14,9 @@ export class JwtStrategy extends PassportStrategy(Strategy) { private readonly jwtValidationAdapter: JwtValidationAdapter, configService: ConfigService ) { - const publicKey = configService.get('JWT_PUBLIC_KEY'); - const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); + const strategyOptions = JwtStrategyOptionsFactory.build(extractJwtFromHeader, configService); - super({ - jwtFromRequest: extractJwtFromHeader, - ignoreExpiration: false, - secretOrKey: publicKey, - algorithms: [algorithm], - issuer: configService.get('SC_DOMAIN'), - audience: configService.get('SC_DOMAIN'), - }); + super(strategyOptions); } async validate(payload: JwtPayload): Promise { diff --git a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts index a8f1dabec5e..46b54cb395d 100644 --- a/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts +++ b/apps/server/src/infra/auth-guard/strategy/ws-jwt.strategy.ts @@ -3,11 +3,11 @@ import { ConfigService } from '@nestjs/config'; import { PassportStrategy } from '@nestjs/passport'; import { WsException } from '@nestjs/websockets'; import { JwtExtractor } from '@shared/common'; -import { ExtractJwt, Strategy } from 'passport-jwt'; +import { Strategy } from 'passport-jwt'; import { JwtValidationAdapter } from '../adapter'; import { AuthGuardConfig } from '../auth-guard.config'; import { ICurrentUser, JwtPayload, StrategyType } from '../interface'; -import { CurrentUserBuilder } from '../mapper'; +import { CurrentUserBuilder, JwtStrategyOptionsFactory } from '../mapper'; @Injectable() export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JWT) { @@ -15,17 +15,9 @@ export class WsJwtStrategy extends PassportStrategy(Strategy, StrategyType.WS_JW private readonly jwtValidationAdapter: JwtValidationAdapter, configService: ConfigService ) { - const publicKey = configService.get('JWT_PUBLIC_KEY'); - const algorithm = configService.get('JWT_SIGNING_ALGORITHM'); + const strategyOptions = JwtStrategyOptionsFactory.build(JwtExtractor.fromCookie('jwt'), configService); - super({ - jwtFromRequest: ExtractJwt.fromExtractors([JwtExtractor.fromCookie('jwt')]), - ignoreExpiration: false, - secretOrKey: publicKey, - algorithms: [algorithm], - issuer: configService.get('SC_DOMAIN'), - audience: configService.get('SC_DOMAIN'), - }); + super(strategyOptions); } async validate(payload: JwtPayload): Promise { From e946b8261554d6247cf82e570ba69509f5aaad4e Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 29 Oct 2024 11:04:05 +0100 Subject: [PATCH 35/42] Use getOrThrow to read values from ConfigService --- .../auth-guard/mapper/jwt-strategy-options.factory.ts | 6 +++--- .../modules/authentication/authentication.module.ts | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts b/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts index 231ef140d92..6e9e5f67fd0 100644 --- a/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts +++ b/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts @@ -7,7 +7,7 @@ export class JwtStrategyOptionsFactory { jwtFromRequestFunction: JwtFromRequestFunction, configService: ConfigService ): StrategyOptions { - const publicKey = configService.get('JWT_PUBLIC_KEY'); + const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); const algorithm = configService.getOrThrow('JWT_SIGNING_ALGORITHM'); const options = { @@ -15,8 +15,8 @@ export class JwtStrategyOptionsFactory { secretOrKey: publicKey, ignoreExpiration: false, algorithms: [algorithm], - issuer: configService.get('SC_DOMAIN'), - audience: configService.get('SC_DOMAIN'), + issuer: configService.getOrThrow('SC_DOMAIN'), + audience: configService.getOrThrow('SC_DOMAIN'), }; return options; diff --git a/apps/server/src/modules/authentication/authentication.module.ts b/apps/server/src/modules/authentication/authentication.module.ts index 5e5a6552105..8f1afbb1a17 100644 --- a/apps/server/src/modules/authentication/authentication.module.ts +++ b/apps/server/src/modules/authentication/authentication.module.ts @@ -25,14 +25,14 @@ const createJwtOptions = (configService: ConfigService) => const signOptions: SignOptions = { algorithm, - expiresIn: configService.get('JWT_LIFETIME'), - issuer: configService.get('SC_DOMAIN'), - audience: configService.get('SC_DOMAIN'), + expiresIn: configService.getOrThrow('JWT_LIFETIME'), + issuer: configService.getOrThrow('SC_DOMAIN'), + audience: configService.getOrThrow('SC_DOMAIN'), header: { typ: 'JWT', alg: algorithm }, }; - const privateKey = configService.get('JWT_PRIVATE_KEY'); - const publicKey = configService.get('JWT_PUBLIC_KEY'); + const privateKey = configService.getOrThrow('JWT_PRIVATE_KEY'); + const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); const options = { privateKey, From 937130f185e5e19c5ef8783e6281b3caaec355ae Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 29 Oct 2024 11:49:42 +0100 Subject: [PATCH 36/42] Define defaultMikroOrmOptions in for AdminApiServerModule separately to fix dependency problem --- .../src/modules/server/admin-api.server.module.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/server/admin-api.server.module.ts b/apps/server/src/modules/server/admin-api.server.module.ts index 8f380ff96bb..342170b7873 100644 --- a/apps/server/src/modules/server/admin-api.server.module.ts +++ b/apps/server/src/modules/server/admin-api.server.module.ts @@ -1,12 +1,13 @@ import { Configuration } from '@hpi-schul-cloud/commons'; import { AuthGuardModule } from '@infra/auth-guard'; -import { MikroOrmModule } from '@mikro-orm/nestjs'; +import { Dictionary, IPrimaryKey } from '@mikro-orm/core'; +import { MikroOrmModule, MikroOrmModuleSyncOptions } from '@mikro-orm/nestjs'; import { DeletionApiModule } from '@modules/deletion/deletion-api.module'; import { FileEntity } from '@modules/files/entity'; import { LegacySchoolAdminApiModule } from '@modules/legacy-school/legacy-school-admin.api-module'; import { ToolAdminApiModule } from '@modules/tool/tool-admin-api.module'; import { UserAdminApiModule } from '@modules/user/user-admin-api.module'; -import { DynamicModule, Module } from '@nestjs/common'; +import { DynamicModule, Module, NotFoundException } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { CqrsModule } from '@nestjs/cqrs'; import { ALL_ENTITIES } from '@shared/domain/entity'; @@ -16,7 +17,6 @@ import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@src/infr import { EtherpadClientModule } from '@src/infra/etherpad-client'; import { RabbitMQWrapperModule, RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; import { AdminApiRegistrationPinModule } from '../registration-pin/admin-api-registration-pin.module'; -import { defaultMikroOrmOptions } from './server.module'; import { adminApiServerConfig } from './admin-api-server.config'; const serverModules = [ @@ -33,6 +33,12 @@ const serverModules = [ AuthGuardModule, ]; +const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = { + findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) => + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions + new NotFoundException(`The requested ${entityName}: ${where} has not been found.`), +}; + @Module({ imports: [ RabbitMQWrapperModule, From 014c9153f61f0468218d605767f561ad7d8b57e9 Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 29 Oct 2024 14:20:46 +0100 Subject: [PATCH 37/42] Add JWT_PUBLIC_KEY to configmap of admin-api-server --- .../templates/admin-api-server-configmap.yml.j2 | 1 + 1 file changed, 1 insertion(+) diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 index 70645a1c4a5..53f76e967b2 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 @@ -22,3 +22,4 @@ data: IDENTITY_MANAGEMENT__TENANT: "{{ IDENTITY_MANAGEMENT__TENANT }}" IDENTITY_MANAGEMENT__CLIENTID: "{{ IDENTITY_MANAGEMENT__CLIENTID }}" TLDRAW__WEBSOCKET_URL: "wss://{{ DOMAIN }}/tldraw-server" + JWT_PUBLIC_KEY: "{{ JWT_PUBLIC_KEY }}" From 7601962c9a24742c56fb13be0bec935a7c8b68df Mon Sep 17 00:00:00 2001 From: dyedwiper Date: Tue, 29 Oct 2024 14:40:44 +0100 Subject: [PATCH 38/42] Add JWT_PUBLIC_KEY to configmap of preview-generator --- .../templates/preview-generator-configmap.yml.j2 | 1 + 1 file changed, 1 insertion(+) diff --git a/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 b/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 index b28813d631d..3fd1be78ae0 100644 --- a/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/preview-generator-configmap.yml.j2 @@ -9,3 +9,4 @@ data: NEST_LOG_LEVEL: "{{ NEST_LOG_LEVEL }}" EXIT_ON_ERROR: "true" SC_DOMAIN: "{{ DOMAIN }}" + JWT_PUBLIC_KEY: "{{ JWT_PUBLIC_KEY }}" From 3e2dd8b83ac9b32e0c7a11fb3bc2376147775d18 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Wed, 30 Oct 2024 15:26:58 +0100 Subject: [PATCH 39/42] Remove JWT_PUBLIC_KEY from admin api server config map --- .../templates/admin-api-server-configmap.yml.j2 | 1 - 1 file changed, 1 deletion(-) diff --git a/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 b/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 index 53f76e967b2..70645a1c4a5 100644 --- a/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 +++ b/ansible/roles/schulcloud-server-core/templates/admin-api-server-configmap.yml.j2 @@ -22,4 +22,3 @@ data: IDENTITY_MANAGEMENT__TENANT: "{{ IDENTITY_MANAGEMENT__TENANT }}" IDENTITY_MANAGEMENT__CLIENTID: "{{ IDENTITY_MANAGEMENT__CLIENTID }}" TLDRAW__WEBSOCKET_URL: "wss://{{ DOMAIN }}/tldraw-server" - JWT_PUBLIC_KEY: "{{ JWT_PUBLIC_KEY }}" From 489d537fae992497da62b11d4df666dd26fb2d65 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Wed, 30 Oct 2024 15:56:06 +0100 Subject: [PATCH 40/42] Fix import for JwtAuthGuardConfig --- .../infra/auth-guard/mapper/jwt-strategy-options.factory.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts b/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts index 6e9e5f67fd0..55a46d56ff8 100644 --- a/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts +++ b/apps/server/src/infra/auth-guard/mapper/jwt-strategy-options.factory.ts @@ -1,11 +1,11 @@ import { ConfigService } from '@nestjs/config'; import { JwtFromRequestFunction, StrategyOptions } from 'passport-jwt'; -import { AuthGuardConfig } from '../auth-guard.config'; +import { JwtAuthGuardConfig } from '../config'; export class JwtStrategyOptionsFactory { static build( jwtFromRequestFunction: JwtFromRequestFunction, - configService: ConfigService + configService: ConfigService ): StrategyOptions { const publicKey = configService.getOrThrow('JWT_PUBLIC_KEY'); const algorithm = configService.getOrThrow('JWT_SIGNING_ALGORITHM'); From 45df9fd1ccee5d1a3d11a37def27a9d53ef46060 Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Tue, 5 Nov 2024 16:40:18 +0100 Subject: [PATCH 41/42] Fix tl draw tests --- apps/server/src/modules/tldraw/config.ts | 15 +++++---------- .../tldraw.controller.401.api.spec.ts | 19 ++++--------------- .../api-test/tldraw.controller.api.spec.ts | 18 ++++-------------- .../modules/tldraw/tldraw-api-test.module.ts | 18 ++++++++++-------- 4 files changed, 23 insertions(+), 47 deletions(-) diff --git a/apps/server/src/modules/tldraw/config.ts b/apps/server/src/modules/tldraw/config.ts index 6803bff3101..947602d77c1 100644 --- a/apps/server/src/modules/tldraw/config.ts +++ b/apps/server/src/modules/tldraw/config.ts @@ -1,9 +1,8 @@ import { Configuration } from '@hpi-schul-cloud/commons'; -import { JwtAuthGuardConfig } from '@infra/auth-guard'; -import { Algorithm } from 'jsonwebtoken'; +import { XApiKeyAuthGuardConfig } from '@infra/auth-guard'; import { env } from 'process'; -export interface TldrawConfig extends JwtAuthGuardConfig { +export interface TldrawConfig extends XApiKeyAuthGuardConfig { TLDRAW_DB_URL: string; NEST_LOG_LEVEL: string; INCOMING_REQUEST_TIMEOUT: number; @@ -40,12 +39,8 @@ export const tldrawS3Config = { secretAccessKey: env.S3_SECRET_KEY as string, }; -const jwtAuthGuardConfig: JwtAuthGuardConfig = { - // Node's process.env escapes newlines. We need to reverse it for the keys to work. - // See: https://stackoverflow.com/questions/30400341/environment-variables-containing-newlines-in-node - JWT_PUBLIC_KEY: (Configuration.get('JWT_PUBLIC_KEY') as string).replace(/\\n/g, '\n'), - JWT_SIGNING_ALGORITHM: Configuration.get('JWT_SIGNING_ALGORITHM') as Algorithm, - SC_DOMAIN: Configuration.get('SC_DOMAIN') as string, +const apiKeyAuthGuardConfig: XApiKeyAuthGuardConfig = { + ADMIN_API__ALLOWED_API_KEYS: Configuration.get('ADMIN_API__ALLOWED_API_KEYS') as string[], }; const tldrawConfig: TldrawConfig = { @@ -66,7 +61,7 @@ const tldrawConfig: TldrawConfig = { TLDRAW_MAX_DOCUMENT_SIZE: Configuration.get('TLDRAW__MAX_DOCUMENT_SIZE') as number, TLDRAW_FINALIZE_DELAY: Configuration.get('TLDRAW__FINALIZE_DELAY') as number, PERFORMANCE_MEASURE_ENABLED: Configuration.get('TLDRAW__PERFORMANCE_MEASURE_ENABLED') as boolean, - ...jwtAuthGuardConfig, + ...apiKeyAuthGuardConfig, }; export const config = () => tldrawConfig; diff --git a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts index 4a8b0bac325..09d5d81cbb4 100644 --- a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts +++ b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.401.api.spec.ts @@ -1,14 +1,9 @@ -import { AuthGuardModule, AuthGuardOptions } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; -import { ServerTestModule } from '@modules/server'; import { INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { courseFactory, TestApiClient, UserAndAccountTestFactory } from '@shared/testing'; -import { Logger } from '@src/core/logger'; -import { TldrawController } from '..'; -import { TldrawRepo } from '../../repo'; -import { TldrawService } from '../../service'; +import { TestApiClient } from '@shared/testing'; import { tldrawEntityFactory } from '../../testing'; +import { TldrawApiTestModule } from '../../tldraw-api-test.module'; const baseRouteName = '/tldraw-document'; describe('tldraw controller (api)', () => { @@ -19,9 +14,7 @@ describe('tldraw controller (api)', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [ServerTestModule, AuthGuardModule.register([AuthGuardOptions.X_API_KEY])], - controllers: [TldrawController], - providers: [Logger, TldrawService, TldrawRepo], + imports: [TldrawApiTestModule.forRoot()], }).compile(); app = module.createNestApplication(); @@ -36,16 +29,12 @@ describe('tldraw controller (api)', () => { describe('when request does not contain token', () => { const setup = async () => { - const { teacherAccount, teacherUser } = UserAndAccountTestFactory.buildTeacher(); - const course = courseFactory.build({ teachers: [teacherUser] }); - await em.persistAndFlush([teacherAccount, teacherUser, course]); - const drawingItemData = tldrawEntityFactory.build(); await em.persistAndFlush([drawingItemData]); em.clear(); - return { teacherUser, drawingItemData }; + return { drawingItemData }; }; it('should return status 401 for delete', async () => { diff --git a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts index 37cb1595aef..9e52ae2c970 100644 --- a/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts +++ b/apps/server/src/modules/tldraw/controller/api-test/tldraw.controller.api.spec.ts @@ -1,15 +1,11 @@ import { XApiKeyGuard } from '@infra/auth-guard'; import { EntityManager } from '@mikro-orm/mongodb'; -import { ServerTestModule } from '@modules/server'; import { ExecutionContext, INestApplication } from '@nestjs/common'; import { Test, TestingModule } from '@nestjs/testing'; -import { courseFactory, TestApiClient, UserAndAccountTestFactory } from '@shared/testing'; -import { Logger } from '@src/core/logger'; +import { TestApiClient } from '@shared/testing'; import { Request } from 'express'; -import { TldrawController } from '..'; -import { TldrawRepo } from '../../repo'; -import { TldrawService } from '../../service'; import { tldrawEntityFactory } from '../../testing'; +import { TldrawApiTestModule } from '../../tldraw-api-test.module'; const baseRouteName = '/tldraw-document'; describe('tldraw controller (api)', () => { @@ -20,9 +16,7 @@ describe('tldraw controller (api)', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - imports: [ServerTestModule], - controllers: [TldrawController], - providers: [Logger, TldrawService, TldrawRepo], + imports: [TldrawApiTestModule.forRoot()], }) .overrideGuard(XApiKeyGuard) .useValue({ @@ -46,16 +40,12 @@ describe('tldraw controller (api)', () => { describe('with valid user', () => { const setup = async () => { - const { teacherAccount, teacherUser } = UserAndAccountTestFactory.buildTeacher(); - const course = courseFactory.build({ teachers: [teacherUser] }); - await em.persistAndFlush([teacherAccount, teacherUser, course]); - const drawingItemData = tldrawEntityFactory.build(); await em.persistAndFlush([drawingItemData]); em.clear(); - return { teacherUser, drawingItemData }; + return { drawingItemData }; }; it('should return status 200 for delete', async () => { diff --git a/apps/server/src/modules/tldraw/tldraw-api-test.module.ts b/apps/server/src/modules/tldraw/tldraw-api-test.module.ts index 6603823f80e..227e209ebbe 100644 --- a/apps/server/src/modules/tldraw/tldraw-api-test.module.ts +++ b/apps/server/src/modules/tldraw/tldraw-api-test.module.ts @@ -1,24 +1,26 @@ import { MongoDatabaseModuleOptions, MongoMemoryDatabaseModule } from '@infra/database'; -import { defaultMikroOrmOptions } from '@modules/server'; import { HttpModule } from '@nestjs/axios'; import { DynamicModule, Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { createConfigModuleOptions } from '@src/config'; -import { Logger, LoggerModule } from '@src/core/logger'; -import { TldrawRedisFactory, TldrawRedisService } from './redis'; +import { CoreModule } from '@src/core'; +import { LoggerModule } from '@src/core/logger'; +import { AuthGuardModule, AuthGuardOptions } from '@src/infra/auth-guard'; import { config } from './config'; import { TldrawController } from './controller'; -import { MetricsService } from './metrics'; -import { TldrawRepo } from './repo'; +import { TldrawDrawing } from './entities'; +import { TldrawBoardRepo, TldrawRepo, YMongodb } from './repo'; import { TldrawService } from './service'; const imports = [ - MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions }), + MongoMemoryDatabaseModule.forRoot({ entities: [TldrawDrawing] }), LoggerModule, ConfigModule.forRoot(createConfigModuleOptions(config)), HttpModule, + AuthGuardModule.register([AuthGuardOptions.X_API_KEY]), + CoreModule, ]; -const providers = [Logger, TldrawService, TldrawRepo, MetricsService, TldrawRedisFactory, TldrawRedisService]; +const providers = [TldrawService, TldrawBoardRepo, TldrawRepo, YMongodb]; @Module({ imports, providers, @@ -27,7 +29,7 @@ export class TldrawApiTestModule { static forRoot(options?: MongoDatabaseModuleOptions): DynamicModule { return { module: TldrawApiTestModule, - imports: [...imports, MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, ...options })], + imports: [...imports, MongoMemoryDatabaseModule.forRoot({ ...options })], controllers: [TldrawController], providers, }; From e08bd9c7d18ece677f91580250391be311e67a1e Mon Sep 17 00:00:00 2001 From: Max Bischof Date: Wed, 6 Nov 2024 08:25:38 +0100 Subject: [PATCH 42/42] Add RabbitMQWrapperTestModule and AuthGuardModule to BoardCollaborationTestingModule --- .../src/modules/board/board-collaboration.testing.module.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/board/board-collaboration.testing.module.ts b/apps/server/src/modules/board/board-collaboration.testing.module.ts index 82c4b46678f..68f7c038104 100644 --- a/apps/server/src/modules/board/board-collaboration.testing.module.ts +++ b/apps/server/src/modules/board/board-collaboration.testing.module.ts @@ -4,8 +4,9 @@ import { ConfigModule } from '@nestjs/config'; import { ALL_ENTITIES } from '@shared/domain/entity'; import { createConfigModuleOptions } from '@src/config'; import { CoreModule } from '@src/core'; +import { AuthGuardModule, AuthGuardOptions } from '@src/infra/auth-guard'; import { MongoMemoryDatabaseModule } from '@src/infra/database'; -import { RabbitMQWrapperModule } from '@src/infra/rabbitmq'; +import { RabbitMQWrapperTestModule } from '@src/infra/rabbitmq'; import { AuthenticationApiModule } from '../authentication/authentication-api.module'; import { AuthorizationModule } from '../authorization'; import { config as boardCollaborationConfig } from './board-collaboration.config'; @@ -20,7 +21,7 @@ const config = () => { imports: [ CoreModule, ConfigModule.forRoot(createConfigModuleOptions(config)), - RabbitMQWrapperModule, + RabbitMQWrapperTestModule, MongoMemoryDatabaseModule.forRoot({ ...defaultMikroOrmOptions, entities: ALL_ENTITIES, @@ -29,6 +30,7 @@ const config = () => { AuthorizationModule, AuthenticationApiModule, BoardWsApiModule, + AuthGuardModule.register([AuthGuardOptions.WS_JWT]), ], providers: [], exports: [],