summaryrefslogtreecommitdiff
path: root/macaroonbakery/json_serializer.py
diff options
context:
space:
mode:
Diffstat (limited to 'macaroonbakery/json_serializer.py')
-rw-r--r--macaroonbakery/json_serializer.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/macaroonbakery/json_serializer.py b/macaroonbakery/json_serializer.py
new file mode 100644
index 0000000..2faea00
--- /dev/null
+++ b/macaroonbakery/json_serializer.py
@@ -0,0 +1,75 @@
+# Copyright 2017 Canonical Ltd.
+# Licensed under the LGPLv3, see LICENCE file for details.
+
+import base64
+import json
+
+from pymacaroons.macaroon import Macaroon
+from pymacaroons.caveat import Caveat
+
+
+class JsonSerializer(object):
+ '''Serializer used to produce JSON macaroon format v1.
+ '''
+ def serialize(self, macaroon):
+ '''Serialize the macaroon in JSON format v1.
+
+ @param macaroon the macaroon to serialize.
+ @return JSON macaroon.
+ '''
+ serialized = {
+ 'identifier': macaroon.identifier,
+ 'signature': macaroon.signature
+ }
+ if macaroon.location:
+ serialized['location'] = macaroon.location
+ if macaroon.caveats:
+ serialized['caveats'] = [
+ caveat_v1_to_dict(caveat) for caveat in macaroon.caveats
+ ]
+ return json.dumps(serialized)
+
+ def deserialize(self, serialized):
+ '''Deserialize a JSON macaroon v1.
+
+ @param serialized the macaroon in JSON format v1.
+ @return the macaroon object.
+ '''
+ from macaroonbakery import utils
+ caveats = []
+ deserialized = json.loads(serialized)
+
+ for c in deserialized['caveats']:
+ caveat = Caveat(
+ caveat_id=c['cid'],
+ verification_key_id=(
+ utils.raw_urlsafe_b64decode(c['vid']) if c.get('vid')
+ else None
+ ),
+ location=(
+ c['cl'] if c.get('cl') else None
+ )
+ )
+ caveats.append(caveat)
+
+ return Macaroon(
+ location=deserialized['location'],
+ identifier=deserialized['identifier'],
+ caveats=caveats,
+ signature=deserialized['signature']
+ )
+
+
+def caveat_v1_to_dict(c):
+ ''' Return a caveat as a dictionary for export as the JSON
+ macaroon v1 format
+ '''
+ serialized = {}
+ if len(c.caveat_id) > 0:
+ serialized['cid'] = c.caveat_id
+ if c.verification_key_id:
+ serialized['vid'] = base64.urlsafe_b64encode(
+ c.verification_key_id).decode('ascii')
+ if c.location:
+ serialized['cl'] = c.location
+ return serialized