Skip to content
This repository was archived by the owner on Aug 1, 2021. It is now read-only.

Commit 2f66588

Browse files
committed
secrets
1 parent a81a94b commit 2f66588

31 files changed

Lines changed: 10517 additions & 27 deletions

src/Backend/Jp.Application/AutoMapper/DomainToViewModelMappingProfile.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public DomainToViewModelMappingProfile()
1717
CreateMap<User, UserViewModel>().ForMember(a => a.Password, o => o.Ignore()).ForMember(a => a.ConfirmPassword, o => o.Ignore());
1818
CreateMap<StoredEvent, EventHistoryData>().ConstructUsing(a => new EventHistoryData() { Action = a.MessageType, Id = a.Id.ToString(), Details = a.Data, When = a.Timestamp.ToString(CultureInfo.InvariantCulture), Who = a.User });
1919
CreateMap<Client, ClientListViewModel>(MemberList.Destination);
20+
CreateMap<IdentityServer4.EntityFramework.Entities.Secret, SecretViewModel>(MemberList.Destination);
2021
}
2122
}
2223
}

src/Backend/Jp.Application/AutoMapper/ViewModelToDomainMappingProfile.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ public ViewModelToDomainMappingProfile()
2929
CreateMap<ProfileViewModel, UpdateProfileCommand>().ConstructUsing(c => new UpdateProfileCommand(c.Id, c.Url, c.Bio, c.Company, c.JobTitle, c.Name, c.PhoneNumber));
3030
CreateMap<ProfilePictureViewModel, UpdateProfilePictureCommand>().ConstructUsing(c => new UpdateProfilePictureCommand(c.Id));
3131

32-
CreateMap<ChangePasswordViewModel, ChangePasswordCommand>().ConstructUsing(c => new ChangePasswordCommand(c.Id,c.OldPassword, c.NewPassword, c.ConfirmPassword));
32+
CreateMap<ChangePasswordViewModel, ChangePasswordCommand>().ConstructUsing(c => new ChangePasswordCommand(c.Id, c.OldPassword, c.NewPassword, c.ConfirmPassword));
3333
CreateMap<SetPasswordViewModel, SetPasswordCommand>().ConstructUsing(c => new SetPasswordCommand(c.Id, c.NewPassword, c.ConfirmPassword));
3434
CreateMap<RemoveAccountViewModel, RemoveAccountCommand>().ConstructUsing(c => new RemoveAccountCommand(c.Id));
3535

3636
/*
3737
* Client commands
3838
*/
3939
CreateMap<Client, UpdateClientCommand>().ConstructUsing(c => new UpdateClientCommand(c));
40+
CreateMap<RemoveSecretViewModel, RemoveSecretCommand>().ConstructUsing(c => new RemoveSecretCommand(c.Id, c.ClientId));
41+
CreateMap<SaveClientSecretViewModel, SaveClientSecretCommand>().ConstructUsing(c => new SaveClientSecretCommand(c.ClientId, c.Description, c.Value, c.Type, c.Expiration, (int)c.Hash.GetValueOrDefault(HashType.Sha256)));
4042
}
4143
}
4244
}

src/Backend/Jp.Application/Interfaces/IClientAppService.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface IClientAppService: IDisposable
1111
Task<IEnumerable<ClientListViewModel>> GetClients();
1212
Task<Client> GetClientDetails(string clientId);
1313
Task Update(Client client);
14-
Task<IEnumerable<Secret>> GetSecrets(string clientId);
14+
Task<IEnumerable<SecretViewModel>> GetSecrets(string clientId);
15+
Task RemoveSecret(RemoveSecretViewModel model);
16+
Task SaveSecret(SaveClientSecretViewModel model);
1517
}
1618
}

src/Backend/Jp.Application/Services/ClientAppService.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Task<IEnumerable<ClientListViewModel>> GetClients()
3939

4040
public async Task<Client> GetClientDetails(string clientId)
4141
{
42-
var resultado = await _clientRepository.GetByUniqueName(clientId);
42+
var resultado = await _clientRepository.GetClient(clientId);
4343
return _mapper.Map<Client>(resultado);
4444
}
4545

@@ -49,15 +49,27 @@ public Task Update(Client client)
4949
return Bus.SendCommand(registerCommand);
5050
}
5151

52-
public async Task<IEnumerable<Secret>> GetSecrets(string clientId)
52+
public async Task<IEnumerable<SecretViewModel>> GetSecrets(string clientId)
5353
{
54-
return _mapper.Map<IEnumerable<Secret>>(await _clientRepository.GetSecrets(clientId));
54+
return _mapper.Map<IEnumerable<SecretViewModel>>(await _clientRepository.GetSecrets(clientId));
5555
}
5656

57+
public Task RemoveSecret(RemoveSecretViewModel model)
58+
{
59+
var registerCommand = _mapper.Map<RemoveSecretCommand>(model);
60+
return Bus.SendCommand(registerCommand);
61+
}
62+
63+
public Task SaveSecret(SaveClientSecretViewModel model)
64+
{
65+
var registerCommand = _mapper.Map<SaveClientSecretCommand>(model);
66+
return Bus.SendCommand(registerCommand);
67+
}
5768

5869
public void Dispose()
5970
{
6071
GC.SuppressFinalize(this);
6172
}
6273
}
74+
6375
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Text;
4+
5+
namespace Jp.Application.ViewModels.ClientsViewModels
6+
{
7+
public class RemoveSecretViewModel
8+
{
9+
[Required]
10+
public int Id { get; set; }
11+
[Required]
12+
public string ClientId { get; set; }
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace Jp.Application.ViewModels.ClientsViewModels
5+
{
6+
public class SaveClientSecretViewModel
7+
{
8+
public string Description { get; set; }
9+
[Required]
10+
public string Value { get; set; }
11+
public DateTime? Expiration { get; set; }
12+
[Required]
13+
public HashType? Hash { get; set; } = 0;
14+
[Required]
15+
public string Type { get; set; }
16+
[Required]
17+
public string ClientId { get; set; }
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Jp.Application.ViewModels.ClientsViewModels
6+
{
7+
public class SecretViewModel
8+
{
9+
public int Id { get; set; }
10+
public string Description { get; set; }
11+
12+
public string Value { get; set; }
13+
public DateTime? Expiration { get; set; }
14+
15+
public HashType Hash { get; set; } = 0;
16+
public string Type { get; set; }
17+
}
18+
public enum HashType
19+
{
20+
Sha256,
21+
Sha512
22+
}
23+
}

src/Backend/Jp.Domain/CommandHandlers/ClientCommandHandler.cs

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,35 @@
1-
using System;
1+
using System.Linq;
22
using System.Threading;
33
using System.Threading.Tasks;
4+
using IdentityServer4.EntityFramework.Entities;
45
using IdentityServer4.EntityFramework.Mappers;
56
using Jp.Domain.Commands.Client;
67
using Jp.Domain.Core.Bus;
78
using Jp.Domain.Core.Notifications;
89
using Jp.Domain.Events.Client;
910
using Jp.Domain.Interfaces;
10-
using Jp.Domain.Models;
1111
using MediatR;
1212

1313
namespace Jp.Domain.CommandHandlers
1414
{
1515
public class ClientCommandHandler : CommandHandler,
1616
IRequestHandler<RegisterClientCommand>,
17-
IRequestHandler<UpdateClientCommand>
17+
IRequestHandler<UpdateClientCommand>,
18+
IRequestHandler<RemoveSecretCommand>,
19+
IRequestHandler<SaveClientSecretCommand>
1820
{
1921
private readonly IClientRepository _clientRepository;
22+
private readonly IClientSecretRepository _clientSecretRepository;
2023

2124
public ClientCommandHandler(
2225
IUnitOfWork uow,
2326
IMediatorHandler bus,
2427
INotificationHandler<DomainNotification> notifications,
25-
IClientRepository clientRepository) : base(uow, bus, notifications)
28+
IClientRepository clientRepository,
29+
IClientSecretRepository clientSecretRepository) : base(uow, bus, notifications)
2630
{
2731
_clientRepository = clientRepository;
32+
_clientSecretRepository = clientSecretRepository;
2833
}
2934

3035

@@ -51,11 +56,11 @@ public async Task Handle(UpdateClientCommand request, CancellationToken cancella
5156
if (!request.IsValid())
5257
{
5358
NotifyValidationErrors(request);
54-
return;
59+
return;
5560
}
5661

5762
// Businness logic here
58-
var savedClient = await _clientRepository.GetByUniqueName(request.Client.ClientId);
63+
var savedClient = await _clientRepository.GetClient(request.Client.ClientId);
5964
if (savedClient == null)
6065
{
6166
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
@@ -72,5 +77,68 @@ public async Task Handle(UpdateClientCommand request, CancellationToken cancella
7277
}
7378

7479
}
80+
81+
public async Task Handle(RemoveSecretCommand request, CancellationToken cancellationToken)
82+
{
83+
if (!request.IsValid())
84+
{
85+
NotifyValidationErrors(request);
86+
return;
87+
}
88+
89+
var savedClient = await _clientRepository.GetClient(request.ClientId);
90+
if (savedClient == null)
91+
{
92+
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
93+
return;
94+
}
95+
96+
if (savedClient.ClientSecrets.All(f => f.Id != request.Id))
97+
{
98+
await Bus.RaiseEvent(new DomainNotification("2", "Invalid secret"));
99+
return;
100+
}
101+
102+
_clientSecretRepository.Remove(request.Id);
103+
104+
if (Commit())
105+
{
106+
await Bus.RaiseEvent(new ClientSecretRemovedEvent(request.Id, request.ClientId));
107+
}
108+
}
109+
110+
public async Task Handle(SaveClientSecretCommand request, CancellationToken cancellationToken)
111+
{
112+
if (!request.IsValid())
113+
{
114+
NotifyValidationErrors(request);
115+
return;
116+
}
117+
118+
var savedClient = await _clientRepository.GetByClientId(request.ClientId);
119+
if (savedClient == null)
120+
{
121+
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
122+
return;
123+
}
124+
125+
var secret = new ClientSecret
126+
{
127+
Client = savedClient,
128+
Description = request.Description,
129+
Expiration = request.Expiration,
130+
Type = request.Type,
131+
Value = request.GetValue()
132+
};
133+
134+
_clientSecretRepository.Add(secret);
135+
136+
if (Commit())
137+
{
138+
await Bus.RaiseEvent(new NewClientSecretEvent(request.Id, request.ClientId, secret.Type, secret.Description));
139+
}
140+
}
75141
}
142+
143+
76144
}

src/Backend/Jp.Domain/Commands/Client/ClientCommand.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ namespace Jp.Domain.Commands.Client
66
public abstract class ClientCommand : Command
77
{
88
public IdentityServer4.Models.Client Client { get; set; }
9+
910
}
1011
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Jp.Domain.Core.Commands;
5+
6+
namespace Jp.Domain.Commands.Client
7+
{
8+
public abstract class ClientSecretCommand : Command
9+
{
10+
public int Id { get; set; }
11+
public string ClientId { get; set; }
12+
public string Description { get; set; }
13+
14+
public string Value { get; set; }
15+
public DateTime? Expiration { get; set; }
16+
17+
public int Hash { get; set; } = 0;
18+
public string Type { get; set; }
19+
}
20+
}

0 commit comments

Comments
 (0)