"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var AuthenticationService_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AuthenticationService = void 0; const common_1 = require("@nestjs/common"); const oidc_service_1 = require("./oidc.service"); const prisma_service_1 = require("../../shared/prisma.service"); const role_sync_service_1 = require("./role-sync.service"); const authorization_service_1 = require("../authorization/authorization.service"); const event_bus_service_1 = require("../../core/event-bus/event-bus.service"); let AuthenticationService = AuthenticationService_1 = class AuthenticationService { oidc; prisma; roleSync; authorization; events; logger = new common_1.Logger(AuthenticationService_1.name); constructor(oidc, prisma, roleSync, authorization, events) { this.oidc = oidc; this.prisma = prisma; this.roleSync = roleSync; this.authorization = authorization; this.events = events; } async authenticate(bearerToken) { // Validate token (signature/iss/aud/exp) const claims = await this.oidc.verifyToken(bearerToken); const sub = claims.sub; if (!sub) throw new Error('Invalid token: missing sub'); // Resolve identity const identity = { sub, email: claims.email, preferred_username: claims.preferred_username, raw: claims }; // Find or create user (materialize) let user = await this.prisma.user.findUnique({ where: { authentikId: sub } }); if (!user) { user = await this.prisma.user.create({ data: { authentikId: sub, username: identity.preferred_username || identity.email || sub, email: identity.email || null } }); } // Synchronize roles const groups = Array.isArray(claims.groups) ? claims.groups : []; await this.roleSync.syncUserRolesFromAuthentik(user.id, groups); // Load permissions const permissions = await this.authorization.getUserPermissions(user.id); // Build request context const rolesRows = await this.prisma.userRole.findMany({ where: { userId: user.id } }); const roles = rolesRows.map((r) => r.roleId); const ctx = { user, identity, roles, permissions }; // Publish domain event this.events.publish({ id: require('crypto').randomUUID(), timestamp: new Date().toISOString(), type: 'UserAuthenticated', payload: { userId: user.id, identity } }); return ctx; } }; exports.AuthenticationService = AuthenticationService; exports.AuthenticationService = AuthenticationService = AuthenticationService_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [oidc_service_1.OidcService, prisma_service_1.PrismaService, role_sync_service_1.RoleSyncService, authorization_service_1.AuthorizationService, event_bus_service_1.EventBus]) ], AuthenticationService); //# sourceMappingURL=authentication.service.js.map