Merge branch 'master' of github.com:yesodweb/yesod
This commit is contained in:
commit
9621d12b9f
@ -31,7 +31,6 @@ module Yesod.Core
|
|||||||
, SessionBackend (..)
|
, SessionBackend (..)
|
||||||
, defaultClientSessionBackend
|
, defaultClientSessionBackend
|
||||||
, clientSessionBackend
|
, clientSessionBackend
|
||||||
, saveClientSession
|
|
||||||
, loadClientSession
|
, loadClientSession
|
||||||
, Header(..)
|
, Header(..)
|
||||||
, BackendSession
|
, BackendSession
|
||||||
|
|||||||
@ -29,7 +29,6 @@ module Yesod.Internal.Core
|
|||||||
, SessionBackend (..)
|
, SessionBackend (..)
|
||||||
, defaultClientSessionBackend
|
, defaultClientSessionBackend
|
||||||
, clientSessionBackend
|
, clientSessionBackend
|
||||||
, saveClientSession
|
|
||||||
, loadClientSession
|
, loadClientSession
|
||||||
, BackendSession
|
, BackendSession
|
||||||
-- * jsLoader
|
-- * jsLoader
|
||||||
@ -389,8 +388,9 @@ defaultYesodRunner _ master _ murl toMaster _ req
|
|||||||
(x, _):_ -> Just x
|
(x, _):_ -> Just x
|
||||||
defaultYesodRunner handler master sub murl toMasterRoute msb req = do
|
defaultYesodRunner handler master sub murl toMasterRoute msb req = do
|
||||||
now <- liftIO getCurrentTime
|
now <- liftIO getCurrentTime
|
||||||
session <- liftIO $
|
let dontSaveSession _ _ = return []
|
||||||
maybe (return []) (\sb -> sbLoadSession sb master req now) msb
|
(session, saveSession) <- liftIO $
|
||||||
|
maybe (return ([], dontSaveSession)) (\sb -> sbLoadSession sb master req now) msb
|
||||||
rr <- liftIO $ parseWaiRequest req session (isJust msb)
|
rr <- liftIO $ parseWaiRequest req session (isJust msb)
|
||||||
let h = {-# SCC "h" #-} do
|
let h = {-# SCC "h" #-} do
|
||||||
case murl of
|
case murl of
|
||||||
@ -419,10 +419,7 @@ defaultYesodRunner handler master sub murl toMasterRoute msb req = do
|
|||||||
newSess
|
newSess
|
||||||
(\n -> Map.insert tokenKey (TE.encodeUtf8 n) newSess)
|
(\n -> Map.insert tokenKey (TE.encodeUtf8 n) newSess)
|
||||||
(reqToken rr)
|
(reqToken rr)
|
||||||
sessionHeaders <- liftIO $ maybe
|
sessionHeaders <- liftIO (saveSession nsToken now)
|
||||||
(return [])
|
|
||||||
(\sb -> sbSaveSession sb master req now session nsToken)
|
|
||||||
msb
|
|
||||||
return $ ("Content-Type", ct) : map headerToPair sessionHeaders
|
return $ ("Content-Type", ct) : map headerToPair sessionHeaders
|
||||||
_ -> return []
|
_ -> return []
|
||||||
return $ yarToResponse yar extraHeaders
|
return $ yarToResponse yar extraHeaders
|
||||||
@ -702,44 +699,36 @@ clientSessionBackend :: Yesod master
|
|||||||
-> Int -- ^ Inactive session valitity in minutes
|
-> Int -- ^ Inactive session valitity in minutes
|
||||||
-> SessionBackend master
|
-> SessionBackend master
|
||||||
clientSessionBackend key timeout = SessionBackend
|
clientSessionBackend key timeout = SessionBackend
|
||||||
{ sbSaveSession = saveClientSession key timeout
|
{ sbLoadSession = loadClientSession key timeout
|
||||||
, sbLoadSession = loadClientSession key
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadClientSession :: Yesod master
|
loadClientSession :: Yesod master
|
||||||
=> CS.Key
|
|
||||||
-> master
|
|
||||||
-> W.Request
|
|
||||||
-> UTCTime
|
|
||||||
-> IO BackendSession
|
|
||||||
loadClientSession key _ req now = return . fromMaybe [] $ do
|
|
||||||
raw <- lookup "Cookie" $ W.requestHeaders req
|
|
||||||
val <- lookup sessionName $ parseCookies raw
|
|
||||||
let host = "" -- fixme, properly lock sessions to client address
|
|
||||||
decodeClientSession key now host val
|
|
||||||
|
|
||||||
saveClientSession :: Yesod master
|
|
||||||
=> CS.Key
|
=> CS.Key
|
||||||
-> Int
|
-> Int
|
||||||
-> master
|
-> master
|
||||||
-> W.Request
|
-> W.Request
|
||||||
-> UTCTime
|
-> UTCTime
|
||||||
-> BackendSession
|
-> IO (BackendSession, SaveSession)
|
||||||
-> BackendSession
|
loadClientSession key timeout master req now = return (sess, save)
|
||||||
-> IO [Header]
|
|
||||||
saveClientSession key timeout master _ now _ sess = do
|
|
||||||
-- fixme should we be caching this?
|
|
||||||
iv <- liftIO CS.randomIV
|
|
||||||
return [AddCookie def
|
|
||||||
{ setCookieName = sessionName
|
|
||||||
, setCookieValue = sessionVal iv
|
|
||||||
, setCookiePath = Just (cookiePath master)
|
|
||||||
, setCookieExpires = Just expires
|
|
||||||
, setCookieDomain = cookieDomain master
|
|
||||||
, setCookieHttpOnly = True
|
|
||||||
}]
|
|
||||||
where
|
where
|
||||||
host = "" -- fixme, properly lock sessions to client address
|
sess = fromMaybe [] $ do
|
||||||
expires = fromIntegral (timeout * 60) `addUTCTime` now
|
raw <- lookup "Cookie" $ W.requestHeaders req
|
||||||
sessionVal iv = encodeClientSession key iv expires host sess
|
val <- lookup sessionName $ parseCookies raw
|
||||||
|
let host = "" -- fixme, properly lock sessions to client address
|
||||||
|
decodeClientSession key now host val
|
||||||
|
save sess' now' = do
|
||||||
|
-- fixme should we be caching this?
|
||||||
|
iv <- liftIO CS.randomIV
|
||||||
|
return [AddCookie def
|
||||||
|
{ setCookieName = sessionName
|
||||||
|
, setCookieValue = sessionVal iv
|
||||||
|
, setCookiePath = Just (cookiePath master)
|
||||||
|
, setCookieExpires = Just expires
|
||||||
|
, setCookieDomain = cookieDomain master
|
||||||
|
, setCookieHttpOnly = True
|
||||||
|
}]
|
||||||
|
where
|
||||||
|
host = "" -- fixme, properly lock sessions to client address
|
||||||
|
expires = fromIntegral (timeout * 60) `addUTCTime` now'
|
||||||
|
sessionVal iv = encodeClientSession key iv expires host sess'
|
||||||
|
|
||||||
|
|||||||
@ -2,6 +2,7 @@ module Yesod.Internal.Session
|
|||||||
( encodeClientSession
|
( encodeClientSession
|
||||||
, decodeClientSession
|
, decodeClientSession
|
||||||
, BackendSession
|
, BackendSession
|
||||||
|
, SaveSession
|
||||||
, SessionBackend(..)
|
, SessionBackend(..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
@ -20,17 +21,15 @@ import qualified Network.Wai as W
|
|||||||
|
|
||||||
type BackendSession = [(Text, S8.ByteString)]
|
type BackendSession = [(Text, S8.ByteString)]
|
||||||
|
|
||||||
data SessionBackend master = SessionBackend
|
type SaveSession = BackendSession -> -- ^ The session contents after running the handler
|
||||||
{ sbSaveSession :: master
|
UTCTime -> -- ^ current time
|
||||||
|
IO [Header]
|
||||||
|
|
||||||
|
newtype SessionBackend master = SessionBackend
|
||||||
|
{ sbLoadSession :: master
|
||||||
-> W.Request
|
-> W.Request
|
||||||
-> UTCTime -- ^ The current time
|
-> UTCTime
|
||||||
-> BackendSession -- ^ The old session (before running handler)
|
-> IO (BackendSession, SaveSession) -- ^ Return the session data and a function to save the session
|
||||||
-> BackendSession -- ^ The final session
|
|
||||||
-> IO [Header]
|
|
||||||
, sbLoadSession :: master
|
|
||||||
-> W.Request
|
|
||||||
-> UTCTime -- ^ The current time
|
|
||||||
-> IO BackendSession
|
|
||||||
}
|
}
|
||||||
|
|
||||||
encodeClientSession :: CS.Key
|
encodeClientSession :: CS.Key
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user