@@ -44,9 +44,12 @@ data BasicAuthResult usr
4444 deriving (Eq , Show , Read , Generic , Typeable , Functor )
4545
4646-- | Datatype wrapping a function used to check authentication.
47- newtype BasicAuthCheck usr = BasicAuthCheck
48- { unBasicAuthCheck :: BasicAuthData
49- -> IO (BasicAuthResult usr )
47+ data BasicAuthCheck usr
48+ = BasicAuthCheck
49+ { basicAuthPresentChallenge :: Bool
50+ -- ^ Decides if we'll send a @WWW-Authenticate@ HTTP header. Sending the header causes browser to
51+ -- surface a prompt for user name and password, which may be undesirable for APIs.
52+ , basicAuthRunCheck :: BasicAuthData -> IO (BasicAuthResult usr )
5053 }
5154 deriving (Generic , Typeable , Functor )
5255
@@ -68,12 +71,14 @@ decodeBAHdr req = do
6871-- | Run and check basic authentication, returning the appropriate http error per
6972-- the spec.
7073runBasicAuth :: Request -> BS. ByteString -> BasicAuthCheck usr -> DelayedIO usr
71- runBasicAuth req realm (BasicAuthCheck ba) =
74+ runBasicAuth req realm (BasicAuthCheck presentChallenge ba) =
7275 case decodeBAHdr req of
7376 Nothing -> plzAuthenticate
7477 Just e -> liftIO (ba e) >>= \ res -> case res of
7578 BadPassword -> plzAuthenticate
7679 NoSuchUser -> plzAuthenticate
7780 Unauthorized -> delayedFailFatal err403
7881 Authorized usr -> return usr
79- where plzAuthenticate = delayedFailFatal err401 { errHeaders = [mkBAChallengerHdr realm] }
82+ where
83+ plzAuthenticate =
84+ delayedFailFatal err401 { errHeaders = [mkBAChallengerHdr realm | presentChallenge] }
0 commit comments