@@ -20,14 +20,21 @@ import Crypto.JOSE
2020 , ToCompact
2121 , encodeCompact
2222 , genJWK
23+ #if MIN_VERSION_jose(0,12,0)
24+ , newJWSHeaderProtected
25+ #else
2326 , newJWSHeader
27+ #endif
2428 , runJOSE
2529 )
2630import Crypto.JWT
2731 ( Audience (.. )
2832 , ClaimsSet
2933 , NumericDate (NumericDate )
3034 , SignedJWT
35+ #if MIN_VERSION_jose(0,12,0)
36+ , RequiredProtection
37+ #endif
3138 , claimAud
3239 , claimNbf
3340 , emptyClaimsSet
@@ -144,7 +151,11 @@ authSpec =
144151 jwt <-
145152 createJWT
146153 theKey
154+ #if MIN_VERSION_jose(0,12,0)
155+ (newJWSHeaderProtected HS256 )
156+ #else
147157 (newJWSHeader (() , HS256 ))
158+ #endif
148159 (claims $ toJSON user)
149160 opts' <- addJwtToCookie cookieCfg jwt
150161 let opts =
@@ -167,7 +178,11 @@ authSpec =
167178 jwt <-
168179 createJWT
169180 theKey
181+ #if MIN_VERSION_jose(0,12,0)
182+ (newJWSHeaderProtected HS256 )
183+ #else
170184 (newJWSHeader (() , HS256 ))
185+ #endif
171186 (claims $ toJSON user)
172187 opts' <- addJwtToCookie cookieCfg jwt
173188 let opts =
@@ -185,7 +200,11 @@ authSpec =
185200 jwt <-
186201 createJWT
187202 theKey
203+ #if MIN_VERSION_jose(0,12,0)
204+ (newJWSHeaderProtected HS256 )
205+ #else
188206 (newJWSHeader (() , HS256 ))
207+ #endif
189208 (claims $ toJSON user)
190209 opts' <- addJwtToCookie cookieCfg jwt
191210 let opts =
@@ -211,7 +230,11 @@ cookieAuthSpec =
211230 aroundAll (testWithApplication . pure $ app cookieOnlyApi) $ do
212231 it " fails if XSRF header and cookie don't match" $ \ port -> property $
213232 \ (user :: User ) -> do
233+ #if MIN_VERSION_jose(0,12,0)
234+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
235+ #else
214236 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
237+ #endif
215238 opts' <- addJwtToCookie cookieCfg jwt
216239 let opts =
217240 addCookie
@@ -221,7 +244,11 @@ cookieAuthSpec =
221244
222245 it " fails with no XSRF header or cookie" $ \ port -> property $
223246 \ (user :: User ) -> do
247+ #if MIN_VERSION_jose(0,12,0)
248+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
249+ #else
224250 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
251+ #endif
225252 opts' <- addJwtToCookie cookieCfg jwt
226253 let opts = opts' & checkResponse ?~ mempty
227254 resp <- getWith opts (url port)
@@ -239,7 +266,11 @@ cookieAuthSpec =
239266
240267 it " succeeds if XSRF header and cookie match, and JWT is valid" $ \ port -> property $
241268 \ (user :: User ) -> do
269+ #if MIN_VERSION_jose(0,12,0)
270+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
271+ #else
242272 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
273+ #endif
243274 opts' <- addJwtToCookie cookieCfg jwt
244275 let opts =
245276 addCookie
@@ -286,14 +317,22 @@ cookieAuthSpec =
286317 aroundAll (testWithApplication . pure $ appWithCookie cookieOnlyApi cookieCfgNoXsrfGet) $ do
287318 it " succeeds with no XSRF header or cookie for GET" $ \ port -> property $
288319 \ (user :: User ) -> do
320+ #if MIN_VERSION_jose(0,12,0)
321+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
322+ #else
289323 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
324+ #endif
290325 opts <- addJwtToCookie cookieCfgNoXsrfGet jwt
291326 resp <- getWith opts (url port)
292327 resp ^? responseBody . _JSON `shouldBe` Just (length $ name user)
293328
294329 it " fails with no XSRF header or cookie for POST" $ \ port -> property $
295330 \ (user :: User ) number -> do
331+ #if MIN_VERSION_jose(0,12,0)
332+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
333+ #else
296334 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
335+ #endif
297336 opts <- addJwtToCookie cookieCfgNoXsrfGet jwt
298337 postWith opts (url port) (toJSON (number :: Int )) `shouldHTTPErrorWith` status401
299338
@@ -304,14 +343,22 @@ cookieAuthSpec =
304343 aroundAll (testWithApplication . pure $ appWithCookie cookieOnlyApi cookieCfgNoXsrf) $ do
305344 it " succeeds with no XSRF header or cookie for GET" $ \ port -> property $
306345 \ (user :: User ) -> do
346+ #if MIN_VERSION_jose(0,12,0)
347+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
348+ #else
307349 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
350+ #endif
308351 opts <- addJwtToCookie cookieCfgNoXsrf jwt
309352 resp <- getWith opts (url port)
310353 resp ^? responseBody . _JSON `shouldBe` Just (length $ name user)
311354
312355 it " succeeds with no XSRF header or cookie for POST" $ \ port -> property $
313356 \ (user :: User ) number -> do
357+ #if MIN_VERSION_jose(0,12,0)
358+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims $ toJSON user)
359+ #else
314360 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims $ toJSON user)
361+ #endif
315362 opts <- addJwtToCookie cookieCfgNoXsrf jwt
316363 resp <- postWith opts (url port) $ toJSON (number :: Int )
317364 resp ^? responseBody . _JSON `shouldBe` Just number
@@ -360,7 +407,11 @@ jwtAuthSpec =
360407 jwt <-
361408 createJWT
362409 theKey
410+ #if MIN_VERSION_jose(0,12,0)
411+ (newJWSHeaderProtected HS256 )
412+ #else
363413 (newJWSHeader (() , HS256 ))
414+ #endif
364415 (claims (toJSON user) & claimAud ?~ Audience [" boo" ])
365416 opts <- addJwtToHeader (jwt <&> encodeCompact)
366417 getWith opts (url port) `shouldHTTPErrorWith` status401
@@ -370,7 +421,11 @@ jwtAuthSpec =
370421 jwt <-
371422 createJWT
372423 theKey
424+ #if MIN_VERSION_jose(0,12,0)
425+ (newJWSHeaderProtected HS256 )
426+ #else
373427 (newJWSHeader (() , HS256 ))
428+ #endif
374429 (claims (toJSON user) & claimAud ?~ Audience [" anythingElse" ])
375430 opts <- addJwtToHeader (jwt <&> encodeCompact)
376431 resp <- getWith opts (url port)
@@ -381,7 +436,11 @@ jwtAuthSpec =
381436 jwt <-
382437 createJWT
383438 theKey
439+ #if MIN_VERSION_jose(0,12,0)
440+ (newJWSHeaderProtected HS256 )
441+ #else
384442 (newJWSHeader (() , HS256 ))
443+ #endif
385444 (claims (toJSON user) & claimNbf ?~ NumericDate future)
386445 opts <- addJwtToHeader (jwt <&> encodeCompact)
387446 getWith opts (url port) `shouldHTTPErrorWith` status401
@@ -403,7 +462,11 @@ jwtAuthSpec =
403462 jwt <-
404463 createJWT
405464 theKey
465+ #if MIN_VERSION_jose(0,12,0)
466+ (newJWSHeaderProtected None )
467+ #else
406468 (newJWSHeader (() , None ))
469+ #endif
407470 (claims $ toJSON user)
408471 opts <- addJwtToHeader (jwt <&> encodeCompact)
409472 getWith opts (url port) `shouldHTTPErrorWith` status401
@@ -413,15 +476,23 @@ jwtAuthSpec =
413476 pendingWith " Need https://github.com/frasertweedale/hs-jose/issues/19"
414477
415478 it " fails if data is not valid JSON" $ \ port -> do
479+ #if MIN_VERSION_jose(0,12,0)
480+ jwt <- createJWT theKey (newJWSHeaderProtected HS256 ) (claims " {{" )
481+ #else
416482 jwt <- createJWT theKey (newJWSHeader (() , HS256 )) (claims " {{" )
483+ #endif
417484 opts <- addJwtToHeader (jwt <&> encodeCompact)
418485 getWith opts (url port) `shouldHTTPErrorWith` status401
419486
420487 it " suceeds as wreq's oauth2Bearer" $ \ port -> property $ \ (user :: User ) -> do
421488 jwt <-
422489 createJWT
423490 theKey
491+ #if MIN_VERSION_jose(0,12,0)
492+ (newJWSHeaderProtected HS256 )
493+ #else
424494 (newJWSHeader (() , HS256 ))
495+ #endif
425496 (claims $ toJSON user)
426497 resp <- case jwt <&> encodeCompact of
427498 Left (e :: Error ) -> fail $ show e
@@ -655,7 +726,11 @@ addJwtToHeader = \case
655726 pure $
656727 defaults & header " Authorization" .~ [" Bearer " <> BSL. toStrict v]
657728
729+ #if MIN_VERSION_jose(0,12,0)
730+ createJWT :: JWK -> JWSHeader Crypto.JWT. RequiredProtection -> ClaimsSet -> IO (Either Error Crypto.JWT. SignedJWT )
731+ #else
658732createJWT :: JWK -> JWSHeader () -> ClaimsSet -> IO (Either Error Crypto.JWT. SignedJWT )
733+ #endif
659734createJWT k a b = runJOSE $ signClaims k a b
660735
661736addJwtToCookie :: ToCompact a => CookieSettings -> Either Error a -> IO Options
0 commit comments