From 79ff2842fa477ee0693ea167c0a74cd7cf080d27 Mon Sep 17 00:00:00 2001 From: Colin Watson Date: Tue, 31 Oct 2017 10:34:41 +0000 Subject: Import py-macaroon-bakery_0.0.3.orig.tar.gz --- macaroonbakery/json_serializer.py | 75 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 macaroonbakery/json_serializer.py (limited to 'macaroonbakery/json_serializer.py') 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 -- cgit v1.2.3