def setUp(self):
     key = RSA.generate(2048)
     key_2 = RSA.generate(2048)
     self.private_key = key.exportKey().decode("utf-8")
     self.private_key_2 = key_2.exportKey().decode("utf-8")
     self.exponent = force_unicode(to_base64url_uint(key.e))
     self.modulus = force_unicode(to_base64url_uint(key.n))
     self.sub = str(uuid.uuid1())
     os.environ["USER_POOL_ID"] = "eu-west-1_abcde1234"
     os.environ["CLIENT_IDS"] = "qwertuiop123654789"
     os.environ["REGION"] = "eu-west-1"
示例#2
0
    def test_rsa_public_key_to_jwk_works_with_from_jwk(self):
        algo = RSAAlgorithm(RSAAlgorithm.SHA256)

        with open(key_path("testkey_rsa.pub"), "r") as rsa_key:
            orig_key = algo.prepare_key(force_unicode(rsa_key.read()))

        parsed_key = algo.from_jwk(algo.to_jwk(orig_key))
        assert parsed_key.public_numbers() == orig_key.public_numbers()
示例#3
0
    def test_custom_json_encoder(self, jws, payload):
        class CustomJSONEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, Decimal):
                    return 'it worked'
                return super(CustomJSONEncoder, self).default(o)

        data = {'some_decimal': Decimal('2.2')}

        with pytest.raises(TypeError):
            jws.encode(payload, 'secret', headers=data)

        token = jws.encode(payload,
                           'secret',
                           headers=data,
                           json_encoder=CustomJSONEncoder)

        header = force_bytes(force_unicode(token).split('.')[0])
        header = json.loads(force_unicode(base64url_decode(header)))

        assert 'some_decimal' in header
        assert header['some_decimal'] == 'it worked'
示例#4
0
    def test_custom_json_encoder(self, jws, payload):
        class CustomJSONEncoder(json.JSONEncoder):
            def default(self, o):
                if isinstance(o, Decimal):
                    return "it worked"
                return super(CustomJSONEncoder, self).default(o)

        data = {"some_decimal": Decimal("2.2")}

        with pytest.raises(TypeError):
            jws.encode(payload, "secret", headers=data)

        token = jws.encode(payload,
                           "secret",
                           headers=data,
                           json_encoder=CustomJSONEncoder)

        header = force_bytes(force_unicode(token).split(".")[0])
        header = json.loads(force_unicode(base64url_decode(header)))

        assert "some_decimal" in header
        assert header["some_decimal"] == "it worked"
示例#5
0
    def test_decodes_valid_es384_jws(self, jws):
        example_payload = {"hello": "world"}
        with open("tests/keys/testkey_ec.pub", "r") as fp:
            example_pubkey = fp.read()
        example_jws = (
            b"eyJhbGciOiJFUzI1NiIsInR5cCI6IkpXVCJ9."
            b"eyJoZWxsbyI6IndvcmxkIn0.TORyNQab_MoXM7DvNKaTwbrJr4UY"
            b"d2SsX8hhlnWelQFmPFSf_JzC2EbLnar92t-bXsDovzxp25ExazrVHkfPkQ")
        decoded_payload = jws.decode(example_jws,
                                     example_pubkey,
                                     algorithms=["ES256"])
        json_payload = json.loads(force_unicode(decoded_payload))

        assert json_payload == example_payload
示例#6
0
    def test_decodes_valid_es384_jws(self, jws):
        example_payload = {"hello": "world"}
        with open("tests/keys/testkey_ec.pub", "r") as fp:
            example_pubkey = fp.read()
        example_jws = (b"eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9"
                       b".eyJoZWxsbyI6IndvcmxkIn0"
                       b".AGtlemKghaIaYh1yeeekFH9fRuNY7hCaw5hUgZ5aG1N"
                       b"2F8FIbiKLaZKr8SiFdTimXFVTEmxpBQ9sRmdsDsnrM-1"
                       b"HAG0_zxxu0JyINOFT2iqF3URYl9HZ8kZWMeZAtXmn6Cw"
                       b"PXRJD2f7N-f7bJ5JeL9VT5beI2XD3FlK3GgRvI-eE-2Ik")
        decoded_payload = jws.decode(example_jws, example_pubkey)
        json_payload = json.loads(force_unicode(decoded_payload))

        assert json_payload == example_payload
示例#7
0
    def test_decodes_valid_es384_jws(self, jws):
        example_payload = {'hello': 'world'}
        with open('tests/keys/testkey_ec.pub', 'r') as fp:
            example_pubkey = fp.read()
        example_jws = (
            b'eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9'
            b'.eyJoZWxsbyI6IndvcmxkIn0'
            b'.AGtlemKghaIaYh1yeeekFH9fRuNY7hCaw5hUgZ5aG1N'
            b'2F8FIbiKLaZKr8SiFdTimXFVTEmxpBQ9sRmdsDsnrM-1'
            b'HAG0_zxxu0JyINOFT2iqF3URYl9HZ8kZWMeZAtXmn6Cw'
            b'PXRJD2f7N-f7bJ5JeL9VT5beI2XD3FlK3GgRvI-eE-2Ik')
        decoded_payload = jws.decode(example_jws, example_pubkey)
        json_payload = json.loads(force_unicode(decoded_payload))

        assert json_payload == example_payload
示例#8
0
    def test_custom_json_encoder(self, jws, payload):

        class CustomJSONEncoder(json.JSONEncoder):

            def default(self, o):
                if isinstance(o, Decimal):
                    return 'it worked'
                return super(CustomJSONEncoder, self).default(o)

        data = {
            'some_decimal': Decimal('2.2')
        }

        with pytest.raises(TypeError):
            jws.encode(payload, 'secret', headers=data)

        token = jws.encode(payload, 'secret', headers=data,
                           json_encoder=CustomJSONEncoder)

        header = force_bytes(force_unicode(token).split('.')[0])
        header = json.loads(force_unicode(base64url_decode(header)))

        assert 'some_decimal' in header
        assert header['some_decimal'] == 'it worked'
示例#9
0
    def test_decodes_valid_rs384_jws(self, jws):
        example_payload = {"hello": "world"}
        with open("tests/keys/testkey_rsa.pub", "r") as fp:
            example_pubkey = fp.read()
        example_jws = (b"eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9"
                       b".eyJoZWxsbyI6IndvcmxkIn0"
                       b".yNQ3nI9vEDs7lEh-Cp81McPuiQ4ZRv6FL4evTYYAh1X"
                       b"lRTTR3Cz8pPA9Stgso8Ra9xGB4X3rlra1c8Jz10nTUju"
                       b"O06OMm7oXdrnxp1KIiAJDerWHkQ7l3dlizIk1bmMA457"
                       b"W2fNzNfHViuED5ISM081dgf_a71qBwJ_yShMMrSOfxDx"
                       b"mX9c4DjRogRJG8SM5PvpLqI_Cm9iQPGMvmYK7gzcq2cJ"
                       b"urHRJDJHTqIdpLWXkY7zVikeen6FhuGyn060Dz9gYq9t"
                       b"uwmrtSWCBUjiN8sqJ00CDgycxKqHfUndZbEAOjcCAhBr"
                       b"qWW3mSVivUfubsYbwUdUG3fSRPjaUPcpe8A")
        decoded_payload = jws.decode(example_jws, example_pubkey)
        json_payload = json.loads(force_unicode(decoded_payload))

        assert json_payload == example_payload
示例#10
0
    def test_decodes_valid_rs384_jws(self, jws):
        example_payload = {'hello': 'world'}
        with open('tests/keys/testkey_rsa.pub', 'r') as fp:
            example_pubkey = fp.read()
        example_jws = (
            b'eyJhbGciOiJSUzM4NCIsInR5cCI6IkpXVCJ9'
            b'.eyJoZWxsbyI6IndvcmxkIn0'
            b'.yNQ3nI9vEDs7lEh-Cp81McPuiQ4ZRv6FL4evTYYAh1X'
            b'lRTTR3Cz8pPA9Stgso8Ra9xGB4X3rlra1c8Jz10nTUju'
            b'O06OMm7oXdrnxp1KIiAJDerWHkQ7l3dlizIk1bmMA457'
            b'W2fNzNfHViuED5ISM081dgf_a71qBwJ_yShMMrSOfxDx'
            b'mX9c4DjRogRJG8SM5PvpLqI_Cm9iQPGMvmYK7gzcq2cJ'
            b'urHRJDJHTqIdpLWXkY7zVikeen6FhuGyn060Dz9gYq9t'
            b'uwmrtSWCBUjiN8sqJ00CDgycxKqHfUndZbEAOjcCAhBr'
            b'qWW3mSVivUfubsYbwUdUG3fSRPjaUPcpe8A')
        decoded_payload = jws.decode(example_jws, example_pubkey)
        json_payload = json.loads(force_unicode(decoded_payload))

        assert json_payload == example_payload
示例#11
0
def test_force_unicode_raises_error_on_invalid_object():
    with pytest.raises(TypeError):
        force_unicode({})
示例#12
0
    def test_rsa_should_accept_unicode_key(self):
        algo = RSAAlgorithm(RSAAlgorithm.SHA256)

        with open(key_path("testkey_rsa")) as rsa_key:
            algo.prepare_key(force_unicode(rsa_key.read()))
示例#13
0
    def test_ed25519_should_accept_unicode_key(self):
        algo = Ed25519Algorithm()

        with open(key_path("testkey_ed25519")) as ec_key:
            algo.prepare_key(force_unicode(ec_key.read()))
示例#14
0
    def test_ec_should_accept_unicode_key(self):
        algo = ECAlgorithm(ECAlgorithm.SHA256)

        with open(key_path("testkey_ec")) as ec_key:
            algo.prepare_key(force_unicode(ec_key.read()))
示例#15
0
# -*- coding: utf-8 -*-\
import json, time, chardet
from jwt.api_jwt import wdJwt
from jwt.utils import base64urlencode, force_unicode

if __name__ == "__main__":
    wd = wdJwt()
    start_time = int(time.time())
    print 'header: ', wd.generate_header()
    payload_str = force_unicode(json.dumps({'nickname': u'王强', 'gender': 1}))

    print 'payload: ', wd.generate_payload(payload_str)
    print 'signature: ', wd.generate_signature()
    first_token = wd.generate_token()
    print 'token: ', first_token
    print 'token code: ', chardet.detect(first_token)
    print 'verify token', wd.verify_token(first_token)
    # FormatError
    try:
        wd.verify_token(first_token + ".d")
    except Exception, e:
        print "verify token ", e

    # LosedError
    try:
        wd.verify_token(first_token + "s")
    except Exception, e:
        print "verify token", e

    # TimeoutTokenError
    time.sleep(7)
def test_force_unicode_raises_error_on_invalid_object():
    with pytest.raises(TypeError):
        force_unicode({})
示例#17
0
    def test_hmac_should_accept_unicode_key(self):
        algo = HMACAlgorithm(HMACAlgorithm.SHA256)

        algo.prepare_key(force_unicode("awesome"))
示例#18
0
    def test_ec_should_accept_unicode_key(self):
        algo = ECAlgorithm(ECAlgorithm.SHA256)

        with open(key_path('testkey_ec'), 'r') as ec_key:
            algo.prepare_key(force_unicode(ec_key.read()))
示例#19
0
    def test_rsa_should_accept_unicode_key(self):
        algo = RSAAlgorithm(RSAAlgorithm.SHA256)

        with open(key_path('testkey_rsa'), 'r') as rsa_key:
            algo.prepare_key(force_unicode(rsa_key.read()))