Esempio n. 1
0
    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))
Esempio n. 2
0
 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))
Esempio n. 3
0
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))
Esempio n. 4
0
 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))
Esempio n. 5
0
    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))
Esempio n. 6
0
    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,
            }
Esempio n. 7
0
    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,
            }
Esempio n. 8
0
File: jws.py Progetto: cloudera/hue
    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,
            }
Esempio n. 9
0
    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'))))
Esempio n. 10
0
File: jwk.py Progetto: cloudera/hue
    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'))))
Esempio n. 11
0
 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'))
Esempio n. 12
0
File: jwk.py Progetto: cloudera/hue
 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)
Esempio n. 14
0
 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)}
Esempio n. 15
0
 def protected(value: str) -> str:
     # wrong type guess (Signature, not bytes) | pylint: disable=no-member
     return json_util.encode_b64jose(value.encode('utf-8'))
Esempio n. 16
0
 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')}
Esempio n. 17
0
 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)
Esempio n. 18
0
File: jws.py Progetto: cloudera/hue
 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'))