validate authentication use case

This commit is contained in:
sbriat
2023-07-11 11:43:56 +02:00
parent 92ce0cd93a
commit 487ae9c38e
13 changed files with 357 additions and 3 deletions

View File

@@ -3,7 +3,7 @@ syntax = "proto3";
package authentication;
service AuthenticationService {
rpc Validate(AuthenticationByUsernamePassword) returns (Id);
rpc Validate(AuthenticationByNamePassword) returns (Id);
rpc Create(Authentication) returns (Id);
rpc AddUsername(Username) returns (Id);
rpc UpdatePassword(Password) returns (Id);
@@ -12,8 +12,8 @@ service AuthenticationService {
rpc Delete(UserId) returns (Empty);
}
message AuthenticationByUsernamePassword {
string username = 1;
message AuthenticationByNamePassword {
string name = 1;
string password = 2;
}

View File

@@ -0,0 +1,11 @@
import { IsNotEmpty, IsString } from 'class-validator';
export class ValidateAuthenticationRequestDto {
@IsString()
@IsNotEmpty()
name: string;
@IsString()
@IsNotEmpty()
password: string;
}

View File

@@ -0,0 +1,37 @@
import {
AggregateID,
IdResponse,
RpcExceptionCode,
RpcValidationPipe,
} from '@mobicoop/ddd-library';
import { Controller, UsePipes } from '@nestjs/common';
import { QueryBus } from '@nestjs/cqrs';
import { GrpcMethod, RpcException } from '@nestjs/microservices';
import { ValidateAuthenticationRequestDto } from './dtos/validate-authentication.request.dto';
import { ValidateAuthenticationQuery } from '@modules/authentication/core/application/queries/validate-authentication/validate-authentication.query';
@UsePipes(
new RpcValidationPipe({
whitelist: true,
forbidUnknownValues: false,
}),
)
@Controller()
export class ValidateAuthenticationGrpcController {
constructor(private readonly queryBus: QueryBus) {}
@GrpcMethod('AuthenticationService', 'Validate')
async validate(data: ValidateAuthenticationRequestDto): Promise<IdResponse> {
try {
const aggregateID: AggregateID = await this.queryBus.execute(
new ValidateAuthenticationQuery(data.name, data.password),
);
return new IdResponse(aggregateID);
} catch (error: any) {
throw new RpcException({
code: RpcExceptionCode.PERMISSION_DENIED,
message: 'Permission denied',
});
}
}
}