Exemple #1
0
    def encode(self, headerobj, encoded_header, payload):
        assert isinstance(headerobj, dict)

        encoded_payload = b64_encode(payload)
        encoded_signature = b64_encode(self.sign(
            headerobj['alg'],
            self._signing_message(encoded_header, encoded_payload),
            headerobj.get('kid')))

        return '.'.join((encoded_payload, encoded_signature))
Exemple #2
0
    def encode(self, headerobj, encoded_header, payload):
        assert isinstance(headerobj, dict)
        assert isinstance(encoded_header, str)
        assert isinstance(payload, bytes)

        encoded_payload = b64_encode(payload)
        encoded_signature = b64_encode(
            self.sign(headerobj["alg"], self._signing_message(encoded_header, encoded_payload), headerobj.get("kid"))
        )

        return ".".join((encoded_payload, encoded_signature))
Exemple #3
0
 def test_b64_encode(self):
     ret = b'{"iss":"joe",\r\n "exp":1300819380,\r\n ' \
         + b'"http://example.com/is_root":true}'
     self.assertEqual(
         utils.b64_encode(ret),
         'eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQog'
         'Imh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ',
     )
Exemple #4
0
    def encode(self, headerobj, payload):
        assert isinstance(headerobj, dict)
        assert isinstance(payload, bytes)

        try:
            impl = self._get_impl(headerobj['alg'])
        except KeyError as why:
            raise MalformedJWT('\'alg\' is required')

        encoded_header = b64_encode(self._json_encode(headerobj))

        return '.'.join((
            encoded_header,
            impl.encode(headerobj, encoded_header, payload)
        ))
Exemple #5
0
    def left_hash(self, alg, target):
        if isinstance(target, str):
            target = target.encode('ascii')

        if alg.startswith('RS') or alg.startswith('HS'):
            if alg.endswith('256'):
                hashfunc = hashlib.sha256
            elif alg.endswith('384'):
                hashfunc = hashlib.sha384
            elif alg.endswith('512'):
                hashfunc = hashlib.sha512
            else:
                raise ValueError('Unknown algorithm')
        else:
            raise ValueError('Unsupported algorithm')

        digest = hashfunc(target).digest()

        return b64_encode(digest[:len(digest)//2])
Exemple #6
0
    def left_hash(self, alg, target):
        if isinstance(target, str):
            target = target.encode('ascii')

        if alg.startswith('RS') or alg.startswith('HS'):
            if alg.endswith('256'):
                hashfunc = hashlib.sha256
            elif alg.endswith('384'):
                hashfunc = hashlib.sha384
            elif alg.endswith('512'):
                hashfunc = hashlib.sha512
            else:
                raise ValueError('Unknown algorithm')
        else:
            raise ValueError('Unsupported algorithm')

        digest = hashfunc(target).digest()

        return b64_encode(digest[:len(digest) // 2])
Exemple #7
0
    def to_dict(self):
        D = super(OctKey, self).to_dict()
        D["k"] = b64_encode(self.k)

        return D