Esempio n. 1
0
        public static async Task <IEnumerable <Client> > GetAllClientsAsync(this IdentityServerDriverProvider ext)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            using (ISession session = ext.GetDriver().Session())
            {
                Node cli    = new Node(type: typeof(Client));
                Node prop   = new Node(type: typeof(Neo4jProperty));
                Node secret = new Node(type: typeof(Secret));
                Node claim  = new Node(type: typeof(Neo4jClaim));
                Rel  rel    = new Rel(type: typeof(Relationships.Has));

                List <Client> result = await session.AsAsync(s =>
                                                             s.ExecuteQuery <Client, IEnumerable <Neo4jProperty>, IEnumerable <Neo4jSecret>, IEnumerable <Neo4jClaim> >(
                                                                 $"MATCH (c{cli.Labels}) " +
                                                                 $"OPTIONAL MATCH (c)-{rel}->(p{prop.Labels}) " +
                                                                 $"OPTIONAL MATCH (c)-{rel}->(s{secret.Labels}) " +
                                                                 $"OPTIONAL MATCH (c)-{rel}->(cl{claim.Labels}) " +
                                                                 $"WITH c, p, s, cl ORDER BY id(c), id(p), id(s), id(cl) " +
                                                                 $"WITH c, {{props: collect(distinct p), secs:collect(distinct s), cls:collect(distinct cl)}} AS val " +
                                                                 $"WITH c, val.props AS p, val.secs AS s, val.cls AS cl ORDER BY id(c) " +
                                                                 $"RETURN c, p, s, cl",
                                                                 (client, props, secrets, claims) =>
                {
                    client.Properties = props?.ToDictionary(p => p.Name, p => p.Value);
                    client.Claims = claims?.Select(p => p.ToClaim())?.ToList();
                    client.ClientSecrets = secrets?.Select(p => p as Secret)?.ToList();
                    return(client);
                })
                                                             .ToList());

                return(result);
            }
        }
Esempio n. 2
0
        public static async Task SetApiResourceScopesAsync(this IdentityServerDriverProvider ext, ApiResource resource, IEnumerable <Scope> scopes)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string name = resource?.Name ?? throw new ArgumentNullException(nameof(resource));

            List <Scope> newScopes = scopes?.ToList() ?? new List <Scope>();

            if (newScopes.Count == 0)
            {
                throw new ArgumentException("No item to set");
            }

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jApiResource));
                Node p   = new Node(type: typeof(Neo4jScope));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Resource.Name)}:${nameof(name)}}}) " +
                    $"OPTIONAL MATCH (n)-{rel}->(p{p.Labels}) " +
                    $"WITH n, collect(p) AS olds " +
                    $"UNWIND ${nameof(newScopes)} AS row " +
                    $"CREATE (n)-{rel}->(q{p.Labels}) " +
                    $"SET q+=row,q.{nameof(IGraphEntity.EntityId)}=id(q), q :{typeof(Neo4jScope).Name} " +
                    $"WITH olds " +
                    $"UNWIND olds AS old " +
                    $"DETACH DELETE old",
                    new { name, newScopes });
            }
        }
Esempio n. 3
0
        public static async Task AddPersistedGrantAsync(this IdentityServerDriverProvider ext, PersistedGrant grant)
        {
            ext   = ext ?? throw new ArgumentNullException(nameof(ext));
            grant = grant ?? throw new ArgumentNullException(nameof(grant));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jPersistedGrant));
                Node c   = new Node(type: typeof(Client));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                Neo4jPersistedGrant newGrant = await session.AsAsync(s =>
                                                                     s.ExecuteQuery <Neo4jPersistedGrant>(
                                                                         $"MATCH (c{c.Labels} {{{nameof(Client.ClientId)}:${nameof(grant)}.{nameof(grant.ClientId)}}}) " +
                                                                         $"CREATE (c)" +
                                                                         $"-{rel}->" +
                                                                         $"(p{n.Labels}) " +
                                                                         $"SET p+=${nameof(grant)}, p.{nameof(IGraphEntity.EntityId)}=id(p), p :{typeof(Neo4jPersistedGrant).Name} RETURN p",
                                                                         new { grant }).FirstOrDefault());

                if (grant is IGraphEntity)
                {
                    (grant as IGraphEntity).EntityId = newGrant.EntityId;
                }
            }
        }
Esempio n. 4
0
        public static async Task SetClientSecretsAsync(this IdentityServerDriverProvider ext, Client client, IEnumerable <Secret> secrets)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string clientId = client?.ClientId ?? throw new ArgumentNullException(nameof(client));

            List <Secret> newSecrets = secrets?.ToList() ?? new List <Secret>();

            if (newSecrets.Count == 0)
            {
                throw new ArgumentException("No item to set");
            }

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jClient));
                Node p   = new Node(type: typeof(Neo4jSecret));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}}) " +
                    $"OPTIONAL MATCH (n)-{rel}->(p{p.Labels}) " +
                    $"WITH n, collect(p) AS olds " +
                    $"UNWIND ${nameof(newSecrets)} AS row " +
                    $"CREATE (n)-{rel}->(q{p.Labels}) " +
                    $"SET q+=row,q.{nameof(IGraphEntity.EntityId)}=id(q), q :{typeof(Neo4jSecret).Name} " +
                    $"WITH olds " +
                    $"UNWIND olds AS old " +
                    $"DETACH DELETE old",
                    new { clientId, newSecrets });
            }
        }
Esempio n. 5
0
        public static async Task ReplaceClientClaimAsync(this IdentityServerDriverProvider ext, Client client, string type, string value)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));
            if (string.IsNullOrEmpty(type))
            {
                throw new ArgumentNullException(nameof(type));
            }
            value = value ?? throw new ArgumentNullException(nameof(value));

            string clientId = client?.ClientId ?? throw new ArgumentNullException(nameof(client));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jClient));
                Node p   = new Node(type: typeof(Neo4jClaim));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}})" +
                    $"-{rel}->" +
                    $"(p{p.Labels} {{{nameof(Neo4jClaim.ClaimType)}:${nameof(type)}}}) " +
                    $"SET p.{nameof(Neo4jClaim.ClaimValue)}=${nameof(value)}",
                    new { clientId, type, value });
            }
        }
Esempio n. 6
0
        public static async Task SetClientPropsAsync(this IdentityServerDriverProvider ext, Client client, IDictionary <string, string> properties)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string clientId = client?.ClientId ?? throw new ArgumentNullException(nameof(client));

            List <Neo4jProperty> props = properties?.Select(p => new Neo4jProperty()
            {
                Name = p.Key, Value = p.Value
            }).ToList() ?? new List <Neo4jProperty>();

            if (props.Count == 0)
            {
                throw new ArgumentException("No item to set");
            }

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jClient));
                Node p   = new Node(type: typeof(Neo4jProperty));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}}) " +
                    $"OPTIONAL MATCH (n)-{rel}->(p{p.Labels}) " +
                    $"WITH n, collect(p) AS olds " +
                    $"UNWIND ${nameof(props)} AS row " +
                    $"CREATE (n)-{rel}->(q{p.Labels}) " +
                    $"SET q+=row,q.{nameof(IGraphEntity.EntityId)}=id(q), q :{typeof(Neo4jProperty).Name} " +
                    $"WITH olds " +
                    $"UNWIND olds AS old " +
                    $"DETACH DELETE old",
                    new { clientId, props });
            }
        }
Esempio n. 7
0
        public static async Task UpdateClientAsync(this IdentityServerDriverProvider ext, Client client)
        {
            ext    = ext ?? throw new ArgumentNullException(nameof(ext));
            client = client ?? throw new ArgumentNullException(nameof(client));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n = new Node(type: typeof(Neo4jClient));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(client)}.{nameof(Client.ClientId)}}}) " +
                    $"SET n+=${nameof(client)}",
                    new { client = client.ExludeProperties(p => new { p.Properties, p.Claims, p.ClientSecrets }) });
            }
        }
Esempio n. 8
0
        public static async Task UpdateResourceAsync(this IdentityServerDriverProvider ext, Resource resource)
        {
            ext      = ext ?? throw new ArgumentNullException(nameof(ext));
            resource = resource ?? throw new ArgumentNullException(nameof(resource));

            Type resType = resource is IdentityResource ? typeof(Neo4jIdentityResource) : typeof(Neo4jApiResource);
            IDictionary <string, object> resArg = resource is IdentityResource?resource.ToPropDictionary() : (resource as ApiResource).ExludeProperties(p => new { p.Scopes, p.ApiSecrets });

            using (ISession session = ext.GetDriver().Session())
            {
                Node n = new Node(type: resType);

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Resource.Name)}:${nameof(resource)}.{nameof(Resource.Name)}}}) " +
                    $"SET n+=${nameof(resource)}",
                    new { resource = resArg });
            }
        }
Esempio n. 9
0
        public static async Task UpdatePersistedGrantAsync(this IdentityServerDriverProvider ext, PersistedGrant grant)
        {
            ext   = ext ?? throw new ArgumentNullException(nameof(ext));
            grant = grant ?? throw new ArgumentNullException(nameof(grant));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(PersistedGrant));
                Node c   = new Node(type: typeof(Client));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (c{c.Labels} {{{nameof(Client.ClientId)}:${nameof(grant)}.{nameof(grant.ClientId)}}})" +
                    $"-{rel}->" +
                    $"(n{n.Labels} {{{nameof(PersistedGrant.Key)}:${nameof(grant)}.{nameof(PersistedGrant.Key)}}}) " +
                    $"SET n+=${nameof(grant)}",
                    new { grant });
            }
        }
Esempio n. 10
0
        public static async Task AddClientAsync(this IdentityServerDriverProvider ext, Client client)
        {
            ext    = ext ?? throw new ArgumentNullException(nameof(ext));
            client = client ?? throw new ArgumentNullException(nameof(client));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n = new Node(type: typeof(Neo4jClient));

                Neo4jClient newClient = await session.AsAsync(s =>
                                                              s.ExecuteQuery <Neo4jClient>($"CREATE (p{n.Labels}) SET p+=${nameof(client)}, p.{nameof(IGraphEntity.EntityId)}=id(p), p :{typeof(Neo4jClient).Name} RETURN p",
                                                                                           new { client = client.ExludeProperties(p => new { p.Properties, p.Claims, p.ClientSecrets }) }).FirstOrDefault());

                if (client is Neo4jClient)
                {
                    (client as Neo4jClient).EntityId = newClient.EntityId;
                }
            }
        }
Esempio n. 11
0
        public static async Task RemoveClientAsync(this IdentityServerDriverProvider ext, Client client)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string clientId = client?.ClientId ?? throw new ArgumentNullException(nameof(client));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jClient));
                Node p   = new Node(type: typeof(Neo4jProperty));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}}) " +
                    $"OPTIONAL MATCH (n)-{rel}->(p) " +
                    $"DETACH DELETE p, n",
                    new { clientId });
            }
        }
Esempio n. 12
0
        public static async Task ClearAllApiResourceScopesAsync(this IdentityServerDriverProvider ext, ApiResource resource)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string name = resource?.Name ?? throw new ArgumentNullException(nameof(resource));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jApiResource));
                Node p   = new Node(type: typeof(Neo4jScope));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Resource.Name)}:${nameof(name)}}})" +
                    $"-{rel}->" +
                    $"(p{p.Labels}) " +
                    $"DETACH DELETE p",
                    new { name });
            }
        }
Esempio n. 13
0
        public static async Task RemoveResourceAsync(this IdentityServerDriverProvider ext, Resource resource)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string name = resource?.Name ?? throw new ArgumentNullException(nameof(resource));

            Type resType = resource is IdentityResource ? typeof(Neo4jIdentityResource) : typeof(Neo4jApiResource);

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: resType);
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Resource.Name)}:${nameof(name)}}}) " +
                    $"OPTIONAL MATCH (n)-{rel}->(p) " +
                    $"DETACH DELETE p, n",
                    new { name });
            }
        }
Esempio n. 14
0
        public static async Task ReplaceClientSecretAsync(this IdentityServerDriverProvider ext, Client client, Secret secret)
        {
            ext    = ext ?? throw new ArgumentNullException(nameof(ext));
            secret = secret ?? throw new ArgumentNullException(nameof(secret));

            string clientId = client?.ClientId ?? throw new ArgumentNullException(nameof(client));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jClient));
                Node p   = new Node(type: typeof(Neo4jSecret));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}})" +
                    $"-{rel}->" +
                    $"(p{p.Labels} {{{nameof(Neo4jSecret.Description)}:${nameof(secret)}.{nameof(secret.Description)}}}) " +
                    $"SET p+=${nameof(secret)}",
                    new { clientId, secret });
            }
        }
Esempio n. 15
0
        public static async Task ReplaceApiResourceSecretAsync(this IdentityServerDriverProvider ext, ApiResource resource, Secret secret)
        {
            ext    = ext ?? throw new ArgumentNullException(nameof(ext));
            secret = secret ?? throw new ArgumentNullException(nameof(secret));

            string name = resource?.Name ?? throw new ArgumentNullException(nameof(resource));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jApiResource));
                Node p   = new Node(type: typeof(Neo4jSecret));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Resource.Name)}:${nameof(name)}}})" +
                    $"-{rel}->" +
                    $"(p{p.Labels} {{{nameof(Neo4jSecret.Description)}:${nameof(secret)}.{nameof(secret.Description)}}}) " +
                    $"SET p+=${nameof(secret)}",
                    new { name, secret });
            }
        }
Esempio n. 16
0
        public static async Task RemovePersistedGrantAsync(this IdentityServerDriverProvider ext, PersistedGrant grant)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));

            string key      = grant?.Key ?? throw new ArgumentNullException(nameof(grant));
            string clientId = grant?.ClientId ?? throw new ArgumentNullException(nameof(grant));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(PersistedGrant));
                Node c   = new Node(type: typeof(Client));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (c{c.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}})" +
                    $"-{rel}->" +
                    $"(n{n.Labels} {{{nameof(PersistedGrant.Key)}:${nameof(key)}}})" +
                    $"DETACH DELETE n",
                    new { key, clientId });
            }
        }
Esempio n. 17
0
        public static async Task AddResourceAsync(this IdentityServerDriverProvider ext, Resource resource)
        {
            ext      = ext ?? throw new ArgumentNullException(nameof(ext));
            resource = resource ?? throw new ArgumentNullException(nameof(resource));

            Type resType = resource is IdentityResource ? typeof(Neo4jIdentityResource) : typeof(Neo4jApiResource);
            IDictionary <string, object> resArg = resource is IdentityResource?resource.ToPropDictionary() : (resource as ApiResource).ExludeProperties(p => new { p.Scopes, p.ApiSecrets });

            using (ISession session = ext.GetDriver().Session())
            {
                Node n = new Node(type: resType);

                Resource newResource = await session.AsAsync(s =>
                                                             s.ExecuteQuery <Resource>($"CREATE (p{n.Labels}) SET p+=${nameof(resource)}, p.{nameof(IGraphEntity.EntityId)}=id(p), p :{typeof(Resource).Name}, p :{resType.Name} RETURN p",
                                                                                       new { resource = resArg }).FirstOrDefault());

                if (resource is IGraphEntity)
                {
                    (resource as IGraphEntity).EntityId = (newResource as IGraphEntity)?.EntityId;
                }
            }
        }
Esempio n. 18
0
        public static async Task RemoveClientPropAsync(this IdentityServerDriverProvider ext, Client client, string name)
        {
            ext = ext ?? throw new ArgumentNullException(nameof(ext));
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            string clientId = client?.ClientId ?? throw new ArgumentNullException(nameof(client));

            using (ISession session = ext.GetDriver().Session())
            {
                Node n   = new Node(type: typeof(Neo4jClient));
                Node p   = new Node(type: typeof(Neo4jProperty));
                Rel  rel = new Rel(type: typeof(Relationships.Has));

                await session.RunAsync(
                    $"MATCH (n{n.Labels} {{{nameof(Client.ClientId)}:${nameof(clientId)}}})" +
                    $"-{rel}->" +
                    $"(p{p.Labels} {{{nameof(Neo4jProperty.Name)}:${nameof(name)}}}) " +
                    $"DETACH DELETE p",
                    new { clientId, name });
            }
        }