예제 #1
0
        public async Task <IdentityResult> DeleteApiScopeClaimsAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));

            try
            {
                var cypher = $@"
                    MATCH 
                        (apiResource:{IdSrv4ApiResource}{{Name: $p0}})
                        -[:{Neo4jConstants.Relationships.HasScope}]->
                        (apiScope:{IdSrv4ClientApiScope}{{Name: $p1}})
                        -[:{Neo4jConstants.Relationships.HasClaim}]->
                        (s:{IdSrv4ClientApiScopeClaim})
                DETACH DELETE s";

                await Session.RunAsync(cypher,
                                       Params.Create(
                                           apiResource.Name,
                                           apiScope.Name));
                await RaiseApiScopeChangeEventAsync(apiResource, apiScope);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
예제 #2
0
        public async Task <Neo4jIdentityServer4ApiScopeClaim> GetApiScopeClaimAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            Neo4jIdentityServer4ApiScopeClaim apiScopeClaim,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));

            var cypher = $@"
                MATCH 
                    (:{IdSrv4ApiResource}{{Name: $p0}})-[:{Neo4jConstants.Relationships.HasScope}]->
                    (:{IdSrv4ClientApiScope}{{Name: $p1}})-[:{Neo4jConstants.Relationships.HasClaim}]->
                    (s:{IdSrv4ClientApiScopeClaim}{{Type: $p2}})
                RETURN s {{ .* }}";

            var result = await Session.RunAsync(cypher,
                                                Params.Create(apiResource.Name, apiScope.Name, apiScopeClaim.Type));

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

            return(record);
        }
        public async Task <IdentityResult> DeleteApiResourceAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            await DeleteApiScopesAsync(apiResource, cancellationToken);

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

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

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
예제 #4
0
        public async Task <IdentityResult> AddApiScopeClaimAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            Neo4jIdentityServer4ApiScopeClaim apiScopeClaim,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));
            apiScopeClaim.ThrowIfNull(nameof(apiScopeClaim));
            try
            {
                var cypher = $@"
                MATCH 
                    (apiResource:{IdSrv4ApiResource}{{Name: $p0}})
                    -[:{Neo4jConstants.Relationships.HasScope}]->
                    (apiScope:{IdSrv4ClientApiScope}{{Name: $p1}})
                MERGE 
                    (l:{IdSrv4ClientApiScopeClaim} {"$p2".AsMapForNoNull<Neo4jIdentityServer4ApiScopeClaim>(apiScopeClaim)})
                MERGE 
                    (apiScope)-[:{Neo4jConstants.Relationships.HasClaim}]->(l)";

                var result = await Session.RunAsync(cypher, Params.Create(apiResource.Name, apiScope.Name, apiScopeClaim));
                await RaiseApiScopeChangeEventAsync(apiResource, apiScope);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
예제 #5
0
        public async Task <IdentityResult> UpdateApiScopeAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));

            try
            {
                var cypher = $@"
                    MATCH 
                        (:{IdSrv4ApiResource}{{Name: $p0}})
                        -[:{Neo4jConstants.Relationships.HasScope}]->
                        (apiScope:{IdSrv4ClientApiScope}{{Name: $p1}})
                    SET apiScope = $p1";

                await Session.RunAsync(cypher, Params.Create(apiResource.Name, apiScope.Name, apiScope.ConvertToMap()));

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
        public async Task <IdentityResult> DeleteRollupAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));

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

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

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
        public async Task <ApiResource> GetRollupAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));

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

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

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

            if (foundRecord == null)
            {
                model = await RollupAsync(apiResource, cancellationToken);
            }
            else
            {
                model = JsonConvert.DeserializeObject <ApiResource>(foundRecord.ApiResourceJson);
            }

            return(model);
        }
        private async Task <IdentityResult> AddRollupAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            ApiResourceRollup rollup,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            rollup.ThrowIfNull(nameof(rollup));

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

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

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
        public async Task <ApiResource> RollupAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            var foundApiResource = await GetApiResourceAsync(apiResource, cancellationToken);

            ApiResource model = null;

            if (foundApiResource != null)
            {
                model = foundApiResource.ToModel();
                var secrets = await GetApiSecretsAsync(apiResource, cancellationToken);

                foreach (var secret in secrets)
                {
                    model.ApiSecrets.Add(secret.ToModel());
                }

                var claims = await GetApiResourceClaimsAsync(apiResource, cancellationToken);

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

                var apiScopes = await GetApiScopesAsync(apiResource, cancellationToken);

                foreach (var apiScope in apiScopes)
                {
                    var apiScopeModel = await GetRollupAsync(apiResource, apiScope, cancellationToken);

                    model.Scopes.Add(apiScopeModel);
                }

                var distinctList = model.Scopes.Distinct(new ModelsScopeComparer());
                model.Scopes = distinctList.ToList();

                var rollup = new ApiResourceRollup()
                {
                    ApiResourceJson = JsonConvert.SerializeObject(model),
                };
                await AddRollupAsync(apiResource, rollup);
            }


            return(model);
        }
예제 #10
0
        public async Task <IdentityResult> DeleteApiScopesAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            var apiScopes = await GetApiScopesAsync(apiResource, cancellationToken);

            foreach (var apiScope in apiScopes)
            {
                await DeleteApiScopeAsync(apiResource, apiScope, cancellationToken);
            }
            return(IdentityResult.Success);
        }
        public async Task <Neo4jIdentityServer4ApiResource> GetApiResourceAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));

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

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

            var grantTypeRecord =
                await result.SingleOrDefaultAsync(r => r.MapTo <Neo4jIdentityServer4ApiResource>("r"));

            return(grantTypeRecord);
        }
        public async Task <IdentityResult> CreateApiResourceAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            try
            {
                var cypher = $@"CREATE (r:{IdSrv4ApiResource} $p0)";
                await Session.RunAsync(cypher, Params.Create(apiResource.ConvertToMap()));
                await RaiseApiResourceChangeEventAsync(apiResource);

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
        public async Task <IList <Neo4jIdentityServer4ApiSecret> > GetApiSecretsAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));

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

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

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

            return(records);
        }
예제 #14
0
        public async Task <Neo4jIdentityServer4ApiScope> GetApiScopeAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));

            var cypher = $@"
                MATCH (apiResource:{IdSrv4ApiResource})-[:{Neo4jConstants.Relationships.HasScope}]->(apiScope:{IdSrv4ClientApiScope})
                WHERE apiResource.Name = $p0 AND apiScope.Name = $p1
                RETURN apiScope {{ .* }}";

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

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

            return(record);
        }
 private async Task RaiseApiResourceChangeEventAsync(Neo4jIdentityServer4ApiResource apiResource)
 {
     await _eventService.RaiseAsync(new ApiResourceChangeEvent <Neo4jIdentityServer4ApiResource>(apiResource));
 }
 /// <summary>
 /// Maps an entity to a model.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <returns></returns>
 public static IdentityServer4.Models.ApiResource ToModel(this Neo4jIdentityServer4ApiResource entity)
 {
     return(Mapper.Map <IdentityServer4.Models.ApiResource>(entity));
 }