コード例 #1
0
        private void SaveResources(CustomConfigurationDbContext ctx, ResourcesDataStorage storage)
        {
            // ApiResources
            ctx.ApiResources.AddRange(storage.ApiResources);
            SaveWithIdentityColumn(ctx, nameof(ctx.ApiResources));

            //1st level
            ctx.ApiClaims.AddRange(storage.ApiResourceClaims);
            SaveWithIdentityColumn(ctx, nameof(ctx.ApiClaims));

            ctx.ApiSecrets.AddRange(storage.ApiSecrets);
            SaveWithIdentityColumn(ctx, nameof(ctx.ApiSecrets));

            //2nd level
            ctx.ApiScopes.AddRange(storage.ApiScopes);
            SaveWithIdentityColumn(ctx, nameof(ctx.ApiScopes));

            ctx.ApiScopeClaims.AddRange(storage.ApiScopeClaims);
            SaveWithIdentityColumn(ctx, nameof(ctx.ApiScopeClaims));



            // IdentityResources
            ctx.IdentityResources.AddRange(storage.IdentityResources);
            SaveWithIdentityColumn(ctx, nameof(ctx.IdentityResources));

            ctx.IdentityClaims.AddRange(storage.IdentityClaims);
            SaveWithIdentityColumn(ctx, nameof(ctx.IdentityClaims));
        }
コード例 #2
0
        /// Read and Map. Not saving changes to Db
        public (Ids3RootDTO, Ids4RootDTO) ReadIds3DbAndMapClientsScopesTreeToIds4Schema(bool enableScopeToApiResource2ndLevelMapping)
        {
            var clients3Source = Ids3Tool.GetIds3ClientsRoot();
            var clients4Target = _mapper.Map <Ids4Entities.Client[]>(clients3Source.Clients);

            //  IdentityResources
            var identityResources4Target = _mapper.Map <Ids4Entities.IdentityResource[]>(clients3Source.Scopes.Where(x => x.Type == (int)ScopeType.Identity));

            //  ApiResources
            var apiResources4Target = _mapper.Map <Ids4Entities.ApiResource[]>(clients3Source.Scopes.Where(x => x.Type == (int)ScopeType.Resource));

            var claims = apiResources4Target.SelectMany(x => x.UserClaims)
                         .Select(x => new
            {
                x.Type,
                x.ApiResourceId,
                ApiResourceName = x.ApiResource?.Name
            }).ToList();

            claims.ForEach(x => Debug.WriteLine($"{x.Type}: ({x.ApiResourceId}-{x.ApiResourceName})"));


            var resourcesStorage = new ResourcesDataStorage(
                identityResources4Target, apiResources4Target,
                new List <string>(),  // don't have any existing in Ids4 Identity => don't care
                new List <string>()); // don't have any Resource n Ids4 => don't care

            foreach (var resourceClaim in resourcesStorage.ApiResourceClaims)
            {
                var t = resourceClaim.Type;
            }

            if (enableScopeToApiResource2ndLevelMapping)
            {
                var apiScopes =
                    _mapper.Map <Ids4Entities.ApiScope[]>(
                        clients3Source.Scopes.Where(x => x.Type == (int)ScopeType.Resource));

                // Transform children Level#1 and Level#2
                for (int i = 0; i < apiResources4Target.Length; i++)
                {
                    var ar = apiResources4Target[i];
                    apiScopes[i].ApiResource = ar;

                    // 1 ApiResource => 1 ApiScope
                    ar.Scopes.Add(apiScopes[i]);

                    for (int j = 0; j < apiScopes[i].UserClaims.Count; j++)
                    {
                        apiScopes[i].UserClaims[j].ApiScope   = apiScopes[i];
                        apiScopes[i].UserClaims[j].ApiScopeId = apiScopes[i].Id;
                    }
                }
            }

            return(clients3Source, new Ids4RootDTO
            {
                Clients = clients4Target,
                IdentityResources = identityResources4Target,
                ApiResources = apiResources4Target
            });
        }
コード例 #3
0
        /// Making Ids4 entities tree from Ids3 and Copy to Ids4 Database
        public (Ids3RootDTO, Ids4RootDTO) CopyClientsScopesTreeFromIds3DbToIds4Db(bool enableScopeToApiResource2ndLevelMapping)
        {
            var existingTargetClients4 = Ids4Tool.GetIds4ClientsRoot();
            var existingClients4Ids    = existingTargetClients4.Clients.Select(x => x.ClientId).ToList();

            // Source
            var clients3Source = Ids3Tool.GetIds3ClientsRoot();
            var clients        = _mapper.Map <Ids4Entities.Client[]>(clients3Source.Clients);

            var storage = new ClientDataStorage(clients.ToList(), existingClients4Ids);

            using (CustomConfigurationDbContext ctx = new CustomConfigurationDbContextFactory(_configuration).CreateDbContext(Array.Empty <string>()))
            {
                DatabaseHelper.SwitchIdentityInsertState(ctx, "OFF");

                // Clients
                foreach (var c in storage.Clients)
                {
                    // Add to DbContext
                    ctx.Clients.Add(c);
                }

                storage.Filter(existingClients4Ids);// optional double check, just for sure
                SaveClientsWithChildren(ctx, storage);


                //  IdentityResources
                var identityResources4Target = _mapper.Map <Ids4Entities.IdentityResource[]>(clients3Source.Scopes.Where(x => x.Type == (int)ScopeType.Identity));

                //  ApiResources
                var apiResources4Target = _mapper.Map <Ids4Entities.ApiResource[]>(clients3Source.Scopes.Where(x => x.Type == (int)ScopeType.Resource));

                // TODO: think about 2nd level claims and rework properly
                // because 2nd level api scope includes into aud claim
                if (enableScopeToApiResource2ndLevelMapping)
                {
                    var apiScopes =
                        _mapper.Map <Ids4Entities.ApiScope[]>(
                            clients3Source.Scopes.Where(x => x.Type == (int)ScopeType.Resource));

                    // Transform children Level#1 and Level#2
                    for (int i = 0; i < apiResources4Target.Length; i++)
                    {
                        var ar = apiResources4Target[i];
                        apiScopes[i].ApiResource = ar;

                        // 1 ApiResource => 1 ApiScope
                        ar.Scopes.Add(apiScopes[i]);

                        for (int j = 0; j < apiScopes[i].UserClaims.Count; j++)
                        {
                            apiScopes[i].UserClaims[j].ApiScope   = apiScopes[i];
                            apiScopes[i].UserClaims[j].ApiScopeId = apiScopes[i].Id;
                        }
                    }
                }

                var existingApiResNames      = existingTargetClients4.ApiResources.Select(x => x.Name).ToList();
                var existingIdentityResNames = existingTargetClients4.IdentityResources.Select(x => x.Name).ToList();

                var resourcesStorage = new ResourcesDataStorage(
                    identityResources4Target, apiResources4Target,
                    existingIdentityResNames,
                    existingApiResNames);

                SaveResources(ctx, resourcesStorage);

                DatabaseHelper.SwitchIdentityInsertState(ctx, "OFF");

                return(clients3Source, new Ids4RootDTO
                {
                    Clients = clients,
                    IdentityResources = identityResources4Target,
                    ApiResources = apiResources4Target
                });
            }
        }