Permalink
Browse files

Ship with default config files

  • Loading branch information...
1 parent cf7af26 commit 6fbf493fa8cc6581aa6908b21ac883e1849a42b7 @mightybyte committed Mar 29, 2012
View
@@ -69,9 +69,9 @@ addHandler = do
-- | The application initializer.
app :: SnapletInit App App
app = makeSnaplet "app" "An snaplet example application." Nothing $ do
- s <- nestSnaplet "" sess $ initCookieSessionManager "site_key.txt"
- "_cookie" Nothing
- d <- nestSnaplet "db" db $ pgsInit
+ s <- nestSnaplet "" sess $
+ initCookieSessionManager "site_key.txt" "_cookie" Nothing
+ d <- nestSnaplet "db" db pgsInit
a <- nestSnaplet "auth" auth $ initPostgresAuth sess d
addRoutes routes
return $ App s d a
View
@@ -1,7 +0,0 @@
-postgresql-simple {
- host = "localhost"
- port = 5432
- user = "mightybyte"
- pass = "aoeu"
- db = "testdb"
-}
View
@@ -0,0 +1,21 @@
+# Currently this option is not enforced. See current auth documentation for
+# more information.
+minPasswordLen = 8
+
+# Name of the cookie to use for remembering the logged in user.
+rememberCookie = "_remember"
+
+# Number of seconds of inactivity before the user is logged out. If ommitted,
+# the user will remain logged in until the end of the session.
+rememberPeriod = 1209600 # 2 weeks
+
+# Lockout strategy. The first value is the max number of invalid login
+# attempts before lockout. The second value is how long the locked lasts. If
+# ommitted, then incorrect passwords will never result in lockout.
+# lockout = [5, 86400]
+
+# File where the auth encryption key is stored.
+siteKey = "site_key.txt"
+
+# Name of the table where the user data is stored.
+authTable = "snap_auth_user"
View
@@ -0,0 +1,5 @@
+host = "localhost"
+port = 5432
+user = "postgres"
+pass = ""
+db = "testdb"
@@ -16,6 +16,10 @@ category: Web
extra-source-files: LICENSE
+data-files:
+ resources/db/snaplet.cfg
+ resources/auth/snaplet.cfg
+
source-repository head
type: git
location: https://github.com/mightybyte/snaplet-postgresql-simple.git
@@ -26,6 +30,7 @@ Library
exposed-modules:
Snap.Snaplet.PostgresqlSimple
Snap.Snaplet.Auth.Backends.PostgresqlSimple
+ Paths_snaplet_postgresql_simple
build-depends:
base >= 4 && < 5,
@@ -2,7 +2,9 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
-module Snap.Snaplet.Auth.Backends.PostgresqlSimple where
+module Snap.Snaplet.Auth.Backends.PostgresqlSimple
+ ( initPostgresAuth
+ ) where
------------------------------------------------------------------------------
import Control.Arrow
@@ -25,6 +27,7 @@ import Snap.Snaplet.PostgresqlSimple
import Snap.Snaplet.Session
import Snap.Snaplet.Session.Common
import Web.ClientSession
+import Paths_snaplet_postgresql_simple
data PostgresAuthManager = PostgresAuthManager
@@ -46,19 +49,34 @@ settingsFromConfig = do
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
+ 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
------------------------------------------------------------------------------
--- |
+-- | Initializer for the postgres backend to the auth snaplet. To use this
+-- in your application first add this line to your application state:
+--
+-- > data App = App
+-- > { ... -- your own application state here
+-- > , _sess :: Snaplet SessionManager
+-- > , _db :: Snaplet Postgres
+-- > , _auth :: Snaplet (AuthManager App)
+-- > }
+--
+-- Then in your initializer you'll have something like this:
+--
+-- > d <- nestSnaplet "db" db pgsInit
+-- > a <- nestSnaplet "auth" auth $ initPostgresAuth sess d
+--
initPostgresAuth
:: Lens b (Snaplet SessionManager) -- ^ Lens to the session snaplet
-> Snaplet Postgres -- ^ The postgres snaplet
-> SnapletInit b (AuthManager b)
-initPostgresAuth sess db = makeSnaplet "PostgresAuth" desc Nothing $ do
+initPostgresAuth sess db = makeSnaplet "postgresql-auth" desc datadir $ do
config <- getSnapletUserConfig
authTable <- liftIO $ C.lookupDefault "snap_auth_user" config "authTable"
authSettings <- settingsFromConfig
@@ -81,6 +99,7 @@ initPostgresAuth sess db = makeSnaplet "PostgresAuth" desc Nothing $ do
}
where
desc = "A PostgreSQL backend for user authentication"
+ datadir = Just $ liftM (++"/resources/auth") getDataDir
------------------------------------------------------------------------------
@@ -209,7 +228,7 @@ data AuthTable
defAuthTable :: AuthTable
defAuthTable
= AuthTable
- { tblName = "user"
+ { tblName = "snap_auth_user"
, colId = "uid"
, colLogin = "login"
, colPassword = "password"
@@ -68,6 +68,8 @@ import Database.PostgreSQL.Simple.QueryParams
import Database.PostgreSQL.Simple.QueryResults
import qualified Database.PostgreSQL.Simple as P
import Snap.Snaplet
+import Paths_snaplet_postgresql_simple
+
------------------------------------------------------------------------------
@@ -85,7 +87,6 @@ class (MonadCatchIO m, MonadState Postgres m) => HasPostgres m where
getPostgresState :: m Postgres
---logErr :: Monad m => String -> Maybe a -> m (Either String a)
logErr :: MonadIO m
=> t -> IO (Maybe a) -> WriterT [t] m (Maybe a)
logErr err m = do
@@ -94,9 +95,9 @@ logErr err m = do
return res
------------------------------------------------------------------------------
--- | Initialise the snaplet
+-- | Initialize the snaplet
pgsInit :: SnapletInit b Postgres
-pgsInit = makeSnaplet "postgresql-simple" description Nothing $ do
+pgsInit = makeSnaplet "postgresql-simple" description datadir $ do
config <- getSnapletUserConfig
(mci,errs) <- runWriterT $ do
host <- logErr "Must specify postgres host" $ C.lookup config "host"
@@ -111,6 +112,7 @@ pgsInit = makeSnaplet "postgresql-simple" description Nothing $ do
return $ Postgres pool Nothing
where
description = "PostgreSQL abstraction"
+ datadir = Just $ liftM (++"/resources/db") getDataDir
------------------------------------------------------------------------------

0 comments on commit 6fbf493

Please sign in to comment.