public async Task <IActionResult> Post([FromBody] IroncladResource model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                return(this.BadRequest(new { Message = $"Cannot create an identity resource without a name" }));
            }

            if (model.UserClaims?.Any() == false)
            {
                return(this.BadRequest(new { Message = $"Cannot create an identity resource without any claims" }));
            }

            var resource = new IdentityServerResource(model.Name, model.DisplayName, model.UserClaims);

            // optional properties
            resource.Enabled = model.Enabled ?? resource.Enabled;

            using (var session = this.store.LightweightSession())
            {
                if (session.Query <PostgresResource>().Any(item => item.Name == model.Name))
                {
                    return(this.StatusCode((int)HttpStatusCode.Conflict, new { Message = "Identity resource already exists" }));
                }

                session.Insert(resource.ToEntity());

                await session.SaveChangesAsync();
            }

            return(this.Created(new Uri(this.HttpContext.GetIdentityServerRelativeUrl("~/api/identityresources/" + model.Name)), null));
        }
Пример #2
0
        public async Task <IdentityServer4.Models.IdentityResource> RollupAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                                                CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            var foundIdentityResource = await GetIdentityResourceAsync(identityResource, cancellationToken);

            IdentityResource model = null;

            if (foundIdentityResource != null)
            {
                model = foundIdentityResource.ToModel();
                var claims = await GetIdentityClaimsAsync(foundIdentityResource, cancellationToken);

                foreach (var claim in claims)
                {
                    model.UserClaims.Add(claim.Type);
                }

                var rollup = new IdentityResourceRollup()
                {
                    IdentityResourceJson = JsonConvert.SerializeObject(model),
                };
                await AddRollupAsync(foundIdentityResource, rollup);
            }


            return(model);
        }
Пример #3
0
        public virtual async Task CreateStandardResourcesAsync()
        {
            var wechatClaimTypes = new string[]
            {
                AbpWeChatClaimTypes.AvatarUrl,
                AbpWeChatClaimTypes.City,
                AbpWeChatClaimTypes.Country,
                AbpWeChatClaimTypes.NickName,
                AbpWeChatClaimTypes.OpenId,
                AbpWeChatClaimTypes.Privilege,
                AbpWeChatClaimTypes.Province,
                AbpWeChatClaimTypes.Sex,
                AbpWeChatClaimTypes.UnionId
            };

            var wechatResource = new IdentityServer4.Models.IdentityResource(
                WeChatOfficialOAuthConsts.ProfileKey,
                WeChatOfficialOAuthConsts.DisplayName,
                wechatClaimTypes);

            foreach (var claimType in wechatClaimTypes)
            {
                await AddClaimTypeIfNotExistsAsync(claimType);
            }

            await AddIdentityResourceIfNotExistsAsync(wechatResource);
        }
Пример #4
0
        public async Task AddResourceAsync(IdentityResource resource)
        {
            var resourceEntity = resource.ToEntity();

            IdentityDbContext.IdentityResources.Add(resourceEntity);
            await IdentityDbContext.SaveChangesAsync();
        }
Пример #5
0
 public RemoveIdentityResourceCommand(string name)
 {
     Resource = new IdentityServer4.Models.IdentityResource()
     {
         Name = name
     };
 }
        private async Task <HttpResponseMessage> CreateNewIdentityResource(IdentityServer4.Models.IdentityResource identityResource)
        {
            var stringContent = new StringContent(JsonConvert.SerializeObject(identityResource), Encoding.UTF8, "application/json");
            var response      = await HttpClient.PostAsync("/api/identityresource", stringContent);

            return(response);
        }
 public Task Create(IdentityServer4.Models.IdentityResource resouce)
 {
     if (resouce == null)
     {
         throw new ArgumentNullException(nameof(resouce));
     }
     State.IdentityResource = resouce.ToEntity();
     return(WriteStateAsync());
 }
        protected virtual async Task AddIdentityResourceIfNotExistsAsync(IdentityServer4.Models.IdentityResource resource)
        {
            if (await IdentityResourceRepository.CheckNameExistAsync(resource.Name))
            {
                return;
            }

            var entity = resource.ToEntity();
            await IdentityResourceRepository.InsertAsync(entity, true);
        }
Пример #9
0
 /// <summary>
 /// Convert from <see cref="IdentityServer4.Models.IdentityResource"/> to <see cref="Scope"/>
 /// </summary>
 /// <param name="identity"></param>
 /// <returns></returns>
 public static Scope ToScope(this IdentityServer4.Models.IdentityResource identity)
 {
     return(new Scope
     {
         Name = identity.Name,
         DisplayName = identity.DisplayName,
         Description = identity.Description,
         Emphasize = identity.Emphasize,
         Required = identity.Required
     });
 }
Пример #10
0
        public async Task UpdateResourceAsync(string id, IdentityResource resource)
        {
            var existingResource = await IdentityDbContext.IdentityResources
                                   .Where(r => r.Name.Equals(id, StringComparison.OrdinalIgnoreCase) &&
                                          !r.IsDeleted)
                                   .SingleOrDefaultAsync();

            resource.ToEntity(existingResource);

            IdentityDbContext.IdentityResources.Update(existingResource);
            await IdentityDbContext.SaveChangesAsync();
        }
Пример #11
0
 public IdentityResource(Guid id, IdentityServer4.Models.IdentityResource resource)
 {
     Id                      = id;
     Name                    = resource.Name;
     DisplayName             = resource.DisplayName;
     Description             = resource.Description;
     Enabled                 = resource.Enabled;
     Required                = resource.Required;
     Emphasize               = resource.Emphasize;
     ShowInDiscoveryDocument = resource.ShowInDiscoveryDocument;
     UserClaims              = resource.UserClaims.Select(claimType => new IdentityClaim(id, claimType)).ToList();
 }
Пример #12
0
        public void IdentityResourceModelToEntityConfigurationIsValid()
        {
            var model = new IdentityServer4.Models.IdentityResource();

            // TODO: set references

            var mappedEntity = model.ToEntity();
            var mappedModel  = mappedEntity.ToModel();

            Assert.NotNull(mappedModel);
            Assert.NotNull(mappedEntity);
            IdentityResourceMappers.Mapper.ConfigurationProvider.AssertConfigurationIsValid();
        }
Пример #13
0
 public IdentityResourceDto(Guid id, IdentityServer4.Models.IdentityResource resource)
 {
     Id                      = id;
     Name                    = resource.Name;
     DisplayName             = resource.DisplayName;
     Description             = resource.Description;
     Enabled                 = resource.Enabled;
     Required                = resource.Required;
     Emphasize               = resource.Emphasize;
     ShowInDiscoveryDocument = resource.ShowInDiscoveryDocument;
     UserClaims              = resource.UserClaims.Select(claimType => new IdentityClaimDto(id, claimType)).ToList();
     Properties              = resource.Properties.ToDictionary(x => x.Key, x => x.Value);
 }
Пример #14
0
        protected virtual async Task AddIdentityResourceIfNotExistsAsync(IdentityServer4.Models.IdentityResource resource)
        {
            if (await IdentityResourceRepository.CheckNameExistAsync(resource.Name))
            {
                return;
            }

            await IdentityResourceRepository.InsertAsync(
                new IdentityResource(
                    GuidGenerator.Create(),
                    resource
                    )
                );
        }
        public async Task AddResourceAsync(IdentityResource resource)
        {
            var resourceEntity = resource.ToEntity();

            IdentityDbContext.IdentityResources.Add(resourceEntity);
            await IdentityDbContext.SaveChangesAsync();

            await EventService.RaiseAsync(
                new EntityCreatedAuditEvent <IdentityResource>(
                    UserResolverService.Username,
                    UserResolverService.ClientId,
                    UserResolverService.Subject,
                    resource.Name,
                    resource,
                    SerializationSettings));
        }
Пример #16
0
        public virtual async Task CreateStandardResourcesAsync()
        {
            var resources = new IdentityServer4.Models.IdentityResource[]
            {
                new IdentityServer4.Models.IdentityResources.OpenId(),
                new IdentityServer4.Models.IdentityResources.Profile(),
                new IdentityServer4.Models.IdentityResources.Email(),
                new IdentityServer4.Models.IdentityResources.Address(),
                new IdentityServer4.Models.IdentityResources.Phone()
            };

            foreach (var resource in resources)
            {
                await AddIfNotExistsAsync(resource);
            }
        }
Пример #17
0
        /// <summary>
        /// 创建或者更新资源
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task <Result> CreateOrUpdateIdentityResourceAsync(IdentityResourceDto dto)
        {
            if (dto.UserClaims == null || dto.UserClaims.Count == 0)
            {
                return(FailedResult("UserClaims不能为空值"));
            }

            IdentityServer4.Models.IdentityResource model = mapper.Map <IdentityServer4.Models.IdentityResource>(dto);
            var entity = model.ToEntity();

            if (!dto.Id.HasValue || dto.Id == 0)
            {
                //创建
                await configurationDbContext.IdentityResources.AddAsync(entity).ConfigureAwait(false);
            }
            else
            {
                entity.Id = dto.Id.Value;

                var fromDbEntity = await IdentityResources.FirstOrDefaultAsync(e => e.Id == entity.Id).ConfigureAwait(false);

                if (fromDbEntity == null)
                {
                    return(FailedResult("不存在身份资源"));
                }

                fromDbEntity = mapper.Map(entity, fromDbEntity);
            }

            try
            {
                int rows = await configurationDbContext.SaveChangesAsync().ConfigureAwait(false);

                if (rows == 0)
                {
                    return(FailedResult("已执行,但未更新数据库"));
                }
                return(OkResult());
            }
            catch (DbUpdateException ex)
            {
                return(FailedResult(ex));
            }
        }
Пример #18
0
        private async Task <IdentityResource> ConvertToModelAsync(IdentityResourceEntity entity)
        {
            var resource = new IdentityResource
            {
                Enabled     = entity.Enabled,
                Name        = entity.Name,
                DisplayName = entity.DisplayName,
                Description = entity.Description,
            };

            await foreach (var claim in _identityClaims.EnumAsync(entity.Name))
            {
                resource.UserClaims.Add(claim.Claim);
            }

            var converted = resource;

            return(converted);
        }
Пример #19
0
        public async Task StoreAsync(IdentityResource resource)
        {
            await _identityResources.InsertAsync(new IdentityResourceEntity(resource.Name) {
                Description             = resource.Description,
                DisplayName             = resource.DisplayName,
                Enabled                 = resource.Enabled,
                Emphasize               = resource.Emphasize,
                Required                = resource.Required,
                ShowInDiscoveryDocument = resource.ShowInDiscoveryDocument,
            });

            foreach (var claim in resource.UserClaims)
            {
                await _identityClaims.InsertAsync(new IdentityClaimEntity
                {
                    Name  = resource.Name,
                    Claim = claim,
                });
            }
        }
        public async Task UpdateResourceAsync(string id, IdentityResource resource)
        {
            var existingResource = await IdentityDbContext.IdentityResources
                                   .Where(r => r.Name == id &&
                                          !r.IsDeleted)
                                   .SingleOrDefaultAsync();

            resource.ToEntity(existingResource);

            IdentityDbContext.IdentityResources.Update(existingResource);
            await IdentityDbContext.SaveChangesAsync();

            await EventService.RaiseAsync(
                new EntityUpdatedAuditEvent <IdentityResource>(
                    UserResolverService.Username,
                    UserResolverService.ClientId,
                    UserResolverService.Subject,
                    resource.Name,
                    resource,
                    SerializationSettings));
        }
        public async Task <IActionResult> Put(string resourceName, [FromBody] IroncladResource model)
        {
            if (model.UserClaims?.Any() == false)
            {
                return(this.BadRequest(new { Message = $"Cannot update an identity resource without any claims" }));
            }

            using (var session = this.store.LightweightSession())
            {
                var document = await session.Query <PostgresResource>().SingleOrDefaultAsync(item => item.Name == resourceName);

                if (document == null)
                {
                    return(this.NotFound(new { Message = $"Identity resource '{resourceName}' not found" }));
                }

                // NOTE (Cameron): Because of the mapping/conversion unknowns we rely upon the Postgres integration to perform that operation which is why we do this...
                var resource = new IdentityServerResource
                {
                    UserClaims = model.UserClaims,
                };

                var entity = resource.ToEntity();

                // update properties (everything supported is an optional update eg. if null is passed we will not update)
                document.DisplayName = model.DisplayName ?? document.DisplayName;
                document.UserClaims  = entity.UserClaims ?? document.UserClaims;
                document.Enabled     = model.Enabled ?? document.Enabled;

                session.Update(document);

                await session.SaveChangesAsync();
            }

            return(this.Ok());
        }
Пример #22
0
 public UpdateIdentityResourceCommand(IdentityServer4.Models.IdentityResource resource, string oldIdentityResourceName)
 {
     Resource = resource;
     this.OldIdentityResourceName = oldIdentityResourceName;
 }
 public UpdateIdentityResourceCommand(IdentityServer4.Models.IdentityResource resource)
 {
     Resource = resource;
 }
Пример #24
0
 private static void SeedIdentityClaims(IAdminStore <Entity.IdentityClaim> identityClaimStore, IdentityServer4.Models.IdentityResource resource)
 {
     foreach (var claim in resource.UserClaims)
     {
         identityClaimStore.CreateAsync(new Entity.IdentityClaim
         {
             Id         = Guid.NewGuid().ToString(),
             IdentityId = resource.Name,
             Type       = claim
         }).GetAwaiter().GetResult();
     }
 }
Пример #25
0
 private static void SeedIdentityProperties(IAdminStore <Entity.IdentityProperty> identityPropertyStore, IdentityServer4.Models.IdentityResource resource)
 {
     foreach (var property in resource.Properties)
     {
         identityPropertyStore.CreateAsync(new Entity.IdentityProperty
         {
             Id         = Guid.NewGuid().ToString(),
             IdentityId = resource.Name,
             Key        = property.Key,
             Value      = property.Value
         }).GetAwaiter().GetResult();
     }
 }
 public RegisterIdentityResourceCommand(IdentityServer4.Models.IdentityResource resource)
 {
     Resource = resource;
 }
Пример #27
0
 public void UpdateResource(string id, IdentityResource resource)
 {
     UpdateResourceAsync(id, resource).Wait();
 }
Пример #28
0
 public void AddResource(IdentityResource resource)
 {
     AddResourceAsync(resource).Wait();
 }
Пример #29
0
 /// <summary>
 /// Maps a model to an entity.
 /// </summary>
 /// <param name="model">The model.</param>
 /// <returns></returns>
 public static IdentityResource ToEntity(
     this IdentityServer4.Models.IdentityResource model)
 {
     return(model == null ? null : Mapper.Map <IdentityResource>(model));
 }
 public static IdentityResource ToEntity(this IdentityServer4.Models.IdentityResource resource)
 {
     return(resource == null ? null : Mapper.Map <IdentityResource>(resource));
 }