From f6dd0b56b93f196a493c37ee2f07f8ce7f8aa569 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Mon, 26 Nov 2012 15:32:25 +0200 Subject: [PATCH] maybeAuthId is now part of YesodAuth typeclass --- yesod-auth/Yesod/Auth.hs | 38 +++++++++++++++++++++++++------------ yesod-auth/yesod-auth.cabal | 2 +- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/yesod-auth/Yesod/Auth.hs b/yesod-auth/Yesod/Auth.hs index 3e8d7106..62e06d61 100644 --- a/yesod-auth/Yesod/Auth.hs +++ b/yesod-auth/Yesod/Auth.hs @@ -19,7 +19,7 @@ module Yesod.Auth , Creds (..) , setCreds -- * User functions - , maybeAuthId + , defaultMaybeAuthId , maybeAuth , requireAuthId , requireAuth @@ -126,6 +126,31 @@ class (Yesod m, PathPiece (AuthId m), RenderMessage m FormMessage) => YesodAuth onLogout :: GHandler s m () onLogout = return () + -- | Retrieves user credentials, if user is authenticated. + -- + -- By default, this calls 'defaultMaybeAuthId' to get the user ID from the + -- session. This can be overridden to allow authentication via other means, + -- such as checking for a special token in a request header. This is + -- especially useful for creating an API to be accessed via some means + -- other than a browser. + -- + -- Since 1.1.2 + maybeAuthId :: GHandler s m (Maybe (AuthId m)) + maybeAuthId = defaultMaybeAuthId + +credsKey :: Text +credsKey = "_ID" + +-- | Retrieves user credentials from the session, if user is authenticated. +-- +-- Since 1.1.2 +defaultMaybeAuthId :: YesodAuth m => GHandler s m (Maybe (AuthId m)) +defaultMaybeAuthId = do + ms <- lookupSession credsKey + case ms of + Nothing -> return Nothing + Just s -> return $ fromPathPiece s + mkYesodSub "Auth" [ ClassP ''YesodAuth [VarT $ mkName "master"] ] @@ -137,9 +162,6 @@ mkYesodSub "Auth" /page/#Text/STRINGS PluginR |] -credsKey :: Text -credsKey = "_ID" - -- | FIXME: won't show up till redirect setCreds :: YesodAuth m => Bool -> Creds m -> GHandler s m () setCreds doRedirects creds = do @@ -210,14 +232,6 @@ handlePluginR plugin pieces = do [] -> notFound ap:_ -> apDispatch ap method pieces --- | Retrieves user credentials, if user is authenticated. -maybeAuthId :: YesodAuth m => GHandler s m (Maybe (AuthId m)) -maybeAuthId = do - ms <- lookupSession credsKey - case ms of - Nothing -> return Nothing - Just s -> return $ fromPathPiece s - maybeAuth :: ( YesodAuth m , b ~ YesodPersistBackend m , b ~ PersistEntityBackend val diff --git a/yesod-auth/yesod-auth.cabal b/yesod-auth/yesod-auth.cabal index 51aa4629..0f7a4428 100644 --- a/yesod-auth/yesod-auth.cabal +++ b/yesod-auth/yesod-auth.cabal @@ -1,5 +1,5 @@ name: yesod-auth -version: 1.1.1.7 +version: 1.1.2 license: MIT license-file: LICENSE author: Michael Snoyman, Patrick Brisbin