Files
production-evyos-systems-an…/ServicesApi/src/middleware/access-control.guard.ts
2025-08-06 16:33:03 +03:00

43 lines
1.4 KiB
TypeScript

import {
CanActivate,
ExecutionContext,
Injectable,
ForbiddenException,
} from '@nestjs/common';
import { RedisHandlers } from '@/src/utils/store/redisHandlers';
import { UrlHandler } from '@/src/utils/navigator/urlHandler';
@Injectable()
export class AuthControlGuard implements CanActivate {
constructor(private cacheService: RedisHandlers) { }
async canActivate(context: ExecutionContext): Promise<boolean> {
const req = context.switchToHttp().getRequest();
const accessToken = this.cacheService.mergeLoginKey(req);
if (!accessToken) { throw new ForbiddenException('Send to Login') }
this.cacheService.renewTtlLoginFromRedis(req);
return true;
}
}
@Injectable()
export class EndpointControlGuard implements CanActivate {
constructor(
private cacheService: RedisHandlers,
private urlHandler: UrlHandler,
) { }
async canActivate(context: ExecutionContext): Promise<boolean> {
const req = context.switchToHttp().getRequest();
const method = req.method;
const path = req.route?.path;
const keyUrl = `${path}:${method.toUpperCase()}`;
const driveToken = await this.urlHandler.getSecureUrlToken(keyUrl);
const accessObject = await this.cacheService.getSelectFromRedis(req);
if (!accessObject) { throw new ForbiddenException('Access denied') }
req.driveToken = `${driveToken}:${accessObject?.value.functionsRetriever}`;
this.cacheService.renewTtlSelectFromRedis(req);
return true;
}
}