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

Commit 2d8ef1e

Browse files
committed
persisted grant
1 parent 4432ca6 commit 2d8ef1e

28 files changed

Lines changed: 7149 additions & 126 deletions

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Jp.Domain.Commands.ApiResource;
88
using Jp.Domain.Commands.Client;
99
using Jp.Domain.Commands.IdentityResource;
10+
using Jp.Domain.Commands.PersistedGrant;
1011
using Jp.Domain.Commands.User;
1112
using Jp.Domain.Commands.UserManagement;
1213

@@ -16,6 +17,11 @@ public class ViewModelToDomainMappingProfile : Profile
1617
{
1718
public ViewModelToDomainMappingProfile()
1819
{
20+
/*
21+
* Persisted grant
22+
*/
23+
CreateMap<RemovePersistedGrantViewModel, RemovePersistedGrantCommand>().ConstructUsing(c => new RemovePersistedGrantCommand(c.Key));
24+
1925
/*
2026
* User Creation Commands
2127
*/
@@ -70,8 +76,8 @@ public ViewModelToDomainMappingProfile()
7076
CreateMap<RemoveApiSecretViewModel, RemoveApiSecretCommand>().ConstructUsing(c => new RemoveApiSecretCommand(c.Id, c.ResourceName));
7177

7278
CreateMap<RemoveApiScopeViewModel, RemoveApiScopeCommand>().ConstructUsing(c => new RemoveApiScopeCommand(c.Id, c.ResourceName));
73-
CreateMap<SaveApiScopeViewModel, SaveApiScopeCommand>().ConstructUsing(c => new SaveApiScopeCommand(c.ResourceName,c.Name, c.Description, c.DisplayName, c.Emphasize, c.ShowInDiscoveryDocument, c.UserClaims));
74-
79+
CreateMap<SaveApiScopeViewModel, SaveApiScopeCommand>().ConstructUsing(c => new SaveApiScopeCommand(c.ResourceName, c.Name, c.Description, c.DisplayName, c.Emphasize, c.ShowInDiscoveryDocument, c.UserClaims));
80+
7581

7682
}
7783
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text;
43
using System.Threading.Tasks;
5-
using IdentityServer4.Models;
64
using Jp.Application.ViewModels;
75

86
namespace Jp.Application.Interfaces
97
{
108
public interface IPersistedGrantAppService: IDisposable
119
{
12-
Task<IEnumerable<PersistedGrant>> GetPersistedGrants();
10+
Task<IEnumerable<PersistedGrantViewModel>> GetPersistedGrants();
11+
Task Remove(RemovePersistedGrantViewModel model);
1312
}
1413
}

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using AutoMapper;
22
using Jp.Application.Interfaces;
3+
using Jp.Application.ViewModels;
34
using Jp.Domain.Core.Bus;
45
using Jp.Domain.Interfaces;
56
using System;
67
using System.Collections.Generic;
78
using System.Linq;
89
using System.Threading.Tasks;
9-
using IdentityServer4.EntityFramework.Mappers;
10-
using IdentityServer4.Models;
10+
using Jp.Domain.Commands.PersistedGrant;
1111

1212
namespace Jp.Application.Services
1313
{
@@ -16,24 +16,36 @@ public class PersistedGrantAppService : IPersistedGrantAppService
1616
private IMapper _mapper;
1717
private IEventStoreRepository _eventStoreRepository;
1818
private readonly IPersistedGrantRepository _persistedGrantRepository;
19+
private readonly IUserService _userService;
1920
public IMediatorHandler Bus { get; set; }
2021

2122
public PersistedGrantAppService(IMapper mapper,
2223
IMediatorHandler bus,
2324
IEventStoreRepository eventStoreRepository,
24-
IPersistedGrantRepository persistedGrantRepository)
25+
IPersistedGrantRepository persistedGrantRepository,
26+
IUserService userService)
2527
{
2628
_mapper = mapper;
2729
Bus = bus;
2830
_eventStoreRepository = eventStoreRepository;
2931
_persistedGrantRepository = persistedGrantRepository;
32+
_userService = userService;
3033
}
3134

32-
public async Task<IEnumerable<PersistedGrant>> GetPersistedGrants()
35+
public async Task<IEnumerable<PersistedGrantViewModel>> GetPersistedGrants()
3336
{
3437
var resultado = await _persistedGrantRepository.GetGrants();
35-
return resultado.Select(s => s.ToModel());
38+
var subjects = await _userService.GetByIdAsync(resultado.Select(s => s.SubjectId).ToArray());
39+
return resultado.Select(s => new PersistedGrantViewModel(s.Key, s.Type, s.SubjectId, s.ClientId, s.CreationTime, s.Expiration, s.Data, subjects.FirstOrDefault(f => f.Id.ToString().ToLower() == s.SubjectId.ToLower())?.Email, subjects.FirstOrDefault(f => f.Id.ToString().ToLower() == s.SubjectId.ToLower())?.Picture));
3640
}
41+
42+
public Task Remove(RemovePersistedGrantViewModel model)
43+
{
44+
// kiss
45+
var command = _mapper.Map<RemovePersistedGrantCommand>(model);
46+
return Bus.SendCommand(command);
47+
}
48+
3749
public void Dispose()
3850
{
3951
GC.SuppressFinalize(this);
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System.ComponentModel.DataAnnotations;
22

3-
namespace Jp.Application.ViewModels.IdentityResourceViewModels
3+
namespace Jp.Application.ViewModels.ApiResouceViewModels
44
{
55
public class RemoveApiResourceViewModel
66
{
77
[Required]
88
public string Name { get; set; }
99
}
10-
1110
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using IdentityServer4.Models;
3+
using Jp.Domain.Interfaces;
4+
5+
namespace Jp.Application.ViewModels
6+
{
7+
public class PersistedGrantViewModel
8+
{
9+
/// <summary>
10+
/// Gets or sets the key.
11+
/// </summary>
12+
/// <value>
13+
/// The key.
14+
/// </value>
15+
public string Key { get; set; }
16+
17+
/// <summary>
18+
/// Gets the type.
19+
/// </summary>
20+
/// <value>
21+
/// The type.
22+
/// </value>
23+
public string Type { get; set; }
24+
25+
/// <summary>
26+
/// Gets the subject identifier.
27+
/// </summary>
28+
/// <value>
29+
/// The subject identifier.
30+
/// </value>
31+
public string SubjectId { get; set; }
32+
33+
/// <summary>
34+
/// Gets the client identifier.
35+
/// </summary>
36+
/// <value>
37+
/// The client identifier.
38+
/// </value>
39+
public string ClientId { get; set; }
40+
41+
/// <summary>
42+
/// Gets or sets the creation time.
43+
/// </summary>
44+
/// <value>
45+
/// The creation time.
46+
/// </value>
47+
public DateTime CreationTime { get; set; }
48+
49+
/// <summary>
50+
/// Gets or sets the expiration.
51+
/// </summary>
52+
/// <value>
53+
/// The expiration.
54+
/// </value>
55+
public DateTime? Expiration { get; set; }
56+
57+
public string Email { get; }
58+
public string Picture { get; }
59+
60+
/// <summary>
61+
/// Gets or sets the data.
62+
/// </summary>
63+
/// <value>
64+
/// The data.
65+
/// </value>
66+
public string Data { get; set; }
67+
public PersistedGrantViewModel() { }
68+
public PersistedGrantViewModel(string key, string type, string subjectId, string clientId, DateTime creationTime, DateTime? expiration, string data, string email, string picture)
69+
{
70+
Key = key;
71+
Type = type;
72+
SubjectId = subjectId;
73+
ClientId = clientId;
74+
CreationTime = creationTime;
75+
Expiration = expiration;
76+
Data = data;
77+
Email = email;
78+
Picture = picture;
79+
}
80+
}
81+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Jp.Application.ViewModels
4+
{
5+
public class RemovePersistedGrantViewModel
6+
{
7+
[Required]
8+
public string Key { get; set; }
9+
}
10+
}

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Jp.Domain.CommandHandlers
1313
{
1414
public class PersistedGrantCommandHandler : CommandHandler,
15-
IRequestHandler<RegisterPersistedGrantCommand>
15+
IRequestHandler<RemovePersistedGrantCommand>
1616
{
1717
private readonly IPersistedGrantRepository _persistedGrantRepository;
1818

@@ -26,7 +26,7 @@ public PersistedGrantCommandHandler(
2626
}
2727

2828

29-
public async Task Handle(RegisterPersistedGrantCommand request, CancellationToken cancellationToken)
29+
public async Task Handle(RemovePersistedGrantCommand request, CancellationToken cancellationToken)
3030
{
3131
if (!request.IsValid())
3232
{
@@ -35,13 +35,12 @@ public async Task Handle(RegisterPersistedGrantCommand request, CancellationToke
3535
}
3636

3737
// Businness logic here
38-
39-
//if (Commit())
40-
//{
41-
// await Bus.RaiseEvent(new PersistedGrantRegisteredEvent(PersistedGrant.Id));
42-
//}
38+
_persistedGrantRepository.Remove(request.Key);
4339

44-
return;
40+
if (Commit())
41+
{
42+
await Bus.RaiseEvent(new PersistedGrantRemovedEvent(request.Key));
43+
}
4544
}
4645

4746
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
using System;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
24
using Jp.Domain.Core.Commands;
35

46
namespace Jp.Domain.Commands.PersistedGrant
57
{
68
public abstract class PersistedGrantCommand : Command
79
{
8-
9-
10-
10+
public string Key { get; set; }
1111
}
12-
}
12+
}

src/Backend/Jp.Domain/Commands/PersistedGrant/RemovePersistedGrantCommand.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ namespace Jp.Domain.Commands.PersistedGrant
44
{
55
public class RemovePersistedGrantCommand : PersistedGrantCommand
66
{
7-
public RemovePersistedGrantCommand()
7+
8+
public RemovePersistedGrantCommand(string key)
89
{
9-
10+
Key = key;
1011
}
1112

1213
public override bool IsValid()

src/Backend/Jp.Domain/EventHandlers/PersistedGrantEventHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ namespace Jp.Domain.EventHandlers
77
{
88

99
public class PersistedGrantEventHandler :
10-
INotificationHandler<PersistedGrantRegisteredEvent>
10+
INotificationHandler<PersistedGrantRemovedEvent>
1111
{
12-
public Task Handle(PersistedGrantRegisteredEvent notification, CancellationToken cancellationToken)
12+
public Task Handle(PersistedGrantRemovedEvent notification, CancellationToken cancellationToken)
1313
{
1414
return Task.CompletedTask;
1515
}

0 commit comments

Comments
 (0)