password policy validation

This commit is contained in:
Jonathan Lamothe
2018-12-12 18:41:34 -05:00
parent 63105399b8
commit 3a92469554
3 changed files with 80 additions and 3 deletions

View File

@@ -31,10 +31,13 @@ module Password (
-- $lenses
pwLength, pwUpper, pwLower, pwDigits, pwSpecial,
-- ** Default Instances
newPWPolicy
newPWPolicy,
-- * Functions
validatePWPolicy
) where
import Control.Lens (makeLenses)
import Control.Lens (makeLenses, (^.))
import Data.Maybe (fromMaybe)
-- | defines a password policy
data PWPolicy = PWPolicy
@@ -62,4 +65,17 @@ makeLenses ''PWPolicy
newPWPolicy :: PWPolicy
newPWPolicy = PWPolicy 16 0 0 0 (Just 0)
-- | validates a password policy
validatePWPolicy
:: PWPolicy
-- ^ the policy being validated
-> Maybe PWPolicy
-- ^ the policy if it is valid, or @"Nothing"@ if it is not
validatePWPolicy x = if needed <= x^.pwLength
then Just x
else Nothing
where
needed = x^.pwUpper + x^.pwLower + x^.pwDigits + special
special = fromMaybe 0 $ x^.pwSpecial
--jl