Skip to content

Commit 3f3c162

Browse files
Merge pull request #588 from TransactionProcessing/task/#586_workflows_to_return_results
Task/#586 workflows to return results
2 parents c409df2 + c56a761 commit 3f3c162

68 files changed

Lines changed: 1409 additions & 2356 deletions

File tree

Some content is hidden

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

SecurityService.BusinessLogic/RequestHandlers/ApiResourceRequestHandler.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
namespace SecurityService.BusinessLogic.RequestHandlers{
1+
using SecurityService.DataTransferObjects.Requests;
2+
using SimpleResults;
3+
4+
namespace SecurityService.BusinessLogic.RequestHandlers{
25
using System;
36
using System.Collections.Generic;
47
using System.Linq;
@@ -13,9 +16,9 @@
1316
using Requests;
1417
using Shared.Exceptions;
1518

16-
public class ApiResourceRequestHandler : IRequestHandler<CreateApiResourceRequest>,
17-
IRequestHandler<GetApiResourceRequest, ApiResource>,
18-
IRequestHandler<GetApiResourcesRequest, List<ApiResource>>{
19+
public class ApiResourceRequestHandler : IRequestHandler<SecurityServiceCommands.CreateApiResourceCommand, Result>,
20+
IRequestHandler<SecurityServiceQueries.GetApiResourceQuery, Result<ApiResource>>,
21+
IRequestHandler<SecurityServiceQueries.GetApiResourcesQuery, Result<List<ApiResource>>>{
1922
#region Fields
2023

2124
private readonly ConfigurationDbContext ConfigurationDbContext;
@@ -32,19 +35,22 @@ public ApiResourceRequestHandler(ConfigurationDbContext configurationDbContext){
3235

3336
#region Methods
3437

35-
public async Task Handle(CreateApiResourceRequest request, CancellationToken cancellationToken){
36-
ApiResource apiResource = new ApiResource{
37-
ApiSecrets = new List<Secret>{
38-
new Secret(request.Secret.ToSha256())
38+
public async Task<Result> Handle(SecurityServiceCommands.CreateApiResourceCommand command, CancellationToken cancellationToken) {
39+
ApiResource apiResource = new ApiResource
40+
{
41+
ApiSecrets = new List<Secret>{
42+
new Secret(command.Secret.ToSha256())
3943
},
40-
Description = request.Description,
41-
DisplayName = request.DisplayName,
42-
Name = request.Name,
43-
UserClaims = request.UserClaims,
44-
};
45-
46-
if (request.Scopes != null && request.Scopes.Any()){
47-
foreach (String scope in request.Scopes){
44+
Description = command.Description,
45+
DisplayName = command.DisplayName,
46+
Name = command.Name,
47+
UserClaims = command.UserClaims,
48+
};
49+
50+
if (command.Scopes != null && command.Scopes.Any())
51+
{
52+
foreach (String scope in command.Scopes)
53+
{
4854
apiResource.Scopes.Add(scope);
4955
}
5056
}
@@ -54,26 +60,28 @@ public async Task Handle(CreateApiResourceRequest request, CancellationToken can
5460

5561
// Save the changes
5662
await this.ConfigurationDbContext.SaveChangesAsync(cancellationToken);
63+
64+
return Result.Success();
5765
}
5866

59-
public async Task<ApiResource> Handle(GetApiResourceRequest request, CancellationToken cancellationToken){
67+
public async Task<Result<ApiResource>> Handle(SecurityServiceQueries.GetApiResourceQuery query, CancellationToken cancellationToken){
6068
ApiResource apiResourceModel = null;
6169

6270
Duende.IdentityServer.EntityFramework.Entities.ApiResource apiResourceEntity = await this.ConfigurationDbContext.ApiResources
63-
.Where(a => a.Name == request.Name).Include(a => a.Scopes)
71+
.Where(a => a.Name == query.Name).Include(a => a.Scopes)
6472
.Include(a => a.UserClaims)
6573
.SingleOrDefaultAsync(cancellationToken:cancellationToken);
6674

6775
if (apiResourceEntity == null){
68-
throw new NotFoundException($"No Api Resource found with Name [{request.Name}]");
76+
return Result.NotFound($"No Api Resource found with Name [{query.Name}]");
6977
}
7078

7179
apiResourceModel = apiResourceEntity.ToModel();
7280

73-
return apiResourceModel;
81+
return Result.Success(apiResourceModel);
7482
}
7583

76-
public async Task<List<ApiResource>> Handle(GetApiResourcesRequest request, CancellationToken cancellationToken){
84+
public async Task<Result<List<ApiResource>>> Handle(SecurityServiceQueries.GetApiResourcesQuery request, CancellationToken cancellationToken){
7785
List<ApiResource> apiResourceModels = new List<ApiResource>();
7886

7987
List<Duende.IdentityServer.EntityFramework.Entities.ApiResource> apiResourceEntities = await this.ConfigurationDbContext.ApiResources.Include(a => a.Scopes)
@@ -86,7 +94,7 @@ public async Task<List<ApiResource>> Handle(GetApiResourcesRequest request, Canc
8694
}
8795
}
8896

89-
return apiResourceModels;
97+
return Result.Success(apiResourceModels);
9098
}
9199

92100
#endregion

SecurityService.BusinessLogic/RequestHandlers/ApiScopeRequestHandler.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
namespace SecurityService.BusinessLogic.RequestHandlers{
1+
using SecurityService.DataTransferObjects.Requests;
2+
using SimpleResults;
3+
4+
namespace SecurityService.BusinessLogic.RequestHandlers{
5+
using System;
26
using System.Collections.Generic;
37
using System.Linq;
48
using System.Threading;
@@ -11,9 +15,9 @@
1115
using Requests;
1216
using Shared.Exceptions;
1317

14-
public class ApiScopeRequestHandler : IRequestHandler<CreateApiScopeRequest>,
15-
IRequestHandler<GetApiScopeRequest, ApiScope>,
16-
IRequestHandler<GetApiScopesRequest, List<ApiScope>>{
18+
public class ApiScopeRequestHandler : IRequestHandler<SecurityServiceCommands.CreateApiScopeCommand, Result>,
19+
IRequestHandler<SecurityServiceQueries.GetApiScopeQuery, Result<ApiScope>>,
20+
IRequestHandler<SecurityServiceQueries.GetApiScopesQuery, Result<List<ApiScope>>>{
1721
#region Fields
1822

1923
private readonly ConfigurationDbContext ConfigurationDbContext;
@@ -30,41 +34,44 @@ public ApiScopeRequestHandler(ConfigurationDbContext configurationDbContext){
3034

3135
#region Methods
3236

33-
public async Task Handle(CreateApiScopeRequest request, CancellationToken cancellationToken){
34-
ApiScope apiScope = new ApiScope{
35-
Description = request.Description,
36-
DisplayName = request.DisplayName,
37-
Name = request.Name,
38-
Emphasize = false,
39-
Enabled = true,
40-
Required = false,
41-
ShowInDiscoveryDocument = true
42-
};
37+
public async Task<Result> Handle(SecurityServiceCommands.CreateApiScopeCommand command, CancellationToken cancellationToken){
38+
ApiScope apiScope = new ApiScope
39+
{
40+
Description = command.Description,
41+
DisplayName = command.DisplayName,
42+
Name = command.Name,
43+
Emphasize = false,
44+
Enabled = true,
45+
Required = false,
46+
ShowInDiscoveryDocument = true
47+
};
4348

4449
// Now translate the model to the entity
4550
await this.ConfigurationDbContext.ApiScopes.AddAsync(apiScope.ToEntity(), cancellationToken);
4651

4752
// Save the changes
4853
await this.ConfigurationDbContext.SaveChangesAsync(cancellationToken);
54+
55+
return Result.Success();
4956
}
5057

51-
public async Task<ApiScope> Handle(GetApiScopeRequest request, CancellationToken cancellationToken){
58+
public async Task<Result<ApiScope>> Handle(SecurityServiceQueries.GetApiScopeQuery query, CancellationToken cancellationToken){
5259
ApiScope apiScopeModel = null;
5360

54-
Duende.IdentityServer.EntityFramework.Entities.ApiScope apiScopeEntity = await this.ConfigurationDbContext.ApiScopes.Where(a => a.Name == request.Name)
61+
Duende.IdentityServer.EntityFramework.Entities.ApiScope apiScopeEntity = await this.ConfigurationDbContext.ApiScopes.Where(a => a.Name == query.Name)
5562
.Include(a => a.Properties).Include(a => a.UserClaims)
5663
.SingleOrDefaultAsync(cancellationToken:cancellationToken);
5764

5865
if (apiScopeEntity == null){
59-
throw new NotFoundException($"No Api Scope found with Name [{request.Name}]");
66+
return Result.NotFound($"No Api Scope found with Name [{query.Name}]");
6067
}
6168

6269
apiScopeModel = apiScopeEntity.ToModel();
6370

64-
return apiScopeModel;
71+
return Result.Success(apiScopeModel);
6572
}
6673

67-
public async Task<List<ApiScope>> Handle(GetApiScopesRequest request, CancellationToken cancellationToken){
74+
public async Task<Result<List<ApiScope>>> Handle(SecurityServiceQueries.GetApiScopesQuery query, CancellationToken cancellationToken){
6875
List<ApiScope> apiScopeModels = new List<ApiScope>();
6976

7077
List<Duende.IdentityServer.EntityFramework.Entities.ApiScope> apiScopeEntities = await this.ConfigurationDbContext.ApiScopes.Include(a => a.Properties)
@@ -77,7 +84,7 @@ public async Task<List<ApiScope>> Handle(GetApiScopesRequest request, Cancellati
7784
}
7885
}
7986

80-
return apiScopeModels;
87+
return Result.Success(apiScopeModels);
8188
}
8289

8390
#endregion

SecurityService.BusinessLogic/RequestHandlers/ClientRequestHandler.cs

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.Linq;
44
using System.Text;
55
using System.Threading.Tasks;
6+
using SecurityService.DataTransferObjects.Requests;
7+
using SimpleResults;
68

79
namespace SecurityService.BusinessLogic.RequestHandlers
810
{
@@ -19,48 +21,57 @@ namespace SecurityService.BusinessLogic.RequestHandlers
1921
using Client = Duende.IdentityServer.Models.Client;
2022
using Secret = Duende.IdentityServer.Models.Secret;
2123

22-
public class ClientRequestHandler : IRequestHandler<CreateClientRequest>,
23-
IRequestHandler<GetClientRequest, Client>,
24-
IRequestHandler<GetClientsRequest, List<Client>>{
24+
public class ClientRequestHandler : IRequestHandler<SecurityServiceCommands.CreateClientCommand, Result>,
25+
IRequestHandler<SecurityServiceQueries.GetClientQuery, Result<Client>>,
26+
IRequestHandler<SecurityServiceQueries.GetClientsQuery, Result<List<Client>>>{
2527
private readonly ConfigurationDbContext ConfigurationDbContext;
2628

2729
public ClientRequestHandler(ConfigurationDbContext configurationDbContext){
2830
this.ConfigurationDbContext = configurationDbContext;
2931
}
3032

31-
public async Task Handle(CreateClientRequest request, CancellationToken cancellationToken){
33+
public async Task<Result> Handle(SecurityServiceCommands.CreateClientCommand command, CancellationToken cancellationToken){
3234
// Validate the grant types list
33-
this.ValidateGrantTypes(request.AllowedGrantTypes);
35+
Result validationResult = this.ValidateGrantTypes(command.AllowedGrantTypes);
36+
if (validationResult.IsFailed) {
37+
return validationResult;
38+
}
3439

3540
// Create the model from the request
36-
Client client = new Client{
37-
ClientId = request.ClientId,
38-
ClientName = request.ClientName,
39-
Description = request.ClientDescription,
40-
ClientSecrets ={
41-
new Secret(request.Secret.ToSha256())
41+
Client client = new Client
42+
{
43+
ClientId = command.ClientId,
44+
ClientName = command.ClientName,
45+
Description = command.ClientDescription,
46+
ClientSecrets ={
47+
new Secret(command.Secret.ToSha256())
4248
},
43-
AllowedGrantTypes = request.AllowedGrantTypes,
44-
AllowedScopes = request.AllowedScopes,
45-
RequireConsent = request.RequireConsent,
46-
AllowOfflineAccess = request.AllowOfflineAccess,
47-
ClientUri = request.ClientUri
48-
};
49-
50-
if (request.AllowedGrantTypes.Contains("hybrid")){
49+
AllowedGrantTypes = command.AllowedGrantTypes,
50+
AllowedScopes = command.AllowedScopes,
51+
RequireConsent = command.RequireConsent,
52+
AllowOfflineAccess = command.AllowOfflineAccess,
53+
ClientUri = command.ClientUri
54+
};
55+
56+
if (command.AllowedGrantTypes.Contains("hybrid"))
57+
{
5158
client.RequirePkce = false;
5259
}
5360

54-
if (request.ClientRedirectUris != null && request.ClientRedirectUris.Any()){
61+
if (command.ClientRedirectUris != null && command.ClientRedirectUris.Any())
62+
{
5563
client.RedirectUris = new List<String>();
56-
foreach (String clientRedirectUri in request.ClientRedirectUris){
64+
foreach (String clientRedirectUri in command.ClientRedirectUris)
65+
{
5766
client.RedirectUris.Add(clientRedirectUri);
5867
}
5968
}
6069

61-
if (request.ClientPostLogoutRedirectUris != null && request.ClientPostLogoutRedirectUris.Any()){
70+
if (command.ClientPostLogoutRedirectUris != null && command.ClientPostLogoutRedirectUris.Any())
71+
{
6272
client.PostLogoutRedirectUris = new List<String>();
63-
foreach (String clientPostLogoutRedirectUri in request.ClientPostLogoutRedirectUris){
73+
foreach (String clientPostLogoutRedirectUri in command.ClientPostLogoutRedirectUris)
74+
{
6475
client.PostLogoutRedirectUris.Add(clientPostLogoutRedirectUri);
6576
}
6677
}
@@ -70,26 +81,28 @@ public async Task Handle(CreateClientRequest request, CancellationToken cancella
7081

7182
// Save the changes
7283
await this.ConfigurationDbContext.SaveChangesAsync(cancellationToken);
84+
85+
return Result.Success();
7386
}
7487

75-
public async Task<Client> Handle(GetClientRequest request, CancellationToken cancellationToken){
88+
public async Task<Result<Client>> Handle(SecurityServiceQueries.GetClientQuery query, CancellationToken cancellationToken){
7689
Client clientModel = null;
7790

7891
Duende.IdentityServer.EntityFramework.Entities.Client clientEntity = await this.ConfigurationDbContext.Clients.Include(c => c.AllowedGrantTypes)
79-
.Include(c => c.AllowedScopes).Where(c => c.ClientId == request.ClientId)
92+
.Include(c => c.AllowedScopes).Where(c => c.ClientId == query.ClientId)
8093
.SingleOrDefaultAsync(cancellationToken: cancellationToken);
8194

8295
if (clientEntity == null)
8396
{
84-
throw new NotFoundException($"No client found with Client Id [{request.ClientId}]");
97+
return Result.NotFound($"No client found with Client Id [{query.ClientId}]");
8598
}
8699

87100
clientModel = clientEntity.ToModel();
88101

89-
return clientModel;
102+
return Result.Success(clientModel);
90103
}
91104

92-
public async Task<List<Client>> Handle(GetClientsRequest request, CancellationToken cancellationToken){
105+
public async Task<Result<List<Client>>> Handle(SecurityServiceQueries.GetClientsQuery query, CancellationToken cancellationToken){
93106
List<Client> clientModels = new List<Client>();
94107

95108
List<Duende.IdentityServer.EntityFramework.Entities.Client> clientEntities = await this.ConfigurationDbContext.Clients.Include(c => c.AllowedGrantTypes)
@@ -104,10 +117,10 @@ public async Task<List<Client>> Handle(GetClientsRequest request, CancellationTo
104117
}
105118
}
106119

107-
return clientModels;
120+
return Result.Success(clientModels);
108121
}
109122

110-
private void ValidateGrantTypes(List<String> allowedGrantTypes){
123+
private Result ValidateGrantTypes(List<String> allowedGrantTypes){
111124
// Get a list of valid grant types
112125
List<String> validTypesList = new List<String>();
113126

@@ -121,8 +134,9 @@ private void ValidateGrantTypes(List<String> allowedGrantTypes){
121134
List<String> invalidGrantTypes = allowedGrantTypes.Where(a => validTypesList.All(v => v != a)).ToList();
122135

123136
if (invalidGrantTypes.Any()){
124-
throw new ArgumentException(nameof(allowedGrantTypes), $"The grant types [{String.Join(", ", invalidGrantTypes)}] are not valid to create a new client");
137+
return Result.Invalid($"The grant types [{String.Join(", ", invalidGrantTypes)}] are not valid to create a new client");
125138
}
139+
return Result.Success();
126140
}
127141
}
128142
}

0 commit comments

Comments
 (0)