aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJulien Tanguy <julien.tanguy@jhome.fr>2015-05-15 22:31:05 +0200
committerJulien Tanguy <julien.tanguy@jhome.fr>2015-05-15 23:10:16 +0200
commit6f3c0dca02c1069115bc2592c439970d2af07cc5 (patch)
tree90ebecf85eec1b041de347c16b931f3cba7ccc2b /src
parent2ba8d1c3034fb99723ba42c066b56ed6b0691a2f (diff)
downloadhmacaroons-6f3c0dca02c1069115bc2592c439970d2af07cc5.tar.gz
hmacaroons-6f3c0dca02c1069115bc2592c439970d2af07cc5.tar.zst
hmacaroons-6f3c0dca02c1069115bc2592c439970d2af07cc5.zip
Add basic exact caveat verifiers
Need more tests Touching #2 Verify first party caveats
Diffstat (limited to 'src')
-rw-r--r--src/Crypto/Macaroon/Verifier.hs47
1 files changed, 43 insertions, 4 deletions
diff --git a/src/Crypto/Macaroon/Verifier.hs b/src/Crypto/Macaroon/Verifier.hs
index e257f5f..cb64c9d 100644
--- a/src/Crypto/Macaroon/Verifier.hs
+++ b/src/Crypto/Macaroon/Verifier.hs
@@ -1,4 +1,5 @@
1{-# LANGUAGE OverloadedStrings #-} 1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE RankNTypes #-}
2{-| 3{-|
3Module : Crypto.Macaroon.Verifier 4Module : Crypto.Macaroon.Verifier
4Copyright : (c) 2015 Julien Tanguy 5Copyright : (c) 2015 Julien Tanguy
@@ -11,7 +12,14 @@ Portability : portable
11 12
12 13
13-} 14-}
14module Crypto.Macaroon.Verifier where 15module Crypto.Macaroon.Verifier (
16 Verified(..)
17 , verifySig
18 , verifyExact
19 , verifyCavs
20 -- , module Data.Attoparsec.ByteString
21 , module Data.Attoparsec.ByteString.Char8
22) where
15 23
16 24
17import Crypto.Hash 25import Crypto.Hash
@@ -19,16 +27,47 @@ import Data.Bool
19import qualified Data.ByteString as BS 27import qualified Data.ByteString as BS
20import Data.Byteable 28import Data.Byteable
21import Data.Foldable 29import Data.Foldable
30import Data.Maybe
31import Data.Attoparsec.ByteString
32import Data.Attoparsec.ByteString.Char8
22 33
23import Crypto.Macaroon.Internal 34import Crypto.Macaroon.Internal
24 35
25 36
26-- | Opaque datatype for now. Might need more explicit errors 37-- | Opaque datatype for now. Might need more explicit errors
27data VResult = VSuccess | VFailure deriving (Show,Eq) 38data Verified = Ok | Failed deriving (Show,Eq)
28 39
29verifySig :: Key -> Macaroon -> VResult 40instance Monoid Verified where
30verifySig k m = bool VFailure VSuccess $ 41 mempty = Ok
42 mappend Ok Ok = Ok
43 mappend _ _ = Failed
44
45
46type CaveatVerifier = Caveat -> Maybe Verified
47
48verifySig :: Key -> Macaroon -> Verified
49verifySig k m = bool Failed Ok $
31 signature m == foldl' hash (toBytes (hmac derivedKey (identifier m) :: HMAC SHA256)) (caveats m) 50 signature m == foldl' hash (toBytes (hmac derivedKey (identifier m) :: HMAC SHA256)) (caveats m)
32 where 51 where
33 hash s c = toBytes (hmac s (vid c `BS.append` cid c) :: HMAC SHA256) 52 hash s c = toBytes (hmac s (vid c `BS.append` cid c) :: HMAC SHA256)
34 derivedKey = toBytes (hmac "macaroons-key-generator" k :: HMAC SHA256) 53 derivedKey = toBytes (hmac "macaroons-key-generator" k :: HMAC SHA256)
54
55verifyCavs :: [Caveat -> Maybe Verified] -> Macaroon -> Verified
56verifyCavs verifiers m = mconcat $ map (\c -> mconcat . catMaybes $ map ($ c) verifiers) (caveats m)
57
58verifyExact :: (Show a, Eq a) => Key -> a -> Parser a -> Caveat -> Maybe Verified
59verifyExact key expected parser cav = if key `BS.isPrefixOf` cid cav then
60 case parseOnly kvparser (cid cav) of
61 Right v -> verify <$> Just v
62 Left _ -> Just Failed
63 else Nothing
64 where
65 kvparser = do
66 key <- string key
67 skipSpace
68 string "="
69 skipSpace
70 parser
71
72 -- *> skipSpace *> string "=" *> skipSpace *> parser <* endOfInput
73 verify a = bool Failed Ok (a == expected)