Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple authentication helpers #962

Merged
merged 1 commit into from
Mar 26, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions yesod-core/Yesod/Core/Handler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ module Yesod.Core.Handler
, lookupCookie
, lookupFile
, lookupHeader
-- **** Lookup authentication data
, lookupBasicAuth
, lookupBearerAuth
-- **** Multi-lookup
, lookupGetParams
, lookupPostParams
Expand Down Expand Up @@ -166,6 +169,8 @@ import Control.Monad.IO.Class (MonadIO, liftIO)

import qualified Network.HTTP.Types as H
import qualified Network.Wai as W
import Network.Wai.Middleware.HttpAuth
( extractBasicAuth, extractBearerAuth )
import Control.Monad.Trans.Class (lift)

import qualified Data.Text as T
Expand Down Expand Up @@ -972,6 +977,26 @@ lookupHeaders key = do
req <- waiRequest
return $ lookup' key $ W.requestHeaders req

-- | Lookup basic authentication data from __Authorization__ header of
-- request. Returns user name and password
lookupBasicAuth :: (MonadHandler m) => m (Maybe (Text, Text))
lookupBasicAuth = fmap (>>= getBA)
(lookupHeader "Authorization")
where
getBA bs = (\(x, y) -> ( decodeUtf8With lenientDecode x
, decodeUtf8With lenientDecode y))
<$> extractBasicAuth bs

-- | Lookup bearer authentication datafrom __Authorization__ header of
-- request. Returns bearer token value
lookupBearerAuth :: (MonadHandler m) => m (Maybe Text)
lookupBearerAuth = fmap (>>= getBR)
(lookupHeader "Authorization")
where
getBR bs = decodeUtf8With lenientDecode
<$> extractBearerAuth bs


-- | Lookup for GET parameters.
lookupGetParams :: MonadHandler m => Text -> m [Text]
lookupGetParams pn = do
Expand Down