Ejemplo n.º 1
0
        private async Task SaveApiResource()
        {
            var apiResource = new ApiResource(
                Guid.NewGuid(),
                "api1",
                "My API",
                "My api resource description"
                );

            apiResource.AddUserClaim("email");
            apiResource.AddUserClaim("role");

            await _apiResourceRepository.InsertAsync(apiResource);
        }
        private void AddApiResources()
        {
            var apiResource = new ApiResource(_testData.ApiResource1Id, "NewApiResource1");

            apiResource.Description = nameof(apiResource.Description);
            apiResource.DisplayName = nameof(apiResource.DisplayName);

            apiResource.AddScope(nameof(ApiScope.Name));
            apiResource.AddUserClaim(nameof(ApiResourceClaim.Type));
            apiResource.AddSecret(nameof(ApiSecret.Value));

            _apiResourceRepository.Insert(apiResource);
            _apiResourceRepository.Insert(new ApiResource(_guidGenerator.Create(), "NewApiResource2"));
            _apiResourceRepository.Insert(new ApiResource(_guidGenerator.Create(), "NewApiResource3"));
        }
Ejemplo n.º 3
0
        private async Task AddApiResources()
        {
            var apiResource = new ApiResource(_testData.ApiResource1Id, "NewApiResource1");

            apiResource.Description = nameof(apiResource.Description);
            apiResource.DisplayName = nameof(apiResource.DisplayName);

            apiResource.AddScope(nameof(ApiScope.Name));
            apiResource.AddUserClaim(nameof(ApiResourceClaim.Type));
            apiResource.AddSecret(nameof(ApiSecret.Value));

            await _apiResourceRepository.InsertAsync(apiResource).ConfigureAwait(false);

            await _apiResourceRepository.InsertAsync(new ApiResource(_guidGenerator.Create(), "NewApiResource2")).ConfigureAwait(false);

            await _apiResourceRepository.InsertAsync(new ApiResource(_guidGenerator.Create(), "NewApiResource3")).ConfigureAwait(false);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Save(CreateApiResourceViewModel model)
        {
            if (!ModelState.IsValid)
            {
                //NotifyModelStateErrors();
                return(BadRequest(new ValidationProblemDetails(ModelState)));
            }
            ApiResource apiResource = new ApiResource(Guid.NewGuid(), model.Name, model.DisplayName, model.Description);

            apiResource.Enabled = true;
            foreach (var claim in model.UserClaims)
            {
                apiResource.AddUserClaim(claim);
            }
            await _apiResourceRepository.InsertAsync(apiResource, true);

            var apires = await _apiResourceRepository.GetAsync(apiResource.Id);

            return(CreatedAtAction(nameof(Details), new { resource = apiResource.Id }, apires));
        }
        public virtual async Task <ApiResourceDto> CreateAsync(ApiResourceCreateUpdateDto input)
        {
            var existed = await _resourceRepository.CheckNameExistAsync(input.Name);

            if (existed)
            {
                throw new UserFriendlyException(L["EntityExisted", nameof(ApiResource),
                                                  nameof(ApiResource.Name),
                                                  input.Name]);
            }

            var apiResource = new ApiResource(GuidGenerator.Create(), input.Name);

            apiResource = ObjectMapper.Map(input, apiResource);
            input.UserClaims.ForEach(x => apiResource.AddUserClaim(x));
            input.Scopes.ForEach(x => apiResource.AddScope(x));

            apiResource = await _resourceRepository.InsertAsync(apiResource, true);

            return(ObjectMapper.Map <ApiResource, ApiResourceDto>(apiResource));
        }
        protected virtual async Task UpdateApiResourceByInputAsync(ApiResource apiResource, ApiResourceCreateOrUpdateDto input)
        {
            if (await IsGrantAsync(IdentityServerPermissions.ApiResources.ManageClaims))
            {
                // 删除不存在的UserClaim
                apiResource.UserClaims.RemoveAll(claim => !input.UserClaims.Contains(claim.Type));
                foreach (var inputClaim in input.UserClaims)
                {
                    var userClaim = apiResource.FindClaim(inputClaim);
                    if (userClaim == null)
                    {
                        apiResource.AddUserClaim(inputClaim);
                    }
                }
            }

            if (await IsGrantAsync(IdentityServerPermissions.ApiResources.ManageScopes))
            {
                // 删除不存在的Scope
                apiResource.Scopes.RemoveAll(scope => !input.Scopes.Any(inputScope => scope.Name == inputScope.Name));
                foreach (var inputScope in input.Scopes)
                {
                    var scope = apiResource.FindScope(inputScope.Name);
                    if (scope == null)
                    {
                        scope = apiResource.AddScope(
                            inputScope.Name, inputScope.DisplayName, inputScope.Description,
                            inputScope.Required, inputScope.Emphasize, inputScope.ShowInDiscoveryDocument);
                    }
                    else
                    {
                        scope.Required                = inputScope.Required;
                        scope.Emphasize               = inputScope.Emphasize;
                        scope.Description             = inputScope.Description;
                        scope.DisplayName             = inputScope.DisplayName;
                        scope.ShowInDiscoveryDocument = inputScope.ShowInDiscoveryDocument;
                        // 删除不存在的ScopeUserClaim
                        scope.UserClaims.RemoveAll(claim => !inputScope.UserClaims.Contains(claim.Type));
                    }

                    foreach (var inputScopeClaim in inputScope.UserClaims)
                    {
                        var scopeUserClaim = scope.FindClaim(inputScopeClaim);
                        if (scopeUserClaim == null)
                        {
                            scope.AddUserClaim(inputScopeClaim);
                        }
                    }
                }
            }

            if (await IsGrantAsync(IdentityServerPermissions.ApiResources.ManageSecrets))
            {
                // 删除不存在的Secret
                apiResource.Secrets.RemoveAll(secret => !input.Secrets.Any(inputSecret => secret.Type == inputSecret.Type && secret.Value == inputSecret.Value));
                foreach (var inputSecret in input.Secrets)
                {
                    // 第一次重复校验已经加密过的字符串
                    if (apiResource.FindSecret(inputSecret.Value, inputSecret.Type) == null)
                    {
                        var apiSecretValue = inputSecret.Value;
                        if (IdentityServerConstants.SecretTypes.SharedSecret.Equals(inputSecret.Type))
                        {
                            if (inputSecret.HashType == HashType.Sha256)
                            {
                                apiSecretValue = inputSecret.Value.Sha256();
                            }
                            else if (inputSecret.HashType == HashType.Sha512)
                            {
                                apiSecretValue = inputSecret.Value.Sha512();
                            }
                        }
                        // 加密之后还需要做一次校验 避免出现重复值
                        var secret = apiResource.FindSecret(apiSecretValue, inputSecret.Type);
                        if (secret == null)
                        {
                            apiResource.AddSecret(apiSecretValue, inputSecret.Expiration, inputSecret.Type, inputSecret.Description);
                        }
                    }
                }
            }

            if (await IsGrantAsync(IdentityServerPermissions.ApiResources.ManageProperties))
            {
                // 删除不存在的属性
                apiResource.Properties.RemoveAll(scope => !input.Properties.ContainsKey(scope.Key));
                foreach (var property in input.Properties)
                {
                    apiResource.Properties[property.Key] = property.Value;
                }
            }
        }