public async Task UpdateConsentAsync(Models.Client client, System.Security.Claims.ClaimsPrincipal user, IEnumerable<string> scopes)
        {
            if (client.AllowRememberConsent)
            {
                using (var db = new CoreDbContext(_connectionString))
                {
                    var clientId = client.ClientId;
                    var subject = user.GetSubjectId();

                    var consent = await db.Consents.FindAsync(subject, clientId);

                    if (scopes.Any())
                    {
                        if (consent == null)
                        {
                            consent = new Entities.Consent
                            {
                                ClientId = client.ClientId,
                                Subject = user.GetSubjectId(),
                            };
                            db.Consents.Add(consent);
                        }

                        consent.Scopes = string.Join(" ", scopes.OrderBy(s => s).ToArray());
                    }
                    else if (consent != null)
                    {
                        db.Consents.Remove(consent);
                    }

                    db.SaveChanges();
                }
            }
        }
        private static void CleanUpTokens(object sender, ElapsedEventArgs e)
        {
            // Clean up expired tokens
            DateTime referenceDate = DateTime.UtcNow;

            using (var db = new CoreDbContext(_connectionString))
            {
                db.Tokens.RemoveRange(db.Tokens.Where(c => c.Expiry < referenceDate));
                db.SaveChanges();
            }
        }
        private static void CleanUpTokens(object sender, ElapsedEventArgs e)
        {
            // Clean up expired tokens
            DateTime referenceDate = DateTime.UtcNow;

            using (var db = new CoreDbContext(_connectionString))
            {
                db.Tokens.RemoveRange(db.Tokens.Where(c => c.Expiry < referenceDate));
                db.SaveChanges();
            }
        }
        public Task <IEnumerable <Models.Scope> > GetScopesAsync()
        {
            using (var db = new CoreDbContext(_connectionString))
            {
                var scopes = db.Scopes
                             .Include("ScopeClaims");

                var models = scopes.ToList().Select(x => x.ToModel());

                return(Task.FromResult(models));
            }
        }
        public Task<IEnumerable<Models.Scope>> GetScopesAsync()
        {
            using (var db = new CoreDbContext(_connectionString))
            {
                var scopes = db.Scopes
                    .Include("ScopeClaims")
                    .ToArray();
                
                var models = scopes.ToList().Select(x => x.ToModel());

                return Task.FromResult(models);
            }
        }
        public Task <bool> RequiresConsentAsync(string client, string subject, IEnumerable <string> scopes)
        {
            var orderedScopes = GetOrderedScopes(scopes);

            using (var db = new CoreDbContext(_connectionString))
            {
                var exists = db.Consents.Any(c => c.ClientId == client &&
                                             c.Scopes == orderedScopes &&
                                             c.Subject == subject);

                return(Task.FromResult(!exists));
            }
        }
Esempio n. 7
0
        public Task <Models.Client> FindClientByIdAsync(string clientId)
        {
            using (var db = new CoreDbContext(_connectionString))
            {
                var client = db.Clients
                             .Include("RedirectUris")
                             .Include("ScopeRestrictions")
                             .SingleOrDefault(x => x.ClientId == clientId);

                Models.Client model = client.ToModel();
                return(Task.FromResult(model));
            }
        }
        public Task<Models.Client> FindClientByIdAsync(string clientId)
        {
            using(var db = new CoreDbContext(_connectionString))
            {
                var client = db.Clients
                    .Include("RedirectUris")
                    .Include("ScopeRestrictions")
                    .SingleOrDefault(x => x.ClientId == clientId);

                Models.Client model = client.ToModel();
                return Task.FromResult(model);    
            }
        }
        public Task <T> GetAsync(string key)
        {
            using (var db = new CoreDbContext(ConnectionString))
            {
                var token = db.Tokens.FirstOrDefault(c => c.Key == key && c.TokenType == TokenType);
                if (token == null || token.Expiry < DateTime.UtcNow)
                {
                    return(Task.FromResult <T>(null));
                }

                T value = ConvertFromJson(token.JsonCode);
                return(Task.FromResult(value));
            }
        }
 public void ConfigureClients(IEnumerable <Client> clients)
 {
     using (var db = new CoreDbContext(_connectionString))
     {
         if (!db.Clients.Any())
         {
             foreach (var c in clients)
             {
                 var e = c.ToEntity();
                 db.Clients.Add(e);
             }
             db.SaveChanges();
         }
     }
 }
        public Task RemoveAsync(string key)
        {
            using (var db = new CoreDbContext(ConnectionString))
            {
                var code = db.Tokens.FirstOrDefault(c => c.Key == key && c.TokenType == TokenType);

                if (code != null)
                {
                    db.Tokens.Remove(code);
                    db.SaveChanges();
                }
            }

            return(Task.FromResult(0));
        }
 public void ConfigureScopes(IEnumerable <Scope> scopes)
 {
     using (var db = new CoreDbContext(_connectionString))
     {
         if (!db.Scopes.Any())
         {
             foreach (var s in scopes)
             {
                 var e = s.ToEntity();
                 db.Scopes.Add(e);
             }
             db.SaveChanges();
         }
     }
 }
 public void ConfigureScopes(IEnumerable<Scope> scopes)
 {
     using (var db = new CoreDbContext(_connectionString))
     {
         if (!db.Scopes.Any())
         {
             foreach (var s in scopes)
             {
                 var e = s.ToEntity();
                 db.Scopes.Add(e);
             }
             db.SaveChanges();
         }
     }
 }
 public void ConfigureClients(IEnumerable<Client> clients)
 {
     using (var db = new CoreDbContext(_connectionString))
     {
         if (!db.Clients.Any())
         {
             foreach (var c in clients)
             {
                 var e = c.ToEntity();
                 db.Clients.Add(e);
             }
             db.SaveChanges();
         }
     }
 }
Esempio n. 15
0
        public Task RemoveAsync(string key)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var db = new CoreDbContext(ConnectionString))
                {
                    var code = db.Tokens.FirstOrDefault(c => c.Key == key);

                    if (code != null)
                    {
                        db.Tokens.Remove(code);
                        db.SaveChanges();
                    }
                }
            }));
        }
Esempio n. 16
0
        public Task <T> GetAsync(string key)
        {
            return(Task <T> .Factory.StartNew(() =>
            {
                using (var db = new CoreDbContext(ConnectionString))
                {
                    Entities.Token dbCode = db.Tokens.FirstOrDefault(c => c.Key == key);

                    if (dbCode == null || dbCode.Expiry < DateTime.UtcNow)
                    {
                        return null;
                    }

                    return JsonConvert.DeserializeObject <T>(dbCode.JsonCode, _jsonSerializerSettings);
                }
            }));
        }
Esempio n. 17
0
        public override Task StoreAsync(string key, RefreshToken value)
        {
            using (var db = new CoreDbContext(ConnectionString))
            {
                var efToken = new Entities.Token
                {
                    Key       = key,
                    JsonCode  = ConvertToJson(value),
                    Expiry    = DateTime.UtcNow.AddSeconds(value.LifeTime),
                    TokenType = TokenType
                };

                db.Tokens.Add(efToken);
                db.SaveChanges();
            }

            return(Task.FromResult(0));
        }
Esempio n. 18
0
        public override Task StoreAsync(string key, AuthorizationCode code)
        {
            using (var db = new CoreDbContext(ConnectionString))
            {
                var efCode = new Entities.Token
                {
                    Key       = key,
                    JsonCode  = ConvertToJson(code),
                    Expiry    = DateTime.UtcNow.AddSeconds(code.Client.AuthorizationCodeLifetime),
                    TokenType = this.TokenType
                };

                db.Tokens.Add(efCode);
                db.SaveChanges();
            }

            return(Task.FromResult(0));
        }
        public override Task StoreAsync(string key, Token value)
        {
            return(Task.Factory.StartNew(() =>
            {
                using (var db = new CoreDbContext(ConnectionString))
                {
                    var efToken = new Entities.Token
                    {
                        Key = key,
                        JsonCode = JsonConvert.SerializeObject(value),
                        Expiry = DateTime.UtcNow.AddSeconds(value.Lifetime)
                    };

                    db.Tokens.Add(efToken);
                    db.SaveChanges();
                }
            }));
        }
Esempio n. 20
0
        public Task UpdateConsentAsync(Models.Client client, System.Security.Claims.ClaimsPrincipal user, IEnumerable <string> scopes)
        {
            if (client.AllowRememberConsent)
            {
                using (var db = new CoreDbContext(_connectionString))
                {
                    var consent = new Entities.Consent
                    {
                        ClientId = client.ClientId,
                        Subject  = user.GetSubjectId(),
                        Scopes   = string.Join(" ", scopes.OrderBy(s => s).ToArray())
                    };

                    db.Consents.Add(consent);
                    db.SaveChanges();
                }
            }

            return(Task.FromResult(0));
        }
        public Task<bool> RequiresConsentAsync(Models.Client client, System.Security.Claims.ClaimsPrincipal user, IEnumerable<string> scopes)
        {
            if (!client.RequireConsent)
            {
                return Task.FromResult(false);
            }

            var orderedScopes = string.Join(" ", scopes.OrderBy(s => s).ToArray());

            string subjectId = user.GetSubjectId();

            using (var db = new CoreDbContext(_connectionString))
            {
                var exists = db.Consents.Any(c => c.ClientId == client.ClientId &&
                                                c.Scopes == orderedScopes &&
                                                c.Subject == subjectId);

                return Task.FromResult(!exists);
            }
        }
Esempio n. 22
0
        public Task <bool> RequiresConsentAsync(Models.Client client, System.Security.Claims.ClaimsPrincipal user, IEnumerable <string> scopes)
        {
            if (!client.RequireConsent)
            {
                return(Task.FromResult(false));
            }

            var orderedScopes = string.Join(" ", scopes.OrderBy(s => s).ToArray());

            string subjectId = user.GetSubjectId();

            using (var db = new CoreDbContext(_connectionString))
            {
                var consent = db.Consents.FirstOrDefault(c => c.ClientId == client.ClientId &&
                                                         c.Scopes == orderedScopes &&
                                                         c.Subject == subjectId);

                return(Task.FromResult(consent == null));
            }
        }