Skip to content

Commit f07422b

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

File tree

5 files changed

+92
-4
lines changed

5 files changed

+92
-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: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE CPP #-}
2+
13
module Servant.Auth.Server.Internal.JWT where
24

35
import Control.Lens
@@ -30,6 +32,7 @@ jwtAuthCheck jwtSettings = do
3032
verifiedJWT <- liftIO $ verifyJWT jwtSettings token
3133
maybe mzero pure verifiedJWT
3234

35+
{- FOURMOLU_DISABLE -}
3336
-- | Creates a JWT containing the specified data. The data is stored in the
3437
-- @dat@ claim. The 'Maybe UTCTime' argument indicates the time at which the
3538
-- token expires.
@@ -45,7 +48,11 @@ makeJWT v cfg expiry = Jose.runJOSE $ do
4548
ejwt <-
4649
Jose.signClaims
4750
(signingKey cfg)
51+
#if MIN_VERSION_jose(0,12,0)
52+
(Jose.newJWSHeaderProtected alg)
53+
#else
4854
(Jose.newJWSHeader ((), alg))
55+
#endif
4956
(addExp $ encodeJWT v)
5057

5158
pure $ Jose.encodeCompact ejwt
@@ -58,7 +65,12 @@ verifyJWT :: FromJWT a => JWTSettings -> BS.ByteString -> IO (Maybe a)
5865
verifyJWT jwtCfg input = do
5966
keys <- validationKeys jwtCfg
6067
verifiedJWT <- Jose.runJOSE $ do
61-
unverifiedJWT <- Jose.decodeCompact (BSL.fromStrict input)
68+
#if MIN_VERSION_jose(0,12,0)
69+
unverifiedJWT :: Jose.SignedJWTWithHeader Jose.JWSHeader <-
70+
#else
71+
unverifiedJWT :: Jose.SignedJWT <-
72+
#endif
73+
Jose.decodeCompact (BSL.fromStrict input)
6274
Jose.verifyClaims
6375
(jwtSettingsToJwtValidationSettings jwtCfg)
6476
keys

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Servant.Auth.ServerSpec (spec) where
1111
import Control.Lens
1212
import Control.Monad (forM_)
1313
import Control.Monad.IO.Class (liftIO)
14+
{- FOURMOLU_DISABLE -}
1415
import 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
)
2631
import 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
658733
createJWT :: JWK -> JWSHeader () -> ClaimsSet -> IO (Either Error Crypto.JWT.SignedJWT)
734+
#endif
659735
createJWT k a b = runJOSE $ signClaims k a b
660736

661737
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)