Permalink
Browse files

Added a function to the auth snaplet allowing settings to be loaded f…

…rom a config file.
  • Loading branch information...
mightybyte committed Apr 13, 2012
1 parent 712adf5 commit 277a85769ad1b00205d2d02243619254817331b2
Showing with 30 additions and 0 deletions.
  1. +1 −0 src/Snap/Snaplet/Auth.hs
  2. +29 −0 src/Snap/Snaplet/Auth/Types.hs
View
@@ -47,6 +47,7 @@ module Snap.Snaplet.Auth
, Role(..)
-- * Other Utilities
+ , authSettingsFromConfig
, withBackend
, encryptPassword
, checkPassword
@@ -197,6 +197,35 @@ defAuthSettings = AuthSettings {
}
+------------------------------------------------------------------------------
+-- | Function to get auth settings from a config file. This function can be
+-- used by the authors of auth snaplet backends in the initializer to let the
+-- user configure the auth snaplet from a config file. All options are
+-- optional and default to what's in defAuthSettings if not supplied.
+-- Here's what the default options would look like in the config file:
+--
+-- > minPasswordLen = 8
+-- > rememberCookie = "_remember"
+-- > rememberPeriod = 1209600 # 2 weeks
+-- > lockout = [5, 86400] # 5 attempts locks you out for 86400 seconds
+-- > siteKey = "site_key.txt"
+authSettingsFromConfig :: Initializer b (AuthManager b) AuthSettings
+authSettingsFromConfig = do
+ config <- getSnapletUserConfig
+ minPasswordLen <- liftIO $ C.lookup config "minPasswordLen"
+ let pw = maybe id (\x s -> s { asMinPasswdLen = x }) minPasswordLen
+ rememberCookie <- liftIO $ C.lookup config "rememberCookie"
+ let rc = maybe id (\x s -> s { asRememberCookieName = x }) rememberCookie
+ rememberPeriod <- liftIO $ C.lookup config "rememberPeriod"
+ let rp = maybe id (\x s -> s { asRememberPeriod = Just x }) rememberPeriod
+ lockout <- liftIO $ C.lookup config "lockout"
+ let lo = maybe id (\x s -> s { asLockout = Just (second fromInteger x) })
+ lockout
+ siteKey <- liftIO $ C.lookup config "siteKey"
+ let sk = maybe id (\x s -> s { asSiteKey = x }) siteKey
+ return $ (pw . rc . rp . lo . sk) defAuthSettings
+
+
------------------------------------------------------------------------------
data BackendError = DuplicateLogin
| BackendError String

0 comments on commit 277a857

Please sign in to comment.