Exemple #1
0
        public bool TrySetKey(string key, out string error)
        {
            error = null;

            if (HasKey)
            {
                error = "Key is already installed.";
                return(false);
            }

            ICryptographicSerializer serializer;

            try
            {
                serializer = new AesSerializer(key);
            }
            catch (Exception ex)
            {
                error = $"Wrong key format. {ex.Message}";
                return(false);
            }
            var encryptedStorage = EncryptedTableStorageDecorator <EncryptionInitModel> .Create(_storage, serializer);

            if (WasEncryptionSet())
            {
                try
                {
                    var existingValue = encryptedStorage.GetDataAsync(InitKey, InitKey).GetAwaiter().GetResult();
                    if (existingValue.Data == InitKey)
                    {
                        Serializer = serializer;
                        return(true);
                    }
                    else
                    {
                        error = "The specified key is incorrect.";
                        return(false);
                    }
                }
                catch (System.Security.Cryptography.CryptographicException)
                {
                    error = "The specified key is incorrect.";
                    return(false);
                }
            }
            else
            {
                // this is a new and the only one key
                encryptedStorage.InsertAsync(new EncryptionInitModel {
                    PartitionKey = InitKey, RowKey = InitKey, Data = InitKey
                }).GetAwaiter().GetResult();
                Serializer = serializer;
                return(true);
            }
        }
 public EncryptedAzureTableStorageDecoratorTest()
 {
     _innerStorage = new NoSqlTableInMemory <TestEntity>();
     Storage       = new EncryptedTableStorageDecorator <TestEntity>(_innerStorage, _cryptoSerializer);
 }
Exemple #3
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.Register <ISmsCommandProducer>(y =>
                                                   new SmsCommandProducer(AzureQueueExt.Create(
                                                                              _smsNotificationsSettings.ConnectionString(x => x.AzureQueue.ConnectionString),
                                                                              _smsNotificationsSettings.CurrentValue.AzureQueue.QueueName)));

            builder.RegisterType <VerificationCodesFactory>().AsImplementedInterfaces().SingleInstance();
            builder.RegisterType <RandomValueGenerator>().AsImplementedInterfaces().SingleInstance();
            builder.RegisterType <DateTimeProvider>().AsImplementedInterfaces().SingleInstance();

            builder.Register(x =>
                             AzureTableStorage <SmsVerificationCodeEntity> .Create(_personalDataConnString,
                                                                                   TableNameSmsVerificationCodes, x.Resolve <ILogFactory>()
                                                                                   )).AsImplementedInterfaces().SingleInstance();
            builder.Register(x =>
                             AzureTableStorage <SmsVerificationPriorityCodeEntity> .Create(_personalDataConnString,
                                                                                           TableNameSmsVerificationPriorityCodes, x.Resolve <ILogFactory>())).AsImplementedInterfaces().SingleInstance();
            builder.RegisterType <SmsVerificationCodeRepository>().AsImplementedInterfaces().SingleInstance();

            builder.Register(x =>
                             AzureTableStorage <EmailVerificationCodeEntity> .Create(_personalDataConnString,
                                                                                     TableNameEmailVerificationCodes, x.Resolve <ILogFactory>())).AsImplementedInterfaces().SingleInstance();
            builder.Register(x =>
                             AzureTableStorage <EmailVerificationPriorityCodeEntity> .Create(_personalDataConnString,
                                                                                             TableNameEmailVerificationPriorityCodes, x.Resolve <ILogFactory>())).AsImplementedInterfaces().SingleInstance();
            builder.RegisterType <EmailVerificationCodeRepository>().AsImplementedInterfaces().SingleInstance();

            builder.Register <ICallTimeLimitsRepository>(y =>
                                                         new CallTimeLimitsRepository(
                                                             AzureTableStorage <ApiCallHistoryRecord> .Create(_logsConnString, TableNameApiCalls, y.Resolve <ILogFactory>())));

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("EncryptionKey")))
            {
                builder
                .Register(x =>
                {
                    var manager = new EncryptedStorageManager(AzureTableStorage <EncryptionInitModel> .Create(
                                                                  _google2faConnString,
                                                                  TableNameGoogle2Fa,
                                                                  x.Resolve <ILogFactory>()));

                    if (!manager.TrySetKey(Environment.GetEnvironmentVariable("EncryptionKey"), out string error))
                    {
                        var exception = new InvalidOperationException("EncryptionKey is not set");
                        x.Resolve <ILogFactory>().CreateLog(this).WriteFatalError("SetEncryptionKey", error, exception);
                        throw exception;
                    }

                    return(manager);
                })
                .As <EncryptedStorageManager>()
                .AutoActivate();

                builder
                .Register(
                    x => EncryptedTableStorageDecorator <Google2FaSecretEntity> .Create(
                        AzureTableStorage <Google2FaSecretEntity> .Create(
                            _google2faConnString,
                            TableNameGoogle2Fa,
                            x.Resolve <ILogFactory>()),
                        x.Resolve <EncryptedStorageManager>().Serializer))
                .As <INoSQLTableStorage <Google2FaSecretEntity> >()
                .SingleInstance();
            }
            else
            {
                builder
                .Register(
                    x => {
                    if (x.Resolve <IHostingEnvironment>().IsProduction())
                    {
                        throw new Exception("Need to set EncryptionKey in Production environment");
                    }

                    return(AzureTableStorage <Google2FaSecretEntity> .Create(
                               _google2faConnString,
                               TableNameGoogle2Fa,
                               x.Resolve <ILogFactory>()));
                })
                .As <INoSQLTableStorage <Google2FaSecretEntity> >()
                .AutoActivate()
                .SingleInstance();
            }

            builder
            .RegisterType <Google2FaRepository>()
            .As <IGoogle2FaRepository>()
            .SingleInstance();
        }