3
module/README.md
Normal file
3
module/README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# module
|
||||
|
||||
This library was generated with [Nx](https://nx.dev).
|
||||
3
module/eslint.config.mjs
Normal file
3
module/eslint.config.mjs
Normal file
@@ -0,0 +1,3 @@
|
||||
import baseConfig from '../eslint.config.mjs';
|
||||
|
||||
export default [...baseConfig];
|
||||
9
module/project.json
Normal file
9
module/project.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "module",
|
||||
"$schema": "../node_modules/nx/schemas/project-schema.json",
|
||||
"sourceRoot": "module/src",
|
||||
"projectType": "library",
|
||||
"tags": [],
|
||||
"// targets": "to see all targets run: nx show project module --web",
|
||||
"targets": {}
|
||||
}
|
||||
1
module/src/index.ts
Normal file
1
module/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './lib/keycloak.module';
|
||||
67
module/src/lib/keycloak.module.ts
Normal file
67
module/src/lib/keycloak.module.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { AuthGuard, ResourceGuard, RoleGuard } from 'nest-keycloak-connect';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import {
|
||||
KeycloakConnectModule,
|
||||
PolicyEnforcementMode,
|
||||
TokenValidation,
|
||||
} from 'nest-keycloak-connect';
|
||||
import { KeycloakConfigKeys } from '@appweb/shared';
|
||||
import { APP_GUARD } from '@nestjs/core';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
KeycloakConnectModule.registerAsync({
|
||||
imports: [ConfigModule],
|
||||
inject: [ConfigService],
|
||||
useFactory: async (config: ConfigService) => {
|
||||
console.debug('URL: ', KeycloakConfigKeys.BASE_URL);
|
||||
console.debug('CLIENT_ID: ', KeycloakConfigKeys.CLIENT_ID);
|
||||
console.debug('REALM: ', KeycloakConfigKeys.REALM);
|
||||
console.debug('CLIENT_SECRET: ', KeycloakConfigKeys.CLIENT_SECRET);
|
||||
// Recuperiamo i valori assicurandoci che non siano undefined
|
||||
const authServerUrl = config.get<string>(KeycloakConfigKeys.BASE_URL);
|
||||
const realm = config.get<string>(KeycloakConfigKeys.REALM);
|
||||
const clientId = config.get<string>(KeycloakConfigKeys.CLIENT_ID);
|
||||
const secret = config.get<string>(KeycloakConfigKeys.CLIENT_SECRET);
|
||||
|
||||
console.debug('authServerUrl: ', authServerUrl);
|
||||
console.debug('realm: ', realm);
|
||||
console.debug('clientId: ', clientId);
|
||||
console.debug('secret: ', secret);
|
||||
|
||||
// Se mancano, NestJS lancerà un errore chiaro all'avvio
|
||||
if (!authServerUrl || !realm || !clientId || !secret) {
|
||||
throw new Error("Mancano variabili d'ambiente critiche per Keycloak");
|
||||
}
|
||||
|
||||
return {
|
||||
authServerUrl,
|
||||
realm,
|
||||
clientId,
|
||||
secret,
|
||||
// Aggiungi queste se necessario per completare il tipo richiesto
|
||||
policyEnforcement: PolicyEnforcementMode.PERMISSIVE,
|
||||
tokenValidation: TokenValidation.ONLINE,
|
||||
};
|
||||
},
|
||||
}),
|
||||
],
|
||||
providers: [
|
||||
// Registriamo i Guard qui dentro!
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: AuthGuard,
|
||||
},
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: ResourceGuard,
|
||||
},
|
||||
{
|
||||
provide: APP_GUARD,
|
||||
useClass: RoleGuard,
|
||||
},
|
||||
],
|
||||
exports: [KeycloakConnectModule],
|
||||
})
|
||||
export class KeycloakModule {}
|
||||
20
module/tsconfig.json
Normal file
20
module/tsconfig.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"extends": "../tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"importHelpers": true,
|
||||
"noImplicitOverride": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noPropertyAccessFromIndexSignature": true
|
||||
},
|
||||
"files": [],
|
||||
"include": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.lib.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
17
module/tsconfig.lib.json
Normal file
17
module/tsconfig.lib.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../dist/out-tsc",
|
||||
"declaration": true,
|
||||
"types": ["node"],
|
||||
"target": "es2021",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"strictNullChecks": true,
|
||||
"noImplicitAny": true,
|
||||
"strictBindCallApply": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"include": ["src/**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user