module Yesod.Session.Manager
  ( SessionManager (..)
  , sessionKeyAppearsReasonable
  , checkedSessionKeyFromCookieValue
  , newSessionKey
  ) where

import Internal.Prelude

import Session.Key
import Yesod.Session.Options
import Yesod.Session.Storage.Operation

-- | Server-wide state for the session mechanism
data SessionManager tx m = SessionManager
  { forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> SessionKeyManager tx
keyManager :: SessionKeyManager tx
  -- ^ A random session key generator
  , forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> forall a. StorageOperation a -> tx a
storage :: forall a. StorageOperation a -> tx a
  -- ^ The storage backend
  , forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> Options tx m
options :: Options tx m
  , forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> forall a. tx a -> m a
runTransaction :: forall a. tx a -> m a
  }

sessionKeyAppearsReasonable :: SessionManager tx m -> SessionKey -> Bool
sessionKeyAppearsReasonable :: forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> SessionKey -> Bool
sessionKeyAppearsReasonable SessionManager {$sel:keyManager:SessionManager :: forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> SessionKeyManager tx
keyManager = SessionKeyManager {SessionKey -> Bool
check :: SessionKey -> Bool
$sel:check:SessionKeyManager :: forall (m :: * -> *). SessionKeyManager m -> SessionKey -> Bool
check}} = SessionKey -> Bool
check

checkedSessionKeyFromCookieValue
  :: SessionManager tx m -> ByteString -> Maybe SessionKey
checkedSessionKeyFromCookieValue :: forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> ByteString -> Maybe SessionKey
checkedSessionKeyFromCookieValue SessionManager tx m
x =
  ByteString -> Maybe SessionKey
sessionKeyFromCookieValue
    (ByteString -> Maybe SessionKey)
-> (SessionKey -> Maybe SessionKey)
-> ByteString
-> Maybe SessionKey
forall (m :: * -> *) a b c.
Monad m =>
(a -> m b) -> (b -> m c) -> a -> m c
>=> (\SessionKey
v -> Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (SessionManager tx m -> SessionKey -> Bool
forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> SessionKey -> Bool
sessionKeyAppearsReasonable SessionManager tx m
x SessionKey
v) Maybe () -> SessionKey -> Maybe SessionKey
forall (f :: * -> *) a b. Functor f => f a -> b -> f b
$> SessionKey
v)

newSessionKey :: SessionManager tx m -> m SessionKey
newSessionKey :: forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> m SessionKey
newSessionKey SessionManager {SessionKeyManager tx
$sel:keyManager:SessionManager :: forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> SessionKeyManager tx
keyManager :: SessionKeyManager tx
keyManager, forall a. tx a -> m a
$sel:runTransaction:SessionManager :: forall (tx :: * -> *) (m :: * -> *).
SessionManager tx m -> forall a. tx a -> m a
runTransaction :: forall a. tx a -> m a
runTransaction} =
  tx SessionKey -> m SessionKey
forall a. tx a -> m a
runTransaction SessionKeyManager tx
keyManager.new