1 {-# LANGUAGE OverloadedStrings #-}
3 Module : Crypto.Macaroon.Internal
4 Copyright : (c) 2015 Julien Tanguy
7 Maintainer : julien.tanguy@jhome.fr
8 Stability : experimental
12 Internal representation of a macaroon
14 module Crypto.Macaroon.Internal where
17 import Control.DeepSeq
18 import Crypto.Cipher.AES
21 import qualified Data.ByteString as BS
22 import qualified Data.ByteString.Base64 as B64
23 import qualified Data.ByteString.Char8 as B8
29 -- |Type alias for Macaroons and Caveat keys and identifiers
30 type Key = BS.ByteString
32 -- |Type alias For Macaroons and Caveat locations
33 type Location = BS.ByteString
35 type Sig = BS.ByteString
37 -- | Main structure of a macaroon
38 data Macaroon = MkMacaroon { location :: Location
41 -- ^ Macaroon Identifier
45 -- ^ Macaroon HMAC signature
49 instance NFData Macaroon where
50 rnf (MkMacaroon loc ident cavs sig) = rnf loc `seq` rnf ident `seq` rnf cavs `seq` rnf sig
54 data Caveat = MkCaveat { cid :: Key
55 -- ^ Caveat identifier
57 -- ^ Caveat verification key identifier
59 -- ^ Caveat target location
63 instance NFData Caveat where
64 rnf (MkCaveat cid vid cl) = rnf cid `seq` rnf vid `seq` rnf cl
67 putPacket :: BS.ByteString -> BS.ByteString -> BS.ByteString
68 putPacket key dat = BS.concat [
69 B8.map toLower . hex . encode $ (fromIntegral size :: Word16)
76 size = 4 + 2 + BS.length key + BS.length dat
83 addCaveat loc cid vid m = m { caveats = cavs ++ [cav'], signature = sig}
86 cav' = MkCaveat cid vid loc
87 sig = toBytes $ (hmac (signature m) (BS.append vid cid) :: HMAC SHA256)