def test_json_flat(self): jobj_to = { 'signature': json_util.encode_b64jose( self.mixed.signature.signature), 'payload': json_util.encode_b64jose(b'foo'), 'header': self.mixed.signature.header, 'protected': json_util.encode_b64jose( self.mixed.signature.protected.encode('utf-8')), } jobj_from = jobj_to.copy() jobj_from['header'] = jobj_from['header'].to_json() self.assertEqual(self.mixed.to_partial_json(flat=True), jobj_to) from josepy.jws import JWS self.assertEqual(self.mixed, JWS.from_json(jobj_from))
def _encode_param(cls, data: int, length: int) -> str: """Encode Base64urlUInt. :type data: long :type key_size: long :rtype: unicode """ return json_util.encode_b64jose( data.to_bytes(byteorder="big", length=length))
def encode_csr(csr): """ Encode CSR as JOSE Base-64 DER. :param cryptography.x509.CertificateSigningRequest csr: The CSR. :rtype: str """ return encode_b64jose(csr.public_bytes(serialization.Encoding.DER))
def _encode_param(cls, data: int) -> str: """Encode Base64urlUInt. :type data: long :rtype: unicode """ length = max(data.bit_length(), 8) # decoding 0 length = math.ceil(length / 8) return json_util.encode_b64jose( data.to_bytes(byteorder="big", length=length))
def test_json_not_flat(self): jobj_to = { 'signatures': (self.mixed.signature,), 'payload': json_util.encode_b64jose(b'foo'), } jobj_from = jobj_to.copy() jobj_from['signatures'] = [jobj_to['signatures'][0].to_json()] self.assertEqual(self.mixed.to_partial_json(flat=False), jobj_to) from josepy.jws import JWS self.assertEqual(self.mixed, JWS.from_json(jobj_from))
def to_partial_json(self, flat: bool = True) -> Dict[str, Any]: assert self.signatures payload = json_util.encode_b64jose(self.payload) if flat and len(self.signatures) == 1: ret = self.signatures[0].to_partial_json() ret['payload'] = payload return ret else: return { 'payload': payload, 'signatures': self.signatures, }
def to_partial_json(self, flat=True): # pylint: disable=arguments-differ assert self.signatures payload = json_util.encode_b64jose(self.payload) if flat and len(self.signatures) == 1: ret = self.signatures[0].to_partial_json() ret['payload'] = payload return ret else: return { 'payload': payload, 'signatures': self.signatures, }
def _encode_param(cls, data): """Encode Base64urlUInt. :type data: long :rtype: unicode """ def _leading_zeros(arg): if len(arg) % 2: return '0' + arg return arg return json_util.encode_b64jose( binascii.unhexlify(_leading_zeros(hex(data)[2:].rstrip('L'))))
def _encode_param(cls, data): """Encode Base64urlUInt. :type data: long :rtype: unicode """ def _leading_zeros(arg): if len(arg) % 2: return '0' + arg return arg return json_util.encode_b64jose(binascii.unhexlify( _leading_zeros(hex(data)[2:].rstrip('L'))))
def protected(value: str) -> str: # pylint: disable=missing-docstring,no-self-argument # wrong type guess (Signature, not bytes) | pylint: disable=no-member return json_util.encode_b64jose(value.encode('utf-8'))
def fields_to_partial_json(self): # TODO: An "alg" member SHOULD also be present to identify the # algorithm intended to be used with the key, unless the # application uses another means or convention to determine # the algorithm used. return {'k': json_util.encode_b64jose(self.key)}
def test_encode_b64jose(self): from josepy.json_util import encode_b64jose encoded = encode_b64jose(b'x') self.assertTrue(isinstance(encoded, six.string_types)) self.assertEqual(u'eA', encoded)
def fields_to_partial_json(self) -> Dict[str, str]: # TODO: An "alg" member SHOULD also be present to identify the # algorithm intended to be used with the key, unless the # application uses another means or convention to determine # the algorithm used. return {'k': json_util.encode_b64jose(self.key)}
def protected(value: str) -> str: # wrong type guess (Signature, not bytes) | pylint: disable=no-member return json_util.encode_b64jose(value.encode('utf-8'))
def setUp(self): from josepy.jwk import JWKOct self.jwk = JWKOct(key=b'foo') self.jobj = {'kty': 'oct', 'k': json_util.encode_b64jose(b'foo')}
def test_encode_b64jose(self): from josepy.json_util import encode_b64jose encoded = encode_b64jose(b'x') self.assertIsInstance(encoded, str) self.assertEqual(u'eA', encoded)
def protected(value): # pylint: disable=missing-docstring,no-self-argument # wrong type guess (Signature, not bytes) | pylint: disable=no-member return json_util.encode_b64jose(value.encode('utf-8'))