/// <summary> /// Encrypts the unencrypted values on the viewmodel and sets them on the entity /// </summary> /// <param name="database"></param> /// <param name="viewModel"></param> private void AddEncryptedValues(Database database, DatabaseViewModel viewModel) { database.Host = _encrypter.Encrypt(viewModel.Host, database.EncryptionKey); database.Port = _encrypter.Encrypt(viewModel.Port, database.EncryptionKey); database.User = _encrypter.Encrypt(viewModel.User, database.EncryptionKey); database.Password = _encrypter.Encrypt(viewModel.Password, database.EncryptionKey); }
public ApplicationEnvironmentToken Save(ApplicationEnvironmentTokenViewModel toCreate) { if (!ApplicationExists(toCreate.ApplicationId)) { throw new EntityValidationException("Application does not exist!"); } if (!EnvironmentExists(toCreate.EnvironmentId)) { throw new EntityValidationException("Environment does not exist!"); } if (!ApplicationEnvironmentExists(toCreate.ApplicationId, toCreate.EnvironmentId)) { throw new EntityValidationException("The application isn't configured for this environment."); } var applicationToken = _db.ApplicationTokens.Active() .FirstOrDefault(x => x.ApplicationId == toCreate.ApplicationId && x.Name == toCreate.Name); if (null == applicationToken) { throw new EntityValidationException("The application does not have a definition for this token"); } var environmentToken = _db.ApplicationEnvironmentTokens.Active() .FirstOrDefault(ae => ae.ApplicationTokenId == applicationToken.ApplicationTokenId && ae.ApplicationEnvironment.EnvironmentId == toCreate.EnvironmentId); var encryptedValue = _encrypter.Encrypt(toCreate.Value, GetEncryptionKey(toCreate.ApplicationId)); if (null == environmentToken) { var applicationEnvironment = _db.ApplicationEnvironments.Active() .First( ae => ae.ApplicationId == toCreate.ApplicationId && ae.EnvironmentId == toCreate.EnvironmentId); environmentToken = new ApplicationEnvironmentToken() { ApplicationEnvironment = applicationEnvironment, ApplicationTokenId = applicationToken.ApplicationTokenId, Value = encryptedValue, Active = true }; _db.ApplicationEnvironmentTokens.Add(environmentToken); } else { environmentToken.Value = encryptedValue; } _db.SaveChanges(); return(Get(environmentToken.ApplicationEnvironmentTokenId, true)); }
public void EncryptApplications() { var applications = _db.Applications.Where(a => a.EncryptionKey == null); foreach (var application in applications) { application.EncryptionKey = _encrypter.GenerateKey(); var tokens = _db.ApplicationEnvironmentTokens.Where( aet => aet.ApplicationToken.ApplicationId == application.ApplicationId); foreach (var token in tokens) { if (!_encrypter.IsEncrypted(token.Value)) { token.Value = _encrypter.Encrypt(token.Value, application.EncryptionKey); } } } _db.SaveChanges(); }