{-# LANGUAGE FlexibleContexts #-}
module Happstack.Server.Auth where
import Control.Monad (MonadPlus(mzero, mplus))
import Data.ByteString.Base64 as Base64
import qualified Data.ByteString.Char8 as B
import qualified Data.Map as M
import Happstack.Server.Monads (Happstack, escape, getHeaderM, setHeaderM)
import Happstack.Server.Response (unauthorized, toResponse)
basicAuth :: (Happstack m) =>
String
-> M.Map String String
-> m a
-> m a
basicAuth realmName authMap = basicAuthBy (validLoginPlaintext authMap) realmName
basicAuthBy :: (Happstack m) =>
(B.ByteString -> B.ByteString -> Bool)
-> String
-> m a
-> m a
basicAuthBy validLogin realmName xs = basicAuthImpl `mplus` xs
where
basicAuthImpl = do
aHeader <- getHeaderM "authorization"
case aHeader of
Nothing -> err
Just x ->
do (name, password) <- parseHeader x
if B.length password > 0
&& B.head password == ':'
&& validLogin name (B.tail password)
then mzero
else err
parseHeader h =
case Base64.decode . B.drop 6 $ h of
(Left _) -> err
(Right bs) -> return (B.break (':'==) bs)
headerName = "WWW-Authenticate"
headerValue = "Basic realm=\"" ++ realmName ++ "\""
err :: (Happstack m) => m a
err = escape $ do
setHeaderM headerName headerValue
unauthorized $ toResponse "Not authorized"
validLoginPlaintext ::
M.Map String String
-> B.ByteString
-> B.ByteString
-> Bool
validLoginPlaintext authMap name password = M.lookup (B.unpack name) authMap == Just (B.unpack password)