Skip to content

Commit ac6703f

Browse files
committed
Allow jose-0.12
1 parent 2cb8d77 commit ac6703f

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

servant-auth/servant-auth-client/servant-auth-client.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ test-suite spec
7474
, transformers >= 0.4.2.0 && < 0.7
7575
, wai >= 3.2.1.2 && < 3.3
7676
, warp >= 3.2.25 && < 3.5
77-
, jose >= 0.10 && < 0.12
77+
, jose >= 0.10 && < 0.13
7878
other-modules:
7979
Servant.Auth.ClientSpec
8080
default-language: Haskell2010

servant-auth/servant-auth-server/servant-auth-server.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ library
4141
, data-default >= 0.2 && < 0.9
4242
, entropy >= 0.4.1.3 && < 0.5
4343
, http-types >= 0.12.2 && < 0.13
44-
, jose >= 0.10 && < 0.12
44+
, jose >= 0.10 && < 0.13
4545
, lens >= 4.16.1 && < 5.4
4646
, memory >= 0.14.16 && < 0.19
4747
, monad-time >= 0.3.1.0 && < 0.5

servant-auth/servant-auth-server/src/Servant/Auth/Server/Internal/JWT.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE CPP #-}
12
module Servant.Auth.Server.Internal.JWT where
23

34
import Control.Lens
@@ -45,7 +46,11 @@ makeJWT v cfg expiry = Jose.runJOSE $ do
4546
ejwt <-
4647
Jose.signClaims
4748
(signingKey cfg)
49+
#if MIN_VERSION_jose(0,12,0)
50+
(Jose.newJWSHeaderProtected alg)
51+
#else
4852
(Jose.newJWSHeader ((), alg))
53+
#endif
4954
(addExp $ encodeJWT v)
5055

5156
pure $ Jose.encodeCompact ejwt
@@ -58,7 +63,12 @@ verifyJWT :: FromJWT a => JWTSettings -> BS.ByteString -> IO (Maybe a)
5863
verifyJWT jwtCfg input = do
5964
keys <- validationKeys jwtCfg
6065
verifiedJWT <- Jose.runJOSE $ do
61-
unverifiedJWT <- Jose.decodeCompact (BSL.fromStrict input)
66+
#if MIN_VERSION_jose(0,12,0)
67+
unverifiedJWT :: Jose.SignedJWTWithHeader Jose.JWSHeader <-
68+
#else
69+
unverifiedJWT :: Jose.SignedJWT <-
70+
#endif
71+
Jose.decodeCompact (BSL.fromStrict input)
6272
Jose.verifyClaims
6373
(jwtSettingsToJwtValidationSettings jwtCfg)
6474
keys

servant-auth/servant-auth-server/test/Servant/Auth/ServerSpec.hs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)
2630
import 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
658732
createJWT :: JWK -> JWSHeader () -> ClaimsSet -> IO (Either Error Crypto.JWT.SignedJWT)
733+
#endif
659734
createJWT k a b = runJOSE $ signClaims k a b
660735

661736
addJwtToCookie :: ToCompact a => CookieSettings -> Either Error a -> IO Options

servant-auth/servant-auth/servant-auth.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ library
3636
base >= 4.16.4.0 && < 4.22
3737
, containers >=0.6.5.1 && < 0.9
3838
, aeson >= 2.0 && < 3
39-
, jose >= 0.10 && < 0.12
39+
, jose >= 0.10 && < 0.13
4040
, lens >= 4.16.1 && < 5.4
4141
, servant >= 0.20.2 && < 0.21
4242
, text >= 1.2.3.0 && < 2.2

0 commit comments

Comments
 (0)