<s> import os <EOL> import pytest <EOL> from oic . oauth2 import MissingSigningKey <EOL> from oic . oic import AuthorizationResponse <EOL> from oic . utils . keyio import key_export , rsa_init , build_keyjar <EOL> from oic . utils . keyio import KeyJar <EOL> from oic . utils . keyio import KeyBundle <EOL> from oic . utils . keyio import keybundle_from_local_file <EOL> from oic . utils . keyio import RSAKey <EOL> __author__ = '<STR_LIT>' <EOL> BASE_PATH = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , "<STR_LIT>" ) ) <EOL> RSAKEY = os . path . join ( BASE_PATH , "<STR_LIT>" ) <EOL> RSA0 = os . path . join ( BASE_PATH , "<STR_LIT>" ) <EOL> JWK0 = { "<STR_LIT>" : [ <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:e>' : '<STR_LIT>' , '<STR_LIT>' : "<STR_LIT:abc>" , <EOL> '<STR_LIT:n>' : '<STR_LIT>' } <EOL> ] } <EOL> JWK1 = { "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:n>" : "<STR_LIT>" , <EOL> "<STR_LIT:e>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { <EOL> "<STR_LIT:k>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } , <EOL> ] } <EOL> JWK2 = { <EOL> "<STR_LIT>" : [ <EOL> { <EOL> "<STR_LIT:e>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:n>" : <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT:e>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:n>" : <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT:e>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:n>" : <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } , <EOL> { <EOL> "<STR_LIT:e>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:n>" : <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : [ <EOL> "<STR_LIT>" <EOL> ] , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> ] <EOL> } <EOL> def test_key_export ( ) : <EOL> kj = KeyJar ( ) <EOL> url = key_export ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> keyjar = kj , sig = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } ) <EOL> assert url == "<STR_LIT>" <EOL> def test_rsa_init ( tmpdir ) : <EOL> path = tmpdir . strpath <EOL> res = rsa_init ( { '<STR_LIT>' : [ '<STR_LIT>' ] , '<STR_LIT:type>' : '<STR_LIT>' , '<STR_LIT:size>' : <NUM_LIT> , <EOL> '<STR_LIT:name>' : os . path . join ( path , "<STR_LIT>" ) } ) <EOL> assert res <EOL> def test_keybundle_from_local_jwk_file ( ) : <EOL> kb = keybundle_from_local_file ( <EOL> "<STR_LIT>" . format ( os . path . join ( BASE_PATH , "<STR_LIT>" ) ) , <EOL> "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert len ( kb ) == <NUM_LIT:1> <EOL> kj = KeyJar ( ) <EOL> kj . issuer_keys [ "<STR_LIT>" ] = [ kb ] <EOL> keys = kj . get_signing_key ( ) <EOL> assert len ( keys ) == <NUM_LIT:1> <EOL> key = keys [ <NUM_LIT:0> ] <EOL> assert isinstance ( key , RSAKey ) <EOL> assert key . kid == "<STR_LIT:abc>" <EOL> def test_build_keyjar ( ) : <EOL> keys = [ <EOL> { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : [ "<STR_LIT>" , "<STR_LIT>" ] } , <EOL> { "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : [ "<STR_LIT>" ] } , <EOL> ] <EOL> jwks , keyjar , kidd = build_keyjar ( keys ) <EOL> for key in jwks [ "<STR_LIT>" ] : <EOL> assert "<STR_LIT:d>" not in key <EOL> assert len ( keyjar [ "<STR_LIT>" ] ) == <NUM_LIT:2> <EOL> assert "<STR_LIT>" in kidd [ "<STR_LIT>" ] <EOL> assert "<STR_LIT>" in kidd [ "<STR_LIT>" ] <EOL> assert "<STR_LIT>" in kidd [ "<STR_LIT>" ] <EOL> class TestKeyBundle ( object ) : <EOL> def test_update ( self ) : <EOL> kc = KeyBundle ( [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] ) <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:1> <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> assert kc . remote is False <EOL> assert kc . source is None <EOL> kc . update ( ) <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:1> <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> assert kc . remote is False <EOL> assert kc . source is None <EOL> def test_update_RSA ( self ) : <EOL> kc = keybundle_from_local_file ( RSAKEY , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert kc . remote is False <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:2> <EOL> key = kc . get ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> assert isinstance ( key , RSAKey ) <EOL> kc . update ( ) <EOL> assert kc . remote is False <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:0> <EOL> assert len ( kc . get ( "<STR_LIT>" ) ) == <NUM_LIT:2> <EOL> key = kc . get ( "<STR_LIT>" ) [ <NUM_LIT:0> ] <EOL> assert isinstance ( key , RSAKey ) <EOL> class TestKeyJar ( object ) : <EOL> def test_keyjar_group_keys ( self ) : <EOL> ks = KeyJar ( ) <EOL> ks [ "<STR_LIT>" ] = KeyBundle ( [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] ) <EOL> ks [ "<STR_LIT>" ] = KeyBundle ( [ <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] ) <EOL> ks [ "<STR_LIT>" ] . append ( <EOL> keybundle_from_local_file ( RSAKEY , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" ] ) ) <EOL> verified_keys = ks . verify_keys ( "<STR_LIT>" ) <EOL> assert len ( verified_keys ) == <NUM_LIT:3> <EOL> assert len ( [ k for k in verified_keys if k . kty == "<STR_LIT>" ] ) == <NUM_LIT:2> <EOL> assert len ( [ k for k in verified_keys if k . kty == "<STR_LIT>" ] ) == <NUM_LIT:1> <EOL> def test_remove_key ( self ) : <EOL> ks = KeyJar ( ) <EOL> ks [ "<STR_LIT>" ] = KeyBundle ( [ { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] ) <EOL> ks [ "<STR_LIT>" ] = [ <EOL> KeyBundle ( [ <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } , <EOL> { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:key>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } ] ) , <EOL> keybundle_from_local_file ( RSAKEY , "<STR_LIT>" , [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> ] <EOL> ks [ "<STR_LIT>" ] = keybundle_from_local_file ( RSA0 , "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> coll = ks [ "<STR_LIT>" ] <EOL> assert len ( coll ) == <NUM_LIT:2> <EOL> keys = ks . get_encrypt_key ( key_type = "<STR_LIT>" , <EOL> owner = "<STR_LIT>" ) <EOL> assert len ( keys ) == <NUM_LIT:1> <EOL> _key = keys [ <NUM_LIT:0> ] <EOL> ks . remove_key ( "<STR_LIT>" , "<STR_LIT>" , _key ) <EOL> coll = ks [ "<STR_LIT>" ] <EOL> assert len ( coll ) == <NUM_LIT:1> <EOL> keys = ks . get_encrypt_key ( key_type = "<STR_LIT>" , <EOL> owner = "<STR_LIT>" ) <EOL> assert len ( keys ) == <NUM_LIT:0> <EOL> keys = ks . verify_keys ( "<STR_LIT>" ) <EOL> assert len ( keys ) == <NUM_LIT:1> <EOL> assert len ( [ k for k in keys if k . kty == "<STR_LIT>" ] ) == <NUM_LIT:1> <EOL> keys = ks . decrypt_keys ( "<STR_LIT>" ) <EOL> assert keys == [ ] <EOL> def test_get_by_kid ( self ) : <EOL> kb = keybundle_from_local_file ( "<STR_LIT>" % BASE_PATH , "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> kj = KeyJar ( ) <EOL> kj . issuer_keys [ "<STR_LIT>" ] = [ kb ] <EOL> _key = kj . get_key_by_kid ( "<STR_LIT:abc>" , "<STR_LIT>" ) <EOL> assert _key <EOL> assert _key . kid == "<STR_LIT:abc>" <EOL> def test_dump_issuer_keys ( self ) : <EOL> kb = keybundle_from_local_file ( "<STR_LIT>" % BASE_PATH , "<STR_LIT>" , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> assert len ( kb ) == <NUM_LIT:1> <EOL> kj = KeyJar ( ) <EOL> kj . issuer_keys [ "<STR_LIT>" ] = [ kb ] <EOL> res = kj . dump_issuer_keys ( "<STR_LIT>" ) <EOL> assert len ( res ) == <NUM_LIT:1> <EOL> assert res [ <NUM_LIT:0> ] == { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:e>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:n>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:abc>' } <EOL> def test_no_use ( self ) : <EOL> kb = KeyBundle ( JWK0 [ "<STR_LIT>" ] ) <EOL> kj = KeyJar ( ) <EOL> kj . issuer_keys [ "<STR_LIT>" ] = [ kb ] <EOL> enc_key = kj . get_encrypt_key ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> assert enc_key != [ ] <EOL> @ pytest . mark . network <EOL> def test_provider ( self ) : <EOL> provider_info = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> } <EOL> ks = KeyJar ( ) <EOL> ks . load_keys ( provider_info , "<STR_LIT>" ) <EOL> assert ks [ "<STR_LIT>" ] [ <NUM_LIT:0> ] . keys ( ) <EOL> def test_issuer_mismatch ( self ) : <EOL> ISSUER = "<STR_LIT>" <EOL> kb = KeyBundle ( JWK2 [ "<STR_LIT>" ] ) <EOL> kj = KeyJar ( ) <EOL> kj . issuer_keys [ ISSUER ] = [ kb ] <EOL> kj . issuer_keys [ "<STR_LIT>" ] = [ ] <EOL> authz_resp = AuthorizationResponse ( ) . from_urlencoded ( <EOL> "<STR_LIT>" ) <EOL> try : <EOL> authz_resp . verify ( keyjar = kj , skew = <NUM_LIT> ) <EOL> except MissingSigningKey : <EOL> authz_resp . verify ( keyjar = kj , sender = ISSUER , skew = <NUM_LIT> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_rsa_init ( ) </s>
