Пример #1
0
        public async Task <ActionResult <UpdateClientSecretResponse> > HandleAsync(int id, RegenerateClientSecretRequest request,
                                                                                   CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(ClientSecret), ToasterType.Success, ToasterVerbs.Updated);
            var validation = await _validator
                             .ValidateAsync <RegenerateClientSecretRequest, UpdateClientSecretResponse, RegenerateRequestValidator>
                                 (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (!_repository.Query().Any(e => e.Id == id && e.Type != AppConstants.SecretTypes.VisibleOneTime))
            {
                return(NotFound(id));
            }

            if (_repository.Query().Any(e => e.Value == request.Value && e.Type != AppConstants.SecretTypes.VisibleOneTime))
            {
                return(AspExtensions.GetBadRequestWithError <UpdateClientSecretResponse>("This client secret already exists."));
            }

            var entity = await _repository.Query().FirstOrDefaultAsync(e => e.Id == id, cancellationToken);

            entity.Value = request.Value.Stamp();

            await _repository.SaveAsync(toaster);

            return(validation.Response);
        }
Пример #2
0
        public override async Task <ActionResult <InsertApiScopeResponse> > HandleAsync(InsertApiScopeRequest request,
                                                                                        CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(ApiScope), ToasterType.Success, ToasterVerbs.Created);
            var validation =
                await _validator.ValidateAsync <InsertApiScopeRequest, InsertApiScopeResponse, InsertApiScopeValidator>
                    (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (_repository.Query().Any(e => e.Name == request.Name))
            {
                return(AspExtensions.GetBadRequestWithError <InsertApiScopeResponse>(
                           $"API Scope \"{request.Name}\" already exists."));
            }

            var entity = new ApiScope
            {
                Name        = request.Name,
                DisplayName = request.DisplayName,
                Description = request.Description
            };

            toaster.Identifier = entity.Name;
            await _repository.InsertAsync(entity);

            await _repository.SaveAsync(toaster);

            return(validation.Response);
        }
Пример #3
0
        public override async Task <ActionResult <InsertClientSecretResponse> > HandleAsync(InsertClientSecretRequest request,
                                                                                            CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(ClientSecret), ToasterType.Success, ToasterVerbs.Created);
            var validation =
                await _validator.ValidateAsync <InsertClientSecretRequest, InsertClientSecretResponse, InsertRequestValidator>
                    (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (_repository.Query().Any(e => e.Value == request.Value && e.Type != AppConstants.SecretTypes.VisibleOneTime))
            {
                return(AspExtensions.GetBadRequestWithError <InsertClientSecretResponse>("This client secret already exists."));
            }

            var entity = new ClientSecret
            {
                Created     = DateTime.UtcNow,
                Description = request.Description,
                Type        = request.Type,
                Expiration  = request.Expiration,
                Value       = request.Value.Stamp(),
                ClientId    = request.OwnerId
            };

            await _repository.InsertAsync(entity);

            await _repository.SaveAsync(toaster);

            return(validation.Response);
        }
Пример #4
0
        public async Task <ActionResult <UpdateClientResponse> > HandleAsync(int id, UpdateClientRequest request,
                                                                             CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(Client), ToasterType.Success, ToasterVerbs.Updated);
            var validation =
                await _validator.ValidateAsync <UpdateClientRequest, UpdateClientResponse, UpdateClientValidator>
                    (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            // if (!_clientRepository.Query().Any(e => e.Id == id && e.ClientId != AppConstants.ReadOnlyEntities.AngularClient))
            if (!_clientRepository.Query().Any(e => e.Id == id))
            {
                return(NotFound(id));
            }

            if (_clientRepository.Query()
                .Any(e => (e.ClientId == request.Name || e.ClientUri == request.ClientUri) && e.Id != id))
            {
                return(AspExtensions.GetBadRequestWithError <UpdateClientResponse>($"Client \"{request.Name}\" already exists."));
            }

            await _clientRepository.Query().Where(e => e.Id == id).UpdateAsync(x => new Client
            {
                ClientId                    = request.Name,
                ClientName                  = request.DisplayName,
                Description                 = request.Description,
                NonEditable                 = request.IsReadOnly,
                Updated                     = DateTime.UtcNow,
                RequirePkce                 = request.RequirePkce,
                AccessTokenLifetime         = request.AccessTokenLifetime * 60, // transform minutes in seconds,
                ClientUri                   = request.ClientUri,
                AllowOfflineAccess          = request.AllowOfflineAccess,
                RequireClientSecret         = request.RequireClientSecret,
                AllowAccessTokensViaBrowser = request.AllowAccessTokensViaBrowser
            }, cancellationToken);

            // Assignment of API Scopes
            if (request.ApiScopes != null && request.ApiScopes.Length > 0)
            {
                // Clean current relations
                await _clientScopeRepository.Query().Where(x => x.ClientId == id).DeleteAsync(cancellationToken);

                // Add new ones
                var clientApiScopes = request.ApiScopes.Select(x => new ClientScope {
                    Scope = x, ClientId = id
                });
                await _clientScopeRepository.InsertRange(clientApiScopes);

                await _clientRepository.SaveAsync(toaster);
            }

            return(validation.Response);
        }
Пример #5
0
        public async Task <ActionResult <UpdateApiScopeResponse> > HandleAsync(int id, UpdateApiScopeRequest request,
                                                                               CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(ApiScope), ToasterType.Success, ToasterVerbs.Updated);
            var validation =
                await _validator.ValidateAsync <UpdateApiScopeRequest, UpdateApiScopeResponse, UpdateApiScopeValidator>
                    (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (!_apiScopeRepository.Query()
                .Any(e => e.Id == id && e.Name != AppConstants.ReadOnlyEntities.IdentityControlApiScope))
            {
                return(NotFound(id));
            }

            if (_apiScopeRepository.Query().Any(e => e.Name == request.Name && e.Id != id))
            {
                return(AspExtensions.GetBadRequestWithError <UpdateApiScopeResponse>(
                           $"API Scope \"{request.Name}\" already exists."));
            }

            var entity = await _apiScopeRepository.Query().FirstOrDefaultAsync(e => e.Id == id, cancellationToken);

            if (entity.Name != request.Name)
            {
                await _clientScopeRepository.Query()
                .Where(x => x.Scope == entity.Name)
                .UpdateAsync(x => new ClientScope {
                    Scope = request.Name
                }, cancellationToken);

                await _apiResourceScopeRepo.Query()
                .Where(x => x.Scope == entity.Name)
                .UpdateAsync(x => new ApiResourceScope {
                    Scope = request.Name
                }, cancellationToken);

                entity.Name = request.Name;
            }

            entity.Description = request.Description;
            entity.DisplayName = request.DisplayName;

            toaster.Identifier = entity.Name;
            await _apiScopeRepository.SaveAsync(toaster);

            return(validation.Response);
        }
Пример #6
0
        public async Task <ActionResult <UpdateApiResourceResponse> > HandleAsync(int id, UpdateApiResourceRequest request,
                                                                                  CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(ApiScope), ToasterType.Success, ToasterVerbs.Updated);
            var validation =
                await _validator.ValidateAsync <UpdateApiResourceRequest, UpdateApiResourceResponse, UpdateApiResourceValidator>
                    (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (!_apiResourceRepository.Query()
                .Any(e => e.Id == id && e.Name != AppConstants.ReadOnlyEntities.IdentityControlApiScope))
            {
                return(NotFound(id));
            }

            if (_apiResourceRepository.Query().Any(e => e.Name == request.Name && e.Id != id))
            {
                return(AspExtensions.GetBadRequestWithError <UpdateApiResourceResponse>(
                           $"API Resource \"{request.Name}\" already exists."));
            }

            await _apiResourceRepository.Query().Where(e => e.Id == id)
            .UpdateAsync(x => new ApiResource
            {
                Name        = request.Name,
                Description = request.Description,
                DisplayName = request.DisplayName
            }, cancellationToken);

            // Assignment of API Scopes
            if (request.ApiScopes != null && request.ApiScopes.Length > 0)
            {
                // Clean current relations
                await _apiResourceScopeRepo.Query().Where(x => x.ApiResourceId == id).DeleteAsync(cancellationToken);

                // Add new relations
                var apiResourceScopes =
                    request.ApiScopes.Select(x => new ApiResourceScope {
                    Scope = x, ApiResourceId = id
                });
                await _apiResourceScopeRepo.InsertRange(apiResourceScopes);

                await _apiResourceRepository.SaveAsync(toaster);
            }

            return(validation.Response);
        }
        public override async Task <ActionResult <UpdateApiResourceSecretResponse> > HandleAsync(
            RegenerateApiResourceSecretRequest[] request,
            CancellationToken cancellationToken = default)
        {
            var secretsCount = await _repository.Query()
                               .Where(entity => request.Select(r => r.Id)
                                      .Contains(entity.Id) && entity.Type != AppConstants.SecretTypes.VisibleOneTime)
                               .CountAsync(cancellationToken);

            if (secretsCount == 0 || secretsCount < request.Length)
            {
                return(NotFound("One ore more instances could not be updated"));
            }

            var toaster = new ToasterEvent(nameof(ApiResourceSecret), ToasterType.Success, ToasterVerbs.Updated, null,
                                           request.Length);

            foreach (var item in request)
            {
                var validation = await _validator
                                 .ValidateAsync <RegenerateApiResourceSecretRequest, UpdateApiResourceSecretResponse,
                                                 RegenerateApiResourceSecretRequestValidator>
                                     (item, toaster, cancellationToken);

                if (validation.Failed)
                {
                    return(validation.Response);
                }
            }

            if (_repository.Query().Any(e => request.Any(x => x.Value == e.Value) &&
                                        e.Type != AppConstants.SecretTypes.VisibleOneTime))
            {
                return(AspExtensions.GetBadRequestWithError <UpdateApiResourceSecretResponse>(
                           "One of the new values is already registered."));
            }

            await _repository.Query()
            .Where(entity => request.Select(r => r.Id)
                   .Contains(entity.Id) && entity.Type != AppConstants.SecretTypes.VisibleOneTime)
            .ForEachAsync(entity => { entity.Value = request.First(r => r.Id == entity.Id).Value.Stamp(); }
                          , cancellationToken);

            await _repository.SaveAsync(toaster, secretsCount);

            return(new UpdateApiResourceSecretResponse
            {
                Succeeded = true
            });
        }
Пример #8
0
        public override async Task <ActionResult <InsertClientResponse> > HandleAsync(InsertClientRequest request,
                                                                                      CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(Client), ToasterType.Success, ToasterVerbs.Created);
            var validation =
                await _validator.ValidateAsync <InsertClientRequest, InsertClientResponse, InsertClientValidator>
                    (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (_clientRepository.Query().Any(e => e.ClientId == request.Name || e.ClientUri == request.ClientUri))
            {
                return(AspExtensions.GetBadRequestWithError <InsertClientResponse>($"Client \"{request.Name}\" already exists."));
            }

            var entity = new Client
            {
                ClientId                    = request.Name,
                ClientName                  = request.DisplayName,
                Description                 = request.Description,
                NonEditable                 = request.IsReadOnly,
                Created                     = DateTime.UtcNow,
                RequirePkce                 = request.RequirePkce,
                AccessTokenLifetime         = request.AccessTokenLifetime * 60, // transform minutes in seconds
                ClientUri                   = request.ClientUri,
                AllowOfflineAccess          = request.AllowOfflineAccess,
                RequireClientSecret         = request.RequireClientSecret,
                AllowAccessTokensViaBrowser = request.AllowAccessTokensViaBrowser
            };

            toaster.Identifier = entity.ClientName;
            await _clientRepository.InsertAsync(entity);

            await _clientRepository.SaveAsync(toaster);

            // Assignment of API Scopes
            if (request.ApiScopes != null && request.ApiScopes.Length > 0)
            {
                var clientApiScopes = request.ApiScopes.Select(x => new ClientScope {
                    Scope = x, ClientId = entity.Id
                });
                await _clientScopeRepository.InsertRange(clientApiScopes);
            }

            return(validation.Response);
        }
Пример #9
0
        public override async Task <ActionResult <InsertApiResourceResponse> > HandleAsync(InsertApiResourceRequest request,
                                                                                           CancellationToken cancellationToken = default)
        {
            var toaster    = new ToasterEvent(nameof(ApiResource), ToasterType.Success, ToasterVerbs.Created);
            var validation = await _validator
                             .ValidateAsync <InsertApiResourceRequest, InsertApiResourceResponse, InsertApiResourceValidator>
                                 (request, toaster, cancellationToken);

            if (validation.Failed)
            {
                return(validation.Response);
            }

            if (_apiResourceRepository.Query().Any(e => e.Name == request.Name))
            {
                return(AspExtensions.GetBadRequestWithError <InsertApiResourceResponse>(
                           $"ApiResource \"{request.Name}\" already exists."));
            }

            var entity = new ApiResource
            {
                Name        = request.Name,
                DisplayName = request.DisplayName,
                Description = request.Description
            };

            toaster.Identifier = entity.Name;
            await _apiResourceRepository.InsertAsync(entity);

            await _apiResourceRepository.SaveAsync(toaster);

            // Assignment of API Scopes
            if (request.ApiScopes != null && request.ApiScopes.Length > 0)
            {
                var apiResourceScopes =
                    request.ApiScopes.Select(x => new ApiResourceScope {
                    Scope = x, ApiResourceId = entity.Id
                });
                await _apiResourceScopeRepo.InsertRange(apiResourceScopes);
            }

            return(validation.Response);
        }