Ejemplo n.º 1
0
        private async Task RemoveClientRelationsAsync(Client client)
        {
            //Remove old allowed scopes
            var clientScopes = await DbContext.ClientScopes.Where(x => x.Client.Id == client.Id).ToListAsync();

            DbContext.ClientScopes.RemoveRange(clientScopes);

            //Remove old grant types
            var clientGrantTypes = await DbContext.ClientGrantTypes.Where(x => x.Client.Id == client.Id).ToListAsync();

            DbContext.ClientGrantTypes.RemoveRange(clientGrantTypes);

            //Remove old redirect uri
            var clientRedirectUris = await DbContext.ClientRedirectUris.Where(x => x.Client.Id == client.Id).ToListAsync();

            DbContext.ClientRedirectUris.RemoveRange(clientRedirectUris);

            //Remove old client cors
            var clientCorsOrigins = await DbContext.ClientCorsOrigins.Where(x => x.Client.Id == client.Id).ToListAsync();

            DbContext.ClientCorsOrigins.RemoveRange(clientCorsOrigins);

            //Remove old client id restrictions
            var clientIdPRestrictions = await DbContext.ClientIdPRestrictions.Where(x => x.Client.Id == client.Id).ToListAsync();

            DbContext.ClientIdPRestrictions.RemoveRange(clientIdPRestrictions);

            //Remove old client post logout redirect
            var clientPostLogoutRedirectUris = await DbContext.ClientPostLogoutRedirectUris.Where(x => x.Client.Id == client.Id).ToListAsync();

            DbContext.ClientPostLogoutRedirectUris.RemoveRange(clientPostLogoutRedirectUris);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add new client, this method doesn't save client secrets, client claims, client properties
        /// </summary>
        /// <param name="client"></param>
        /// <returns>This method return new client id</returns>
        public virtual async Task <int> AddClientAsync(Client client)
        {
            DbContext.Clients.Add(client);

            await AutoSaveChangesAsync();

            return(client.Id);
        }
Ejemplo n.º 3
0
        public virtual async Task <int> UpdateClientAsync(Client client)
        {
            //Remove old relations
            await RemoveClientRelationsAsync(client);

            //Update with new data
            DbContext.Clients.Update(client);

            return(await AutoSaveChangesAsync());
        }
Ejemplo n.º 4
0
        public virtual async Task <bool> CanInsertClientAsync(Client client, bool isCloned = false)
        {
            if (client.Id == 0 || isCloned)
            {
                var existsWithClientName = await DbContext.Clients.Where(x => x.ClientId == client.ClientId).SingleOrDefaultAsync();

                return(existsWithClientName == null);
            }
            else
            {
                var existsWithClientName = await DbContext.Clients.Where(x => x.ClientId == client.ClientId && x.Id != client.Id).SingleOrDefaultAsync();

                return(existsWithClientName == null);
            }
        }
Ejemplo n.º 5
0
        public virtual async Task <int> RemoveClientAsync(Client client)
        {
            DbContext.Clients.Remove(client);

            return(await AutoSaveChangesAsync());
        }
Ejemplo n.º 6
0
        public virtual async Task <int> CloneClientAsync(Client client,
                                                         bool cloneClientCorsOrigins            = true,
                                                         bool cloneClientGrantTypes             = true,
                                                         bool cloneClientIdPRestrictions        = true,
                                                         bool cloneClientPostLogoutRedirectUris = true,
                                                         bool cloneClientScopes       = true,
                                                         bool cloneClientRedirectUris = true,
                                                         bool cloneClientClaims       = true,
                                                         bool cloneClientProperties   = true
                                                         )
        {
            var clientToClone = await DbContext.Clients
                                .Include(x => x.AllowedGrantTypes)
                                .Include(x => x.RedirectUris)
                                .Include(x => x.PostLogoutRedirectUris)
                                .Include(x => x.AllowedScopes)
                                .Include(x => x.ClientSecrets)
                                .Include(x => x.Claims)
                                .Include(x => x.IdentityProviderRestrictions)
                                .Include(x => x.AllowedCorsOrigins)
                                .Include(x => x.Properties)
                                .AsNoTracking()
                                .FirstOrDefaultAsync(x => x.Id == client.Id);

            clientToClone.ClientName = client.ClientName;
            clientToClone.ClientId   = client.ClientId;

            //Clean original ids
            clientToClone.Id = 0;
            clientToClone.AllowedCorsOrigins.ForEach(x => x.Id           = 0);
            clientToClone.RedirectUris.ForEach(x => x.Id                 = 0);
            clientToClone.PostLogoutRedirectUris.ForEach(x => x.Id       = 0);
            clientToClone.AllowedScopes.ForEach(x => x.Id                = 0);
            clientToClone.ClientSecrets.ForEach(x => x.Id                = 0);
            clientToClone.IdentityProviderRestrictions.ForEach(x => x.Id = 0);
            clientToClone.Claims.ForEach(x => x.Id            = 0);
            clientToClone.AllowedGrantTypes.ForEach(x => x.Id = 0);
            clientToClone.Properties.ForEach(x => x.Id        = 0);

            //Client secret will be skipped
            clientToClone.ClientSecrets.Clear();

            if (!cloneClientCorsOrigins)
            {
                clientToClone.AllowedCorsOrigins.Clear();
            }

            if (!cloneClientGrantTypes)
            {
                clientToClone.AllowedGrantTypes.Clear();
            }

            if (!cloneClientIdPRestrictions)
            {
                clientToClone.IdentityProviderRestrictions.Clear();
            }

            if (!cloneClientPostLogoutRedirectUris)
            {
                clientToClone.PostLogoutRedirectUris.Clear();
            }

            if (!cloneClientScopes)
            {
                clientToClone.AllowedScopes.Clear();
            }

            if (!cloneClientRedirectUris)
            {
                clientToClone.RedirectUris.Clear();
            }

            if (!cloneClientClaims)
            {
                clientToClone.Claims.Clear();
            }

            if (!cloneClientProperties)
            {
                clientToClone.Properties.Clear();
            }

            await DbContext.Clients.AddAsync(clientToClone);

            await AutoSaveChangesAsync();

            var id = clientToClone.Id;

            return(id);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Maps an entity to a model.
 /// </summary>
 /// <param name="entity">The entity.</param>
 /// <returns></returns>
 public static Models.Client ToModel(this Duende.IdentityServer.EntityFramework.Entities.Client entity)
 {
     return(Mapper.Map <Models.Client>(entity));
 }