def to_dict(self): try: cid = convert_to_string(self.caveat_id) except UnicodeEncodeError: cid = convert_to_string(standard_b64encode(self.caveat_id_bytes)) return { 'cid': cid, 'vid': (standard_b64encode(self.verification_key_id) if self.verification_key_id else None), 'cl': self.location }
def to_dict(self): try: cid = convert_to_string(self.caveat_id) except UnicodeEncodeError: cid = convert_to_string(standard_b64encode(self.caveat_id_bytes)) return { 'cid': cid, 'vid': ( standard_b64encode(self.verification_key_id) if self.verification_key_id else None ), 'cl': self.location }
def deserialize(cls, serialized, serializer=None): serialized = convert_to_string(serialized) serializer = serializer or BinarySerializer() if serialized: return serializer.deserialize(serialized) else: raise MacaroonInitException( 'Must supply serialized macaroon.' )
def inspect(self): combined = 'location {loc}\n'.format(loc=self.location) try: combined += 'identifier {id}\n'.format(id=self.identifier) except UnicodeEncodeError: combined += 'identifier64 {id}\n'.format(id=convert_to_string( standard_b64encode(self.identifier_bytes))) for caveat in self.caveats: try: combined += 'cid {cid}\n'.format( cid=convert_to_string(caveat.caveat_id)) except UnicodeEncodeError: combined += 'cid64 {cid}\n'.format(cid=convert_to_string( standard_b64encode(caveat.caveat_id_bytes))) if caveat.verification_key_id and caveat.location: vid = convert_to_string( standard_b64encode(caveat.verification_key_id)) combined += 'vid {vid}\n'.format(vid=vid) combined += 'cl {cl}\n'.format(cl=caveat.location) combined += 'signature {sig}'.format(sig=self.signature) return combined
def inspect(self): combined = 'location {loc}\n'.format(loc=self.location) combined += 'identifier {id}\n'.format(id=self.identifier) for caveat in self.caveats: combined += 'cid {cid}\n'.format(cid=caveat.caveat_id) if caveat.verification_key_id and caveat.location: vid = convert_to_string( standard_b64encode(caveat.verification_key_id) ) combined += 'vid {vid}\n'.format(vid=vid) combined += 'cl {cl}\n'.format(cl=caveat.location) combined += 'signature {sig}'.format(sig=self.signature) return combined
def _serialize_v1(self, macaroon): '''Serialize the macaroon in JSON format v1. @param macaroon the macaroon to serialize. @return JSON macaroon. ''' serialized = { 'identifier': utils.convert_to_string(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): 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(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 caveat_id(self): return convert_to_string(self._caveat_id)
def signifier(self): return convert_to_string(self._signifier)
def signature(self): return convert_to_string(self._signature)
def location(self): return convert_to_string(self._location)
def _signatures_match(self, macaroon_signature, computed_signature): return constant_time_compare(convert_to_string(macaroon_signature), convert_to_string(computed_signature))
def deserialize(self, serialized): if len(serialized) == 0: raise ValueError('empty macaroon') serialized = convert_to_string(serialized) decoded = raw_b64decode(serialized) return self.deserialize_raw(decoded)
def verification_key_id(self): return convert_to_string(self._verification_key_id)
def caveat_id(self): from pymacaroons.macaroon import MACAROON_V1 if self._version == MACAROON_V1: return convert_to_string(self._caveat_id) return self._caveat_id
def satisfy_exact(self, predicate): if predicate is None: raise TypeError('Predicate cannot be none.') self.predicates.append(convert_to_string(predicate))
def _signatures_match(self, macaroon_signature, computed_signature): return constant_time_compare( convert_to_string(macaroon_signature), convert_to_string(computed_signature) )
def identifier(self): return convert_to_string(self._identifier)
def decrypt(self, signature, field_data): key = truncate_or_pad(signature) box = SecretBox(key=key) encoded = convert_to_bytes(field_data[len(self.signifier):]) decrypted = box.decrypt(standard_b64decode(encoded)) return convert_to_string(decrypted)
def identifier(self): if self.version == MACAROON_V1: return convert_to_string(self._identifier) return self._identifier