@@ -11,6 +11,7 @@ module Servant.Auth.ServerSpec (spec) where
1111import Control.Lens
1212import Control.Monad (forM_ )
1313import Control.Monad.IO.Class (liftIO )
14+ {- FOURMOLU_DISABLE -}
1415import Crypto.JOSE
1516 ( Alg (HS256 , None )
1617 , Error
@@ -20,14 +21,21 @@ import Crypto.JOSE
2021 , ToCompact
2122 , encodeCompact
2223 , genJWK
24+ #if MIN_VERSION_jose(0,12,0)
25+ , newJWSHeaderProtected
26+ #else
2327 , newJWSHeader
28+ #endif
2429 , runJOSE
2530 )
2631import Crypto.JWT
2732 ( Audience (.. )
2833 , ClaimsSet
2934 , NumericDate (NumericDate )
3035 , SignedJWT
36+ #if MIN_VERSION_jose(0,12,0)
37+ , RequiredProtection
38+ #endif
3139 , claimAud
3240 , claimNbf
3341 , emptyClaimsSet
@@ -144,7 +152,11 @@ authSpec =
144152 jwt <-
145153 createJWT
146154 theKey
155+ #if MIN_VERSION_jose(0,12,0)
156+ (newJWSHeaderProtected HS256 )
157+ #else
147158 (newJWSHeader (() , HS256 ))
159+ #endif
148160 (claims $ toJSON user)
149161 opts' <- addJwtToCookie cookieCfg jwt
150162 let opts =
@@ -167,7 +179,11 @@ authSpec =
167179 jwt <-
168180 createJWT
169181 theKey
182+ #if MIN_VERSION_jose(0,12,0)
183+ (newJWSHeaderProtected HS256 )
184+ #else
170185 (newJWSHeader (() , HS256 ))
186+ #endif
171187 (claims $ toJSON user)
172188 opts' <- addJwtToCookie cookieCfg jwt
173189 let opts =
@@ -185,7 +201,11 @@ authSpec =
185201 jwt <-
186202 createJWT
187203 theKey
204+ #if MIN_VERSION_jose(0,12,0)
205+ (newJWSHeaderProtected HS256 )
206+ #else
188207 (newJWSHeader (() , HS256 ))
208+ #endif
189209 (claims $ toJSON user)
190210 opts' <- addJwtToCookie cookieCfg jwt
191211 let opts =
@@ -211,7 +231,11 @@ cookieAuthSpec =
211231 aroundAll (testWithApplication . pure $ app cookieOnlyApi) $ do
212232 it " fails if XSRF header and cookie don't match" $ \ port -> property $
213233 \ (user :: User ) -> do
234+ #if MIN_VERSION_jose(0,12,0)
235+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
236+ #else
214237 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
238+ #endif
215239 opts' <- addJwtToCookie cookieCfg jwt
216240 let opts =
217241 addCookie
@@ -221,7 +245,11 @@ cookieAuthSpec =
221245
222246 it " fails with no XSRF header or cookie" $ \ port -> property $
223247 \ (user :: User ) -> do
248+ #if MIN_VERSION_jose(0,12,0)
249+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
250+ #else
224251 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
252+ #endif
225253 opts' <- addJwtToCookie cookieCfg jwt
226254 let opts = opts' & checkResponse ?~ mempty
227255 resp <- getWith opts (url port)
@@ -239,7 +267,11 @@ cookieAuthSpec =
239267
240268 it " succeeds if XSRF header and cookie match, and JWT is valid" $ \ port -> property $
241269 \ (user :: User ) -> do
270+ #if MIN_VERSION_jose(0,12,0)
271+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
272+ #else
242273 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
274+ #endif
243275 opts' <- addJwtToCookie cookieCfg jwt
244276 let opts =
245277 addCookie
@@ -286,14 +318,22 @@ cookieAuthSpec =
286318 aroundAll (testWithApplication . pure $ appWithCookie cookieOnlyApi cookieCfgNoXsrfGet) $ do
287319 it " succeeds with no XSRF header or cookie for GET" $ \ port -> property $
288320 \ (user :: User ) -> do
321+ #if MIN_VERSION_jose(0,12,0)
322+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
323+ #else
289324 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
325+ #endif
290326 opts <- addJwtToCookie cookieCfgNoXsrfGet jwt
291327 resp <- getWith opts (url port)
292328 resp ^? responseBody . _JSON `shouldBe` Just (length $ name user)
293329
294330 it " fails with no XSRF header or cookie for POST" $ \ port -> property $
295331 \ (user :: User ) number -> do
332+ #if MIN_VERSION_jose(0,12,0)
333+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
334+ #else
296335 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
336+ #endif
297337 opts <- addJwtToCookie cookieCfgNoXsrfGet jwt
298338 postWith opts (url port) (toJSON (number :: Int )) `shouldHTTPErrorWith` status401
299339
@@ -304,14 +344,22 @@ cookieAuthSpec =
304344 aroundAll (testWithApplication . pure $ appWithCookie cookieOnlyApi cookieCfgNoXsrf) $ do
305345 it " succeeds with no XSRF header or cookie for GET" $ \ port -> property $
306346 \ (user :: User ) -> do
347+ #if MIN_VERSION_jose(0,12,0)
348+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
349+ #else
307350 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
351+ #endif
308352 opts <- addJwtToCookie cookieCfgNoXsrf jwt
309353 resp <- getWith opts (url port)
310354 resp ^? responseBody . _JSON `shouldBe` Just (length $ name user)
311355
312356 it " succeeds with no XSRF header or cookie for POST" $ \ port -> property $
313357 \ (user :: User ) number -> do
358+ #if MIN_VERSION_jose(0,12,0)
359+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
360+ #else
314361 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
362+ #endif
315363 opts <- addJwtToCookie cookieCfgNoXsrf jwt
316364 resp <- postWith opts (url port) $ toJSON (number :: Int )
317365 resp ^? responseBody . _JSON `shouldBe` Just number
@@ -360,7 +408,11 @@ jwtAuthSpec =
360408 jwt <-
361409 createJWT
362410 theKey
411+ #if MIN_VERSION_jose(0,12,0)
412+ (newJWSHeaderProtected HS256 )
413+ #else
363414 (newJWSHeader (() , HS256 ))
415+ #endif
364416 (claims (toJSON user) & claimAud ?~ Audience [" boo" ])
365417 opts <- addJwtToHeader (jwt <&> encodeCompact)
366418 getWith opts (url port) `shouldHTTPErrorWith` status401
@@ -370,7 +422,11 @@ jwtAuthSpec =
370422 jwt <-
371423 createJWT
372424 theKey
425+ #if MIN_VERSION_jose(0,12,0)
426+ (newJWSHeaderProtected HS256 )
427+ #else
373428 (newJWSHeader (() , HS256 ))
429+ #endif
374430 (claims (toJSON user) & claimAud ?~ Audience [" anythingElse" ])
375431 opts <- addJwtToHeader (jwt <&> encodeCompact)
376432 resp <- getWith opts (url port)
@@ -381,7 +437,11 @@ jwtAuthSpec =
381437 jwt <-
382438 createJWT
383439 theKey
440+ #if MIN_VERSION_jose(0,12,0)
441+ (newJWSHeaderProtected HS256 )
442+ #else
384443 (newJWSHeader (() , HS256 ))
444+ #endif
385445 (claims (toJSON user) & claimNbf ?~ NumericDate future)
386446 opts <- addJwtToHeader (jwt <&> encodeCompact)
387447 getWith opts (url port) `shouldHTTPErrorWith` status401
@@ -403,7 +463,11 @@ jwtAuthSpec =
403463 jwt <-
404464 createJWT
405465 theKey
466+ #if MIN_VERSION_jose(0,12,0)
467+ (newJWSHeaderProtected None )
468+ #else
406469 (newJWSHeader (() , None ))
470+ #endif
407471 (claims $ toJSON user)
408472 opts <- addJwtToHeader (jwt <&> encodeCompact)
409473 getWith opts (url port) `shouldHTTPErrorWith` status401
@@ -413,15 +477,23 @@ jwtAuthSpec =
413477 pendingWith " Need https://github.com/frasertweedale/hs-jose/issues/19"
414478
415479 it " fails if data is not valid JSON" $ \ port -> do
480+ #if MIN_VERSION_jose(0,12,0)
481+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims " {{" )
482+ #else
416483 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims " {{" )
484+ #endif
417485 opts <- addJwtToHeader (jwt <&> encodeCompact)
418486 getWith opts (url port) `shouldHTTPErrorWith` status401
419487
420488 it " suceeds as wreq's oauth2Bearer" $ \ port -> property $ \ (user :: User ) -> do
421489 jwt <-
422490 createJWT
423491 theKey
492+ #if MIN_VERSION_jose(0,12,0)
493+ (newJWSHeaderProtected HS256 )
494+ #else
424495 (newJWSHeader (() , HS256 ))
496+ #endif
425497 (claims $ toJSON user)
426498 resp <- case jwt <&> encodeCompact of
427499 Left (e :: Error ) -> fail $ show e
@@ -655,7 +727,11 @@ addJwtToHeader = \case
655727 pure $
656728 defaults & header " Authorization" .~ [" Bearer " <> BSL. toStrict v]
657729
730+ #if MIN_VERSION_jose(0,12,0)
731+ createJWT :: JWK -> JWSHeader Crypto.JWT. RequiredProtection -> ClaimsSet -> IO (Either Error Crypto.JWT. SignedJWT )
732+ #else
658733createJWT :: JWK -> JWSHeader () -> ClaimsSet -> IO (Either Error Crypto.JWT. SignedJWT )
734+ #endif
659735createJWT k a b = runJOSE $ signClaims k a b
660736
661737addJwtToCookie :: ToCompact a => CookieSettings -> Either Error a -> IO Options
0 commit comments