aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJulien Tanguy <julien.tanguy@jhome.fr>2015-05-15 23:02:22 +0200
committerJulien Tanguy <julien.tanguy@jhome.fr>2015-05-15 23:10:16 +0200
commit857f2f3ba8ba2de9ab65ea3c66eafb718fe4e1a6 (patch)
treeaeb04d4d242ff510236821b51ef4884d8c3930ed
parent6f3c0dca02c1069115bc2592c439970d2af07cc5 (diff)
downloadhmacaroons-857f2f3ba8ba2de9ab65ea3c66eafb718fe4e1a6.tar.gz
hmacaroons-857f2f3ba8ba2de9ab65ea3c66eafb718fe4e1a6.tar.zst
hmacaroons-857f2f3ba8ba2de9ab65ea3c66eafb718fe4e1a6.zip
Add generalized function verifier
-rw-r--r--src/Crypto/Macaroon/Verifier.hs15
-rw-r--r--test/Crypto/Macaroon/Verifier/Tests.hs30
2 files changed, 26 insertions, 19 deletions
diff --git a/src/Crypto/Macaroon/Verifier.hs b/src/Crypto/Macaroon/Verifier.hs
index cb64c9d..012d156 100644
--- a/src/Crypto/Macaroon/Verifier.hs
+++ b/src/Crypto/Macaroon/Verifier.hs
@@ -16,6 +16,7 @@ module Crypto.Macaroon.Verifier (
16 Verified(..) 16 Verified(..)
17 , verifySig 17 , verifySig
18 , verifyExact 18 , verifyExact
19 , verifyFun
19 , verifyCavs 20 , verifyCavs
20 -- , module Data.Attoparsec.ByteString 21 -- , module Data.Attoparsec.ByteString
21 , module Data.Attoparsec.ByteString.Char8 22 , module Data.Attoparsec.ByteString.Char8
@@ -55,10 +56,13 @@ verifySig k m = bool Failed Ok $
55verifyCavs :: [Caveat -> Maybe Verified] -> Macaroon -> Verified 56verifyCavs :: [Caveat -> Maybe Verified] -> Macaroon -> Verified
56verifyCavs verifiers m = mconcat $ map (\c -> mconcat . catMaybes $ map ($ c) verifiers) (caveats m) 57verifyCavs verifiers m = mconcat $ map (\c -> mconcat . catMaybes $ map ($ c) verifiers) (caveats m)
57 58
58verifyExact :: (Show a, Eq a) => Key -> a -> Parser a -> Caveat -> Maybe Verified 59verifyExact :: (Eq a) => Key -> a -> Parser a -> Caveat -> Maybe Verified
59verifyExact key expected parser cav = if key `BS.isPrefixOf` cid cav then 60verifyExact k expected = verifyFun k (expected ==)
61
62verifyFun :: Key -> (a -> Bool) -> Parser a -> Caveat -> Maybe Verified
63verifyFun key f parser cav = if key `BS.isPrefixOf` cid cav then
60 case parseOnly kvparser (cid cav) of 64 case parseOnly kvparser (cid cav) of
61 Right v -> verify <$> Just v 65 Right v -> (bool Failed Ok . f) <$> Just v
62 Left _ -> Just Failed 66 Left _ -> Just Failed
63 else Nothing 67 else Nothing
64 where 68 where
@@ -67,7 +71,4 @@ verifyExact key expected parser cav = if key `BS.isPrefixOf` cid cav then
67 skipSpace 71 skipSpace
68 string "=" 72 string "="
69 skipSpace 73 skipSpace
70 parser 74 parser <* endOfInput
71
72 -- *> skipSpace *> string "=" *> skipSpace *> parser <* endOfInput
73 verify a = bool Failed Ok (a == expected)
diff --git a/test/Crypto/Macaroon/Verifier/Tests.hs b/test/Crypto/Macaroon/Verifier/Tests.hs
index 37d0230..54c8db1 100644
--- a/test/Crypto/Macaroon/Verifier/Tests.hs
+++ b/test/Crypto/Macaroon/Verifier/Tests.hs
@@ -12,6 +12,7 @@ This test suite is based on the pymacaroons test suite:
12module Crypto.Macaroon.Verifier.Tests where 12module Crypto.Macaroon.Verifier.Tests where
13 13
14 14
15import Data.List
15import qualified Data.ByteString.Char8 as B8 16import qualified Data.ByteString.Char8 as B8
16import Test.Tasty 17import Test.Tasty
17import Test.Tasty.HUnit 18import Test.Tasty.HUnit
@@ -50,6 +51,9 @@ exVerifiers = [ verifyExact "test" "caveat" (many' letter_ascii)
50exVerifiers' = [ verifyExact "test" "caveat" (many' letter_ascii) 51exVerifiers' = [ verifyExact "test" "caveat" (many' letter_ascii)
51 , verifyExact "value" 43 decimal 52 , verifyExact "value" 43 decimal
52 ] 53 ]
54funVerifiers = [ verifyFun "test" ("cav" `isPrefixOf`) (many' letter_ascii)
55 , verifyFun "value" (<= 43) decimal
56 ]
53 57
54{- 58{-
55 - Tests 59 - Tests
@@ -73,15 +77,17 @@ one = testCase "Macaroon with one caveat" $
73two = testCase "Macaroon with two caveats" $ 77two = testCase "Macaroon with two caveats" $
74 Ok @=? verifySig sec m3 78 Ok @=? verifySig sec m3
75 79
76exactCavs = testGroup "Exact Caveats" [ zero', one', two' , one'', two''] 80exactCavs = testGroup "Exact Caveats" [
77 81 testGroup "Ignoring non-relevant" [
78zero' = testCase "Zero caveat win" $ 82 testCase "Zero caveat" $ Ok @=? verifyCavs exVerifiers m
79 Ok @=? verifyCavs exVerifiers m 83 , testCase "One caveat" $ Ok @=? verifyCavs exVerifiers' m2
80one' = testCase "One caveat win" $ 84 ]
81 Ok @=? verifyCavs exVerifiers m2 85 , testCase "One caveat win" $ Ok @=? verifyCavs exVerifiers m2
82one'' = testCase "Ignoring non-relevant" $ 86 , testCase "Two caveat win" $ Ok @=? verifyCavs exVerifiers m3
83 Ok @=? verifyCavs exVerifiers' m2 87 , testCase "Two caveat fail" $ Failed @=? verifyCavs exVerifiers' m3
84two' = testCase "Two caveat win" $ 88 ]
85 Ok @=? verifyCavs exVerifiers m3 89
86two'' = testCase "Two caveat fail" $ 90funCavs = testGroup "Function Caveats" [
87 Failed @=? verifyCavs exVerifiers' m3 91 testCase "One caveat win" $ Ok @=? verifyCavs funVerifiers m2
92 , testCase "Two caveat win" $ Ok @=? verifyCavs funVerifiers m3
93 ]