<s> import json <EOL> import mock <EOL> import unittest <EOL> from base64 import b64encode <EOL> from copy import copy , deepcopy <EOL> from itertools import product <EOL> from time import time <EOL> from Crypto . Cipher import AES <EOL> from Crypto . PublicKey import RSA <EOL> from Crypto . Random import get_random_bytes <EOL> import jose <EOL> rsa_key = RSA . generate ( <NUM_LIT> ) <EOL> rsa_priv_key = { <EOL> '<STR_LIT:k>' : rsa_key . exportKey ( '<STR_LIT>' ) , <EOL> } <EOL> rsa_pub_key = { <EOL> '<STR_LIT:k>' : rsa_key . publickey ( ) . exportKey ( '<STR_LIT>' ) , <EOL> } <EOL> claims = { '<STR_LIT>' : '<STR_LIT>' } <EOL> def legacy_encrypt ( claims , jwk , adata = '<STR_LIT>' , add_header = None , alg = '<STR_LIT>' , <EOL> enc = '<STR_LIT>' , rng = get_random_bytes , compression = None , version = None ) : <EOL> header = dict ( ( add_header or { } ) . items ( ) + [ <EOL> ( '<STR_LIT>' , enc ) , ( '<STR_LIT>' , alg ) ] ) <EOL> if version == <NUM_LIT:1> : <EOL> claims = deepcopy ( claims ) <EOL> assert jose . _TEMP_VER_KEY not in claims <EOL> claims [ jose . _TEMP_VER_KEY ] = version <EOL> assert jose . _TEMP_VER_KEY not in header <EOL> header [ jose . _TEMP_VER_KEY ] = version <EOL> plaintext = jose . json_encode ( claims ) <EOL> if compression is not None : <EOL> header [ '<STR_LIT>' ] = compression <EOL> try : <EOL> ( compress , _ ) = jose . COMPRESSION [ compression ] <EOL> except KeyError : <EOL> raise jose . Error ( <EOL> '<STR_LIT>' . format ( compression ) ) <EOL> plaintext = compress ( plaintext ) <EOL> ( ( cipher , _ ) , key_size ) , ( ( hash_fn , _ ) , hash_mod ) = jose . JWA [ enc ] <EOL> iv = rng ( AES . block_size ) <EOL> if version == <NUM_LIT:1> : <EOL> encryption_key = rng ( hash_mod . digest_size ) <EOL> cipher_key = encryption_key [ - hash_mod . digest_size / <NUM_LIT:2> : ] <EOL> mac_key = encryption_key [ : - hash_mod . digest_size / <NUM_LIT:2> ] <EOL> else : <EOL> encryption_key = rng ( ( key_size // <NUM_LIT:8> ) + hash_mod . digest_size ) <EOL> cipher_key = encryption_key [ : - hash_mod . digest_size ] <EOL> mac_key = encryption_key [ - hash_mod . digest_size : ] <EOL> ciphertext = cipher ( plaintext , cipher_key , iv ) <EOL> hash = hash_fn ( jose . _jwe_hash_str ( ciphertext , iv , adata , version ) , mac_key , hash_mod ) <EOL> ( cipher , _ ) , _ = jose . JWA [ alg ] <EOL> encryption_key_ciphertext = cipher ( encryption_key , jwk ) <EOL> return jose . JWE ( * map ( jose . b64encode_url , <EOL> ( jose . json_encode ( header ) , <EOL> encryption_key_ciphertext , <EOL> iv , <EOL> ciphertext , <EOL> jose . auth_tag ( hash ) ) ) ) <EOL> class TestLegacyDecrypt ( unittest . TestCase ) : <EOL> def test_jwe ( self ) : <EOL> bad_key = { '<STR_LIT:k>' : RSA . generate ( <NUM_LIT> ) . exportKey ( '<STR_LIT>' ) } <EOL> jwe = legacy_encrypt ( claims , rsa_pub_key ) <EOL> token = jose . serialize_compact ( jwe ) <EOL> jwt = jose . decrypt ( jose . deserialize_compact ( token ) , rsa_priv_key ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> self . assertNotIn ( jose . _TEMP_VER_KEY , claims ) <EOL> try : <EOL> jose . decrypt ( jose . deserialize_compact ( token ) , bad_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> def test_version1 ( self ) : <EOL> bad_key = { '<STR_LIT:k>' : RSA . generate ( <NUM_LIT> ) . exportKey ( '<STR_LIT>' ) } <EOL> jwe = legacy_encrypt ( claims , rsa_pub_key , version = <NUM_LIT:1> ) <EOL> token = jose . serialize_compact ( jwe ) <EOL> jwt = jose . decrypt ( jose . deserialize_compact ( token ) , rsa_priv_key ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> self . assertEqual ( jwt . header . get ( jose . _TEMP_VER_KEY ) , <NUM_LIT:1> ) <EOL> class TestSerializeDeserialize ( unittest . TestCase ) : <EOL> def test_serialize ( self ) : <EOL> try : <EOL> jose . deserialize_compact ( '<STR_LIT>' ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> class TestJWE ( unittest . TestCase ) : <EOL> encs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> algs = ( ( '<STR_LIT>' , rsa_key ) , ) <EOL> def test_jwe ( self ) : <EOL> bad_key = { '<STR_LIT:k>' : RSA . generate ( <NUM_LIT> ) . exportKey ( '<STR_LIT>' ) } <EOL> for ( alg , jwk ) , enc in product ( self . algs , self . encs ) : <EOL> jwe = jose . encrypt ( claims , rsa_pub_key , enc = enc , alg = alg ) <EOL> try : <EOL> json . loads ( jose . b64decode_url ( jwe . ciphertext ) ) <EOL> self . fail ( ) <EOL> except ValueError : <EOL> pass <EOL> token = jose . serialize_compact ( jwe ) <EOL> jwt = jose . legacy_decrypt ( jose . deserialize_compact ( token ) , rsa_priv_key ) <EOL> self . assertNotIn ( jose . _TEMP_VER_KEY , claims ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> try : <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( token ) , bad_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> def test_jwe_add_header ( self ) : <EOL> add_header = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> for ( alg , jwk ) , enc in product ( self . algs , self . encs ) : <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key , <EOL> add_header = add_header ) ) <EOL> jwt = jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> self . assertEqual ( jwt . header [ '<STR_LIT:foo>' ] , add_header [ '<STR_LIT:foo>' ] ) <EOL> def test_jwe_adata ( self ) : <EOL> adata = '<STR_LIT>' <EOL> for ( alg , jwk ) , enc in product ( self . algs , self . encs ) : <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key , <EOL> adata = adata ) ) <EOL> jwt = jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> adata = adata ) <EOL> try : <EOL> hdr , dt = jose . legacy_decrypt ( jose . deserialize_compact ( et ) , <EOL> rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> def test_jwe_invalid_base64 ( self ) : <EOL> try : <EOL> jose . legacy_decrypt ( '<STR_LIT>' , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> pass <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_jwe_no_error_with_exp_claim ( self ) : <EOL> claims = { jose . CLAIM_EXPIRATION_TIME : int ( time ( ) ) + <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> def test_jwe_expired_error_with_exp_claim ( self ) : <EOL> claims = { jose . CLAIM_EXPIRATION_TIME : int ( time ( ) ) - <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> try : <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Expired as e : <EOL> pass <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> jose . _format_timestamp ( claims [ jose . CLAIM_EXPIRATION_TIME ] ) <EOL> ) <EOL> ) <EOL> def test_jwe_no_error_with_iat_claim ( self ) : <EOL> claims = { jose . CLAIM_ISSUED_AT : int ( time ( ) ) - <NUM_LIT:15> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> expiry_seconds = <NUM_LIT:20> ) <EOL> def test_jwe_expired_error_with_iat_claim ( self ) : <EOL> expiry_seconds = <NUM_LIT:10> <EOL> claims = { jose . CLAIM_ISSUED_AT : int ( time ( ) ) - <NUM_LIT:15> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> try : <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> expiry_seconds = expiry_seconds ) <EOL> self . fail ( ) <EOL> except jose . Expired as e : <EOL> pass <EOL> expiration_time = claims [ jose . CLAIM_ISSUED_AT ] + expiry_seconds <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> jose . _format_timestamp ( expiration_time ) <EOL> ) <EOL> ) <EOL> def test_jwe_no_error_with_nbf_claim ( self ) : <EOL> claims = { jose . CLAIM_NOT_BEFORE : int ( time ( ) ) - <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> def test_jwe_not_yet_valid_error_with_nbf_claim ( self ) : <EOL> claims = { jose . CLAIM_NOT_BEFORE : int ( time ( ) ) + <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> try : <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . NotYetValid as e : <EOL> pass <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> jose . _format_timestamp ( claims [ jose . CLAIM_NOT_BEFORE ] ) <EOL> ) <EOL> ) <EOL> def test_jwe_ignores_expired_token_if_validate_claims_is_false ( self ) : <EOL> claims = { jose . CLAIM_EXPIRATION_TIME : int ( time ( ) ) - <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . encrypt ( claims , rsa_pub_key ) ) <EOL> jose . legacy_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> validate_claims = False ) <EOL> def test_format_timestamp ( self ) : <EOL> self . assertEquals ( <EOL> jose . _format_timestamp ( <NUM_LIT> ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_jwe_compression ( self ) : <EOL> local_claims = copy ( claims ) <EOL> for v in xrange ( <NUM_LIT:1000> ) : <EOL> local_claims [ '<STR_LIT>' + str ( v ) ] = '<STR_LIT:0>' * <NUM_LIT:100> <EOL> jwe = jose . serialize_compact ( jose . encrypt ( local_claims , rsa_pub_key ) ) <EOL> _ , _ , _ , uncompressed_ciphertext , _ = jwe . split ( '<STR_LIT:.>' ) <EOL> jwe = jose . serialize_compact ( jose . encrypt ( local_claims , rsa_pub_key , <EOL> compression = '<STR_LIT>' ) ) <EOL> _ , _ , _ , compressed_ciphertext , _ = jwe . split ( '<STR_LIT:.>' ) <EOL> self . assertTrue ( len ( compressed_ciphertext ) < <EOL> len ( uncompressed_ciphertext ) ) <EOL> jwt = jose . legacy_decrypt ( jose . deserialize_compact ( jwe ) , rsa_priv_key ) <EOL> self . assertEqual ( jwt . claims , local_claims ) <EOL> def test_encrypt_invalid_compression_error ( self ) : <EOL> try : <EOL> jose . encrypt ( claims , rsa_pub_key , compression = '<STR_LIT>' ) <EOL> self . fail ( ) <EOL> except jose . Error : <EOL> pass <EOL> def test_decrypt_invalid_compression_error ( self ) : <EOL> jwe = jose . encrypt ( claims , rsa_pub_key , compression = '<STR_LIT>' ) <EOL> header = jose . b64encode_url ( <EOL> jose . json_encode ( <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , <EOL> jose . _TEMP_VER_KEY : jose . _TEMP_VER , "<STR_LIT>" : "<STR_LIT>" } <EOL> ) <EOL> ) <EOL> try : <EOL> jose . legacy_decrypt ( jose . JWE ( * ( ( header , ) + ( jwe [ <NUM_LIT:1> : ] ) ) ) , <EOL> rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( <EOL> e . message , '<STR_LIT>' ) <EOL> class TestSpecCompliantJWE ( unittest . TestCase ) : <EOL> encs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> algs = ( ( '<STR_LIT>' , rsa_key ) , ) <EOL> def test_jwe ( self ) : <EOL> bad_key = { '<STR_LIT:k>' : RSA . generate ( <NUM_LIT> ) . exportKey ( '<STR_LIT>' ) } <EOL> for ( alg , jwk ) , enc in product ( self . algs , self . encs ) : <EOL> jwe = jose . spec_compliant_encrypt ( claims , rsa_pub_key , enc = enc , alg = alg ) <EOL> try : <EOL> json . loads ( jose . b64decode_url ( jwe . ciphertext ) ) <EOL> self . fail ( ) <EOL> except ValueError : <EOL> pass <EOL> token = jose . serialize_compact ( jwe ) <EOL> jwt = jose . spec_compliant_decrypt ( jose . deserialize_compact ( token ) , rsa_priv_key ) <EOL> self . assertNotIn ( jose . _TEMP_VER_KEY , claims ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> try : <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( token ) , bad_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> def test_jwe_add_header ( self ) : <EOL> add_header = { '<STR_LIT:foo>' : '<STR_LIT:bar>' } <EOL> for ( alg , jwk ) , enc in product ( self . algs , self . encs ) : <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key , <EOL> add_header = add_header ) ) <EOL> jwt = jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> self . assertEqual ( jwt . header [ '<STR_LIT:foo>' ] , add_header [ '<STR_LIT:foo>' ] ) <EOL> def test_jwe_invalid_base64 ( self ) : <EOL> try : <EOL> jose . spec_compliant_decrypt ( '<STR_LIT>' , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> pass <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_jwe_no_error_with_exp_claim ( self ) : <EOL> claims = { jose . CLAIM_EXPIRATION_TIME : int ( time ( ) ) + <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> def test_jwe_expired_error_with_exp_claim ( self ) : <EOL> claims = { jose . CLAIM_EXPIRATION_TIME : int ( time ( ) ) - <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> try : <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Expired as e : <EOL> pass <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> jose . _format_timestamp ( claims [ jose . CLAIM_EXPIRATION_TIME ] ) <EOL> ) <EOL> ) <EOL> def test_jwe_no_error_with_iat_claim ( self ) : <EOL> claims = { jose . CLAIM_ISSUED_AT : int ( time ( ) ) - <NUM_LIT:15> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> expiry_seconds = <NUM_LIT:20> ) <EOL> def test_jwe_expired_error_with_iat_claim ( self ) : <EOL> expiry_seconds = <NUM_LIT:10> <EOL> claims = { jose . CLAIM_ISSUED_AT : int ( time ( ) ) - <NUM_LIT:15> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> try : <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> expiry_seconds = expiry_seconds ) <EOL> self . fail ( ) <EOL> except jose . Expired as e : <EOL> pass <EOL> expiration_time = claims [ jose . CLAIM_ISSUED_AT ] + expiry_seconds <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> jose . _format_timestamp ( expiration_time ) <EOL> ) <EOL> ) <EOL> def test_jwe_no_error_with_nbf_claim ( self ) : <EOL> claims = { jose . CLAIM_NOT_BEFORE : int ( time ( ) ) - <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> def test_jwe_not_yet_valid_error_with_nbf_claim ( self ) : <EOL> claims = { jose . CLAIM_NOT_BEFORE : int ( time ( ) ) + <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> try : <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . NotYetValid as e : <EOL> pass <EOL> self . assertEquals ( <EOL> e . args [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' . format ( <EOL> jose . _format_timestamp ( claims [ jose . CLAIM_NOT_BEFORE ] ) <EOL> ) <EOL> ) <EOL> def test_jwe_ignores_expired_token_if_validate_claims_is_false ( self ) : <EOL> claims = { jose . CLAIM_EXPIRATION_TIME : int ( time ( ) ) - <NUM_LIT:5> } <EOL> et = jose . serialize_compact ( jose . spec_compliant_encrypt ( claims , rsa_pub_key ) ) <EOL> jose . spec_compliant_decrypt ( jose . deserialize_compact ( et ) , rsa_priv_key , <EOL> validate_claims = False ) <EOL> def test_format_timestamp ( self ) : <EOL> self . assertEquals ( <EOL> jose . _format_timestamp ( <NUM_LIT> ) , <EOL> '<STR_LIT>' <EOL> ) <EOL> def test_jwe_compression ( self ) : <EOL> local_claims = copy ( claims ) <EOL> for v in xrange ( <NUM_LIT:1000> ) : <EOL> local_claims [ '<STR_LIT>' + str ( v ) ] = '<STR_LIT:0>' * <NUM_LIT:100> <EOL> jwe = jose . serialize_compact ( <EOL> jose . spec_compliant_encrypt ( local_claims , rsa_pub_key ) <EOL> ) <EOL> _ , _ , _ , uncompressed_ciphertext , _ = jwe . split ( '<STR_LIT:.>' ) <EOL> jwe = jose . serialize_compact ( <EOL> jose . spec_compliant_encrypt ( local_claims , rsa_pub_key , <EOL> add_header = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> ) <EOL> _ , _ , _ , compressed_ciphertext , _ = jwe . split ( '<STR_LIT:.>' ) <EOL> self . assertTrue ( len ( compressed_ciphertext ) < <EOL> len ( uncompressed_ciphertext ) ) <EOL> jwt = jose . spec_compliant_decrypt ( jose . deserialize_compact ( jwe ) , <EOL> rsa_priv_key ) <EOL> self . assertEqual ( jwt . claims , local_claims ) <EOL> def test_encrypt_invalid_compression_error ( self ) : <EOL> try : <EOL> jose . spec_compliant_encrypt ( claims , rsa_pub_key , <EOL> add_header = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . fail ( ) <EOL> except jose . Error : <EOL> pass <EOL> def test_decrypt_invalid_compression_error ( self ) : <EOL> with mock . patch . dict ( jose . COMPRESSION , <EOL> { '<STR_LIT>' : jose . COMPRESSION [ '<STR_LIT>' ] } ) : <EOL> jwe = jose . spec_compliant_encrypt ( claims , rsa_pub_key , <EOL> add_header = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> try : <EOL> jose . spec_compliant_decrypt ( jwe , rsa_priv_key ) <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( <EOL> e . message , '<STR_LIT>' ) <EOL> class TestJWS ( unittest . TestCase ) : <EOL> def test_jws_sym ( self ) : <EOL> algs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , ) <EOL> jwk = { '<STR_LIT:k>' : '<STR_LIT:password>' } <EOL> for alg in algs : <EOL> st = jose . serialize_compact ( jose . sign ( claims , jwk , alg = alg ) ) <EOL> jwt = jose . verify ( jose . deserialize_compact ( st ) , jwk , alg ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> def test_jws_asym ( self ) : <EOL> algs = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> for alg in algs : <EOL> st = jose . serialize_compact ( jose . sign ( claims , rsa_priv_key , <EOL> alg = alg ) ) <EOL> jwt = jose . verify ( jose . deserialize_compact ( st ) , rsa_pub_key , alg ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> def test_jws_signature_mismatch_error ( self ) : <EOL> alg = '<STR_LIT>' <EOL> jwk = { '<STR_LIT:k>' : '<STR_LIT:password>' } <EOL> jws = jose . sign ( claims , jwk , alg = alg ) <EOL> try : <EOL> jose . verify ( jose . JWS ( jws . header , jws . payload , '<STR_LIT>' ) , jwk , alg ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> def test_jws_invalid_algorithm_error ( self ) : <EOL> sign_alg = '<STR_LIT>' <EOL> verify_alg = '<STR_LIT>' <EOL> jwk = { '<STR_LIT:k>' : '<STR_LIT:password>' } <EOL> jws = jose . sign ( claims , jwk , alg = sign_alg ) <EOL> try : <EOL> jose . verify ( jose . JWS ( jws . header , jws . payload , '<STR_LIT>' ) , jwk , <EOL> verify_alg ) <EOL> except jose . Error as e : <EOL> self . assertEqual ( e . message , '<STR_LIT>' ) <EOL> PRIVATE_KEY = """<STR_LIT>""" <EOL> SPEC_COMPLIANT_TOKEN = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> LEGACY_V1_TOKEN = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> class TestDecryptCompatibility ( unittest . TestCase ) : <EOL> def test_jwe_decrypt_compliant ( self ) : <EOL> jwk = { '<STR_LIT:k>' : PRIVATE_KEY } <EOL> legacy_patch = mock . patch . object ( <EOL> jose , '<STR_LIT>' , wraps = jose . legacy_decrypt <EOL> ) <EOL> spec_patch = mock . patch . object ( <EOL> jose , '<STR_LIT>' , wraps = jose . spec_compliant_decrypt <EOL> ) <EOL> with legacy_patch as legacy_mock , spec_patch as spec_mock : <EOL> jwt = jose . decrypt ( <EOL> jose . deserialize_compact ( SPEC_COMPLIANT_TOKEN ) , jwk <EOL> ) <EOL> self . assertEqual ( legacy_mock . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( spec_mock . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> expected_header = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> self . assertEqual ( jwt . header , expected_header ) <EOL> def test_jwe_decrypt_legacy_v1 ( self ) : <EOL> jwk = { '<STR_LIT:k>' : PRIVATE_KEY } <EOL> legacy_patch = mock . patch . object ( <EOL> jose , '<STR_LIT>' , wraps = jose . legacy_decrypt <EOL> ) <EOL> spec_patch = mock . patch . object ( <EOL> jose , '<STR_LIT>' , wraps = jose . spec_compliant_decrypt <EOL> ) <EOL> with legacy_patch as legacy_mock , spec_patch as spec_mock : <EOL> jwt = jose . decrypt ( jose . deserialize_compact ( LEGACY_V1_TOKEN ) , jwk ) <EOL> self . assertEqual ( legacy_mock . call_count , <NUM_LIT:1> ) <EOL> self . assertEqual ( spec_mock . call_count , <NUM_LIT:0> ) <EOL> self . assertEqual ( jwt . claims , claims ) <EOL> expected_header = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:1> <EOL> } <EOL> self . assertEqual ( jwt . header , expected_header ) <EOL> class TestUtils ( unittest . TestCase ) : <EOL> def test_b64encode_url_utf8 ( self ) : <EOL> istr = '<STR_LIT>' . encode ( '<STR_LIT:utf8>' ) <EOL> encoded = jose . b64encode_url ( istr ) <EOL> self . assertEqual ( jose . b64decode_url ( encoded ) , istr ) <EOL> def test_b64encode_url_ascii ( self ) : <EOL> istr = '<STR_LIT>' <EOL> encoded = jose . b64encode_url ( istr ) <EOL> self . assertEqual ( jose . b64decode_url ( encoded ) , istr ) <EOL> def test_b64encode_url ( self ) : <EOL> istr = '<STR_LIT>' <EOL> self . assertEqual ( b64encode ( istr ) [ - <NUM_LIT:1> ] , '<STR_LIT:=>' ) <EOL> self . assertNotEqual ( jose . b64encode_url ( istr ) , '<STR_LIT:=>' ) <EOL> class TestJWA ( unittest . TestCase ) : <EOL> def test_lookup ( self ) : <EOL> impl = jose . _JWA . _impl <EOL> jose . _JWA . _impl = dict ( ( k , k ) for k in ( <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( jose . JWA [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( jose . JWA [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( jose . JWA [ '<STR_LIT>' ] , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> self . assertEqual ( jose . JWA [ '<STR_LIT>' ] , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> jose . _JWA . _impl = impl <EOL> def test_invalid_error ( self ) : <EOL> try : <EOL> jose . JWA [ '<STR_LIT>' ] <EOL> self . fail ( ) <EOL> except jose . Error as e : <EOL> self . assertTrue ( e . message . startswith ( '<STR_LIT>' ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
