41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { HttpService } from '@nestjs/axios';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { lastValueFrom } from 'rxjs';
|
|
import { Action } from '../../domain/dtos/action.enum';
|
|
import { Domain } from '../../domain/dtos/domain.enum';
|
|
import { IMakeDecision } from '../../domain/interfaces/decision-maker';
|
|
import { ContextItem } from '../../domain/dtos/context-item';
|
|
import { Decision } from './decision';
|
|
import { Authorization } from '../../domain/entities/authorization';
|
|
|
|
@Injectable()
|
|
export class OpaDecisionMaker extends IMakeDecision {
|
|
constructor(
|
|
private readonly _configService: ConfigService,
|
|
private readonly _httpService: HttpService,
|
|
) {
|
|
super();
|
|
}
|
|
|
|
async decide(
|
|
uuid: string,
|
|
domain: Domain,
|
|
action: Action,
|
|
context: Array<ContextItem>,
|
|
): Promise<Authorization> {
|
|
const { data } = await lastValueFrom(
|
|
this._httpService.post<Decision>(
|
|
this._configService.get<string>('OPA_URL') + domain + '/' + action,
|
|
{
|
|
input: {
|
|
uuid,
|
|
...context,
|
|
},
|
|
},
|
|
),
|
|
);
|
|
return new Authorization(data.result.allow);
|
|
}
|
|
}
|