aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Crypto/Macaroon
diff options
context:
space:
mode:
Diffstat (limited to 'src/Crypto/Macaroon')
-rw-r--r--src/Crypto/Macaroon/Binder.hs28
-rw-r--r--src/Crypto/Macaroon/Internal.hs88
2 files changed, 116 insertions, 0 deletions
diff --git a/src/Crypto/Macaroon/Binder.hs b/src/Crypto/Macaroon/Binder.hs
new file mode 100644
index 0000000..3ec3d67
--- /dev/null
+++ b/src/Crypto/Macaroon/Binder.hs
@@ -0,0 +1,28 @@
1{-|
2Module : Crypto.Macaroon.Binder
3Copyright : (c) 2015 Julien Tanguy
4License : BSD3
5
6Maintainer : julien.tanguy@jhome.fr
7Stability : experimental
8Portability : portable
9
10
11
12-}
13module Crypto.Macaroon.Binder where
14
15import Crypto.Hash
16import Data.Byteable
17import qualified Data.ByteString as BS
18
19import Crypto.Macaroon.Internal
20
21-- | Datatype for binding discharging and authorizing macaroons together
22newtype Binder = Binder { bind :: Macaroon -> Macaroon -> BS.ByteString }
23
24
25-- | Binder which concatenates the two signatures and hashes them
26hashSigs :: Binder
27hashSigs = Binder $ \m m' -> toBytes $ (HMAC . hash $ BS.append (toBytes $ signature m') (toBytes $ signature m) :: HMAC SHA256)
28
diff --git a/src/Crypto/Macaroon/Internal.hs b/src/Crypto/Macaroon/Internal.hs
new file mode 100644
index 0000000..fc50486
--- /dev/null
+++ b/src/Crypto/Macaroon/Internal.hs
@@ -0,0 +1,88 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-|
3Module : Crypto.Macaroon.Internal
4Copyright : (c) 2015 Julien Tanguy
5License : BSD3
6
7Maintainer : julien.tanguy@jhome.fr
8Stability : experimental
9Portability : portable
10
11
12Internal representation of a macaroon
13-}
14module Crypto.Macaroon.Internal where
15
16
17import Control.DeepSeq
18import Crypto.Cipher.AES
19import Crypto.Hash
20import Data.Byteable
21import qualified Data.ByteString as BS
22import qualified Data.ByteString.Base64 as B64
23import qualified Data.ByteString.Char8 as B8
24import Data.Char
25import Data.Hex
26import Data.Serialize
27import Data.Word
28
29-- |Type alias for Macaroons and Caveat keys and identifiers
30type Key = BS.ByteString
31
32-- |Type alias For Macaroons and Caveat locations
33type Location = BS.ByteString
34
35type Sig = BS.ByteString
36
37-- | Main structure of a macaroon
38data Macaroon = MkMacaroon { location :: Location
39 -- ^ Target location
40 , identifier :: Key
41 -- ^ Macaroon Identifier
42 , caveats :: [Caveat]
43 -- ^ List of caveats
44 , signature :: Sig
45 -- ^ Macaroon HMAC signature
46 } deriving (Eq)
47
48
49instance NFData Macaroon where
50 rnf (MkMacaroon loc ident cavs sig) = rnf loc `seq` rnf ident `seq` rnf cavs `seq` rnf sig
51
52
53-- | Caveat structure
54data Caveat = MkCaveat { cid :: Key
55 -- ^ Caveat identifier
56 , vid :: Key
57 -- ^ Caveat verification key identifier
58 , cl :: Location
59 -- ^ Caveat target location
60
61 } deriving (Eq)
62
63instance NFData Caveat where
64 rnf (MkCaveat cid vid cl) = rnf cid `seq` rnf vid `seq` rnf cl
65
66
67putPacket :: BS.ByteString -> BS.ByteString -> BS.ByteString
68putPacket key dat = BS.concat [
69 B8.map toLower . hex . encode $ (fromIntegral size :: Word16)
70 , key
71 , " "
72 , dat
73 , "\n"
74 ]
75 where
76 size = 4 + 2 + BS.length key + BS.length dat
77
78addCaveat :: Location
79 -> Key
80 -> Key
81 -> Macaroon
82 -> Macaroon
83addCaveat loc cid vid m = m { caveats = cavs ++ [cav'], signature = sig}
84 where
85 cavs = caveats m
86 cav' = MkCaveat cid vid loc
87 sig = toBytes $ (hmac (signature m) (BS.append vid cid) :: HMAC SHA256)
88