@@ -44,9 +44,12 @@ data BasicAuthResult usr
44
44
deriving (Eq , Show , Read , Generic , Typeable , Functor )
45
45
46
46
-- | 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 )
50
53
}
51
54
deriving (Generic , Typeable , Functor )
52
55
@@ -68,12 +71,14 @@ decodeBAHdr req = do
68
71
-- | Run and check basic authentication, returning the appropriate http error per
69
72
-- the spec.
70
73
runBasicAuth :: Request -> BS. ByteString -> BasicAuthCheck usr -> DelayedIO usr
71
- runBasicAuth req realm (BasicAuthCheck ba) =
74
+ runBasicAuth req realm (BasicAuthCheck presentChallenge ba) =
72
75
case decodeBAHdr req of
73
76
Nothing -> plzAuthenticate
74
77
Just e -> liftIO (ba e) >>= \ res -> case res of
75
78
BadPassword -> plzAuthenticate
76
79
NoSuchUser -> plzAuthenticate
77
80
Unauthorized -> delayedFailFatal err403
78
81
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