def to_dict(self):
     '''Return a dict representation of the macaroon data in JSON format.
     @return a dict
     '''
     if self.version < VERSION_3:
         if len(self._caveat_data) > 0:
             raise ValueError('cannot serialize pre-version3 macaroon with '
                              'external caveat data')
         return json.loads(
             self._macaroon.serialize(json_serializer.JsonSerializer()))
     serialized = {
         'm':
         json.loads(
             self._macaroon.serialize(json_serializer.JsonSerializer())),
         'v':
         self._version,
     }
     if self._namespace is not None:
         serialized['ns'] = self._namespace.serialize_text().decode('utf-8')
     caveat_data = {}
     for id in self._caveat_data:
         key = base64.b64encode(id).decode('utf-8')
         value = base64.b64encode(self._caveat_data[id]).decode('utf-8')
         caveat_data[key] = value
     if len(caveat_data) > 0:
         serialized['cdata'] = caveat_data
     return serialized
Beispiel #2
0
def macaroon_to_json_string(macaroon):
    '''Serialize macaroon object to a JSON-encoded string.

    @param macaroon object to be serialized.
    @return a string serialization form of the macaroon.
    '''
    return macaroon.serialize(json_serializer.JsonSerializer())
Beispiel #3
0
def serialize_macaroon_string(macaroon):
    '''Serialize macaroon object to string.

    @param macaroon object to be serialized.
    @return a string serialization form of the macaroon.
    '''
    return macaroon.serialize(json_serializer.JsonSerializer())
Beispiel #4
0
 def test_macaroon_to_dict(self):
     m = pymacaroons.Macaroon(
         key=b'rootkey', identifier=b'some id', location='here', version=2)
     as_dict = bakery.macaroon_to_dict(m)
     data = json.dumps(as_dict)
     m1 = pymacaroons.Macaroon.deserialize(data, json_serializer.JsonSerializer())
     self.assertEqual(m1.signature, m.signature)
     pymacaroons.Verifier().verify(m1, b'rootkey')
Beispiel #5
0
def deserialize(json_macaroon):
    '''Deserialize a JSON macaroon into a macaroon object from pymacaroons.

    @param the JSON macaroon to deserialize as a dict.
    @return the deserialized macaroon object.
    '''
    return Macaroon.deserialize(json.dumps(json_macaroon),
                                json_serializer.JsonSerializer())
Beispiel #6
0
def macaroon_from_dict(json_macaroon):
    '''Return a pymacaroons.Macaroon object from the given
    JSON-deserialized dict.

    @param JSON-encoded macaroon as dict
    @return the deserialized macaroon object.
    '''
    return Macaroon.deserialize(json.dumps(json_macaroon),
                                json_serializer.JsonSerializer())
    def from_dict(cls, json_dict):
        '''Return a macaroon obtained from the given dictionary as
        deserialized from JSON.
        @param json_dict The deserialized JSON object.
        '''
        json_macaroon = json_dict.get('m')
        if json_macaroon is None:
            # Try the v1 format if we don't have a macaroon field.
            m = pymacaroons.Macaroon.deserialize(
                json.dumps(json_dict), json_serializer.JsonSerializer())
            macaroon = Macaroon(root_key=None,
                                id=None,
                                namespace=legacy_namespace(),
                                version=_bakery_version(m.version))
            macaroon._macaroon = m
            return macaroon

        version = json_dict.get('v', None)
        if version is None:
            raise ValueError('no version specified')
        if (version < VERSION_3 or version > LATEST_VERSION):
            raise ValueError('unknown bakery version {}'.format(version))
        m = pymacaroons.Macaroon.deserialize(json.dumps(json_macaroon),
                                             json_serializer.JsonSerializer())
        if m.version != macaroon_version(version):
            raise ValueError('underlying macaroon has inconsistent version; '
                             'got {} want {}'.format(
                                 m.version, macaroon_version(version)))
        namespace = checkers.deserialize_namespace(json_dict.get('ns'))
        cdata = json_dict.get('cdata', {})
        caveat_data = {}
        for id64 in cdata:
            id = b64decode(id64)
            data = b64decode(cdata[id64])
            caveat_data[id] = data
        macaroon = Macaroon(root_key=None,
                            id=None,
                            namespace=namespace,
                            version=version)
        macaroon._caveat_data = caveat_data
        macaroon._macaroon = m
        return macaroon
Beispiel #8
0
    def deserialize_json(cls, serialized_json):
        serialized = json.loads(serialized_json)
        json_macaroon = serialized.get('m')
        if json_macaroon is None:
            # Try the v1 format if we don't have a macaroon filed
            m = pymacaroons.Macaroon.deserialize(
                serialized_json, json_serializer.JsonSerializer())
            macaroon = Macaroon(root_key=None,
                                id=None,
                                namespace=macaroonbakery.legacy_namespace(),
                                version=_bakery_version(m.version))
            macaroon._macaroon = m
            return macaroon

        version = serialized.get('v', None)
        if version is None:
            raise ValueError('no version specified')
        if (version < macaroonbakery.BAKERY_V3
                or version > macaroonbakery.LATEST_BAKERY_VERSION):
            raise ValueError('unknow bakery version {}'.format(version))
        m = pymacaroons.Macaroon.deserialize(json.dumps(json_macaroon),
                                             json_serializer.JsonSerializer())
        if m.version != macaroon_version(version):
            raise ValueError('underlying macaroon has inconsistent version; '
                             'got {} want {}'.format(
                                 m.version, macaroon_version(version)))
        namespace = checkers.deserialize_namespace(serialized.get('ns'))
        cdata = serialized.get('cdata', {})
        caveat_data = {}
        for id64 in cdata:
            id = utils.raw_b64decode(id64)
            data = utils.raw_b64decode(cdata[id64])
            caveat_data[id] = data
        macaroon = Macaroon(root_key=None,
                            id=None,
                            namespace=namespace,
                            version=version)
        macaroon._caveat_data = caveat_data
        macaroon._macaroon = m
        return macaroon
Beispiel #9
0
def macaroon_to_dict(macaroon):
    '''Turn macaroon into JSON-serializable dict object
    @param pymacaroons.Macaroon.
    '''
    return json.loads(macaroon.serialize(json_serializer.JsonSerializer()))