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
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
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
def _decode_secret_part_v2_v3(version, data): if len(data) < 1: raise VerificationError('secret part too short') got_version = six.byte2int(data[:1]) data = data[1:] if version != got_version: raise VerificationError( 'unexpected secret part version, got {} want {}'.format( got_version, version)) root_key_length, read = decode_uvarint(data) data = data[read:] root_key = data[:root_key_length] data = data[root_key_length:] if version >= VERSION_3: namespace_length, read = decode_uvarint(data) data = data[read:] ns_data = data[:namespace_length] data = data[namespace_length:] ns = checkers.deserialize_namespace(ns_data) else: ns = legacy_namespace() return root_key, data, ns
def test_serialize(self): tests = [('empty namespace', None, b''), ('standard namespace', { 'std': '' }, b'std:'), ('several elements', { 'std': '', 'http://blah.blah': 'blah', 'one': 'two', 'foo.com/x.v0.1': 'z', }, b'foo.com/x.v0.1:z http://blah.blah:blah one:two std:'), ('sort by URI not by field', { 'a': 'one', 'a1': 'two', }, b'a:one a1:two')] for test in tests: ns = checkers.Namespace(test[1]) data = ns.serialize_text() self.assertEquals(data, test[2]) self.assertEquals(str(ns), test[2].decode('utf-8')) # Check that it can be deserialize to the same thing: ns1 = checkers.deserialize_namespace(data) self.assertEquals(ns1, ns)
def test_serialize(self): tests = [ ('empty namespace', None, b''), ('standard namespace', {'std': ''}, b'std:'), ('several elements', { 'std': '', 'http://blah.blah': 'blah', 'one': 'two', 'foo.com/x.v0.1': 'z', }, b'foo.com/x.v0.1:z http://blah.blah:blah one:two std:'), ('sort by URI not by field', { 'a': 'one', 'a1': 'two', }, b'a:one a1:two') ] for test in tests: ns = checkers.Namespace(test[1]) data = ns.serialize_text() self.assertEquals(data, test[2]) self.assertEquals(str(ns), test[2].decode('utf-8')) # Check that it can be deserialize to the same thing: ns1 = checkers.deserialize_namespace(data) self.assertEquals(ns1, ns)