-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement handler to decrpyt message with symmetric key
- Loading branch information
1 parent
7692ff9
commit c542e2d
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
Modules/Messages/src/Messages.Application/Messages/DTOs/ViewMessageDTO.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
17 changes: 17 additions & 0 deletions
17
...es/Messages/src/Messages.Application/Messages/Queries/ViewMessages/GetViewMessageQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
61 changes: 61 additions & 0 deletions
61
Modules/Messages/src/Messages.Application/Messages/Queries/ViewMessages/Handler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |