{-# LANGUAGE CPP #-}
module Servant.Auth.Server.Internal.BasicAuth where
#if !MIN_VERSION_servant_server(0,16,0)
#define ServerError ServantErr
#endif
import qualified Data.ByteString as BS
import Servant (BasicAuthData (..),
ServerError (..), err401)
import Servant.Server.Internal.BasicAuth (decodeBAHdr,
mkBAChallengerHdr)
import Servant.Auth.Server.Internal.Types
wwwAuthenticatedErr :: BS.ByteString -> ServerError
wwwAuthenticatedErr :: ByteString -> ServerError
wwwAuthenticatedErr ByteString
realm = ServerError
err401 { errHeaders :: [Header]
errHeaders = [ByteString -> Header
mkBAChallengerHdr ByteString
realm] }
type family BasicAuthCfg
class FromBasicAuthData a where
fromBasicAuthData :: BasicAuthData -> BasicAuthCfg -> IO (AuthResult a)
basicAuthCheck :: FromBasicAuthData usr => BasicAuthCfg -> AuthCheck usr
basicAuthCheck :: BasicAuthCfg -> AuthCheck usr
basicAuthCheck BasicAuthCfg
cfg = (Request -> IO (AuthResult usr)) -> AuthCheck usr
forall val. (Request -> IO (AuthResult val)) -> AuthCheck val
AuthCheck ((Request -> IO (AuthResult usr)) -> AuthCheck usr)
-> (Request -> IO (AuthResult usr)) -> AuthCheck usr
forall a b. (a -> b) -> a -> b
$ \Request
req -> case Request -> Maybe BasicAuthData
decodeBAHdr Request
req of
Maybe BasicAuthData
Nothing -> AuthResult usr -> IO (AuthResult usr)
forall (m :: * -> *) a. Monad m => a -> m a
return AuthResult usr
forall val. AuthResult val
Indefinite
Just BasicAuthData
baData -> BasicAuthData -> BasicAuthCfg -> IO (AuthResult usr)
forall a.
FromBasicAuthData a =>
BasicAuthData -> BasicAuthCfg -> IO (AuthResult a)
fromBasicAuthData BasicAuthData
baData BasicAuthCfg
cfg