finalize migration (yay)
This commit is contained in:
@@ -1,11 +1,52 @@
|
||||
import { type Request, type RequestHandler, type Response } from "express";
|
||||
import KeycloakConnect from "keycloak-connect";
|
||||
import { inject } from "../../../infrastructure/di/index.js";
|
||||
import { Injectable } from "../../../infrastructure/di/injectable-decorator.js";
|
||||
import { sessionStore } from "../../../infrastructure/sessionHandler.js";
|
||||
import keycloakConfig from "./keycloak.json" with { type: "json" };
|
||||
import { EnvironmentService } from "../../environmentService.js";
|
||||
//TODO transform into service and load config based on environment (see keycloak-admin)
|
||||
|
||||
export const KC_SECURITY_NAME = "bearerAuth";
|
||||
|
||||
export const keycloak = new KeycloakConnect(
|
||||
{ store: sessionStore },
|
||||
keycloakConfig,
|
||||
);
|
||||
export const kcUserConfig = keycloakConfig;
|
||||
@Injectable()
|
||||
export class KeycloakUser {
|
||||
private readonly environment = inject(EnvironmentService);
|
||||
private readonly keycloakConnect: Promise<KeycloakConnect.Keycloak>;
|
||||
|
||||
private async getConfig() {
|
||||
if (this.environment.isDev()) {
|
||||
const config = await import("./keycloak.dev.json", {
|
||||
with: { type: "json" },
|
||||
});
|
||||
return config.default;
|
||||
} else {
|
||||
const config = await import("./keycloak.prod.json", {
|
||||
with: { type: "json" },
|
||||
});
|
||||
return config.default;
|
||||
}
|
||||
}
|
||||
|
||||
public constructor() {
|
||||
this.keycloakConnect = new Promise(async resolve => {
|
||||
const config = await this.getConfig();
|
||||
const client = new KeycloakConnect({ store: sessionStore }, config);
|
||||
resolve(client);
|
||||
});
|
||||
}
|
||||
|
||||
public async checkRoles(
|
||||
roles: string[],
|
||||
request: Request,
|
||||
response: Response,
|
||||
) {
|
||||
const client = await this.keycloakConnect;
|
||||
const grant = await client.getGrant(request, response ?? ({} as any));
|
||||
return roles.every(role => grant.access_token?.hasRealmRole(role));
|
||||
}
|
||||
|
||||
public async middleware(): Promise<RequestHandler[]> {
|
||||
const client = await this.keycloakConnect;
|
||||
return client.middleware();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user