Skip to content

Commit

Permalink
feat: implement handler to decrpyt message with symmetric key
Browse files Browse the repository at this point in the history
  • Loading branch information
HunorTotBagi committed Feb 9, 2024
1 parent 7692ff9 commit c542e2d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.Mapping;
using Backbone.DevelopmentKit.Identity.ValueObjects;
using Backbone.Modules.Messages.Domain.Entities;
using Backbone.Modules.Messages.Domain.Ids;

namespace Backbone.Modules.Messages.Application.Messages.DTOs;

public class ViewMessageDTO : IMapTo<Message>
{
public MessageId Id { get; set; }

public DateTime CreatedAt { get; set; }

public string Body { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Backbone.Modules.Messages.Application.Messages.DTOs;
using Backbone.Modules.Messages.Domain.Entities;
using Backbone.Modules.Messages.Domain.Ids;
using MediatR;

namespace Backbone.Modules.Messages.Application.Messages.Queries.ViewMessages;

public class GetViewMessageQuery : IRequest<ViewMessageDTO>
{
public MessageId MessageId { get; set; }
public string SymmetricKey { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.Mapping;
using Backbone.BuildingBlocks.Application.Abstractions.Infrastructure.UserContext;
using Backbone.Modules.Messages.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Messages.Application.Messages.DTOs;
using Backbone.Modules.Messages.Application.Messages.Queries.GetMessage;
using Backbone.Modules.Messages.Domain.Entities;
using Backbone.Modules.Messages.Domain.Ids;
using MediatR;
using NSec.Cryptography;

namespace Backbone.Modules.Messages.Application.Messages.Queries.ViewMessages;
public class Handler : IRequestHandler<GetViewMessageQuery, ViewMessageDTO>
{
private readonly IMessagesRepository _messagesRepository;
private readonly IMapper _mapper;
private readonly IUserContext _userContext;

public Handler()
{
}

public Handler(IMapper mapper)
{
_mapper = mapper;
}

public Handler(IUserContext userContext, IMapper mapper, IMessagesRepository messagesRepository)
{
_userContext = userContext;
_mapper = mapper;
_messagesRepository = messagesRepository;
}

public async Task<ViewMessageDTO> Handle(GetViewMessageQuery request, CancellationToken cancellationToken)
{
if (request.MessageId == null || request.SymmetricKey == null)
{
throw new NullReferenceException(nameof(request));
}

var message = await _messagesRepository.Find(request.MessageId, _userContext.GetAddress(), cancellationToken, track: true);

var decryptedBody = message.Decrypt(request.SymmetricKey);

// Create ViewMessageDTO object
var response = new ViewMessageDTO
{
Id = message.Id,
CreatedAt = message.CreatedAt,
Body = decryptedBody
};

return response;
}
}

0 comments on commit c542e2d

Please sign in to comment.