Skip to content

Commit

Permalink
Added type constraint to onHtmlError. Modified plugins to support this
Browse files Browse the repository at this point in the history
changed type
  • Loading branch information
jprider63 committed Dec 22, 2013
1 parent e17523b commit b57ac44
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 27 deletions.
24 changes: 12 additions & 12 deletions yesod-auth/Yesod/Auth.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ class (Yesod master, PathPiece (AuthId master), RenderMessage master FormMessage
=> HandlerT master IO (Maybe (AuthId master))
maybeAuthId = defaultMaybeAuthId

-- | Called on login error for HTTP requests. By default, calls
-- @setMessage
onError :: (MonadResourceBase m) => Route master -> Text -> HandlerT master m Html
onError dest msg = do
setMessage $ toHtml msg
fmap asHtml $ redirect dest
where
asHtml :: Html -> Html
asHtml = id
-- | Called on login error for HTTP requests. By default, calls
-- @setMessage
onError :: (MonadResourceBase m) => Route master -> Text -> HandlerT master m Html
onError dest msg = do
setMessage $ toHtml msg
fmap asHtml $ redirect dest
where
asHtml :: Html -> Html
asHtml = id

-- | Internal session key used to hold the authentication information.
--
Expand Down Expand Up @@ -243,10 +243,10 @@ loginErrorMessageMasterI dest msg = do

-- | For HTML, set the message and redirect to the route.
-- For JSON, send the message and a 401 status
loginErrorMessage :: MonadResourceBase m
=> Route site
loginErrorMessage :: (YesodAuth master, MonadResourceBase m)
=> Route master
-> Text
-> HandlerT site m a
-> HandlerT master m a
loginErrorMessage dest msg =
sendResponseStatus unauthorized401 =<< (
selectRep $ do
Expand Down
3 changes: 2 additions & 1 deletion yesod-auth/Yesod/Auth/BrowserId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ authBrowserId bis@BrowserIdSettings {..} = AuthPlugin
case memail of
Nothing -> do
$logErrorS "yesod-auth" "BrowserID assertion failure"
loginErrorMessage LoginR "BrowserID login error."
tm <- getRouteToParent
lift $ loginErrorMessage (tm LoginR) "BrowserID login error."
Just email -> lift $ setCreds True Creds
{ credsPlugin = pid
, credsIdent = email
Expand Down
10 changes: 7 additions & 3 deletions yesod-auth/Yesod/Auth/Email.hs
Original file line number Diff line number Diff line change
Expand Up @@ -395,15 +395,19 @@ postPasswordR = do
Nothing -> loginErrorMessageI LoginR Msg.BadSetPass
Just aid -> return aid

tm <- getRouteToParent

needOld <- lift $ needOldPassword aid
when needOld $ do
current <- lift $ runInputPost $ ireq textField "current"
mrealpass <- lift $ getPassword aid
case mrealpass of
Nothing -> loginErrorMessage setpassR "You do not currently have a password set on your account"
Nothing ->
lift $ loginErrorMessage (tm setpassR) "You do not currently have a password set on your account"
Just realpass
| isValidPass current realpass -> return ()
| otherwise -> loginErrorMessage setpassR "Invalid current password, please try again"
| otherwise ->
lift $ loginErrorMessage (tm setpassR) "Invalid current password, please try again"

(new, confirm) <- lift $ runInputPost $ (,)
<$> ireq textField "new"
Expand All @@ -413,7 +417,7 @@ postPasswordR = do

isSecure <- lift $ checkPasswordSecurity aid new
case isSecure of
Left e -> loginErrorMessage setpassR e
Left e -> lift $ loginErrorMessage (tm setpassR) e
Right () -> return ()

salted <- liftIO $ saltPass new
Expand Down
16 changes: 10 additions & 6 deletions yesod-auth/Yesod/Auth/GoogleEmail.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ authGoogleEmail =
, ("openid.ui.icon", "true")
] (authHttpManager master)
either
(\err -> loginErrorMessage LoginR $ T.pack $ show (err :: SomeException))
(\err -> do
tm <- getRouteToParent
lift $ loginErrorMessage (tm LoginR) $ T.pack $ show (err :: SomeException))
redirect
eres
dispatch "GET" ["complete", ""] = dispatch "GET" ["complete"] -- compatibility issues
Expand All @@ -71,13 +73,15 @@ completeHelper :: YesodAuth master => [(Text, Text)] -> AuthHandler master ()
completeHelper gets' = do
master <- lift getYesod
eres <- lift $ try $ OpenId.authenticateClaimed gets' (authHttpManager master)
either onFailure onSuccess eres
tm <- getRouteToParent
either (onFailure tm) (onSuccess tm) eres
where
onFailure err = loginErrorMessage LoginR $ T.pack $ show (err :: SomeException)
onSuccess oir = do
onFailure tm err = do
lift $ loginErrorMessage (tm LoginR) $ T.pack $ show (err :: SomeException)
onSuccess tm oir = do
let OpenId.Identifier ident = OpenId.oirOpLocal oir
memail <- lookupGetParam "openid.ext1.value.email"
case (memail, "https://www.google.com/accounts/o8/id" `T.isPrefixOf` ident) of
(Just email, True) -> lift $ setCreds True $ Creds pid email []
(_, False) -> loginErrorMessage LoginR "Only Google login is supported"
(Nothing, _) -> loginErrorMessage LoginR "No email address provided"
(_, False) -> lift $ loginErrorMessage (tm LoginR) "Only Google login is supported"
(Nothing, _) -> lift $ loginErrorMessage (tm LoginR) "No email address provided"
4 changes: 3 additions & 1 deletion yesod-auth/Yesod/Auth/HashDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ postLoginR uniq = do
(validateUser <$> (uniq =<< mu) <*> mp)
if isValid
then lift $ setCreds True $ Creds "hashdb" (fromMaybe "" mu) []
else loginErrorMessage LoginR "Invalid username/password"
else do
tm <- getRouteToParent
lift $ loginErrorMessage (tm LoginR) "Invalid username/password"


-- | A drop in for the getAuthId method of your YesodAuth instance which
Expand Down
12 changes: 8 additions & 4 deletions yesod-auth/Yesod/Auth/OpenId.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ $newline never
master <- lift getYesod
eres <- lift $ try $ OpenId.getForwardUrl oid complete' Nothing extensionFields (authHttpManager master)
case eres of
Left err -> loginErrorMessage LoginR $ T.pack $
show (err :: SomeException)
Left err -> do
tm <- getRouteToParent
lift $ loginErrorMessage (tm LoginR) $ T.pack $
show (err :: SomeException)
Right x -> redirect x
Nothing -> loginErrorMessageI LoginR Msg.NoOpenID
dispatch "GET" ["complete", ""] = dispatch "GET" ["complete"] -- compatibility issues
Expand All @@ -89,8 +91,10 @@ completeHelper idType gets' = do
eres <- try $ OpenId.authenticateClaimed gets' (authHttpManager master)
either onFailure onSuccess eres
where
onFailure err = loginErrorMessage LoginR $ T.pack $
show (err :: SomeException)
onFailure err = do
tm <- getRouteToParent
lift $ loginErrorMessage (tm LoginR) $ T.pack $
show (err :: SomeException)
onSuccess oir = do
let claimed =
case OpenId.oirClaimed oir of
Expand Down

0 comments on commit b57ac44

Please sign in to comment.