Example #1
0
        public static ApiResource ToApi(this Entity.ProtectResource api)
        {
            if (api == null)
            {
                return(null);
            }
            var cultureId = CultureInfo.CurrentCulture.Name;
            var resources = api.Resources;

            return(new ApiResource
            {
                ApiSecrets = api.Secrets.Select(s => new Secret {
                    Value = s.Value, Expiration = s.Expiration
                }).ToList(),
                Description = resources.FirstOrDefault(r => r.ResourceKind == Entity.EntityResourceKind.Description &&
                                                       r.CultureId == cultureId)?.Value ?? api.Description,
                DisplayName = resources.FirstOrDefault(r => r.ResourceKind == Entity.EntityResourceKind.DisplayName &&
                                                       r.CultureId == cultureId)?.Value ?? api.DisplayName,
                Enabled = api.Enabled,
                Name = api.Id,
                Properties = api.Properties.ToDictionary(p => p.Key, p => p.Value),
                Scopes = api.ApiScopes.Select(s => s.ApiScopeId).ToList(),
                UserClaims = api.ApiClaims.Select(c => c.Type).ToList()
            });
        }
Example #2
0
        public static ApiResource ToApi(this Entity.ProtectResource api)
        {
            if (api == null)
            {
                return(null);
            }

            return(new ApiResource
            {
                ApiSecrets = api.Secrets.Select(s => new Secret {
                    Value = s.Value, Expiration = s.Expiration
                }).ToList(),
                Description = api.Description,
                DisplayName = api.DisplayName,
                Enabled = api.Enabled,
                Name = api.Id,
                Properties = api.Properties.ToDictionary(p => p.Key, p => p.Value),
                Scopes = api.Scopes.Select(s => new Scope
                {
                    Description = s.Description,
                    DisplayName = s.DisplayName,
                    Emphasize = s.Emphasize,
                    Name = s.Scope,
                    Required = s.Required,
                    ShowInDiscoveryDocument = s.ShowInDiscoveryDocument,
                    UserClaims = api.ApiScopeClaims
                                 .Where(s => s.ApiScpopeId == s.Id)
                                 .Select(c => c.Type).ToList()
                }).ToList(),
                UserClaims = api.ApiClaims.Select(c => c.Type).ToList()
            });
        }
        public async Task DeleteAsync_should_remove_entity_id_from_parents()
        {
            using var store = new RavenDbTestDriverWrapper().GetDocumentStore();

            var entity = new Entity.ApiApiScope
            {
                Id         = Guid.NewGuid().ToString(),
                ApiId      = "test",
                ApiScopeId = "test"
            };
            var api = new Entity.ProtectResource
            {
                Id        = "test",
                ApiScopes = new List <Entity.ApiApiScope>
                {
                    new Entity.ApiApiScope
                    {
                        Id = $"{typeof(Entity.ApiApiScope).Name.ToLowerInvariant()}/{entity.Id}"
                    }
                }
            };
            var apiScope = new Entity.ApiScope
            {
                Id   = "test",
                Apis = new List <Entity.ApiApiScope>
                {
                    new Entity.ApiApiScope
                    {
                        Id = $"{typeof(Entity.ApiApiScope).Name.ToLowerInvariant()}/{entity.Id}"
                    }
                }
            };

            using var s1 = store.OpenAsyncSession();
            await s1.StoreAsync(api, $"{nameof(Entity.ProtectResource).ToLowerInvariant()}/{api.Id}");

            await s1.StoreAsync(apiScope, $"{nameof(Entity.ApiScope).ToLowerInvariant()}/{apiScope.Id}");

            await s1.StoreAsync(entity, $"{typeof(Entity.ApiApiScope).Name.ToLowerInvariant()}/{entity.Id}");

            await s1.SaveChangesAsync();

            var loggerMock = new Mock <ILogger <AdminStore <Entity.ApiApiScope> > >();

            using var session = store.OpenAsyncSession();

            var sut = new ApiApiScopeStore(new ScopedAsynDocumentcSession(session), loggerMock.Object);


            await sut.DeleteAsync(entity.Id);

            using var s2 = store.OpenAsyncSession();
            var apiUpdated = await s2.LoadAsync <Entity.ProtectResource>($"{nameof(Entity.ProtectResource).ToLowerInvariant()}/test");

            Assert.DoesNotContain(apiUpdated.ApiScopes, i => i.Id == $"{typeof(Entity.ApiApiScope).Name.ToLowerInvariant()}/{entity.Id}");
            var apiScopeUpdated = await s2.LoadAsync <Entity.ApiScope>($"{nameof(Entity.ApiScope).ToLowerInvariant()}/test");

            Assert.DoesNotContain(apiScopeUpdated.Apis, i => i.Id == $"{typeof(Entity.ApiApiScope).Name.ToLowerInvariant()}/{entity.Id}");
        }