wai-session-alt (empty) → 0.0.0
raw patch · 4 files changed
+145/−0 lines, 4 filesdep +basedep +blaze-builderdep +bytestringsetup-changed
Dependencies added: base, blaze-builder, bytestring, cookie, http-types, time, vault, wai-transformers
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- src/Network/Wai/Session.hs +84/−0
- wai-session-alt.cabal +29/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Athan Clark++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Athan Clark nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Network/Wai/Session.hs view
@@ -0,0 +1,84 @@+{-# LANGUAGE+ OverloadedStrings+ #-}++{- |+Module : Network.Wai.Session+Copyright : (c) 2015 Athan Clark++License : BSD-3+Maintainer : athan.clark@gmail.com+Stability : experimental+Portability : GHC++A simple interface for storing and parsing session information into cookies,+which slightly differs from the <https://hackage.haskell.org/package/wai-session wai-session>+interface designed by Greg Weber.+-}++module Network.Wai.Session where++import Data.Time+import qualified Data.ByteString as BS+import Blaze.ByteString.Builder (toByteString)++import Data.Vault.Lazy (Key)+import qualified Data.Vault.Lazy as V+import Network.Wai.Trans+import Network.HTTP.Types+import Web.Cookie++++data SessionConfig m k v = SessionConfig+ { renderKey :: k -> BS.ByteString -- ^ serialize the key+ , renderVal :: v -> BS.ByteString -- ^ serialize the value+ , parseKey :: BS.ByteString -> Maybe k -- ^ parse the serialized key+ , parseVal :: BS.ByteString -> Maybe v -- ^ parse the serialized value+ , keyName :: BS.ByteString -- ^ name used as a cookie+ , valName :: BS.ByteString -- ^ name used as a cooke+ , expire :: Integer -- ^ expiration time in Seconds+ , newVal :: k -> v -> m (Maybe v) -- ^ method to getting another value -+ -- this could ping a nonce cache in @m@+ -- for instance.+ , vaultVar :: Key k -- ^ The vault 'Data.Vault.Lazy.Key' used+ -- to store the /session/ key+ -- when 'newVal' is successful.+ }+++sessionMiddleware :: Monad m => SessionConfig m k v -> MiddlewareT m+sessionMiddleware cfg app req respond =+ case parseSessionCookies cfg (requestHeaders req) of+ Nothing -> app req respond+ Just (key,val) -> do+ mVal <- newVal cfg key val+ case mVal of+ Nothing -> app req respond+ Just val' ->+ let f = mapResponseHeaders (++ renderSessionCookies cfg key val')+ req' = req {vault = V.insert (vaultVar cfg) key (vault req)}+ in app req' (respond . f)+++parseSessionCookies :: SessionConfig m k v -> RequestHeaders -> Maybe (k, v)+parseSessionCookies cfg xs = do+ cookies <- parseCookies <$> lookup "Cookie" xs+ key <- parseKey cfg =<< lookup (keyName cfg) cookies+ val <- parseVal cfg =<< lookup (valName cfg) cookies+ return (key, val)++renderSessionCookies :: SessionConfig m k v -> k -> v -> ResponseHeaders+renderSessionCookies cfg key val = repeat "Set-Cookie" `zip` cookies+ where+ cookies = (toByteString . renderSetCookie) <$>+ [ def { setCookieName = keyName cfg+ , setCookieValue = renderKey cfg key+ , setCookieMaxAge = Just $ secondsToDiffTime (expire cfg)+ }+ , def { setCookieName = valName cfg+ , setCookieValue = renderVal cfg val+ , setCookieMaxAge = Just $ secondsToDiffTime (expire cfg)+ }+ ]+
+ wai-session-alt.cabal view
@@ -0,0 +1,29 @@+Name: wai-session-alt+Version: 0.0.0+Author: Athan Clark <athan.clark@gmail.com>+Maintainer: Athan Clark <athan.clark@gmail.com>+License: BSD3+License-File: LICENSE+Synopsis: An alternative session middleware for WAI.+-- Description:+Cabal-Version: >= 1.10+Build-Type: Simple+Category: Web++Library+ Default-Language: Haskell2010+ HS-Source-Dirs: src+ GHC-Options: -Wall+ Exposed-Modules: Network.Wai.Session+ Build-Depends: base >= 4.8 && < 5+ , blaze-builder+ , bytestring+ , cookie+ , http-types+ , time+ , vault+ , wai-transformers++Source-Repository head+ Type: git+ Location: https://github.com/athanclark/wai-session-alt