public class PinAuthorization : IPinAuthorization, IModifiablePin, IDisposable
{
private readonly SecureString _pin;
public PinAuthorization(SecureString pin)
{
ValidateValidateInternal(pin); // see comments below
_pin = pin;
}
private void ValidateValidateInternal(SecureString pin)
{
if (pin.Length != 4)
{
throw new ArgumentException("PIN must be 4 digits.", pin);
}
// further validation logic would break the security of the string.
// this is an indication that it's not this type's responsibility
// to know how to validate itself.
}
public bool Validate(SecureString pin)
{
return pin == _pin;
}
public IPinAuthorization Change(SecureString current, SecureString @new)
{
if (_pin != current)
{
throw new ArgumentException("Invalid PIN.", current);
}
return new PinAuthorization(@new);
}
public void Dispose()
{
_pin.Dispose();
}
}
Now, if you want to implement some FingerprintAuthorization, you can make it implement IPinAuthorization and validate against a SecureString-encoded finger print (however you want to do that), and the type will not be implementing IModifiablePin, since you don't change your finger prints. Or do you? Well, at least you have the flexibility of doing either. At a glance though, it seems the class has so little responsibility, that it possibly wouldn't even need to be reimplemented or adapted in any way.