Пример #1
0
        public async Task <bool> DeleteAsync(string id)
        {
            try
            {
                var record = await _context.ResourceOwners
                             .Include(r => r.Claims)
                             .Include(r => r.Consents)
                             .FirstOrDefaultAsync(r => r.Id == id).ConfigureAwait(false);

                if (record == null)
                {
                    return(false);
                }

                _context.ResourceOwners.Remove(record);
                await _context.SaveChangesAsync();

                return(true);
            }
            catch (Exception ex)
            {
                _managerEventSource.Failure(ex);
                return(false);
            }
        }
        public async Task <bool> InsertAsync(AddClaimParameter claim)
        {
            if (claim == null)
            {
                throw new ArgumentNullException(nameof(claim));
            }

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var newClaim = new Models.Claim
                    {
                        Code           = claim.Code,
                        IsIdentifier   = false, //claim.IsIdentifier,
                        CreateDateTime = DateTime.UtcNow,
                        UpdateDateTime = DateTime.UtcNow
                    };

                    _context.Claims.Add(newClaim);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }

            return(true);
        }
Пример #3
0
        /// <summary>
        /// Add the profiles.
        /// </summary>
        /// <param name="profiles"></param>
        /// <returns></returns>
        public async Task <bool> Add(IEnumerable <ResourceOwnerProfile> profiles)
        {
            if (profiles == null)
            {
                throw new ArgumentNullException(nameof(profiles));
            }

            var result = true;

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var ps = profiles.Select(p => p.ToModel());
                    _context.AddRange(ps);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    result = false;
                }
            }

            return(result);
        }
        public async Task <bool> DeleteAsync(Core.Common.JsonWebKey jsonWebKey)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var jsonWebKeyToBeRemoved = await _context.JsonWebKeys.FirstOrDefaultAsync(j => j.Kid == jsonWebKey.Kid).ConfigureAwait(false);

                    if (jsonWebKeyToBeRemoved == null)
                    {
                        return(false);
                    }

                    _context.JsonWebKeys.Remove(jsonWebKeyToBeRemoved);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }

            return(true);
        }
        public async Task <bool> InsertAsync(Domains.Scope scope)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var record = new Models.Scope
                    {
                        Name                 = scope.Name,
                        Description          = scope.Description,
                        IsDisplayedInConsent = scope.IsDisplayedInConsent,
                        IsExposed            = scope.IsExposed,
                        IsOpenIdScope        = scope.IsOpenIdScope,
                        Type                 = (Models.ScopeType)scope.Type,
                        ScopeClaims          = new List <Models.ScopeClaim>(),
                        CreateDateTime       = DateTime.UtcNow,
                        UpdateDateTime       = DateTime.UtcNow
                    };

                    if (scope.Claims != null &&
                        scope.Claims.Any())
                    {
                        foreach (var type in scope.Claims)
                        {
                            var rec = _context.Claims.FirstOrDefault(c => c.Code == type);
                            if (rec == null)
                            {
                                rec = new Models.Claim {
                                    Code = type
                                };
                                _context.Claims.Add(rec);
                            }

                            record.ScopeClaims.Add(new Models.ScopeClaim {
                                Claim = rec
                            });
                        }
                    }

                    _context.Scopes.Add(record);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }

            return(true);
        }
Пример #6
0
        public async Task <bool> Add(AuthenticationContextclassReference acr)
        {
            _context.AuthenticationContextclassReferences.Add(new Models.AuthenticationContextclassReference
            {
                AmrLst      = acr.AmrLst == null ? string.Empty : string.Join(",", acr.AmrLst),
                DisplayName = acr.DisplayName,
                Name        = acr.Name,
                IsDefault   = acr.IsDefault,
                Type        = (int)acr.Type
            });
            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(true);
        }
        public async Task <bool> Update(CredentialSetting passwordSettings)
        {
            var record = await _context.CredentialSettings.FirstOrDefaultAsync().ConfigureAwait(false);

            if (record == null)
            {
                _context.CredentialSettings.Add(new Models.CredentialSetting
                {
                    AuthenticationIntervalsInSeconds = passwordSettings.AuthenticationIntervalsInSeconds,
                    IsBlockAccountPolicyEnabled      = passwordSettings.IsBlockAccountPolicyEnabled,
                    NumberOfAuthenticationAttempts   = passwordSettings.NumberOfAuthenticationAttempts,
                    CredentialType = passwordSettings.CredentialType,
                    ExpiresIn      = passwordSettings.ExpiresIn,
                    Options        = passwordSettings.Options
                });
            }
            else
            {
                record.AuthenticationIntervalsInSeconds = passwordSettings.AuthenticationIntervalsInSeconds;
                record.IsBlockAccountPolicyEnabled      = passwordSettings.IsBlockAccountPolicyEnabled;
                record.NumberOfAuthenticationAttempts   = passwordSettings.NumberOfAuthenticationAttempts;
                record.ExpiresIn = passwordSettings.ExpiresIn;
                record.Options   = passwordSettings.Options;
            }

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(true);
        }
 public async Task <bool> AddAsync(ConfirmationCode code)
 {
     using (var transaction = _context.Database.BeginTransaction())
     {
         try
         {
             var record = code.ToModel();
             _context.ConfirmationCodes.Add(record);
             await _context.SaveChangesAsync().ConfigureAwait(false);;
             transaction.Commit();
             return(true);
         }
         catch (Exception ex)
         {
             transaction.Rollback();
             _managerEventSource.Failure(ex);
             return(false);
         }
     }
 }
Пример #9
0
        public async Task <bool> Add(IEnumerable <ResourceOwnerCredential> resourceOwnerCredentials)
        {
            foreach (var resourceOwnerCredential in resourceOwnerCredentials)
            {
                _context.ResourceOwnerCredentials.Add(new Models.ResourceOwnerCredential
                {
                    BlockedDateTime    = resourceOwnerCredential.BlockedDateTime,
                    ExpirationDateTime = resourceOwnerCredential.ExpirationDateTime,
                    FirstAuthenticationFailureDateTime = resourceOwnerCredential.FirstAuthenticationFailureDateTime,
                    IsBlocked        = resourceOwnerCredential.IsBlocked,
                    NumberOfAttempts = resourceOwnerCredential.NumberOfAttempts,
                    ResourceOwnerId  = resourceOwnerCredential.UserId,
                    Type             = resourceOwnerCredential.Type,
                    Value            = resourceOwnerCredential.Value
                });
            }

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(true);
        }
        public async Task <Core.Models.Consent> InsertAsync(Core.Models.Consent record)
        {
            Core.Models.Consent result = null;
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var clientId        = record.Client.ClientId;
                    var resourceOwnerId = record.ResourceOwner.Id;
                    var client          = await _context.Clients.FirstOrDefaultAsync(c => c.ClientId == clientId).ConfigureAwait(false);

                    var resourceOwner = await _context.ResourceOwners.FirstOrDefaultAsync(r => r.Id == resourceOwnerId).ConfigureAwait(false);

                    var assignedClaims = new List <ConsentClaim>();
                    var assignedScopes = new List <ConsentScope>();
                    if (record.Claims != null)
                    {
                        var claimCodes = record.Claims;
                        var codes      = await _context.Claims.Where(c => claimCodes.Contains(c.Code)).Select(c => c.Code).ToListAsync().ConfigureAwait(false);

                        foreach (var code in codes)
                        {
                            assignedClaims.Add(new ConsentClaim
                            {
                                ClaimCode = code
                            });
                        }
                    }

                    if (record.GrantedScopes != null)
                    {
                        var scopeNames = record.GrantedScopes.Select(g => g.Name);
                        var names      = await _context.Scopes.Where(s => scopeNames.Contains(s.Name)).Select(s => s.Name).ToListAsync().ConfigureAwait(false);

                        foreach (var name in names)
                        {
                            assignedScopes.Add(new ConsentScope
                            {
                                ScopeName = name
                            });
                        }
                    }

                    var newConsent = new Consent
                    {
                        Id            = Guid.NewGuid().ToString(),
                        Client        = client,
                        ResourceOwner = resourceOwner,
                        ConsentClaims = assignedClaims,
                        ConsentScopes = assignedScopes
                    };

                    var insertedConsent = _context.Consents.Add(newConsent);
                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    result = insertedConsent.Entity.ToDomain();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                }
            }

            return(result);
        }
        public async Task <bool> UpdateAsync(Core.Common.Models.Client client)
        {
            using (var translation = _context.Database.BeginTransaction())
            {
                try
                {
                    var grantTypes = client.GrantTypes == null
                        ? string.Empty
                        : ConcatListOfIntegers(client.GrantTypes.Select(k => (int)k).ToList());
                    var responseTypes = client.ResponseTypes == null
                        ? string.Empty
                        : ConcatListOfIntegers(client.ResponseTypes.Select(r => (int)r).ToList());
                    var connectedClient = await _context.Clients
                                          .Include(c => c.ClientScopes)
                                          .FirstOrDefaultAsync(c => c.ClientId == client.ClientId)
                                          .ConfigureAwait(false);

                    connectedClient.ClientName                  = client.ClientName;
                    connectedClient.ClientUri                   = client.ClientUri;
                    connectedClient.Contacts                    = ConcatListOfStrings(client.Contacts);
                    connectedClient.DefaultAcrValues            = client.DefaultAcrValues;
                    connectedClient.DefaultMaxAge               = client.DefaultMaxAge;
                    connectedClient.GrantTypes                  = grantTypes;
                    connectedClient.IdTokenEncryptedResponseAlg = client.IdTokenEncryptedResponseAlg;
                    connectedClient.IdTokenEncryptedResponseEnc = client.IdTokenEncryptedResponseEnc;
                    connectedClient.IdTokenSignedResponseAlg    = client.IdTokenSignedResponseAlg;
                    connectedClient.InitiateLoginUri            = client.InitiateLoginUri;
                    connectedClient.JwksUri                     = client.JwksUri;
                    connectedClient.LogoUri                     = client.LogoUri;
                    connectedClient.PolicyUri                   = client.PolicyUri;
                    connectedClient.RedirectionUrls             = ConcatListOfStrings(client.RedirectionUrls);
                    connectedClient.RequestObjectEncryptionAlg  = client.RequestObjectEncryptionAlg;
                    connectedClient.RequestObjectEncryptionEnc  = client.RequestObjectEncryptionEnc;
                    connectedClient.RequestObjectSigningAlg     = client.RequestObjectSigningAlg;
                    connectedClient.RequestUris                 = ConcatListOfStrings(client.RequestUris);
                    connectedClient.PostLogoutRedirectUris      = ConcatListOfStrings(client.PostLogoutRedirectUris);
                    connectedClient.RequireAuthTime             = client.RequireAuthTime;
                    connectedClient.ResponseTypes               = responseTypes;
                    connectedClient.SectorIdentifierUri         = client.SectorIdentifierUri;
                    connectedClient.SubjectType                 = client.SubjectType;
                    connectedClient.TokenEndPointAuthMethod     = (TokenEndPointAuthenticationMethods)client.TokenEndPointAuthMethod;
                    connectedClient.TokenEndPointAuthSigningAlg = client.TokenEndPointAuthSigningAlg;
                    connectedClient.TosUri = client.TosUri;
                    connectedClient.UserInfoEncryptedResponseAlg = client.UserInfoEncryptedResponseAlg;
                    connectedClient.UserInfoEncryptedResponseEnc = client.UserInfoEncryptedResponseEnc;
                    connectedClient.UserInfoSignedResponseAlg    = client.UserInfoSignedResponseAlg;
                    connectedClient.ScimProfile    = client.ScimProfile;
                    connectedClient.UpdateDateTime = DateTime.UtcNow;
                    var scopesNotToBeDeleted = new List <string>();
                    if (client.AllowedScopes != null)
                    {
                        foreach (var scope in client.AllowedScopes)
                        {
                            var record = connectedClient.ClientScopes.FirstOrDefault(c => c.ScopeName == scope.Name);
                            if (record == null)
                            {
                                record = new ClientScope
                                {
                                    ClientId  = connectedClient.ClientId,
                                    ScopeName = scope.Name
                                };
                                connectedClient.ClientScopes.Add(record);
                            }

                            scopesNotToBeDeleted.Add(record.ScopeName);
                        }
                    }

                    var scopeNames = connectedClient.ClientScopes.Select(o => o.ScopeName).ToList();
                    foreach (var scopeName in scopeNames.Where(id => !scopesNotToBeDeleted.Contains(id)))
                    {
                        connectedClient.ClientScopes.Remove(connectedClient.ClientScopes.First(s => s.ScopeName == scopeName));
                    }

                    await _context.SaveChangesAsync();

                    translation.Commit();
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    translation.Rollback();
                    return(false);
                }
            }

            return(true);
        }