Esempio n. 1
0
    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
Esempio n. 2
0
    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