"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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessTokenGuard = void 0;
const common_1 = require("@nestjs/common");
const jwt_1 = require("@nestjs/jwt");
const constants_1 = require("../constants");
const permission_metadata_service_1 = require("../services/permission-metadata.service");
const nestjs_cls_1 = require("nestjs-cls");
const setting_service_1 = require("../services/setting.service");
let AccessTokenGuard = class AccessTokenGuard {
    constructor(jwtService, permissionsService, settingService, cls) {
        this.jwtService = jwtService;
        this.permissionsService = permissionsService;
        this.settingService = settingService;
        this.cls = cls;
    }
    async canActivate(context) {
        const request = context.switchToHttp().getRequest();
        const token = this.extractTokenFromHeader(request);
        if (!token) {
            throw new common_1.UnauthorizedException();
        }
        try {
            const jwtConfiguration = {
                secret: this.settingService.getConfigValue("secret"),
                audience: this.settingService.getConfigValue("audience"),
                issuer: this.settingService.getConfigValue("issuer"),
                accessTokenTtl: this.settingService.getConfigValue("accessTokenTtl"),
                refreshTokenTtl: this.settingService.getConfigValue("refreshTokenTtl")
            };
            const payload = await this.jwtService.verifyAsync(token, jwtConfiguration);
            const permissions = await this.permissionsService.findAllUsingRoles(payload.roles);
            payload.permissions = permissions.map((permission) => permission.name);
            request[constants_1.REQUEST_USER_KEY] = payload;
            this.cls.set(constants_1.REQUEST_USER_KEY, payload);
        }
        catch {
            throw new common_1.UnauthorizedException();
        }
        return true;
    }
    extractTokenFromHeader(request) {
        const [_, token] = request.headers.authorization?.split(' ') ?? [];
        return token;
    }
};
exports.AccessTokenGuard = AccessTokenGuard;
exports.AccessTokenGuard = AccessTokenGuard = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [jwt_1.JwtService,
        permission_metadata_service_1.PermissionMetadataService,
        setting_service_1.SettingService,
        nestjs_cls_1.ClsService])
], AccessTokenGuard);
//# sourceMappingURL=access-token.guard.js.map