mirror of
https://gitlab.com/mobicoop/v3/service/auth.git
synced 2026-01-12 05:42:40 +00:00
plug opa in auth
This commit is contained in:
7
src/modules/authorization/domain/dtos/action.enum.ts
Normal file
7
src/modules/authorization/domain/dtos/action.enum.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export enum Action {
|
||||
create = 'create',
|
||||
read = 'read',
|
||||
update = 'update',
|
||||
delete = 'delete',
|
||||
list = 'list',
|
||||
}
|
||||
20
src/modules/authorization/domain/dtos/decision.request.ts
Normal file
20
src/modules/authorization/domain/dtos/decision.request.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { IsArray, IsNotEmpty, IsString } from 'class-validator';
|
||||
import { Action } from './action.enum';
|
||||
import { Domain } from './domain.enum';
|
||||
|
||||
export class DecisionRequest {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
domain: Domain;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
action: Action;
|
||||
|
||||
@IsArray()
|
||||
context?: Array<{ name: string; value: string }>;
|
||||
}
|
||||
3
src/modules/authorization/domain/dtos/domain.enum.ts
Normal file
3
src/modules/authorization/domain/dtos/domain.enum.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export enum Domain {
|
||||
user = 'user',
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class ValidateAuthorizationRequest {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
uuid: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
action: string;
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
export class Authorization {
|
||||
uuid: string;
|
||||
action: string;
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Action } from '../dtos/action.enum';
|
||||
import { Domain } from '../dtos/domain.enum';
|
||||
|
||||
@Injectable()
|
||||
export abstract class IMakeDecision {
|
||||
abstract decide(
|
||||
uuid: string,
|
||||
domain: Domain,
|
||||
action: Action,
|
||||
context: Array<{ name: string; value: string }>,
|
||||
): Promise<boolean>;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { QueryHandler } from '@nestjs/cqrs';
|
||||
import { OpaDecisionMaker } from '../../adapters/secondaries/opa.decision-maker';
|
||||
import { DecisionQuery } from '../../queries/decision.query';
|
||||
|
||||
@QueryHandler(DecisionQuery)
|
||||
export class DecisionUseCase {
|
||||
constructor(private readonly _decisionMaker: OpaDecisionMaker) {}
|
||||
|
||||
async execute(decisionQuery: DecisionQuery): Promise<boolean> {
|
||||
return this._decisionMaker.decide(
|
||||
decisionQuery.uuid,
|
||||
decisionQuery.domain,
|
||||
decisionQuery.action,
|
||||
decisionQuery.context,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +0,0 @@
|
||||
import { QueryHandler } from '@nestjs/cqrs';
|
||||
import { ValidateAuthorizationQuery } from '../../queries/validate-authorization.query';
|
||||
|
||||
@QueryHandler(ValidateAuthorizationQuery)
|
||||
export class ValidateAuthorizationUseCase {
|
||||
async execute(validate: ValidateAuthorizationQuery): Promise<boolean> {
|
||||
return Promise.resolve(validate.action == 'authorized');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user