33using System . Linq ;
44using System . Text ;
55using System . Threading . Tasks ;
6+ using SecurityService . DataTransferObjects . Requests ;
7+ using SimpleResults ;
68
79namespace 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