@@ -1,4 +1,5 @@
11{-# LANGUAGE CPP #-}
2+{-# LANGUAGE TypeApplications #-}
23module Servant.Auth.ServerSpec (spec) where
34
45#if !MIN_VERSION_servant_server(0,16,0)
@@ -25,6 +26,7 @@ import Data.Aeson (FromJSON, ToJSON, Value,
2526import Data.Aeson.Lens (_JSON)
2627import qualified Data.ByteString as BS
2728import qualified Data.ByteString.Lazy as BSL
29+import Data.Text (Text, pack)
2830import Data.CaseInsensitive (mk)
2931import Data.Foldable (find)
3032import Data.Monoid
@@ -40,6 +42,7 @@ import Network.HTTP.Types (Status, status200,
4042import Network.Wai (responseLBS)
4143import Network.Wai.Handler.Warp (testWithApplication)
4244import Network.Wreq (Options, auth, basicAuth,
45+ checkResponse,
4346 cookieExpiryTime, cookies,
4447 defaults, get, getWith, postWith,
4548 header, oauth2Bearer,
@@ -183,8 +186,21 @@ cookieAuthSpec
183186 it "fails with no XSRF header or cookie" $ \port -> property
184187 $ \(user :: User) -> do
185188 jwt <- createJWT theKey (newJWSHeader ((), HS256)) (claims $ toJSON user)
186− opts <- addJwtToCookie cookieCfg jwt
187− getWith opts (url port) `shouldHTTPErrorWith` status401
189+ opts' <- addJwtToCookie cookieCfg jwt
190+ let opts = opts' & checkResponse .~ Just mempty
191+ resp <- getWith opts (url port)
192+ resp ^. responseStatus `shouldBe` status401
193+ (resp ^. responseCookieJar) `shouldNotHaveCookies` ["XSRF-TOKEN"]
194+
195+ -- Validating that the XSRF cookie isn't added for UVerb routes either.
196+ -- These routes can return a 401 response directly without using throwError / throwAll,
197+ -- which revealed a bug:
198+ --
199+ -- https://github.com/haskell-servant/servant/issues/1570#issuecomment-1076374449
200+ resp <- getWith opts (url port ++ "/uverb")
201+ resp ^. responseStatus `shouldBe` status401
202+ (resp ^. responseCookieJar) `shouldNotHaveCookies` ["XSRF-TOKEN"]
203+
188204
189205 it "succeeds if XSRF header and cookie match, and JWT is valid" $ \port -> property
190206 $ \(user :: User) -> do
@@ -406,13 +422,14 @@ type API auths
406422 = Auth auths User :>
407423 ( Get '[JSON] Int
408424 :<|> ReqBody '[JSON] Int :> Post '[JSON] Int
409− :<|> NamedRoutes DummyRoutes
425+ :<|> "named" :> NamedRoutes DummyRoutes
410426 :<|> "header" :> Get '[JSON] (Headers '[Header "Blah" Int] Int)
411427#if MIN_VERSION_servant_server(0,15,0)
412428 :<|> "stream" :> StreamGet NoFraming OctetStream (SourceIO BS.ByteString)
413429#endif
414430 :<|> "raw" :> Raw
415431 )
432+ :<|> "uverb" :> Auth auths User :> UVerb 'GET '[JSON] '[WithStatus 200 Int, WithStatus 401 Text, WithStatus 403 Text]
416433 :<|> "login" :> ReqBody '[JSON] User :> Post '[JSON] (Headers '[ Header "Set-Cookie" SetCookie
417434 , Header "Set-Cookie" SetCookie ] NoContent)
418435 :<|> "logout" :> Get '[JSON] (Headers '[ Header "Set-Cookie" SetCookie
@@ -490,6 +507,11 @@ server ccfg =
490507 :<|> raw
491508 Indefinite -> throwAll err401
492509 _ -> throwAll err403
510+ ) :<|>
511+ (\authResult -> case authResult of
512+ Authenticated usr -> respond (WithStatus @200 (42 :: Int))
513+ Indefinite -> respond (WithStatus @401 $ pack "Authentication required")
514+ _ -> respond (WithStatus @403 $ pack "Forbidden")
493515 )
494516 :<|> getLogin
495517 :<|> getLogout
@@ -570,6 +592,12 @@ shouldMatchCookieNames cj patterns
570592 = fmap cookie_name (destroyCookieJar cj)
571593 `shouldMatchList` patterns
572594
595+shouldNotHaveCookies :: HCli.CookieJar -> [BS.ByteString] -> Expectation
596+shouldNotHaveCookies cj patterns =
597+ sequence_ $ (\cookieName -> cookieNames `shouldNotContain` [cookieName]) <$> patterns
598+ where cookieNames :: [BS.ByteString]
599+ cookieNames = cookie_name <$> destroyCookieJar cj
600+
573601shouldMatchCookieNameValues :: HCli.CookieJar -> [(BS.ByteString, BS.ByteString)] -> Expectation
574602shouldMatchCookieNameValues cj patterns
575603 = fmap ((,) <$> cookie_name <*> cookie_value) (destroyCookieJar cj)