Factored out DEST code into separate functions
This commit is contained in:
parent
f94a3ea7e0
commit
a39f84a575
@ -26,6 +26,8 @@ module Yesod.Definitions
|
|||||||
, authDisplayName
|
, authDisplayName
|
||||||
, encryptedCookies
|
, encryptedCookies
|
||||||
, langKey
|
, langKey
|
||||||
|
, destCookieName
|
||||||
|
, destCookieTimeout
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import qualified Network.Wai as W
|
import qualified Network.Wai as W
|
||||||
@ -85,3 +87,9 @@ encryptedCookies = [pack authDisplayName, pack authCookieName]
|
|||||||
|
|
||||||
langKey :: String
|
langKey :: String
|
||||||
langKey = "_LANG"
|
langKey = "_LANG"
|
||||||
|
|
||||||
|
destCookieName :: String
|
||||||
|
destCookieName = "DEST"
|
||||||
|
|
||||||
|
destCookieTimeout :: Int
|
||||||
|
destCookieTimeout = 120
|
||||||
|
|||||||
@ -116,7 +116,7 @@ authOpenidForm = do
|
|||||||
rr <- getRawRequest
|
rr <- getRawRequest
|
||||||
case getParams rr "dest" of
|
case getParams rr "dest" of
|
||||||
[] -> return ()
|
[] -> return ()
|
||||||
(x:_) -> addCookie 120 "DEST" x
|
(x:_) -> addCookie destCookieTimeout destCookieName x
|
||||||
let html =
|
let html =
|
||||||
HtmlList
|
HtmlList
|
||||||
[ case getParams rr "message" of
|
[ case getParams rr "message" of
|
||||||
@ -146,20 +146,16 @@ authOpenidForward = do
|
|||||||
|
|
||||||
authOpenidComplete :: YesodApproot y => Handler y ()
|
authOpenidComplete :: YesodApproot y => Handler y ()
|
||||||
authOpenidComplete = do
|
authOpenidComplete = do
|
||||||
ar <- getApproot
|
|
||||||
rr <- getRawRequest
|
rr <- getRawRequest
|
||||||
let gets' = rawGetParams rr
|
let gets' = rawGetParams rr
|
||||||
let dest = case cookies rr "DEST" of
|
|
||||||
[] -> ar
|
|
||||||
(x:_) -> x
|
|
||||||
res <- runAttemptT $ OpenId.authenticate gets'
|
res <- runAttemptT $ OpenId.authenticate gets'
|
||||||
let onFailure err = redirect RedirectTemporary
|
let onFailure err = redirect RedirectTemporary
|
||||||
$ "/auth/openid/?message="
|
$ "/auth/openid/?message="
|
||||||
++ encodeUrl (show err)
|
++ encodeUrl (show err)
|
||||||
let onSuccess (OpenId.Identifier ident) = do
|
let onSuccess (OpenId.Identifier ident) = do
|
||||||
deleteCookie "DEST"
|
ar <- getApproot
|
||||||
header authCookieName ident
|
header authCookieName ident
|
||||||
redirect RedirectTemporary dest
|
redirectToDest RedirectTemporary ar
|
||||||
attempt onFailure onSuccess res
|
attempt onFailure onSuccess res
|
||||||
|
|
||||||
rpxnowLogin :: YesodAuth y => Handler y ()
|
rpxnowLogin :: YesodAuth y => Handler y ()
|
||||||
@ -181,15 +177,11 @@ rpxnowLogin = do
|
|||||||
(('#':rest):_) -> rest
|
(('#':rest):_) -> rest
|
||||||
(s:_) -> s
|
(s:_) -> s
|
||||||
(d:_) -> d
|
(d:_) -> d
|
||||||
let dest' = case cookies rr "DEST" of
|
|
||||||
[] -> dest
|
|
||||||
(x:_) -> x
|
|
||||||
ident <- Rpxnow.authenticate apiKey token
|
ident <- Rpxnow.authenticate apiKey token
|
||||||
onRpxnowLogin ident
|
onRpxnowLogin ident
|
||||||
header authCookieName $ Rpxnow.identifier ident
|
header authCookieName $ Rpxnow.identifier ident
|
||||||
header authDisplayName $ getDisplayName ident
|
header authDisplayName $ getDisplayName ident
|
||||||
deleteCookie "DEST"
|
redirectToDest RedirectTemporary dest
|
||||||
redirect RedirectTemporary dest'
|
|
||||||
|
|
||||||
data MissingToken = MissingToken
|
data MissingToken = MissingToken
|
||||||
deriving (Show, Typeable)
|
deriving (Show, Typeable)
|
||||||
@ -216,13 +208,7 @@ authCheck = do
|
|||||||
authLogout :: YesodAuth y => Handler y ()
|
authLogout :: YesodAuth y => Handler y ()
|
||||||
authLogout = do
|
authLogout = do
|
||||||
deleteCookie authCookieName
|
deleteCookie authCookieName
|
||||||
rr <- getRawRequest
|
getApproot >>= redirectToDest RedirectTemporary
|
||||||
ar <- getApproot
|
|
||||||
let dest = case cookies rr "DEST" of
|
|
||||||
[] -> ar
|
|
||||||
(x:_) -> x
|
|
||||||
deleteCookie "DEST"
|
|
||||||
redirect RedirectTemporary dest
|
|
||||||
|
|
||||||
-- | Gets the identifier for a user if available.
|
-- | Gets the identifier for a user if available.
|
||||||
maybeIdentifier :: (Functor m, Monad m, RequestReader m) => m (Maybe String)
|
maybeIdentifier :: (Functor m, Monad m, RequestReader m) => m (Maybe String)
|
||||||
@ -239,23 +225,19 @@ displayName = do
|
|||||||
-- | Gets the identifier for a user. If user is not logged in, redirects them
|
-- | Gets the identifier for a user. If user is not logged in, redirects them
|
||||||
-- to the login page.
|
-- to the login page.
|
||||||
authIdentifier :: YesodAuth y => Handler y String
|
authIdentifier :: YesodAuth y => Handler y String
|
||||||
authIdentifier = do
|
authIdentifier = maybeIdentifier >>= maybe redirectLogin return
|
||||||
mi <- maybeIdentifier
|
|
||||||
ar <- getApproot
|
-- | Redirect the user to the 'defaultLoginPath', setting the DEST cookie
|
||||||
case mi of
|
-- appropriately.
|
||||||
Nothing -> do
|
redirectLogin :: YesodAuth y => Handler y a
|
||||||
rp <- requestPath
|
redirectLogin =
|
||||||
let dest = ar ++ rp
|
defaultLoginPath `fmap` getYesod >>= redirectSetDest RedirectTemporary
|
||||||
lp <- defaultLoginPath `fmap` getYesod
|
|
||||||
addCookie 120 "DEST" dest
|
|
||||||
redirect RedirectTemporary lp
|
|
||||||
Just x -> return x
|
|
||||||
|
|
||||||
-- | Determinge the path requested by the user (ie, the path info). This
|
-- | Determinge the path requested by the user (ie, the path info). This
|
||||||
-- includes the query string.
|
-- includes the query string.
|
||||||
requestPath :: (Functor m, Monad m, RequestReader m) => m String
|
requestPath :: (Functor m, Monad m, RequestReader m) => m String
|
||||||
requestPath = do
|
requestPath = do
|
||||||
env <- parseEnv
|
env <- waiRequest
|
||||||
let q = case B8.unpack $ Network.Wai.queryString env of
|
let q = case B8.unpack $ Network.Wai.queryString env of
|
||||||
"" -> ""
|
"" -> ""
|
||||||
q'@('?':_) -> q'
|
q'@('?':_) -> q'
|
||||||
@ -264,3 +246,25 @@ requestPath = do
|
|||||||
where
|
where
|
||||||
dropSlash ('/':x) = x
|
dropSlash ('/':x) = x
|
||||||
dropSlash x = x
|
dropSlash x = x
|
||||||
|
|
||||||
|
-- | Redirect to the given URL, and set a cookie with the current URL so the
|
||||||
|
-- user will ultimately be sent back here.
|
||||||
|
redirectSetDest :: YesodApproot y => RedirectType -> String -> Handler y a
|
||||||
|
redirectSetDest rt dest = do
|
||||||
|
ar <- getApproot
|
||||||
|
rp <- requestPath
|
||||||
|
let curr = ar ++ rp
|
||||||
|
addCookie destCookieTimeout destCookieName curr
|
||||||
|
redirect rt dest
|
||||||
|
|
||||||
|
-- | Read the 'destCookieName' cookie and redirect to this destination. If the
|
||||||
|
-- cookie is missing, then use the default path provided.
|
||||||
|
redirectToDest :: RedirectType -> String -> Handler y a
|
||||||
|
redirectToDest rt def = do
|
||||||
|
rr <- getRawRequest
|
||||||
|
dest <- case cookies rr destCookieName of
|
||||||
|
[] -> return def
|
||||||
|
(x:_) -> do
|
||||||
|
deleteCookie destCookieName
|
||||||
|
return x
|
||||||
|
redirect rt dest
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user