{-# LANGUAGE DefaultSignatures #-}
module Servant.Server.Auth.Token.Config(
AuthConfig(..)
, HasAuthConfig(..)
, defaultAuthConfig
) where
import Control.Monad.Cont (ContT)
import Control.Monad.Except (ExceptT)
import Control.Monad.IO.Class
import Control.Monad.Reader (ReaderT)
import qualified Control.Monad.RWS.Lazy as LRWS
import qualified Control.Monad.RWS.Strict as SRWS
import qualified Control.Monad.State.Lazy as LS
import qualified Control.Monad.State.Strict as SS
import qualified Control.Monad.Writer.Lazy as LW
import qualified Control.Monad.Writer.Strict as SW
import Control.Monad.Trans.Class (MonadTrans(lift))
import Data.Text (Text)
import Data.Time
import Data.UUID
import Data.UUID.V4
import Servant.Server
import Servant.API.Auth.Token
class Monad m => HasAuthConfig m where
getAuthConfig :: m AuthConfig
default getAuthConfig :: (m ~ t n, MonadTrans t, HasAuthConfig n) => m AuthConfig
getAuthConfig = lift getAuthConfig
instance HasAuthConfig m => HasAuthConfig (ContT r m)
instance HasAuthConfig m => HasAuthConfig (ExceptT e m)
instance HasAuthConfig m => HasAuthConfig (ReaderT r m)
instance (HasAuthConfig m, Monoid w) => HasAuthConfig (LRWS.RWST r w s m)
instance (HasAuthConfig m, Monoid w) => HasAuthConfig (SRWS.RWST r w s m)
instance HasAuthConfig m => HasAuthConfig (LS.StateT s m)
instance HasAuthConfig m => HasAuthConfig (SS.StateT s m)
instance (HasAuthConfig m, Monoid w) => HasAuthConfig (LW.WriterT w m)
instance (HasAuthConfig m, Monoid w) => HasAuthConfig (SW.WriterT w m)
data AuthConfig = AuthConfig {
defaultExpire :: !NominalDiffTime
, restoreExpire :: !NominalDiffTime
, restoreCodeSender :: !(RespUserInfo -> RestoreCode -> IO ())
, restoreCodeGenerator :: !(IO RestoreCode)
, maximumExpire :: !(Maybe NominalDiffTime)
, passwordsStrength :: !Int
, passwordValidator :: !(Text -> Maybe Text)
, servantErrorFormer :: !(ServantErr -> ServantErr)
, defaultPageSize :: !Word
, singleUseCodeSender :: !(RespUserInfo -> SingleUseCode -> IO ())
, singleUseCodeExpire :: !NominalDiffTime
, singleUseCodeGenerator :: !(IO SingleUseCode)
, singleUseCodePermanentMaximum :: !Word
, singleUseCodeDefaultCount :: !Word
}
defaultAuthConfig :: AuthConfig
defaultAuthConfig = AuthConfig {
defaultExpire = fromIntegral (600 :: Int)
, restoreExpire = fromIntegral (3*24*3600 :: Int)
, restoreCodeSender = const $ const $ return ()
, restoreCodeGenerator = uuidCodeGenerate
, maximumExpire = Nothing
, passwordsStrength = 17
, passwordValidator = const Nothing
, servantErrorFormer = id
, defaultPageSize = 50
, singleUseCodeSender = const $ const $ return ()
, singleUseCodeExpire = fromIntegral (60 * 60 :: Int)
, singleUseCodeGenerator = uuidSingleUseCodeGenerate
, singleUseCodePermanentMaximum = 100
, singleUseCodeDefaultCount = 20
}
uuidCodeGenerate :: IO RestoreCode
uuidCodeGenerate = toText <$> liftIO nextRandom
uuidSingleUseCodeGenerate :: IO RestoreCode
uuidSingleUseCodeGenerate = toText <$> liftIO nextRandom