-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Cookie-based session management for the Simple web framework -- -- Adds cookie-based session management to simple Controllers. To -- add to an application, declare the Controller setting's type an -- instance of HasSession, and wrap routes with -- withSession. For example: -- --
--   data AppSettings = ...
--   
--   instance HasSession AppSettings where
--     ...
--   
-- --
--   controllerApp settings $ withSessions $ do
--     routeName \"posts\" $ ...
--   
-- -- Then, in your controllers you can seemlessly get and set keys from the -- session: -- --
--   get "/profile" $ do
--     muserId <- sessionLookup "current_user_id"
--     case muserIf of
--       Nothing -> respond $ redirectTo "/login"
--       Just userId -> [handle request]
--   
@package simple-session @version 0.8.1.0 -- | Adds cookie-based session management to simple Controllers. To -- add to an application, declare the Controller setting's type an -- instance of HasSession, and wrap routes with -- withSession. For example: -- --
--   data AppSettings = ...
--   
--   instance HasSession AppSettings where
--     ...
--   
-- --
--   controllerApp settings $ withSessions $ do
--     routeName \"posts\" $ ...
--   
module Web.Simple.Session -- | Plaintext mapping of the session map. Both keys and values are -- ByteStrings. type Session = Map ByteString ByteString -- | Instances of this class can be used as states by a Controller -- states to manage cookie-based user sessions. Instances must minimally -- implement getSession and setSession. By default, the -- secret session key is taken from the environment variable -- "SESSION_KEY", or a default dummy key is used if the environment -- variable "ENV" is set to "development". You can override this -- behaviour by implementing the sessionKey method. If the -- controller state contains a dedicated field of type 'Maybe Session', a -- reasonable implementation would be: -- --
--   data MyAppSettings = MyAppSettings { myAppSess :: Maybe Session, ...}
--   
--   instance HasSession MyAppSettings where
--      getSession = myAppSess <$> controllerState
--      setSession sess = do
--        cs <- controllerState
--        putState $ cs { myAppSess = sess }
--   
class HasSession hs where sessionKey = liftIO $ do { env <- getEnvironment; case lookup "SESSION_KEY" env of { Just key -> return $ pack key Nothing -> case lookup "ENV" env of { Just e | e == "development" -> return "test-session-key" _ -> (error "SESSION_KEY environment variable not set") } } } sessionKey :: HasSession hs => Controller hs ByteString getSession :: HasSession hs => hs -> Maybe Session setSession :: HasSession hs => Session -> Controller hs () -- | A middleware wrapper around a Controller that sets the -- "Set-Cookie" header in the HTTP response if the Session is present, -- i.e. if it was accessed/modified by the Controller. withSession :: HasSession hs => Controller hs a -> Controller hs a -- | Lookup a key from the current Requests session. sessionLookup :: HasSession hs => ByteString -> Controller hs (Maybe ByteString) -- | Insert or replace a key in the current Requests session. sessionInsert :: HasSession hs => ByteString -> ByteString -> Controller hs () -- | Remove a key from the current Requests session. sessionDelete :: HasSession hs => ByteString -> Controller hs () -- | Clear the entire Session. sessionClear :: HasSession hs => Controller hs () -- | Returns the current Session, either from the getSession -- cache or by parsing the cookie from the Request using -- sessionFromCookie. session :: HasSession hs => Controller hs Session -- | Parses and validates a serialized Session given the secret. If -- the Session is invalid (i.e. the hmac does not match), an empty -- Session is returned. parseSession :: ByteString -> ByteString -> Session -- | Serializes a Session by applying a sha256 hmac with the given -- secret key to the serialized Session (using -- renderSimpleQuery), base64 encoding the result, and prepending -- it to the serialized Session. dumpSession :: ByteString -> Session -> ByteString -- | Adds a "Set-Cookie" with the given key-value tuple to the -- Response. The path set on the cookie is "/", meaning it applies -- to all routes on the domain, and no expiration is set. addCookie :: (ByteString, ByteString) -> Response -> Response instance HasSession (Maybe Session)