Example #1
0
        public async Task <IdentityResult> DeleteRollupAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                             CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));

            try
            {
                var cypher = $@"
                MATCH 
                    (:{IdSrv4IdentityResource}{{Name: $p0}})-[:{Neo4jConstants.Relationships.HasRollup}]->
                    (r:{IdSrv4IdentityResourceRollup}) 
                DETACH DELETE r";

                await Session.RunAsync(cypher,
                                       Params.Create(identityResource.Name));

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Example #2
0
        private async Task <IdentityResult> AddRollupAsync(
            Neo4jIdentityServer4IdentityResource identityResource,
            IdentityResourceRollup rollup,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            rollup.ThrowIfNull(nameof(rollup));

            try
            {
                var cypher = $@"
                    MATCH (c:{IdSrv4IdentityResource}{{Name: $p0}})        
                    MERGE (rollup:{IdSrv4IdentityResourceRollup} {"$p1".AsMapForNoNull(rollup)})
                    MERGE (c)-[:{Neo4jConstants.Relationships.HasRollup}]->(rollup)";

                var result = await Session.RunAsync(cypher, Params.Create(identityResource.Name, rollup));

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Example #3
0
        public async Task <IdentityResource> GetRollupAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));

            var cypher = $@"
                MATCH 
                    (:{IdSrv4IdentityResource}{{Name: $p0}})-[:{Neo4jConstants.Relationships.HasRollup}]->
                    (r:{IdSrv4IdentityResourceRollup})
                RETURN r{{ .* }}";

            var result = await Session.RunAsync(cypher,
                                                Params.Create(identityResource.Name));

            IdentityServer4.Models.IdentityResource model = null;
            var foundRecord =
                await result.SingleOrDefaultAsync(r => r.MapTo <IdentityResourceRollup>("r"));

            if (foundRecord == null)
            {
                model = await RollupAsync(identityResource, cancellationToken);
            }
            else
            {
                model = JsonConvert.DeserializeObject <IdentityResource>(foundRecord.IdentityResourceJson);
            }

            return(model);
        }
Example #4
0
        public async Task <IdentityResult> AddIdentityClaimAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                                 Neo4jIdentityServer4IdentityClaim identityClaim, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            identityClaim.ThrowIfNull(nameof(identityClaim));

            try
            {
                var cypher = $@"
                MATCH 
                    (r:{IdSrv4IdentityResource}{{Name: $p0}}) 
                MERGE 
                    (l:{IdSrv4IdentityClaim} {"$p1".AsMapForNoNull<Neo4jIdentityServer4IdentityClaim>(identityClaim)})
                MERGE 
                    (r)-[:{Neo4jConstants.Relationships.HasClaim}]->(l)";

                var result = await Session.RunAsync(cypher, Params.Create(identityResource.Name, identityClaim));
                await RaiseIdentityResourceChangeEventAsync(identityResource);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Example #5
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);
        }
Example #6
0
        public async Task <IdentityResult> DeleteIdentityClaimAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                                    Neo4jIdentityServer4IdentityClaim identityClaim, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            identityClaim.ThrowIfNull(nameof(identityClaim));

            try
            {
                var cypher = $@"
                MATCH 
                    (r:{IdSrv4IdentityResource}{{Name: $p0}})-[:{Neo4jConstants.Relationships.HasClaim}]->
                    (c:{IdSrv4IdentityClaim}{{Type: $p1}}) 
                DETACH DELETE c";

                await Session.RunAsync(cypher,
                                       Params.Create(
                                           identityResource.Name,
                                           identityClaim.Type));
                await RaiseIdentityResourceChangeEventAsync(identityResource);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Example #7
0
        public async Task <IdentityResult> DeleteIdentityResourceAsync(
            Neo4jIdentityServer4IdentityResource identityResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            await DeleteIdentityClaimsAsync(identityResource, cancellationToken);

            try
            {
                var cypher = $@"
                MATCH (r:{IdSrv4IdentityResource}{{Name: $p0}})
                DETACH DELETE r";

                await Session.RunAsync(cypher, Params.Create(identityResource.Name));
                await RaiseIdentityResourceChangeEventAsync(identityResource);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Example #8
0
        public async Task <Neo4jIdentityServer4IdentityResource> GetIdentityResourceAsync(
            Neo4jIdentityServer4IdentityResource identityResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));

            return(await FindIdentityResourceByNameAsync(identityResource.Name, cancellationToken));
        }
Example #9
0
        public async Task <Neo4jIdentityServer4IdentityResource> GetIdentityResourceAsync(
            Neo4jIdentityServer4IdentityResource identityResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));

            var cypher = $@"
                MATCH (r:{IdSrv4IdentityResource}{{Name: $p0}})
                RETURN r {{ .* }}";

            var result = await Session.RunAsync(cypher, Params.Create(identityResource.Name));

            var record =
                await result.SingleOrDefaultAsync(r => r.MapTo <Neo4jIdentityServer4IdentityResource>("r"));

            return(record);
        }
Example #10
0
        public async Task <IdentityResult> CreateIdentityResourceAsync(
            Neo4jIdentityServer4IdentityResource identityResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            try
            {
                var cypher = $@"CREATE (r:{IdSrv4IdentityResource} $p0)";
                await Session.RunAsync(cypher, Params.Create(identityResource.ConvertToMap()));
                await RaiseIdentityResourceChangeEventAsync(identityResource);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Example #11
0
        public async Task <Neo4jIdentityServer4IdentityClaim> GetIdentityClaimAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                                                    Neo4jIdentityServer4IdentityClaim identityClaim, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));
            identityClaim.ThrowIfNull(nameof(identityClaim));

            var cypher = $@"
                MATCH 
                    (r:{IdSrv4IdentityResource}{{Name: $p0}})-[:{Neo4jConstants.Relationships.HasClaim}]->
                    (c:{IdSrv4IdentityClaim}{{Type: $p1}}) 
                RETURN c {{ .* }}";

            var result = await Session.RunAsync(cypher, Params.Create(identityResource.Name, identityClaim.Type));

            var record =
                await result.SingleOrDefaultAsync(r => r.MapTo <Neo4jIdentityServer4IdentityClaim>("c"));

            return(record);
        }
Example #12
0
        public async Task <IList <Neo4jIdentityServer4IdentityClaim> > GetIdentityClaimsAsync(Neo4jIdentityServer4IdentityResource identityResource,
                                                                                              CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            identityResource.ThrowIfNull(nameof(identityResource));

            var cypher = $@"
                MATCH 
                    (:{IdSrv4IdentityResource}{{Name: $p0}})-[:{Neo4jConstants.Relationships.HasClaim}]->
                    (s:{IdSrv4IdentityClaim})
                RETURN 
                    s{{ .* }}";

            var result = await Session.RunAsync(cypher, Params.Create(identityResource.Name));

            var records = await result.ToListAsync(r => r.MapTo <Neo4jIdentityServer4IdentityClaim>("s"));

            return(records);
        }
Example #13
0
 private async Task RaiseIdentityResourceChangeEventAsync(Neo4jIdentityServer4IdentityResource IdentityResource)
 {
     await _eventService.RaiseAsync(new IdentityResourceChangeEvent <Neo4jIdentityServer4IdentityResource>(IdentityResource));
 }
Example #14
0
 /// <summary>
 /// Maps an entity to a model.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <returns></returns>
 public static IdentityServer4.Models.IdentityResource ToModel(this Neo4jIdentityServer4IdentityResource entity)
 {
     return(Mapper.Map <IdentityServer4.Models.IdentityResource>(entity));
 }