Beispiel #1
0
        public async Task <IdentityResult> DeleteRollupAsync(
            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}]->
                    (:{IdSrv4ClientApiScope}{{Name: $p1}})-[:{Neo4jConstants.Relationships.HasRollup}]->(r:{IdSrv4ApiScopeRollup})
             
                DETACH DELETE r";

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

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Beispiel #2
0
        private async Task <IdentityResult> AddRollupAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            ApiScopeRollup rollup,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));
            rollup.ThrowIfNull(nameof(rollup));

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

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

                return(IdentityResult.Success);
            }
            catch (ClientException ex)
            {
                return(ex.ToIdentityResult());
            }
        }
Beispiel #3
0
        public async Task <Scope> GetRollupAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            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.HasRollup}]->
                    (r:{IdSrv4ApiScopeRollup})
                RETURN r{{ .* }}";

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

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

            if (foundRecord == null)
            {
                model = await RollupAsync(apiResource, apiScope, cancellationToken);
            }
            else
            {
                model = JsonConvert.DeserializeObject <Scope>(foundRecord.ApiScopeJson);
            }
            return(model);
        }
Beispiel #4
0
        public async Task <IdentityResult> AddApiScopeAsync(
            Neo4jIdentityServer4ApiResource apiResource,
            Neo4jIdentityServer4ApiScope apiScope,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();
            apiResource.ThrowIfNull(nameof(apiResource));
            apiScope.ThrowIfNull(nameof(apiScope));
            try
            {
                var cypher = $@"
                MATCH 
                    (r:{IdSrv4ApiResource}{{Name: $p0}})
                MERGE 
                    (l:{IdSrv4ClientApiScope} {"$p1".AsMapForNoNull<Neo4jIdentityServer4ApiScope>(apiScope)})
                MERGE 
                    (r)-[:{Neo4jConstants.Relationships.HasScope}]->(l)";

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

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

            await RaiseApiScopeChangeEventAsync(apiResource, apiScope);

            var finalResult   = new Client();
            var apiScopeFound = await GetApiScopeAsync(apiResource, apiScope, cancellationToken);

            var model          = apiScopeFound.ToModel();
            var apiScopeClaims = await GetApiScopeClaimsAsync(apiResource, apiScopeFound, cancellationToken);

            if (apiScopeClaims != null)
            {
                foreach (var item in apiScopeClaims)
                {
                    model.UserClaims.Add(item.Type);
                }
            }
            var rollup = new ApiScopeRollup()
            {
                ApiScopeJson = JsonConvert.SerializeObject(model),
            };
            var result = await AddRollupAsync(apiResource, apiScopeFound, rollup, cancellationToken);

            return(model);
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
0
        private async Task RaiseApiScopeChangeEventAsync(Neo4jIdentityServer4ApiResource apiResource, Neo4jIdentityServer4ApiScope apiScope)
        {
            await RaiseApiResourceChangeEventAsync(apiResource);

            await _eventService.RaiseAsync(new ApiScopeChangeEvent <Neo4jIdentityServer4ApiResource, Neo4jIdentityServer4ApiScope>(apiResource, apiScope));
        }
 /// <summary>
 /// Maps an entity to a model.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <returns></returns>
 public static IdentityServer4.Models.Scope ToModel(this Neo4jIdentityServer4ApiScope entity)
 {
     return(Mapper.Map <IdentityServer4.Models.Scope>(entity));
 }