def test_decode_b64jose_minimum_size(self): from josepy.json_util import decode_b64jose self.assertEqual(b'foo', decode_b64jose(u'Zm9v', size=3, minimum=True)) self.assertEqual(b'foo', decode_b64jose(u'Zm9v', size=2, minimum=True)) self.assertRaises(errors.DeserializationError, decode_b64jose, u'Zm9v', size=4, minimum=True)
def from_json(cls, jobj): if 'signature' in jobj and 'signatures' in jobj: raise errors.DeserializationError('Flat mixed with non-flat') elif 'signature' in jobj: # flat return cls(payload=json_util.decode_b64jose(jobj.pop('payload')), signatures=(cls.signature_cls.from_json(jobj),)) else: return cls(payload=json_util.decode_b64jose(jobj['payload']), signatures=tuple(cls.signature_cls.from_json(sig) for sig in jobj['signatures']))
def from_json(cls, jobj: Mapping[str, Any]) -> 'JWS': if 'signature' in jobj and 'signatures' in jobj: raise errors.DeserializationError('Flat mixed with non-flat') elif 'signature' in jobj: # flat filtered = {key: value for key, value in jobj.items() if key != "payload"} return cls(payload=json_util.decode_b64jose(jobj['payload']), signatures=(cls.signature_cls.from_json(filtered),)) else: return cls(payload=json_util.decode_b64jose(jobj['payload']), signatures=tuple(cls.signature_cls.from_json(sig) for sig in jobj['signatures']))
def from_json(cls, jobj): if 'signature' in jobj and 'signatures' in jobj: raise errors.DeserializationError('Flat mixed with non-flat') elif 'signature' in jobj: # flat return cls(payload=json_util.decode_b64jose(jobj.pop('payload')), signatures=(cls.signature_cls.from_json(jobj), )) else: return cls(payload=json_util.decode_b64jose(jobj['payload']), signatures=tuple( cls.signature_cls.from_json(sig) for sig in jobj['signatures']))
def _decode_param(cls, data: str) -> int: """Decode Base64urlUInt.""" try: binary = json_util.decode_b64jose(data) if not binary: raise errors.DeserializationError() return int.from_bytes(binary, byteorder="big") except ValueError: # invalid literal for long() with base 16 raise errors.DeserializationError()
def _decode_param(cls, data: str, name: str, valid_length: int) -> int: """Decode Base64urlUInt.""" try: binary = json_util.decode_b64jose(data) if len(binary) != valid_length: raise errors.DeserializationError( f'Expected parameter "{name}" to be {valid_length} bytes ' f'after base64-decoding; got {len(binary)} bytes instead') return int.from_bytes(binary, byteorder="big") except ValueError: # invalid literal for long() with base 16 raise errors.DeserializationError()
def decode_csr(b64der): """ Decode JOSE Base-64 DER-encoded CSR. :param str b64der: The encoded CSR. :rtype: `cryptography.x509.CertificateSigningRequest` :return: The decoded CSR. """ try: return x509.load_der_x509_csr(decode_b64jose(b64der), default_backend()) except ValueError as error: raise DeserializationError(error)
def protected(value: str) -> str: # pylint: disable=missing-docstring,no-self-argument return json_util.decode_b64jose(value).decode('utf-8')
def _decode_param(cls, data): """Decode Base64urlUInt.""" try: return int(binascii.hexlify(json_util.decode_b64jose(data)), 16) except ValueError: # invalid literal for long() with base 16 raise errors.DeserializationError()
def fields_from_json(cls, jobj): return cls(key=json_util.decode_b64jose(jobj['k']))
def fields_from_json(cls, jobj: Mapping[str, Any]) -> 'JWKOct': return cls(key=json_util.decode_b64jose(jobj['k']))
def protected(value: str) -> str: return json_util.decode_b64jose(value).decode('utf-8')
def test_decode_b64jose(self): from josepy.json_util import decode_b64jose decoded = decode_b64jose(u'eA') self.assertIsInstance(decoded, bytes) self.assertEqual(b'x', decoded)
def test_decode_b64jose(self): from josepy.json_util import decode_b64jose decoded = decode_b64jose(u'eA') self.assertTrue(isinstance(decoded, six.binary_type)) self.assertEqual(b'x', decoded)
def protected(value): # pylint: disable=missing-docstring,no-self-argument return json_util.decode_b64jose(value).decode('utf-8')