Snap.Snaplet.Session

snap-0.9.2.2: Top-level package for the Snap Web Framework

Safe Haskell | None

Snap.Snaplet.Session

Synopsis

- data SessionManager = forall a . ISessionManager a => SessionManager a
- class ISessionManager r where
  - load :: r -> Snap r
  - commit :: r -> Snap ()
  - reset :: r -> Snap r
  - touch :: r -> r
  - insert :: Text -> Text -> r -> r
  - lookup :: Text -> r -> Maybe Text
  - delete :: Text -> r -> r
  - csrf :: r -> Text
  - toList :: r -> [(Text, Text)]
- withSession :: Lens b (Snaplet SessionManager) -> Handler b v a -> Handler b v a
- commitSession :: Handler b SessionManager ()
- setInSession :: Text -> Text -> Handler b SessionManager ()
- getFromSession :: Text -> Handler b SessionManager (Maybe Text)
- deleteFromSession :: Text -> Handler b SessionManager ()
- csrfToken :: Handler b SessionManager Text
- sessionToList :: Handler b SessionManager [(Text, Text)]
- resetSession :: Handler b SessionManager ()
- touchSession :: Handler b SessionManager ()
- module Snap.Snaplet.Session.Common
- type SecureCookie t = (UTCTime, t)
- getSecureCookie :: (MonadSnap m, Serialize t) => ByteString -> Key -> Maybe Int -> m (Maybe t)
- setSecureCookie :: (MonadSnap m, Serialize t) => ByteString -> Key -> Maybe Int -> t -> m ()
- checkTimeout :: MonadSnap m => Maybe Int -> UTCTime -> m Bool

# Documentation

data SessionManager

Any Haskell record that is a member of the `ISessionManager` typeclass can be stuffed inside a `SessionManager` to enable all session-related functionality.

Constructors

forall a . ISessionManager a => SessionManager a |

class ISessionManager r where

Methods

load :: r -> Snap r

Load a session from given payload.

Will always be called before any other operation. If possible, cache and do nothing when called multiple times within the same request cycle.

commit :: r -> Snap ()

Commit session, return a possibly updated paylaod

reset :: r -> Snap r

Reset session

touch :: r -> r

Touch session

insert :: Text -> Text -> r -> r

Insert a key-value pair into session

lookup :: Text -> r -> Maybe Text

Lookup a key in session

delete :: Text -> r -> r

Delete a key in session

csrf :: r -> Text

Return a session-specific CSRF protection token. See `mkCSRFToken` for help in creating the value.

toList :: r -> [(Text, Text)]

Return all key-value pairs as an association list

Instances

ISessionManager CookieSessionManager |

withSession :: Lens b (Snaplet SessionManager) -> Handler b v a -> Handler b v a

Wrap around a handler, committing any changes in the session at the end

commitSession :: Handler b SessionManager ()

Commit changes to session within the current request cycle

setInSession :: Text -> Text -> Handler b SessionManager ()

Set a key-value pair in the current session

getFromSession :: Text -> Handler b SessionManager (Maybe Text)

Get a key from the current session

deleteFromSession :: Text -> Handler b SessionManager ()

Remove a key from the current session

csrfToken :: Handler b SessionManager Text

Returns a CSRF Token unique to the current session

sessionToList :: Handler b SessionManager [(Text, Text)]

Return session contents as an association list

resetSession :: Handler b SessionManager ()

Deletes the session cookie, effectively resetting the session

touchSession :: Handler b SessionManager ()

Touch the session so the timeout gets refreshed

module Snap.Snaplet.Session.Common

type SecureCookie t = (UTCTime, t)

Arbitrary payload with timestamp.

getSecureCookie

Arguments

:: (MonadSnap m, Serialize t) |
=> ByteString | Cookie name
-> Key | Encryption key
-> Maybe Int | Timeout in seconds
-> m (Maybe t) |

setSecureCookie

Arguments

:: (MonadSnap m, Serialize t) |
=> ByteString | Cookie name
-> Key | Encryption key
-> Maybe Int | Max age in seconds
-> t | Serializable payload
-> m () |

Inject the payload

checkTimeout :: MonadSnap m => Maybe Int -> UTCTime -> m Bool

Validate session against timeout policy.

- If timeout is set to `Nothing`, never trigger a time-out.
- Otherwise, do a regular time-out check based on current time and given timestamp.