private void Add(Scope scope)
        {
            Console.Write("\t{0}: ", scope.Name);

            using (var db = CreateContext())
            {
                var s = db.Scopes.SingleOrDefault(x => x.Name == scope.Name);
                if (s != null)
                {
                    Console.WriteLine("error: already exists.");
                    return;
                }

                var entity = scope.ToEntity();
                db.Scopes.Add(entity);
                try
                {
                    db.SaveChanges();
                    Console.WriteLine("success");
                }
                catch(Exception ex)
                {
                    Console.WriteLine("error: {0}", ex.Message);
                }
            }
        }
        public void CanSerializeAndDeserializeAScope()
        {
            var s1 = new Scope
            {
                Name = "email",
                Required = true,
                IsOpenIdScope = true,
                Emphasize = true,
                DisplayName = "email foo",
                Description = "desc foo",
                Claims = new ScopeClaim[] { 
                    new ScopeClaim{Name = "email", Description = "email"}
                }
            };
            var s2 = new Scope
            {
                Name = "read",
                Required = true,
                IsOpenIdScope = false,
                Emphasize = true,
                DisplayName = "foo",
                Description = "desc",
            };
            var converter = new ScopeConverter(new Scope[] { s1, s2 });

            var settings = new JsonSerializerSettings();
            settings.Converters.Add(converter);
            var json = JsonConvert.SerializeObject(s1, settings);

            var result = JsonConvert.DeserializeObject<Scope>(json, settings);
            Assert.AreSame(s1, result);
        }
        public void CanSerializeAndDeserializeAScope()
        {
            var s1 = new Scope
            {
                Name = "email",
                Required = true,
                Type = ScopeType.Identity,
                Emphasize = true,
                DisplayName = "email foo",
                Description = "desc foo",
                Claims = new List<ScopeClaim> { 
                    new ScopeClaim{Name = "email", Description = "email"}
                }
            };
            var s2 = new Scope
            {
                Name = "read",
                Required = true,
                Type = ScopeType.Resource,
                Emphasize = true,
                DisplayName = "foo",
                Description = "desc",
            };
            var converter = new ScopeConverter(new InMemoryScopeStore(new Scope[] { s1, s2 }));

            var settings = new JsonSerializerSettings();
            settings.Converters.Add(converter);
            var json = JsonConvert.SerializeObject(s1, settings);

            var result = JsonConvert.DeserializeObject<Scope>(json, settings);
            Assert.AreSame(s1, result);
        }
        public static IEnumerable<Scope> Get()
        {
            var scopes = new List<Scope>();

            scopes.AddRange(StandardScopes.All);

            var profileScope = new Scope
            {
                Name = ApplicationScopes.AppProfile,
                DisplayName = "User ProfileScope",
                Description = "Scope for Application specific user details.",
                Type = ScopeType.Identity,
                IncludeAllClaimsForUser = true,
                ShowInDiscoveryDocument = true,
                Claims = new List<ScopeClaim>
                {
                    new ScopeClaim(ApplicationClaimTypes.DisplayName, alwaysInclude: true)
                }
            };
            scopes.Add(profileScope);

            var mvcScope = new Scope
            {
                Name = ApplicationScopes.MvcApp,
                DisplayName = "Mvc App Scope",
                Description = "Scope for Mvc Application.",
                Type = ScopeType.Resource,
                IncludeAllClaimsForUser = true,
                ShowInDiscoveryDocument = true,
                Claims = new List<ScopeClaim>
                {
                    new ScopeClaim(ApplicationClaimTypes.Role, alwaysInclude: true),
                    new ScopeClaim(ApplicationClaimTypes.CallApi, alwaysInclude: true)
                }
            };
            scopes.Add(mvcScope);

            var consoleScope = new Scope
            {
                Name = ApplicationScopes.ConsoleApp,
                DisplayName = "Console App Scope",
                Description = "Scope for Console Application.",
                Type = ScopeType.Resource,
                IncludeAllClaimsForUser = true,
                ShowInDiscoveryDocument = true,
                Claims = new List<ScopeClaim>
                {
                    new ScopeClaim(ApplicationClaimTypes.Role, alwaysInclude: true),
                    new ScopeClaim(ApplicationClaimTypes.Values, alwaysInclude: true)
                }
            };
            scopes.Add(consoleScope);

            return scopes;
        }
            public async Task CanFindASingleScope(NpgsqlScopeStore store, Scope scope)
            {
                // Given
                await store.SaveScopeAsync(scope);

                // When
                var scopes = await store.FindScopesAsync(new[] { scope.Name });

                // Then
                scopes.Count().ShouldBe(1);

            }
 private void Add(Scope[] scopes)
 {
     if (scopes != null && scopes.Any())
     {
         Console.WriteLine();
         Console.WriteLine(" Adding Scopes");
         foreach (var scope in scopes)
         {
             Add(scope);
         }
     }
 }
        protected override void ProcessRecord()
        {
            var db = new ScopeConfigurationDbContext(this.ConnectionString, this.Schema);

            var scope = new Thinktecture.IdentityServer.Core.Models.Scope
            {
                Name                    = this.Name,
                DisplayName             = this.DisplayName,
                Description             = this.Description,
                Type                    = this.ScopeType,
                ShowInDiscoveryDocument = this.Discoverable,
                Emphasize               = this.Emphasize,
                Required                = this.Required,
            };

            db.Scopes.Add(scope.ToEntity());
            db.SaveChanges();
            WriteObject(scope);
        }
        protected override void ProcessRecord()
        {
            var db = new ScopeConfigurationDbContext(this.ConnectionString, this.Schema);

            var scope = new Thinktecture.IdentityServer.Core.Models.Scope
            {
                Name = this.Name,
                DisplayName = this.DisplayName,
                Description = this.Description,
                Type = this.ScopeType,
                ShowInDiscoveryDocument = this.Discoverable,
                Emphasize = this.Emphasize,
                Required = this.Required,
            };

            db.Scopes.Add(scope.ToEntity());
            db.SaveChanges();
            WriteObject(scope);
        }
 protected override void ProcessRecord()
 {
     var scope = new Scope()
     {
         Claims = (Claims ?? new ScopeClaim[] {}).ToList(),
         ClaimsRule = ClaimsRule,
         Description = Description,
         DisplayName = DisplayName,
         Emphasize = Emphasize.GetValueOrDefault(DefaultValues.Emphasize),
         Enabled = Enabled.GetValueOrDefault(DefaultValues.Enabled),
         IncludeAllClaimsForUser =
             IncludeAllClaimsForUser.GetValueOrDefault(DefaultValues.IncludeAllClaimsForUser),
         Name = Name,
         Required = Required.GetValueOrDefault(DefaultValues.Required),
         ShowInDiscoveryDocument =
             ShowInDiscoveryDocument.GetValueOrDefault(DefaultValues.ShowInDiscoveryDocument),
         Type = Type.GetValueOrDefault(DefaultValues.Type)
     };
     
     WriteObject(scope);
 }
 public async Task Insert(NpgsqlScopeStore store, Scope scope)
 {
     await store.SaveScopeAsync(scope);
 }
        public ScopeConverter(Scope[] scopes)
        {
            if (scopes == null) throw new ArgumentNullException("scopes");

            _scopes = scopes;
        }
 public async Task Save(Scope scope)
 {
     BsonDocument doc = new ScopeSerializer().Serialize(scope);
     IMongoCollection<BsonDocument> collection = _db.GetCollection<BsonDocument>(_settings.ScopeCollection);
     var result = await collection.ReplaceOneAsync(Filter.ById(scope.Name), doc, new UpdateOptions() {IsUpsert = true} );
     _log.Debug(result.ToString);
 }
        public Task SaveScopeAsync(Scope scope)
        {
            string query = $"INSERT INTO {_schema}.scopes(name, is_public, model) " +
                           "VALUES (@name, @public, @model)";

            return _conn.ExecuteCommand(query,
                async cmd =>
                {
                    var serialized = _serializer.Serialize(scope);

                    cmd.Parameters.AddWithValue("name", scope.Name);
                    cmd.Parameters.AddWithValue("public", scope.ShowInDiscoveryDocument);
                    cmd.Parameters.AddWithValue("model", serialized);

                    await cmd.ExecuteNonQueryAsync();

                    return 0;
                });
        }