From 857f2f3ba8ba2de9ab65ea3c66eafb718fe4e1a6 Mon Sep 17 00:00:00 2001 From: Julien Tanguy Date: Fri, 15 May 2015 23:02:22 +0200 Subject: Add generalized function verifier --- src/Crypto/Macaroon/Verifier.hs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src') 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 ( Verified(..) , verifySig , verifyExact + , verifyFun , verifyCavs -- , module Data.Attoparsec.ByteString , module Data.Attoparsec.ByteString.Char8 @@ -55,10 +56,13 @@ verifySig k m = bool Failed Ok $ verifyCavs :: [Caveat -> Maybe Verified] -> Macaroon -> Verified verifyCavs verifiers m = mconcat $ map (\c -> mconcat . catMaybes $ map ($ c) verifiers) (caveats m) -verifyExact :: (Show a, Eq a) => Key -> a -> Parser a -> Caveat -> Maybe Verified -verifyExact key expected parser cav = if key `BS.isPrefixOf` cid cav then +verifyExact :: (Eq a) => Key -> a -> Parser a -> Caveat -> Maybe Verified +verifyExact k expected = verifyFun k (expected ==) + +verifyFun :: Key -> (a -> Bool) -> Parser a -> Caveat -> Maybe Verified +verifyFun key f parser cav = if key `BS.isPrefixOf` cid cav then case parseOnly kvparser (cid cav) of - Right v -> verify <$> Just v + Right v -> (bool Failed Ok . f) <$> Just v Left _ -> Just Failed else Nothing where @@ -67,7 +71,4 @@ verifyExact key expected parser cav = if key `BS.isPrefixOf` cid cav then skipSpace string "=" skipSpace - parser - - -- *> skipSpace *> string "=" *> skipSpace *> parser <* endOfInput - verify a = bool Failed Ok (a == expected) + parser <* endOfInput -- cgit v1.2.3