Authentication Proxy Pattern

Yet another variant of the proxy pattern, in this post I will cover the authentication proxy pattern which is used to check the access permissions for a request.

In this example I will show you how to implement this pattern in a banking-like application, it will have an account with three methods, get balance, deposit and withdraw, and the proxy will manage the authentication.

Step 1: The creation of our account interface (IAccount.cs)

using System;

namespace Structural.Proxy.Authentication.Base
{
    public interface IAccount
    {
        double GetBalance();
        double Withdraw(double amount);
        double Deposit(double amount);
    }
}

Step 2: Then we will create our account object (Account.cs) and it will acccess the data source.

using System;

using Structural.Proxy.Authentication.Base;

namespace Structural.Proxy.Authentication.Data
{
    public class Account : IAccount
    {
        int accountNumber;
        double accountBalance;

        public Account(int accountNumber)
        {
            this.accountNumber = accountNumber;

            //Pretend the account balance is coming from a data source
            accountBalance = 577.20;
        }

        public double GetBalance()
        {
            return accountBalance;
        }

        public double Withdraw(double amount)
        {
            if (accountBalance >= amount)
            {
                accountBalance -= amount;
                return accountBalance;
            }
            Console.WriteLine("Invalid amount for withdrawal.");
            return -1;
        }

        public double Deposit(double amount)
        {
            accountBalance += amount;
            return accountBalance;
        }
    }
}

Step 3: In this step we will create the account proxy object (AccountProxy.cs) this will stand between the client and the account itself.

using System;

using Structural.Proxy.Authentication.Base;
using Structural.Proxy.Authentication.Data;

namespace Structural.Proxy.Authentication.Proxy
{
    public class AccountProxy : IAccount
    {
        int accountNumber;
        string password;

        Account account;

        public AccountProxy(int accountNumber)
        {
            this.accountNumber = accountNumber;

            //Pretend the password is coming from a data source
            password = "secret";
        }

        public bool Authenticate(string password)
        {
            if (this.password == password)
            {
                account = new Account(accountNumber);
                Console.WriteLine("Authentication succeeded.");
                return true;
            }

            Console.WriteLine("Authentication failed.");
            return false;
        }

        public double GetBalance()
        {
            if (account != null)
                return account.GetBalance();
            else
            {
                Console.WriteLine("Authentication required.");
                return -2;
            }
        }

        public double Withdraw(double amount)
        {
            if (account != null)
                return account.Withdraw(amount);
            else
            {
                Console.WriteLine("Authentication required.");
                return -2;
            }
        }

        public double Deposit(double amount)
        {
            if (account != null)
                return account.Deposit(amount);
            else
            {
                Console.WriteLine("Authentication required.");
                return -2;
            }
        }
    }
}

Step 4: Now we code our main method like this.

using System;

using Structural.Proxy.Authentication.Proxy;

namespace Structural.Proxy.Authentication
{
    class Program
    {
        static void Main(string[] args)
        {
            AccountProxy accountProxy = new AccountProxy(123);

            Console.WriteLine(accountProxy.GetBalance());
            Console.WriteLine(accountProxy.Deposit(1));
            Console.WriteLine(accountProxy.Withdraw(1));

            accountProxy.Authenticate("444");

            accountProxy.Authenticate("secret");

            Console.WriteLine(accountProxy.Withdraw(15000));
            Console.WriteLine(accountProxy.GetBalance());
            Console.WriteLine(accountProxy.Deposit(500));
            Console.WriteLine(accountProxy.Withdraw(1000));

            Console.ReadKey();
        }
    }
}

Step 5: This is the output we receive.

Authentication required.
-2
Authentication required.
-2
Authentication required.
-2
Authentication failed.
Authentication succeeded.
Invalid amount for withdrawal.
-1
577.2
1077.2
77.2

Okay, that’s all for this proxy… I hope you find it useful.

Regards,
Esteban.

Advertisement

One Response to Authentication Proxy Pattern

  1. Pingback: Proxy Pattern « A blog by Esteban Murchio

Follow

Get every new post delivered to your Inbox.