コード例 #1
0
        /// <summary>
        ///     Replicate client account cache data.
        /// </summary>
        /// <param name="replicationData"> Replication data. </param>
        /// <param name="principal"> The bank service user replicating data. </param>
        private void ReplicateCacheData(IReplicationItem replicationData, IPrincipal principal)
        {
            Console.WriteLine($"Replicating cache data from {principal.Identity.Name}");
            var client = BankCache.GetClientFromCache(_bankCache, replicationData.Client.Name);

            client.Pin     = replicationData.Client.Pin;
            client.Account = new Account(replicationData.Client.Account.Balance);
            _bankCache.StoreData();
        }
コード例 #2
0
        public decimal CheckBalance(byte[] signature, ITransaction transaction)
        {
            var clientName = GetClientNameFromAuthContext(ServiceSecurityContext.Current.AuthorizationContext);

            var hash = HashData(transaction);

            VerifySignature(signature, hash, clientName);

            var client = BankCache.GetClientFromCache(_bankCache, clientName);

            Task.Run(() =>
            {
                ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                 _applicationName,
                                                                 clientName,
                                                                 "Requested check balance.",
                                                                 EventLogEntryType.Information));
            });

            if (client != null)
            {
                if (!client.CheckPin(transaction.Pin))
                {
                    Task.Run(() =>
                    {
                        ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                         _applicationName,
                                                                         clientName,
                                                                         "Invalid Pin.",
                                                                         EventLogEntryType.Error));
                    });

                    throw new SecurityException("Invalid Pin.");
                }
            }
            else
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     "Client not found in cache. Possibly using old certificate.",
                                                                     EventLogEntryType.Error));
                });

                throw new FaultException("Client not found in cache. Possibly using old certificate.");
            }

            return(client.Account.Balance);
        }
コード例 #3
0
        private static void Main(string[] args)
        {
            var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());

            if (!principal.IsInRole("BankServices"))
            {
                Console.WriteLine($"Only user in BankServices role can run {nameof(BankServiceApp)}");
                Console.WriteLine("Press any key to exit...");
                Console.ReadLine();
                return;
            }

            using (ICache bankCache = new BankCache())
            {
                ServiceLocator.RegisterService(bankCache);
                using (IArbitrationServiceProvider arbitrationService = new ArbitrationServiceProvider())
                {
                    ServiceLocator.RegisterService(arbitrationService);
                    using (var replicatorProxy = new ReplicatorProxy())
                    {
                        ProxyPool.RegisterProxy <IReplicator>(replicatorProxy);

                        using (var auditProxy = new BankAuditServiceProxy())
                        {
                            ProxyPool.RegisterProxy <IBankAuditService>(auditProxy);

                            if (CertificateManager.Instance.GetCACertificate() == null)
                            {
                                try
                                {
                                    var caCertificate = CertificateManager.Instance.GetPrivateCertificateFromFile(
                                        BankAppConfig.CACertificatePath,
                                        BankAppConfig.CACertificatePass);
                                    CertificateManager.Instance.SetCACertificate(caCertificate);
                                }
                                catch
                                {
                                    Console.ReadLine();
                                    throw;
                                }
                            }

                            arbitrationService.RegisterService(new BankServicesHost());
                            arbitrationService.OpenServices();
                            Console.ReadLine();
                        }
                    }
                }
            }
        }
コード例 #4
0
 public void TearDown()
 {
     _cache = null;
 }
コード例 #5
0
 public void Setup()
 {
     _cache = new BankCache();
 }
コード例 #6
0
ファイル: BankMasterCardService.cs プロジェクト: hodoje/sses
        public NewCardResults RequestNewCard(string password)
        {
            if (!Thread.CurrentPrincipal.IsInRole("Clients"))
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     ExtractUsernameFromFullName(Thread.CurrentPrincipal.Identity.Name),
                                                                     "Request for new card failed client isn't in client's role.",
                                                                     EventLogEntryType.FailureAudit));
                });
                throw new SecurityException("Principal isn't part of Clients role.");
            }

            try
            {
                var clientName = ExtractUsernameFromFullName(Thread.CurrentPrincipal.Identity.Name);

                Console.WriteLine($"Client {clientName} requested new card.");

                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     "Request for new card!",
                                                                     EventLogEntryType.Information));
                });

                if (CertificateManager.Instance.GetCertificateFromStore(
                        StoreLocation.LocalMachine,
                        StoreName.My,
                        clientName) != null)
                {
                    throw new InvalidOperationException("You already have issued card.");
                }

                RevokeCertificate(clientName);

                var CACertificate = CertificateManager.Instance.GetCACertificate();
                CertificateManager.Instance.CreateAndStoreNewCertificate(
                    clientName,
                    password,
                    CACertificate,
                    BankAppConfig.BankTransactionServiceCertificatePath);

                var cert = CertificateManager.Instance.GetCertificateFromStore(
                    StoreLocation.LocalMachine,
                    StoreName.My,
                    clientName);

                if (cert == null)
                {
                    throw new ArgumentNullException(nameof(cert));
                }

                var resultData = new NewCardResults
                {
                    PinCode = GenerateRandomPin()
                };

                var client = BankCache.GetClientFromCache(_bankCache, clientName);
                client.ResetPin(null, resultData.PinCode);

                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     "Successfully created a card!",
                                                                     EventLogEntryType.Information));
                });

                _bankCache.StoreData();

                var replicationData = new ReplicationItem(
                    client,
                    ReplicationType.UserData | ReplicationType.CertificateData,
                    cert);
                _replicatorProxy.ReplicateData(replicationData);


                return(resultData);
            }
            catch (ArgumentNullException ane)
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     Thread.CurrentPrincipal.Identity.Name,
                                                                     ane.Message,
                                                                     EventLogEntryType.Error));
                });

                throw new FaultException <CustomServiceException>(new CustomServiceException(ane.Message + "was null!"));
            }
        }
コード例 #7
0
ファイル: BankMasterCardService.cs プロジェクト: hodoje/sses
        public NewCardResults RequestResetPin()
        {
            if (!Thread.CurrentPrincipal.IsInRole("Clients"))
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     ExtractUsernameFromFullName(Thread.CurrentPrincipal.Identity.Name),
                                                                     "Request for new card failed client isn't in client's role.",
                                                                     EventLogEntryType.FailureAudit));
                });
                throw new SecurityException("Client isn't in required role.");
            }

            var clientName = ExtractUsernameFromFullName(Thread.CurrentPrincipal.Identity.Name);

            try
            {
                var client = BankCache.GetClientFromCache(_bankCache, clientName);

                Console.WriteLine("Client requested pin reset.");

                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     "Requested pin reset.",
                                                                     EventLogEntryType.Information));
                });

                var results = new NewCardResults {
                    PinCode = GenerateRandomPin()
                };

                client.ResetPin(null, results.PinCode);
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     "New pin generated.",
                                                                     EventLogEntryType.Information));
                });

                _replicatorProxy.ReplicateData(new ReplicationItem(client));

                return(results);
            }
            catch (ArgumentNullException ane)
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     ane.Message,
                                                                     EventLogEntryType.Error));
                });

                throw new ArgumentNullException(ane.Message);
            }
        }
コード例 #8
0
ファイル: BankMasterCardService.cs プロジェクト: hodoje/sses
        public bool RevokeExistingCard(string pin)
        {
            if (!Thread.CurrentPrincipal.IsInRole("Clients"))
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     ExtractUsernameFromFullName(Thread.CurrentPrincipal.Identity.Name),
                                                                     "Request for new card failed client isn't in client's role.",
                                                                     EventLogEntryType.FailureAudit));
                });
                throw new SecurityException("Client isn't in required role.");
            }

            try
            {
                // Check if client exists
                var clientName = ExtractUsernameFromFullName(Thread.CurrentPrincipal.Identity.Name);
                var client     = default(IClient);
                client = BankCache.GetClientFromCache(_bankCache, clientName);

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

                // if he exists in the system, authorize him
                if (client.CheckPin(pin))
                {
                    Console.WriteLine($"Client {clientName} requested card revocation.");
                    Task.Run(() =>
                    {
                        ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                         _applicationName,
                                                                         clientName,
                                                                         "Requested card revocation.",
                                                                         EventLogEntryType.Information));
                    });

                    var cert = CertificateManager.Instance.GetCertificateFromStore(
                        StoreLocation.LocalMachine,
                        StoreName.My,
                        clientName);

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

                    var revoked = RevokeCertificate(clientName);

                    if (revoked)
                    {
                        Task.Run(() =>
                        {
                            ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                             _applicationName,
                                                                             clientName,
                                                                             "Successfully revoked the card.",
                                                                             EventLogEntryType.Information));
                        });

                        client.Pin = null;
                        _bankCache.StoreData();

                        // We also replicate client data since pin wa removed
                        var replicationData = new ReplicationItem(
                            client,
                            ReplicationType.UserData | ReplicationType.CertificateData |
                            ReplicationType.RevokeCertificate,
                            cert);
                        _replicatorProxy.ReplicateData(replicationData);
                    }

                    return(revoked);
                }

                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     "Invalid pin.",
                                                                     EventLogEntryType.FailureAudit));
                });

                throw new SecurityException("Invalid pin.");
            }
            catch (ArgumentNullException ane)
            {
                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     Thread.CurrentPrincipal.Identity.Name,
                                                                     ane.Message,
                                                                     EventLogEntryType.Error));
                });

                throw;
            }
        }
コード例 #9
0
        public bool ExecuteTransaction(byte[] signature, ITransaction transaction)
        {
            var clientName = GetClientNameFromAuthContext(ServiceSecurityContext.Current.AuthorizationContext);

            var hash = HashData(transaction);

            VerifySignature(signature, hash, clientName);

            var client = BankCache.GetClientFromCache(_bankCache, clientName);

            if (client != null)
            {
                if (!client.CheckPin(transaction.Pin))
                {
                    Task.Run(() =>
                    {
                        ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                         _applicationName,
                                                                         clientName,
                                                                         "Invalid Pin.",
                                                                         EventLogEntryType.Error));
                    });

                    throw new SecurityException("Invalid Pin.");
                }
            }

            var success = false;

            switch (transaction.TransactionType)
            {
            case TransactionType.Deposit:
                client.Account.Deposit(transaction.Amount);
                success = true;

                Task.Run(() =>
                {
                    ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                     _applicationName,
                                                                     clientName,
                                                                     $"Deposit made with {transaction.Amount} amount.",
                                                                     EventLogEntryType.Information));
                });

                break;

            case TransactionType.Withdrawal:
                if (client.Account.Balance >= transaction.Amount)
                {
                    client.Account.Withdraw(transaction.Amount);
                    if (++client.Withdraw >= _withdrawLimitForAudit)
                    {
                        Task.Run(() =>
                        {
                            ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                             _applicationName,
                                                                             clientName,
                                                                             $"Client exceeded withdraw transaction amount by {client.Withdraw - _withdrawLimitForAudit}.",
                                                                             EventLogEntryType.Information));
                        });
                    }
                    success = true;

                    Task.Run(() =>
                    {
                        ProxyPool.GetProxy <IBankAuditService>().Log(new EventLogData(
                                                                         _applicationName,
                                                                         clientName,
                                                                         $"Withdrawal made with {transaction.Amount} amount.",
                                                                         EventLogEntryType.Information));
                    });
                }

                break;

            default:
                throw new InvalidOperationException("Invalid operation. For check balance use dedicated method.");
            }

            if (success)
            {
                _bankCache.StoreData();
                _replicatorProxy.ReplicateData(new ReplicationItem(client));
            }

            return(success);
        }