Skip to content

Commit 4a39dbe

Browse files
committed
add realm flag
1 parent 27173c9 commit 4a39dbe

File tree

6 files changed

+15
-10
lines changed

6 files changed

+15
-10
lines changed

servant-client/test/Servant/ClientTestUtils.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ basicAuthHandler =
237237
if username == "servant" && password == "server"
238238
then return (Authorized ())
239239
else return Unauthorized
240-
in BasicAuthCheck check
240+
in BasicAuthCheck True check
241241

242242
basicServerContext :: Context '[ BasicAuthCheck () ]
243243
basicServerContext = basicAuthHandler :. EmptyContext

servant-http-streams/test/Servant/ClientSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ basicAuthHandler =
222222
if username == "servant" && password == "server"
223223
then return (Authorized ())
224224
else return Unauthorized
225-
in BasicAuthCheck check
225+
in BasicAuthCheck True check
226226

227227
basicServerContext :: Context '[ BasicAuthCheck () ]
228228
basicServerContext = basicAuthHandler :. EmptyContext

servant-server/src/Servant/Server.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ module Servant.Server
4343
, descendIntoNamedContext
4444

4545
-- * Basic Authentication
46-
, BasicAuthCheck(BasicAuthCheck, unBasicAuthCheck)
46+
, BasicAuthCheck(BasicAuthCheck, basicAuthRunCheck, basicAuthPresentChallenge)
4747
, BasicAuthResult(..)
4848

4949
-- * General Authentication

servant-server/src/Servant/Server/Internal/BasicAuth.hs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7073
runBasicAuth :: 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] }

servant-server/test/Servant/Server/ErrorSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ errorOrderAuthCheck =
4444
if username == "servant" && password == "server"
4545
then return (Authorized ())
4646
else return Unauthorized
47-
in BasicAuthCheck check
47+
in BasicAuthCheck True check
4848

4949
------------------------------------------------------------------------------
5050
-- * Error Order {{{

servant-server/test/Servant/ServerSpec.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ basicAuthServer =
744744

745745
basicAuthContext :: Context '[ BasicAuthCheck () ]
746746
basicAuthContext =
747-
let basicHandler = BasicAuthCheck $ \(BasicAuthData usr pass) ->
747+
let basicHandler = BasicAuthCheck True $ \(BasicAuthData usr pass) ->
748748
if usr == "servant" && pass == "server"
749749
then return (Authorized ())
750750
else return Unauthorized

0 commit comments

Comments
 (0)