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

Commit e0b77bd

Browse files
committed
api resource
1 parent e2474f6 commit e0b77bd

42 files changed

Lines changed: 6037 additions & 54 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using Jp.Domain.Core.Events;
88
using Jp.Domain.Models;
99
using System.Globalization;
10+
using Jp.Application.ViewModels.ApiResouceViewModels;
1011

1112
namespace Jp.Application.AutoMapper
1213
{
@@ -22,6 +23,7 @@ public DomainToViewModelMappingProfile()
2223
CreateMap<IdentityServer4.EntityFramework.Entities.ClientProperty, ClientPropertyViewModel>();
2324
CreateMap<IdentityServer4.EntityFramework.Entities.ClientClaim, ClientClaimViewModel>();
2425
CreateMap<IdentityServer4.EntityFramework.Entities.IdentityResource, IdentityResourceListView>(MemberList.Destination);
26+
CreateMap<IdentityServer4.EntityFramework.Entities.ApiScope, ScopeViewModel>(MemberList.Destination);
2527
}
2628
}
2729
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ public ViewModelToDomainMappingProfile()
6969
CreateMap<SaveApiSecretViewModel, SaveApiSecretCommand>().ConstructUsing(c => new SaveApiSecretCommand(c.ResourceName, c.Description, c.Value, c.Type, c.Expiration, (int)c.Hash.GetValueOrDefault(HashType.Sha256)));
7070
CreateMap<RemoveApiSecretViewModel, RemoveApiSecretCommand>().ConstructUsing(c => new RemoveApiSecretCommand(c.Id, c.ResourceName));
7171

72+
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+
7275

7376
}
7477
}
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
4-
using System.Threading.Tasks;
51
using IdentityServer4.Models;
62
using Jp.Application.ViewModels;
73
using Jp.Application.ViewModels.ApiResouceViewModels;
8-
using Jp.Application.ViewModels.ClientsViewModels;
94
using Jp.Application.ViewModels.IdentityResourceViewModels;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Threading.Tasks;
108

119
namespace Jp.Application.Interfaces
1210
{
13-
public interface IApiResourceAppService: IDisposable
11+
public interface IApiResourceAppService : IDisposable
1412
{
1513
Task<IEnumerable<ApiResourceListViewModel>> GetApiResources();
1614
Task<ApiResource> GetDetails(string name);
@@ -20,5 +18,8 @@ public interface IApiResourceAppService: IDisposable
2018
Task<IEnumerable<SecretViewModel>> GetSecrets(string name);
2119
Task RemoveSecret(RemoveApiSecretViewModel model);
2220
Task SaveSecret(SaveApiSecretViewModel model);
21+
Task<IEnumerable<ScopeViewModel>> GetScopes(string name);
22+
Task RemoveScope(RemoveApiScopeViewModel model);
23+
Task SaveScope(SaveApiScopeViewModel model);
2324
}
2425
}

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
51
using AutoMapper;
62
using IdentityServer4.EntityFramework.Mappers;
73
using IdentityServer4.Models;
@@ -12,6 +8,10 @@
128
using Jp.Domain.Commands.ApiResource;
139
using Jp.Domain.Core.Bus;
1410
using Jp.Domain.Interfaces;
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Threading.Tasks;
1515

1616
namespace Jp.Application.Services
1717
{
@@ -21,25 +21,28 @@ public class ApiResourceAppService : IApiResourceAppService
2121
private IEventStoreRepository _eventStoreRepository;
2222
private readonly IApiResourceRepository _apiResourceRepository;
2323
private readonly IApiSecretRepository _secretRepository;
24+
private readonly IApiScopeRepository _apiScopeRepository;
2425
public IMediatorHandler Bus { get; set; }
2526

2627
public ApiResourceAppService(IMapper mapper,
2728
IMediatorHandler bus,
2829
IEventStoreRepository eventStoreRepository,
2930
IApiResourceRepository apiResourceRepository,
30-
IApiSecretRepository secretRepository)
31+
IApiSecretRepository secretRepository,
32+
IApiScopeRepository apiScopeRepository)
3133
{
3234
_mapper = mapper;
3335
Bus = bus;
3436
_eventStoreRepository = eventStoreRepository;
3537
_apiResourceRepository = apiResourceRepository;
3638
_secretRepository = secretRepository;
39+
_apiScopeRepository = apiScopeRepository;
3740
}
3841

39-
public Task<IEnumerable<ApiResourceListViewModel>> GetApiResources()
42+
public async Task<IEnumerable<ApiResourceListViewModel>> GetApiResources()
4043
{
41-
var resultado = _apiResourceRepository.GetAll().Select(s => _mapper.Map<ApiResourceListViewModel>(s)).ToList();
42-
return Task.FromResult<IEnumerable<ApiResourceListViewModel>>(resultado);
44+
var resultado = await _apiResourceRepository.GetResources();
45+
return resultado.Select(s => _mapper.Map<ApiResourceListViewModel>(s)).ToList();
4346
}
4447

4548
public async Task<ApiResource> GetDetails(string name)
@@ -84,6 +87,23 @@ public Task SaveSecret(SaveApiSecretViewModel model)
8487
return Bus.SendCommand(registerCommand);
8588
}
8689

90+
public async Task<IEnumerable<ScopeViewModel>> GetScopes(string name)
91+
{
92+
return _mapper.Map<IEnumerable<ScopeViewModel>>(await _apiScopeRepository.GetScopesByResource(name));
93+
}
94+
95+
public Task RemoveScope(RemoveApiScopeViewModel model)
96+
{
97+
var registerCommand = _mapper.Map<RemoveApiScopeCommand>(model);
98+
return Bus.SendCommand(registerCommand);
99+
}
100+
101+
102+
public Task SaveScope(SaveApiScopeViewModel model)
103+
{
104+
var registerCommand = _mapper.Map<SaveApiScopeCommand>(model);
105+
return Bus.SendCommand(registerCommand);
106+
}
87107

88108
public void Dispose()
89109
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ public class ScopesAppService : IScopesAppService
1414
private IMapper _mapper;
1515
private IEventStoreRepository _eventStoreRepository;
1616
private readonly IIdentityResourceRepository _identityResourcesRepository;
17-
private readonly IApiResourceRepository _apiResourceRepository;
17+
private readonly IApiScopeRepository _apiResourceRepository;
1818
public IMediatorHandler Bus { get; set; }
1919

2020
public ScopesAppService(IMapper mapper,
2121
IMediatorHandler bus,
2222
IEventStoreRepository eventStoreRepository,
2323
IIdentityResourceRepository identityResourcesRepository,
24-
IApiResourceRepository apiResourceRepository)
24+
IApiScopeRepository apiResourceRepository)
2525
{
2626
_mapper = mapper;
2727
Bus = bus;
@@ -38,9 +38,9 @@ public void Dispose()
3838

3939
public async Task<IEnumerable<string>> GetScopes(string search)
4040
{
41-
var identityScopes = await _identityResourcesRepository.GetScopes(search);
42-
var apiScopes = await _apiResourceRepository.GetScopes(search);
43-
identityScopes.AddRange(apiScopes);
41+
var identityScopes = await _identityResourcesRepository.SearchScopes(search);
42+
var apiScopes = await _apiResourceRepository.SearchScopes(search);
43+
identityScopes.AddRange(apiScopes.Select(x => x.Name));
4444
return identityScopes.OrderBy(a => a);
4545
}
4646
}

src/Backend/Jp.Application/ViewModels/ApiResouceViewModels/ApiResourceViewModel.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using System;
2-
using System.ComponentModel.DataAnnotations;
3-
4-
namespace Jp.Application.ViewModels
1+
namespace Jp.Application.ViewModels.ApiResouceViewModels
52
{
63
public class ApiResourceListViewModel
74
{
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace Jp.Application.ViewModels.ApiResouceViewModels
4+
{
5+
public class RemoveApiScopeViewModel
6+
{
7+
[Required]
8+
public int Id { get; set; }
9+
[Required]
10+
public string ResourceName { get; set; }
11+
}
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System.Collections.Generic;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace Jp.Application.ViewModels.ApiResouceViewModels
5+
{
6+
public class SaveApiScopeViewModel
7+
{
8+
[Required]
9+
public string ResourceName { get; set; }
10+
/// <summary>
11+
/// Name of the scope. This is the value a client will use to request the scope.
12+
/// </summary>
13+
[Required]
14+
public string Name { get; set; }
15+
16+
/// <summary>
17+
/// Display name. This value will be used e.g. on the consent screen.
18+
/// </summary>
19+
public string DisplayName { get; set; }
20+
21+
/// <summary>
22+
/// Description. This value will be used e.g. on the consent screen.
23+
/// </summary>
24+
public string Description { get; set; }
25+
26+
/// <summary>
27+
/// Specifies whether the user can de-select the scope on the consent screen. Defaults to false.
28+
/// </summary>
29+
public bool Required { get; set; } = false;
30+
31+
/// <summary>
32+
/// Specifies whether the consent screen will emphasize this scope. Use this setting for sensitive or important scopes. Defaults to false.
33+
/// </summary>
34+
public bool Emphasize { get; set; } = false;
35+
36+
/// <summary>
37+
/// Specifies whether this scope is shown in the discovery document. Defaults to true.
38+
/// </summary>
39+
public bool ShowInDiscoveryDocument { get; set; } = true;
40+
41+
public IEnumerable<string> UserClaims { get; set; } = new HashSet<string>();
42+
}
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Jp.Application.ViewModels
2+
{
3+
public class ScopeViewModel
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; }
7+
public string Description { get; set; }
8+
}
9+
}

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

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using System.Linq;
1+
using IdentityServer4.EntityFramework.Entities;
22
using IdentityServer4.EntityFramework.Mappers;
33
using Jp.Domain.Commands.ApiResource;
44
using Jp.Domain.Core.Bus;
55
using Jp.Domain.Core.Notifications;
66
using Jp.Domain.Events.ApiResource;
77
using Jp.Domain.Interfaces;
88
using MediatR;
9+
using System.Linq;
910
using System.Threading;
1011
using System.Threading.Tasks;
11-
using IdentityServer4.EntityFramework.Entities;
1212

1313
namespace Jp.Domain.CommandHandlers
1414
{
@@ -17,20 +17,25 @@ public class ApiResourceCommandHandler : CommandHandler,
1717
IRequestHandler<UpdateApiResourceCommand>,
1818
IRequestHandler<RemoveApiResourceCommand>,
1919
IRequestHandler<RemoveApiSecretCommand>,
20-
IRequestHandler<SaveApiSecretCommand>
20+
IRequestHandler<SaveApiSecretCommand>,
21+
IRequestHandler<RemoveApiScopeCommand>,
22+
IRequestHandler<SaveApiScopeCommand>
2123
{
2224
private readonly IApiResourceRepository _apiResourceRepository;
2325
private readonly IApiSecretRepository _apiSecretRepository;
26+
private readonly IApiScopeRepository _apiScopeRepository;
2427

2528
public ApiResourceCommandHandler(
2629
IUnitOfWork uow,
2730
IMediatorHandler bus,
2831
INotificationHandler<DomainNotification> notifications,
2932
IApiResourceRepository apiResourceService,
30-
IApiSecretRepository apiSecretRepository) : base(uow, bus, notifications)
33+
IApiSecretRepository apiSecretRepository,
34+
IApiScopeRepository apiScopeRepository) : base(uow, bus, notifications)
3135
{
3236
_apiResourceRepository = apiResourceService;
3337
_apiSecretRepository = apiSecretRepository;
38+
_apiScopeRepository = apiScopeRepository;
3439
}
3540

3641

@@ -168,5 +173,70 @@ public async Task Handle(SaveApiSecretCommand request, CancellationToken cancell
168173
await Bus.RaiseEvent(new ApiSecretSavedEvent(request.Id, request.ResourceName));
169174
}
170175
}
176+
177+
public async Task Handle(RemoveApiScopeCommand request, CancellationToken cancellationToken)
178+
{
179+
if (!request.IsValid())
180+
{
181+
NotifyValidationErrors(request);
182+
return;
183+
}
184+
185+
var savedClient = await _apiResourceRepository.GetResource(request.ResourceName);
186+
if (savedClient == null)
187+
{
188+
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
189+
return;
190+
}
191+
192+
if (savedClient.Scopes.All(f => f.Id != request.Id))
193+
{
194+
await Bus.RaiseEvent(new DomainNotification("3", "Invalid scope"));
195+
return;
196+
}
197+
198+
var scopeToremove = savedClient.Scopes.First(f => f.Id == request.Id);
199+
_apiScopeRepository.Remove(scopeToremove.Id);
200+
201+
if (Commit())
202+
{
203+
await Bus.RaiseEvent(new ApiScopeRemovedEvent(request.Id, request.ResourceName, scopeToremove.Name));
204+
}
205+
}
206+
207+
public async Task Handle(SaveApiScopeCommand request, CancellationToken cancellationToken)
208+
{
209+
if (!request.IsValid())
210+
{
211+
NotifyValidationErrors(request);
212+
return;
213+
}
214+
215+
var savedClient = await _apiResourceRepository.GetByName(request.ResourceName);
216+
if (savedClient == null)
217+
{
218+
await Bus.RaiseEvent(new DomainNotification("1", "Client not found"));
219+
return;
220+
}
221+
222+
var secret = new ApiScope()
223+
{
224+
ApiResource = savedClient,
225+
Description = request.Description,
226+
Required = request.Required,
227+
DisplayName = request.DisplayName,
228+
Emphasize = request.Emphasize,
229+
Name = request.Name,
230+
ShowInDiscoveryDocument = request.ShowInDiscoveryDocument,
231+
UserClaims = request.UserClaims.Select(s => new ApiScopeClaim(){ Type = s}).ToList(),
232+
};
233+
234+
_apiScopeRepository.Add(secret);
235+
236+
if (Commit())
237+
{
238+
await Bus.RaiseEvent(new ApiSecretSavedEvent(request.Id, request.ResourceName));
239+
}
240+
}
171241
}
172242
}

0 commit comments

Comments
 (0)