diff options
author | Julien Tanguy <julien.tanguy@jhome.fr> | 2015-05-16 12:51:22 +0200 |
---|---|---|
committer | Julien Tanguy <julien.tanguy@jhome.fr> | 2015-05-16 12:51:22 +0200 |
commit | 62576139b8dbf2cd0d3c04e927b9df2d0805a199 (patch) | |
tree | 3bf3c0104c4051aa94b8cce5b667b0b19a6c6d5b | |
parent | 90695615c54b5939d7286e777cb1b19a221616b9 (diff) | |
download | hmacaroons-62576139b8dbf2cd0d3c04e927b9df2d0805a199.tar.gz hmacaroons-62576139b8dbf2cd0d3c04e927b9df2d0805a199.tar.zst hmacaroons-62576139b8dbf2cd0d3c04e927b9df2d0805a199.zip |
Add Sig/cav verifier
[ci skip]
-rw-r--r-- | src/Crypto/Macaroon/Verifier.hs | 10 | ||||
-rw-r--r-- | test/Crypto/Macaroon/Verifier/Tests.hs | 18 |
2 files changed, 24 insertions, 4 deletions
diff --git a/src/Crypto/Macaroon/Verifier.hs b/src/Crypto/Macaroon/Verifier.hs index 4eedff5..02cb448 100644 --- a/src/Crypto/Macaroon/Verifier.hs +++ b/src/Crypto/Macaroon/Verifier.hs | |||
@@ -14,14 +14,14 @@ Portability : portable | |||
14 | -} | 14 | -} |
15 | module Crypto.Macaroon.Verifier ( | 15 | module Crypto.Macaroon.Verifier ( |
16 | Verified(..) | 16 | Verified(..) |
17 | , CaveatVerifier(..) | 17 | , CaveatVerifier |
18 | , (<???>) | 18 | , (<???>) |
19 | , verifyMacaroon | ||
19 | , verifySig | 20 | , verifySig |
20 | , verifyExact | 21 | , verifyExact |
21 | , verifyFun | 22 | , verifyFun |
22 | , verifyCavs | ||
23 | -- , module Data.Attoparsec.ByteString | ||
24 | , module Data.Attoparsec.ByteString.Char8 | 23 | , module Data.Attoparsec.ByteString.Char8 |
24 | , verifyCavs | ||
25 | ) where | 25 | ) where |
26 | 26 | ||
27 | 27 | ||
@@ -66,6 +66,10 @@ verifySig k m = bool Failed Ok $ | |||
66 | hash s c = toBytes (hmac s (vid c `BS.append` cid c) :: HMAC SHA256) | 66 | hash s c = toBytes (hmac s (vid c `BS.append` cid c) :: HMAC SHA256) |
67 | derivedKey = toBytes (hmac "macaroons-key-generator" k :: HMAC SHA256) | 67 | derivedKey = toBytes (hmac "macaroons-key-generator" k :: HMAC SHA256) |
68 | 68 | ||
69 | verifyMacaroon :: Key -> [CaveatVerifier] -> Macaroon -> Verified | ||
70 | verifyMacaroon secret verifiers m = verifySig secret m `mappend` verifyCavs verifiers m | ||
71 | |||
72 | |||
69 | verifyCavs :: [CaveatVerifier] -> Macaroon -> Verified | 73 | verifyCavs :: [CaveatVerifier] -> Macaroon -> Verified |
70 | verifyCavs verifiers m = foldMap (\c -> fromMaybe Failed $ foldMap (($ c) . vFun) verifiers) (caveats m) | 74 | verifyCavs verifiers m = foldMap (\c -> fromMaybe Failed $ foldMap (($ c) . vFun) verifiers) (caveats m) |
71 | 75 | ||
diff --git a/test/Crypto/Macaroon/Verifier/Tests.hs b/test/Crypto/Macaroon/Verifier/Tests.hs index 5f09bca..101fa26 100644 --- a/test/Crypto/Macaroon/Verifier/Tests.hs +++ b/test/Crypto/Macaroon/Verifier/Tests.hs | |||
@@ -61,7 +61,7 @@ allvs = [exTC, exTZ, exV42, exV43, funTCPre, funTV43lte] | |||
61 | sigs = testProperty "Signatures" $ \sm -> verifySig (secret sm) (macaroon sm) == Ok | 61 | sigs = testProperty "Signatures" $ \sm -> verifySig (secret sm) (macaroon sm) == Ok |
62 | 62 | ||
63 | firstParty = testGroup "First party caveats" [ | 63 | firstParty = testGroup "First party caveats" [ |
64 | testGroup "Pure verifiers" [ | 64 | testGroup "Pure verifiers" [ |
65 | testProperty "Zero caveat" $ | 65 | testProperty "Zero caveat" $ |
66 | forAll (sublistOf allvs) (\vs -> Ok == verifyCavs vs m) | 66 | forAll (sublistOf allvs) (\vs -> Ok == verifyCavs vs m) |
67 | , testProperty "One caveat" $ | 67 | , testProperty "One caveat" $ |
@@ -77,4 +77,20 @@ firstParty = testGroup "First party caveats" [ | |||
77 | , Failed === verifyCavs vs m3 | 77 | , Failed === verifyCavs vs m3 |
78 | ]) | 78 | ]) |
79 | ] | 79 | ] |
80 | , testGroup "Pure verifiers with sig" [ | ||
81 | testProperty "Zero caveat" $ | ||
82 | forAll (sublistOf allvs) (\vs -> Ok == verifyMacaroon sec vs m) | ||
83 | , testProperty "One caveat" $ | ||
84 | forAll (sublistOf allvs) (\vs -> disjoin [ | ||
85 | Ok == verifyMacaroon sec vs m2 .&&. any (`elem` vs) [exTC,funTCPre] .&&. (exTZ `notElem` vs) | ||
86 | , Failed === verifyMacaroon sec vs m2 | ||
87 | ]) | ||
88 | , testProperty "Two Exact" $ | ||
89 | forAll (sublistOf allvs) (\vs -> disjoin [ | ||
90 | Ok == verifyMacaroon sec vs m3 .&&. | ||
91 | any (`elem` vs) [exTC,funTCPre] .&&. (exTZ `notElem` vs) .&&. | ||
92 | any (`elem` vs) [exV42,funTV43lte] .&&. (exV43 `notElem` vs) | ||
93 | , Failed === verifyMacaroon sec vs m3 | ||
94 | ]) | ||
95 | ] | ||
80 | ] | 96 | ] |