Skip to content

Commit

Permalink
Moved security into backend. Fronted should not depend on KC
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoff-kiefer committed Oct 5, 2023
1 parent 00dff3c commit fcdf8a1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
17 changes: 2 additions & 15 deletions src/frontend/frontend.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { defineConfig } from '@mikro-orm/postgresql';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { APP_GUARD } from '@nestjs/core';
import { AuthGuard, KeycloakConnectModule, ResourceGuard, RoleGuard } from 'nest-keycloak-connect';
import { AuthGuard, ResourceGuard, RoleGuard } from 'nest-keycloak-connect';

import { FrontendApiModule } from '../modules/frontend/frontend-api.module.js';
import { DbConfig, KeycloakConfig, loadConfigFiles, loadEnvConfig, ServerConfig } from '../shared/config/index.js';
import { DbConfig, loadConfigFiles, loadEnvConfig, ServerConfig } from '../shared/config/index.js';
import { mappingErrorHandler } from '../shared/error/mapping.error.js';
import { HealthModule } from '../health/health.module.js';

Expand Down Expand Up @@ -37,19 +37,6 @@ import { HealthModule } from '../health/health.module.js';
},
inject: [ConfigService],
}),
KeycloakConnectModule.registerAsync({
useFactory: (config: ConfigService<ServerConfig, true>) => {
const keycloakConfig: KeycloakConfig = config.getOrThrow<KeycloakConfig>('KEYCLOAK');

return {
authServerUrl: keycloakConfig.BASE_URL,
realm: keycloakConfig.REALM_NAME,
clientId: keycloakConfig.CLIENT_ID,
secret: keycloakConfig.SECRET,
};
},
inject: [ConfigService],
}),
FrontendApiModule,
HealthModule,
],
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import { FrontendModule } from './frontend.module.js';
async function bootstrap(): Promise<void> {
const app: INestApplication = await NestFactory.create(FrontendModule);
app.useGlobalPipes(new GlobalValidationPipe());
app.setGlobalPrefix('api', {
exclude: ['health'],
});
const swagger: Omit<OpenAPIObject, 'paths'> = new DocumentBuilder()
.setTitle('dBildungs IAM')
.setDescription('The dBildungs IAM server API description')
.setVersion('1.0')
.build();

app.setGlobalPrefix('api', {
exclude: ['health'],
});
SwaggerModule.setup('docs', app, SwaggerModule.createDocument(app, swagger));

const configService: ConfigService<ServerConfig, true> = app.get(ConfigService<ServerConfig, true>);
Expand Down
2 changes: 2 additions & 0 deletions src/health/health.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
} from '@nestjs/terminus';
import { EntityManager } from '@mikro-orm/postgresql';
import { ApiExcludeController } from '@nestjs/swagger';
import { Unprotected } from 'nest-keycloak-connect';

@Controller('health')
@Unprotected()
@ApiExcludeController()
export class HealthController {
public constructor(
Expand Down
16 changes: 15 additions & 1 deletion src/server/server.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { MikroOrmModule } from '@mikro-orm/nestjs';
import { Module } from '@nestjs/common';
import { defineConfig } from '@mikro-orm/postgresql';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DbConfig, loadConfigFiles, loadEnvConfig, ServerConfig } from '../shared/config/index.js';
import { DbConfig, KeycloakConfig, loadConfigFiles, loadEnvConfig, ServerConfig } from '../shared/config/index.js';
import { mappingErrorHandler } from '../shared/error/index.js';
import { PersonApiModule } from '../modules/person/person-api.module.js';
import { HealthModule } from '../health/health.module.js';
import { KeycloakAdministrationModule } from '../modules/keycloak-administration/keycloak-administration.module.js';
import { OrganisationApiModule } from '../modules/organisation/organisation-api.module.js';
import { KeycloakConnectModule } from 'nest-keycloak-connect';

@Module({
imports: [
Expand Down Expand Up @@ -43,6 +44,19 @@ import { OrganisationApiModule } from '../modules/organisation/organisation-api.
},
inject: [ConfigService],
}),
KeycloakConnectModule.registerAsync({
useFactory: (config: ConfigService<ServerConfig, true>) => {
const keycloakConfig: KeycloakConfig = config.getOrThrow<KeycloakConfig>('KEYCLOAK');

return {
authServerUrl: keycloakConfig.BASE_URL,
realm: keycloakConfig.REALM_NAME,
clientId: keycloakConfig.CLIENT_ID,
secret: keycloakConfig.SECRET,
};
},
inject: [ConfigService],
}),
PersonApiModule,
OrganisationApiModule,
KeycloakAdministrationModule,
Expand Down

0 comments on commit fcdf8a1

Please sign in to comment.