diff options
Diffstat (limited to 'src/Crypto')
-rw-r--r-- | src/Crypto/Macaroon.hs | 6 | ||||
-rw-r--r-- | src/Crypto/Macaroon/Internal.hs | 10 |
2 files changed, 14 insertions, 2 deletions
diff --git a/src/Crypto/Macaroon.hs b/src/Crypto/Macaroon.hs index 42e4a07..07043f7 100644 --- a/src/Crypto/Macaroon.hs +++ b/src/Crypto/Macaroon.hs | |||
@@ -25,6 +25,7 @@ module Crypto.Macaroon ( | |||
25 | , Caveat | 25 | , Caveat |
26 | , Key | 26 | , Key |
27 | , Location | 27 | , Location |
28 | , Sig | ||
28 | -- * Accessing functions | 29 | -- * Accessing functions |
29 | -- ** Macaroons | 30 | -- ** Macaroons |
30 | , location | 31 | , location |
@@ -65,18 +66,23 @@ create secret ident loc = MkMacaroon loc ident [] (toBytes (hmac derivedKey iden | |||
65 | where | 66 | where |
66 | derivedKey = toBytes (hmac "macaroons-key-generator" secret :: HMAC SHA256) | 67 | derivedKey = toBytes (hmac "macaroons-key-generator" secret :: HMAC SHA256) |
67 | 68 | ||
69 | -- | Caveat target location | ||
68 | caveatLoc :: Caveat -> Location | 70 | caveatLoc :: Caveat -> Location |
69 | caveatLoc = cl | 71 | caveatLoc = cl |
70 | 72 | ||
73 | -- | Caveat identifier | ||
71 | caveatId :: Caveat -> Key | 74 | caveatId :: Caveat -> Key |
72 | caveatId = cid | 75 | caveatId = cid |
73 | 76 | ||
77 | -- | Caveat verification identifier | ||
74 | caveatVId :: Caveat -> Key | 78 | caveatVId :: Caveat -> Key |
75 | caveatVId = vid | 79 | caveatVId = vid |
76 | 80 | ||
81 | -- | Inspect a macaroon's contents. For debugging purposes. | ||
77 | inspect :: Macaroon -> String | 82 | inspect :: Macaroon -> String |
78 | inspect = show | 83 | inspect = show |
79 | 84 | ||
85 | -- | Serialize a macaroon in an URL-safe Base64 encoding | ||
80 | serialize :: Macaroon -> BS.ByteString | 86 | serialize :: Macaroon -> BS.ByteString |
81 | serialize m = B8.filter (/= '=') . B64.encode $ packets | 87 | serialize m = B8.filter (/= '=') . B64.encode $ packets |
82 | where | 88 | where |
diff --git a/src/Crypto/Macaroon/Internal.hs b/src/Crypto/Macaroon/Internal.hs index 82ce0b4..810591c 100644 --- a/src/Crypto/Macaroon/Internal.hs +++ b/src/Crypto/Macaroon/Internal.hs | |||
@@ -27,9 +27,10 @@ import Data.List | |||
27 | -- |Type alias for Macaroons and Caveat keys and identifiers | 27 | -- |Type alias for Macaroons and Caveat keys and identifiers |
28 | type Key = BS.ByteString | 28 | type Key = BS.ByteString |
29 | 29 | ||
30 | -- |Type alias For Macaroons and Caveat locations | 30 | -- |Type alias for Macaroons and Caveat locations |
31 | type Location = BS.ByteString | 31 | type Location = BS.ByteString |
32 | 32 | ||
33 | -- |Type alias for Macaroons signatures | ||
33 | type Sig = BS.ByteString | 34 | type Sig = BS.ByteString |
34 | 35 | ||
35 | -- | Main structure of a macaroon | 36 | -- | Main structure of a macaroon |
@@ -43,6 +44,7 @@ data Macaroon = MkMacaroon { location :: Location | |||
43 | -- ^ Macaroon HMAC signature | 44 | -- ^ Macaroon HMAC signature |
44 | } deriving (Eq) | 45 | } deriving (Eq) |
45 | 46 | ||
47 | -- | show instance conforming to the @inspect@ "specification" | ||
46 | instance Show Macaroon where | 48 | instance Show Macaroon where |
47 | -- We use intercalate because unlines would add a trailing newline | 49 | -- We use intercalate because unlines would add a trailing newline |
48 | show (MkMacaroon l i c s) = intercalate "\n" [ | 50 | show (MkMacaroon l i c s) = intercalate "\n" [ |
@@ -52,6 +54,7 @@ instance Show Macaroon where | |||
52 | , "signature " ++ B8.unpack (hex s) | 54 | , "signature " ++ B8.unpack (hex s) |
53 | ] | 55 | ] |
54 | 56 | ||
57 | -- | NFData instance for use in the benchmark | ||
55 | instance NFData Macaroon where | 58 | instance NFData Macaroon where |
56 | rnf (MkMacaroon loc ident cavs sig) = rnf loc `seq` rnf ident `seq` rnf cavs `seq` rnf sig | 59 | rnf (MkMacaroon loc ident cavs sig) = rnf loc `seq` rnf ident `seq` rnf cavs `seq` rnf sig |
57 | 60 | ||
@@ -66,6 +69,7 @@ data Caveat = MkCaveat { cid :: Key | |||
66 | 69 | ||
67 | } deriving (Eq) | 70 | } deriving (Eq) |
68 | 71 | ||
72 | -- | show instance conforming to the @inspect@ "specification" | ||
69 | instance Show Caveat where | 73 | instance Show Caveat where |
70 | show (MkCaveat c v l) | v == BS.empty = "cid " ++ B8.unpack c | 74 | show (MkCaveat c v l) | v == BS.empty = "cid " ++ B8.unpack c |
71 | | otherwise = unlines [ "cid " ++ B8.unpack c | 75 | | otherwise = unlines [ "cid " ++ B8.unpack c |
@@ -74,10 +78,12 @@ instance Show Caveat where | |||
74 | ] | 78 | ] |
75 | 79 | ||
76 | 80 | ||
81 | -- | NFData instance for use in the benchmark | ||
77 | instance NFData Caveat where | 82 | instance NFData Caveat where |
78 | rnf (MkCaveat cid vid cl) = rnf cid `seq` rnf vid `seq` rnf cl | 83 | rnf (MkCaveat cid vid cl) = rnf cid `seq` rnf vid `seq` rnf cl |
79 | 84 | ||
80 | 85 | -- | Primitive to add a First or Third party caveat to a macaroon | |
86 | -- For internal use only | ||
81 | addCaveat :: Location | 87 | addCaveat :: Location |
82 | -> Key | 88 | -> Key |
83 | -> Key | 89 | -> Key |