Skip to content

Commit

Permalink
Replaced tests.utils ensure_unicode and ensure_bytes with jwt.utils v…
Browse files Browse the repository at this point in the history
…ersions
  • Loading branch information
mark-adams committed Aug 7, 2016
1 parent d9e4a0d commit bfa24f3
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 92 deletions.
4 changes: 0 additions & 4 deletions jwt/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
string_types = (text_type, binary_type)


def is_string_type(val):
return any([isinstance(val, typ) for typ in string_types])


def timedelta_total_seconds(delta):
try:
delta.total_seconds
Expand Down
36 changes: 19 additions & 17 deletions tests/contrib/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import base64

from jwt.utils import force_bytes, force_unicode

import pytest

from ..utils import ensure_bytes, ensure_unicode, key_path
from ..utils import key_path

try:
from jwt.contrib.algorithms.pycrypto import RSAAlgorithm
Expand All @@ -29,7 +31,7 @@ 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(ensure_unicode(rsa_key.read()))
algo.prepare_key(force_unicode(rsa_key.read()))

def test_rsa_should_reject_non_string_key(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)
Expand All @@ -40,9 +42,9 @@ def test_rsa_should_reject_non_string_key(self):
def test_rsa_sign_should_generate_correct_signature_value(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

expected_sig = base64.b64decode(ensure_bytes(
expected_sig = base64.b64decode(force_bytes(
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
Expand All @@ -63,17 +65,17 @@ def test_rsa_sign_should_generate_correct_signature_value(self):
def test_rsa_verify_should_return_false_if_signature_invalid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

jwt_sig = base64.b64decode(ensure_bytes(
jwt_sig = base64.b64decode(force_bytes(
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
'sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX'
'fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA'
'APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA=='))

jwt_sig += ensure_bytes('123') # Signature is now invalid
jwt_sig += force_bytes('123') # Signature is now invalid

with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
Expand All @@ -84,9 +86,9 @@ def test_rsa_verify_should_return_false_if_signature_invalid(self):
def test_rsa_verify_should_return_true_if_signature_valid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

jwt_sig = base64.b64decode(ensure_bytes(
jwt_sig = base64.b64decode(force_bytes(
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
Expand Down Expand Up @@ -122,14 +124,14 @@ 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(ensure_unicode(ec_key.read()))
algo.prepare_key(force_unicode(ec_key.read()))

def test_ec_sign_should_generate_correct_signature_value(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

expected_sig = base64.b64decode(ensure_bytes(
expected_sig = base64.b64decode(force_bytes(
'AC+m4Jf/xI3guAC6w0w37t5zRpSCF6F4udEz5LiMiTIjCS4vcVe6dDOxK+M'
'mvkF8PxJuvqxP2CO3TR3okDPCl/NjATTO1jE+qBZ966CRQSSzcCM+tzcHzw'
'LZS5kbvKu0Acd/K6Ol2/W3B1NeV5F/gjvZn/jOwaLgWEUYsg0o4XVrAg65'))
Expand All @@ -147,14 +149,14 @@ def test_ec_sign_should_generate_correct_signature_value(self):
def test_ec_verify_should_return_false_if_signature_invalid(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

jwt_sig = base64.b64decode(ensure_bytes(
jwt_sig = base64.b64decode(force_bytes(
'AC+m4Jf/xI3guAC6w0w37t5zRpSCF6F4udEz5LiMiTIjCS4vcVe6dDOxK+M'
'mvkF8PxJuvqxP2CO3TR3okDPCl/NjATTO1jE+qBZ966CRQSSzcCM+tzcHzw'
'LZS5kbvKu0Acd/K6Ol2/W3B1NeV5F/gjvZn/jOwaLgWEUYsg0o4XVrAg65'))

jwt_sig += ensure_bytes('123') # Signature is now invalid
jwt_sig += force_bytes('123') # Signature is now invalid

with open(key_path('testkey_ec.pub'), 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
Expand All @@ -165,9 +167,9 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
def test_ec_verify_should_return_true_if_signature_valid(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

jwt_sig = base64.b64decode(ensure_bytes(
jwt_sig = base64.b64decode(force_bytes(
'AC+m4Jf/xI3guAC6w0w37t5zRpSCF6F4udEz5LiMiTIjCS4vcVe6dDOxK+M'
'mvkF8PxJuvqxP2CO3TR3okDPCl/NjATTO1jE+qBZ966CRQSSzcCM+tzcHzw'
'LZS5kbvKu0Acd/K6Ol2/W3B1NeV5F/gjvZn/jOwaLgWEUYsg0o4XVrAg65'))
Expand Down
8 changes: 4 additions & 4 deletions tests/keys/__init__.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import json
import os

from jwt.utils import base64url_decode
from jwt.utils import base64url_decode, force_bytes

from tests.utils import ensure_bytes, int_from_bytes
from tests.utils import int_from_bytes

BASE_PATH = os.path.dirname(os.path.abspath(__file__))


def decode_value(val):
decoded = base64url_decode(ensure_bytes(val))
decoded = base64url_decode(force_bytes(val))
return int_from_bytes(decoded, 'big')


def load_hmac_key():
with open(os.path.join(BASE_PATH, 'jwk_hmac.json'), 'r') as infile:
keyobj = json.load(infile)

return base64url_decode(ensure_bytes(keyobj['k']))
return base64url_decode(force_bytes(keyobj['k']))

try:
from cryptography.hazmat.primitives.asymmetric import ec
Expand Down
52 changes: 26 additions & 26 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

from jwt.algorithms import Algorithm, HMACAlgorithm, NoneAlgorithm
from jwt.exceptions import InvalidKeyError
from jwt.utils import base64url_decode
from jwt.utils import base64url_decode, force_bytes, force_unicode

import pytest

from .keys import load_hmac_key
from .utils import ensure_bytes, ensure_unicode, key_path
from .utils import key_path

try:
from jwt.algorithms import RSAAlgorithm, ECAlgorithm, RSAPSSAlgorithm
Expand Down Expand Up @@ -67,7 +67,7 @@ def test_hmac_should_reject_nonstring_key(self):
def test_hmac_should_accept_unicode_key(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)

algo.prepare_key(ensure_unicode('awesome'))
algo.prepare_key(force_unicode('awesome'))

def test_hmac_should_throw_exception_if_key_is_pem_public_key(self):
algo = HMACAlgorithm(HMACAlgorithm.SHA256)
Expand Down Expand Up @@ -138,7 +138,7 @@ 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(ensure_unicode(rsa_key.read()))
algo.prepare_key(force_unicode(rsa_key.read()))

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_rsa_should_reject_non_string_key(self):
Expand All @@ -151,17 +151,17 @@ def test_rsa_should_reject_non_string_key(self):
def test_rsa_verify_should_return_false_if_signature_invalid(self):
algo = RSAAlgorithm(RSAAlgorithm.SHA256)

message = ensure_bytes('Hello World!')
message = force_bytes('Hello World!')

sig = base64.b64decode(ensure_bytes(
sig = base64.b64decode(force_bytes(
'yS6zk9DBkuGTtcBzLUzSpo9gGJxJFOGvUqN01iLhWHrzBQ9ZEz3+Ae38AXp'
'10RWwscp42ySC85Z6zoN67yGkLNWnfmCZSEv+xqELGEvBJvciOKsrhiObUl'
'2mveSc1oeO/2ujkGDkkkJ2epn0YliacVjZF5+/uDmImUfAAj8lzjnHlzYix'
'sn5jGz1H07jYYbi9diixN8IUhXeTafwFg02IcONhum29V40Wu6O5tAKWlJX'
'fHJnNUzAEUOXS0WahHVb57D30pcgIji9z923q90p5c7E2cU8V+E1qe8NdCA'
'APCDzZZ9zQ/dgcMVaBrGrgimrcLbPjueOKFgSO+SSjIElKA=='))

sig += ensure_bytes('123') # Signature is now invalid
sig += force_bytes('123') # Signature is now invalid

with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
pub_key = algo.prepare_key(keyfile.read())
Expand All @@ -179,8 +179,8 @@ def test_rsa_jwk_public_and_private_keys_should_parse_and_verify(self):
with open(key_path('jwk_rsa_key.json'), 'r') as keyfile:
priv_key = algo.from_jwk(keyfile.read())

signature = algo.sign(ensure_bytes('Hello World!'), priv_key)
assert algo.verify(ensure_bytes('Hello World!'), pub_key, signature)
signature = algo.sign(force_bytes('Hello World!'), priv_key)
assert algo.verify(force_bytes('Hello World!'), pub_key, signature)

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_rsa_jwk_private_key_with_other_primes_is_invalid(self):
Expand Down Expand Up @@ -337,7 +337,7 @@ 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(ensure_unicode(ec_key.read()))
algo.prepare_key(force_unicode(ec_key.read()))

@pytest.mark.skipif(not has_crypto, reason='Not supported without cryptography library')
def test_ec_should_accept_pem_private_key_bytes(self):
Expand All @@ -350,10 +350,10 @@ def test_ec_should_accept_pem_private_key_bytes(self):
def test_ec_verify_should_return_false_if_signature_invalid(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

message = ensure_bytes('Hello World!')
message = force_bytes('Hello World!')

# Mess up the signature by replacing a known byte
sig = base64.b64decode(ensure_bytes(
sig = base64.b64decode(force_bytes(
'AC+m4Jf/xI3guAC6w0w37t5zRpSCF6F4udEz5LiMiTIjCS4vcVe6dDOxK+M'
'mvkF8PxJuvqxP2CO3TR3okDPCl/NjATTO1jE+qBZ966CRQSSzcCM+tzcHzw'
'LZS5kbvKu0Acd/K6Ol2/W3B1NeV5F/gjvZn/jOwaLgWEUYsg0o4XVrAg65'.replace('r', 's')))
Expand All @@ -368,9 +368,9 @@ def test_ec_verify_should_return_false_if_signature_invalid(self):
def test_ec_verify_should_return_false_if_signature_wrong_length(self):
algo = ECAlgorithm(ECAlgorithm.SHA256)

message = ensure_bytes('Hello World!')
message = force_bytes('Hello World!')

sig = base64.b64decode(ensure_bytes('AC+m4Jf/xI3guAC6w0w3'))
sig = base64.b64decode(force_bytes('AC+m4Jf/xI3guAC6w0w3'))

with open(key_path('testkey_ec.pub'), 'r') as keyfile:
pub_key = algo.prepare_key(keyfile.read())
Expand All @@ -382,7 +382,7 @@ def test_ec_verify_should_return_false_if_signature_wrong_length(self):
def test_rsa_pss_sign_then_verify_should_return_true(self):
algo = RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256)

message = ensure_bytes('Hello World!')
message = force_bytes('Hello World!')

with open(key_path('testkey_rsa'), 'r') as keyfile:
priv_key = algo.prepare_key(keyfile.read())
Expand All @@ -398,17 +398,17 @@ def test_rsa_pss_sign_then_verify_should_return_true(self):
def test_rsa_pss_verify_should_return_false_if_signature_invalid(self):
algo = RSAPSSAlgorithm(RSAPSSAlgorithm.SHA256)

jwt_message = ensure_bytes('Hello World!')
jwt_message = force_bytes('Hello World!')

jwt_sig = base64.b64decode(ensure_bytes(
jwt_sig = base64.b64decode(force_bytes(
'ywKAUGRIDC//6X+tjvZA96yEtMqpOrSppCNfYI7NKyon3P7doud5v65oWNu'
'vQsz0fzPGfF7mQFGo9Cm9Vn0nljm4G6PtqZRbz5fXNQBH9k10gq34AtM02c'
'/cveqACQ8gF3zxWh6qr9jVqIpeMEaEBIkvqG954E0HT9s9ybHShgHX9mlWk'
'186/LopP4xe5c/hxOQjwhv6yDlTiwJFiqjNCvj0GyBKsc4iECLGIIO+4mC4'
'daOCWqbpZDuLb1imKpmm8Nsm56kAxijMLZnpCcnPgyb7CqG+B93W9GHglA5'
'drUeR1gRtO7vqbZMsCAQ4bpjXxwbYyjQlEVuMl73UL6sOWg=='))

jwt_sig += ensure_bytes('123') # Signature is now invalid
jwt_sig += force_bytes('123') # Signature is now invalid

with open(key_path('testkey_rsa.pub'), 'r') as keyfile:
jwt_pub_key = algo.prepare_key(keyfile.read())
Expand All @@ -430,15 +430,15 @@ def test_hmac_verify_should_return_true_for_test_vector(self):
Reference: https://tools.ietf.org/html/rfc7520#section-4.4
"""
signing_input = ensure_bytes(
signing_input = force_bytes(
'eyJhbGciOiJIUzI1NiIsImtpZCI6IjAxOGMwYWU1LTRkOWItNDcxYi1iZmQ2LWVlZ'
'jMxNGJjNzAzNyJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ'
'29pbmcgb3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIG'
'lmIHlvdSBkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmc'
'gd2hlcmUgeW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4'
)

signature = base64url_decode(ensure_bytes(
signature = base64url_decode(force_bytes(
's0h6KThzkfBBBkLspW1h84VsJZFTsPPqMDA7g1Md7p0'
))

Expand All @@ -456,15 +456,15 @@ def test_rsa_verify_should_return_true_for_test_vector(self):
Reference: https://tools.ietf.org/html/rfc7520#section-4.1
"""
signing_input = ensure_bytes(
signing_input = force_bytes(
'eyJhbGciOiJSUzI1NiIsImtpZCI6ImJpbGJvLmJhZ2dpbnNAaG9iYml0b24uZXhhb'
'XBsZSJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb'
'3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdS'
'Bkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmU'
'geW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4'
)

signature = base64url_decode(ensure_bytes(
signature = base64url_decode(force_bytes(
'MRjdkly7_-oTPTS3AXP41iQIGKa80A0ZmTuV5MEaHoxnW2e5CZ5NlKtainoFmKZop'
'dHM1O2U4mwzJdQx996ivp83xuglII7PNDi84wnB-BDkoBwA78185hX-Es4JIwmDLJ'
'K3lfWRa-XtL0RnltuYv746iYTh_qHRD68BNt1uSNCrUCTJDt5aAE6x8wW1Kt9eRo4'
Expand All @@ -487,15 +487,15 @@ def test_rsapss_verify_should_return_true_for_test_vector(self):
Reference: https://tools.ietf.org/html/rfc7520#section-4.2
"""
signing_input = ensure_bytes(
signing_input = force_bytes(
'eyJhbGciOiJQUzM4NCIsImtpZCI6ImJpbGJvLmJhZ2dpbnNAaG9iYml0b24uZXhhb'
'XBsZSJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb'
'3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdS'
'Bkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmU'
'geW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4'
)

signature = base64url_decode(ensure_bytes(
signature = base64url_decode(force_bytes(
'cu22eBqkYDKgIlTpzDXGvaFfz6WGoz7fUDcfT0kkOy42miAh2qyBzk1xEsnk2IpN6'
'-tPid6VrklHkqsGqDqHCdP6O8TTB5dDDItllVo6_1OLPpcbUrhiUSMxbbXUvdvWXz'
'g-UD8biiReQFlfz28zGWVsdiNAUf8ZnyPEgVFn442ZdNqiVJRmBqrYRXe8P_ijQ7p'
Expand All @@ -518,15 +518,15 @@ def test_ec_verify_should_return_true_for_test_vector(self):
Reference: https://tools.ietf.org/html/rfc7520#section-4.3
"""
signing_input = ensure_bytes(
signing_input = force_bytes(
'eyJhbGciOiJFUzUxMiIsImtpZCI6ImJpbGJvLmJhZ2dpbnNAaG9iYml0b24uZXhhb'
'XBsZSJ9.SXTigJlzIGEgZGFuZ2Vyb3VzIGJ1c2luZXNzLCBGcm9kbywgZ29pbmcgb'
'3V0IHlvdXIgZG9vci4gWW91IHN0ZXAgb250byB0aGUgcm9hZCwgYW5kIGlmIHlvdS'
'Bkb24ndCBrZWVwIHlvdXIgZmVldCwgdGhlcmXigJlzIG5vIGtub3dpbmcgd2hlcmU'
'geW91IG1pZ2h0IGJlIHN3ZXB0IG9mZiB0by4'
)

signature = base64url_decode(ensure_bytes(
signature = base64url_decode(force_bytes(
'AE_R_YZCChjn4791jSQCrdPZCNYqHXCTZH0-JZGYNlaAjP2kqaluUIIUnC9qvbu9P'
'lon7KRTzoNEuT4Va2cmL1eJAQy3mtPBu_u_sDDyYjnAMDxXPn7XrT0lw-kvAD890j'
'l8e2puQens_IEKBpHABlsbEPX6sFY8OcGDqoRuBomu9xQ2'
Expand Down
Loading

0 comments on commit bfa24f3

Please sign in to comment.