def _deserialize_v2(self, serialized): from pymacaroons.macaroon import Macaroon, MACAROON_V2 from pymacaroons.caveat import Caveat from pymacaroons.exceptions import MacaroonDeserializationException # skip the initial version byte. serialized = serialized[1:] serialized, section = self._parse_section_v2(serialized) loc = '' if len(section) > 0 and section[0].field_type == self._LOCATION: loc = section[0].data.decode('utf-8') section = section[1:] if len(section) != 1 or section[0].field_type != self._IDENTIFIER: raise MacaroonDeserializationException('invalid macaroon header') macaroon = Macaroon( identifier=section[0].data, location=loc, version=MACAROON_V2, ) while True: rest, section = self._parse_section_v2(serialized) serialized = rest if len(section) == 0: break cav = Caveat(version=MACAROON_V2) if len(section) > 0 and section[0].field_type == self._LOCATION: cav.location = section[0].data.decode('utf-8') section = section[1:] if len(section) == 0 or section[0].field_type != self._IDENTIFIER: raise MacaroonDeserializationException( 'no identifier in caveat') cav.caveat_id = section[0].data section = section[1:] if len(section) == 0: # First party caveat. if cav.location is not None: raise MacaroonDeserializationException( 'location not allowed in first party caveat') macaroon.caveats.append(cav) continue if len(section) != 1: raise MacaroonDeserializationException( 'extra fields found in caveat') if section[0].field_type != self._VID: raise MacaroonDeserializationException( 'invalid field found in caveat') cav.verification_key_id = section[0].data macaroon.caveats.append(cav) serialized, packet = self._parse_packet_v2(serialized) if packet.field_type != self._SIGNATURE: raise MacaroonDeserializationException( 'unexpected field found instead of signature') macaroon.signature = binascii.hexlify(packet.data) return macaroon
def deserialize(self, serialized): from pymacaroons.macaroon import Macaroon from pymacaroons.caveat import Caveat from pymacaroons.exceptions import MacaroonDeserializationException macaroon = Macaroon() decoded = urlsafe_b64decode(convert_to_bytes( serialized + "=" * (-len(serialized) % 4) )) index = 0 while index < len(decoded): packet_length = int( struct.unpack( b"4s", decoded[index:index + self.PACKET_PREFIX_LENGTH] )[0], 16 ) packet = decoded[ index + self.PACKET_PREFIX_LENGTH:index + packet_length ] key, value = self._depacketize(packet) if key == b'location': macaroon.location = value elif key == b'identifier': macaroon.identifier = value elif key == b'cid': macaroon.caveats.append(Caveat(caveat_id=value)) elif key == b'vid': macaroon.caveats[-1].verification_key_id = value elif key == b'cl': macaroon.caveats[-1].location = value elif key == b'signature': macaroon.signature = binascii.hexlify(value) else: raise MacaroonDeserializationException( 'Key {key} not valid key for this format. ' 'Value: {value}'.format( key=key, value=value ) ) index = index + packet_length return macaroon
def mock_discharge(*args, **kwargs): return [ Macaroon( location="api.snapcraft.io", signature= "d9533461d7835e4851c7e3b639144406cf768597dea6e133232fbd2385a5c050", ) ]
def deserialize(self, serialized): from pymacaroons.macaroon import Macaroon from pymacaroons.caveat import Caveat caveats = [] deserialized = json.loads(convert_to_string(serialized)) for c in deserialized['caveats']: caveat = Caveat(caveat_id=c['cid'], verification_key_id=(standard_b64decode(c['vid']) if c['vid'] else None), location=c['cl']) caveats.append(caveat) return Macaroon(location=deserialized['location'], identifier=deserialized['identifier'], caveats=caveats, signature=deserialized['signature'])
def _deserialize_v2(self, deserialized): '''Deserialize a JSON macaroon v2. @param serialized the macaroon in JSON format v2. @return the macaroon object. ''' from pymacaroons.macaroon import Macaroon, MACAROON_V2 from pymacaroons.caveat import Caveat caveats = [] for c in deserialized.get('c', []): caveat = Caveat(caveat_id=_read_json_binary_field(c, 'i'), verification_key_id=_read_json_binary_field( c, 'v'), location=_read_json_binary_field(c, 'l'), version=MACAROON_V2) caveats.append(caveat) return Macaroon(location=_read_json_binary_field(deserialized, 'l'), identifier=_read_json_binary_field(deserialized, 'i'), caveats=caveats, signature=binascii.hexlify( _read_json_binary_field(deserialized, 's')), version=MACAROON_V2)
def _deserialize_v1(self, deserialized): '''Deserialize a JSON macaroon in v1 format. @param serialized the macaroon in v1 JSON format. @return the macaroon object. ''' from pymacaroons.macaroon import Macaroon, MACAROON_V1 from pymacaroons.caveat import Caveat caveats = [] for c in deserialized.get('caveats', []): caveat = Caveat(caveat_id=c['cid'], verification_key_id=(utils.raw_b64decode(c['vid']) if c.get('vid') else None), location=(c['cl'] if c.get('cl') else None), version=MACAROON_V1) caveats.append(caveat) return Macaroon(location=deserialized.get('location'), identifier=deserialized['identifier'], caveats=caveats, signature=deserialized['signature'], version=MACAROON_V1)