Files
RayLab-Core/dist/modules/auth/guards/oidc.guard.js
T
2026-08-02 20:01:29 +07:00

48 lines
2.3 KiB
JavaScript

"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 OidcGuard_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OidcGuard = void 0;
const common_1 = require("@nestjs/common");
const oidc_service_1 = require("../oidc.service");
const authentication_service_1 = require("../authentication.service");
let OidcGuard = OidcGuard_1 = class OidcGuard {
oidc;
authn;
logger = new common_1.Logger(OidcGuard_1.name);
constructor(oidc, authn) {
this.oidc = oidc;
this.authn = authn;
}
async canActivate(context) {
const req = context.switchToHttp().getRequest();
const auth = req.headers['authorization'] || req.headers['Authorization'];
if (!auth || typeof auth !== 'string' || !auth.startsWith('Bearer '))
throw new common_1.UnauthorizedException('Missing bearer token');
const token = auth.substring(7).trim();
try {
const ctx = await this.authn.authenticate(token);
// attach context to request under a structured key
req.raylabContext = ctx;
return true;
}
catch (e) {
this.logger.debug('Authentication failed', e.message);
throw new common_1.UnauthorizedException('Invalid token or authentication failed');
}
}
};
exports.OidcGuard = OidcGuard;
exports.OidcGuard = OidcGuard = OidcGuard_1 = __decorate([
(0, common_1.Injectable)(),
__metadata("design:paramtypes", [oidc_service_1.OidcService, authentication_service_1.AuthenticationService])
], OidcGuard);
//# sourceMappingURL=oidc.guard.js.map