Skip to content
This repository was archived by the owner on Jun 1, 2023. It is now read-only.

Commit 9e3964d

Browse files
committed
Updated to work with oidcmsg 0.5.0
1 parent c050b9a commit 9e3964d

5 files changed

Lines changed: 65 additions & 69 deletions

File tree

chrp/rp.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import cherrypy
99

10-
from oidcmsg.key_jar import build_keyjar
10+
from oidcmsg.key_jar import build_keyjar, init_key_jar
1111
from oidcmsg.key_jar import KeyJar
1212

1313
from oidcrp import RPHandler
@@ -25,29 +25,6 @@
2525
SIGKEY_NAME = 'sigkey.jwks'
2626

2727

28-
def get_jwks(private_path, keydefs, public_path):
29-
if os.path.isfile(private_path):
30-
_jwks = open(private_path, 'r').read()
31-
_kj = KeyJar()
32-
_kj.import_jwks(json.loads(_jwks), '')
33-
else:
34-
_kj = build_keyjar(keydefs)[1]
35-
jwks = _kj.export_jwks(private=True)
36-
head, tail = os.path.split(private_path)
37-
if not os.path.isdir(head):
38-
os.makedirs(head)
39-
fp = open(private_path, 'w')
40-
fp.write(json.dumps(jwks))
41-
fp.close()
42-
43-
jwks = _kj.export_jwks() # public part
44-
fp = open(public_path, 'w')
45-
fp.write(json.dumps(jwks))
46-
fp.close()
47-
48-
return _kj
49-
50-
5128
if __name__ == '__main__':
5229
import argparse
5330

@@ -103,8 +80,9 @@ def get_jwks(private_path, keydefs, public_path):
10380

10481
_base_url = config.BASEURL
10582

106-
_kj = get_jwks(config.PRIVATE_JWKS_PATH, config.KEYDEFS,
107-
config.PUBLIC_JWKS_PATH)
83+
_kj = init_key_jar(public_path=config.PUBLIC_JWKS_PATH,
84+
private_path=config.PRIVATE_JWKS_PATH,
85+
key_defs=config.KEYDEFS)
10886

10987
if args.insecure:
11088
_kj.verify_ssl = False

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def run_tests(self):
6565
"Topic :: Software Development :: Libraries :: Python Modules"],
6666
install_requires=[
6767
'cryptojwt>=0.3.1',
68-
'oidcservice>=0.5.10',
69-
'oidcmsg>=0.3.5'
68+
'oidcservice>=0.5.12',
69+
'oidcmsg>=0.5.0'
7070
],
7171
tests_require=[
7272
'pytest',

src/oidcrp/oauth2/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from oidcservice.service import SUCCESSFUL
1111
from oidcservice.service import build_services
1212
from oidcservice.service_context import ServiceContext
13+
from oidcservice.state_interface import StateInterface
1314

1415
from oidcrp.http import HTTPLib
1516
from oidcrp.util import get_deserialization_method
@@ -59,7 +60,7 @@ def __init__(self, state_db, ca_certs=None, client_authn_factory=None,
5960
:return: Client instance
6061
"""
6162

62-
self.state_db = state_db
63+
self.session_interface = StateInterface(state_db)
6364
self.http = httplib or HTTPLib(ca_certs=ca_certs,
6465
verify_ssl=verify_ssl,
6566
client_cert=client_cert,

tests/test_11_oauth2.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
from cryptojwt.jwk import rsa_load
77

88
from oidcmsg.key_bundle import KeyBundle
9-
from oidcmsg.oauth2 import AccessTokenRequest, AuthorizationResponse
9+
from oidcmsg.oauth2 import AccessTokenRequest
1010
from oidcmsg.oauth2 import AccessTokenResponse
1111
from oidcmsg.oauth2 import AuthorizationRequest
12+
from oidcmsg.oauth2 import AuthorizationResponse
1213
from oidcmsg.oauth2 import RefreshAccessTokenRequest
1314
from oidcmsg.oidc import IdToken
1415
from oidcmsg.time_util import utc_time_sans_frac
1516

16-
from oidcservice.client_auth import CLIENT_AUTHN_METHOD
1717
from oidcservice.state_interface import State
1818

1919
from oidcrp.oauth2 import Client
2020

2121
sys.path.insert(0, '.')
2222

2323
_dirname = os.path.dirname(os.path.abspath(__file__))
24-
BASE_PATH = os.path.join(_dirname, "data", "keys")
24+
BASE_PATH = os.path.join(_dirname, "keys")
2525

2626
_key = rsa_load(os.path.join(BASE_PATH, "rsa.key"))
2727
KC_RSA = KeyBundle({"key": _key, "kty": "RSA", "use": "sig"})
@@ -60,7 +60,7 @@ def test_construct_authorization_request(self):
6060
'redirect_uri': 'https://example.com/auth_cb',
6161
'response_type': ['code']}
6262

63-
self.client.state_db.set('ABCDE', State(iss='issuer').to_json())
63+
self.client.session_interface.create_state('issuer','ABCDE')
6464
msg = self.client.service['authorization'].construct(
6565
request_args=req_args)
6666
assert isinstance(msg, AuthorizationRequest)
@@ -75,10 +75,12 @@ def test_construct_accesstoken_request(self):
7575
redirect_uri='https://example.com/cli/authz_cb',
7676
state='state'
7777
)
78+
self.client.session_interface.store_item(auth_request, 'auth_request',
79+
'ABCDE')
80+
7881
auth_response = AuthorizationResponse(code='access_code')
79-
_state = State(auth_response=auth_response.to_json(),
80-
auth_request=auth_request.to_json())
81-
self.client.state_db.set('ABCDE', _state.to_json())
82+
self.client.session_interface.store_item(auth_response,'auth_response',
83+
'ABCDE')
8284

8385
msg = self.client.service['accesstoken'].construct(
8486
request_args=req_args, state='ABCDE')
@@ -96,14 +98,16 @@ def test_construct_refresh_token_request(self):
9698
redirect_uri='https://example.com/cli/authz_cb',
9799
state='state'
98100
)
101+
self.client.session_interface.store_item(auth_request, 'auth_request',
102+
'ABCDE')
99103
auth_response = AuthorizationResponse(code='access_code')
104+
self.client.session_interface.store_item(auth_response,'auth_response',
105+
'ABCDE')
100106
token_response = AccessTokenResponse(refresh_token="refresh_with_me",
101107
access_token="access")
102-
_state = State(auth_response=auth_response.to_json(),
103-
auth_request=auth_request.to_json(),
104-
token_response=token_response.to_json())
108+
self.client.session_interface.store_item(token_response,
109+
'token_response', 'ABCDE')
105110

106-
self.client.state_db.set('ABCDE', _state.to_json())
107111
req_args = {}
108112
msg = self.client.service['refresh_token'].construct(
109113
request_args=req_args, state='ABCDE')

tests/test_14_oidc.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,22 @@
66
from cryptojwt.jwk import rsa_load
77

88
from oidcmsg.key_bundle import KeyBundle
9-
from oidcmsg.oauth2 import AccessTokenRequest, AuthorizationResponse
9+
from oidcmsg.oauth2 import AccessTokenRequest
1010
from oidcmsg.oauth2 import AccessTokenResponse
1111
from oidcmsg.oauth2 import AuthorizationRequest
12+
from oidcmsg.oauth2 import AuthorizationResponse
1213
from oidcmsg.oauth2 import RefreshAccessTokenRequest
1314
from oidcmsg.oidc import IdToken
1415
from oidcmsg.time_util import utc_time_sans_frac
1516

16-
from oidcservice.client_auth import CLIENT_AUTHN_METHOD
1717
from oidcservice.state_interface import State
1818

1919
from oidcrp.oidc import RP
2020

2121
sys.path.insert(0, '.')
2222

2323
_dirname = os.path.dirname(os.path.abspath(__file__))
24-
BASE_PATH = os.path.join(_dirname, "data", "keys")
24+
BASE_PATH = os.path.join(_dirname, "keys")
2525

2626
_key = rsa_load(os.path.join(BASE_PATH, "rsa.key"))
2727
KC_RSA = KeyBundle({"key": _key, "kty": "RSA", "use": "sig"})
@@ -52,14 +52,16 @@ def create_client(self):
5252
'redirect_uris': ['https://example.com/cli/authz_cb'],
5353
'client_id': 'client_1',
5454
'client_secret': 'abcdefghijklmnop',
55-
}
55+
}
5656
self.client = RP(DB(), config=conf)
57-
self.client.state_db.set('ABCDE', State(iss='issuer').to_json())
57+
self.client.session_interface.create_state('issuer', 'ABCDE')
5858

5959
def test_construct_authorization_request(self):
60-
req_args = {'state': 'ABCDE',
61-
'redirect_uri': 'https://example.com/auth_cb',
62-
'response_type': ['code']}
60+
req_args = {
61+
'state': 'ABCDE',
62+
'redirect_uri': 'https://example.com/auth_cb',
63+
'response_type': ['code']
64+
}
6365
msg = self.client.service['authorization'].construct(
6466
request_args=req_args)
6567
assert isinstance(msg, AuthorizationRequest)
@@ -69,11 +71,14 @@ def test_construct_accesstoken_request(self):
6971
auth_request = AuthorizationRequest(
7072
redirect_uri='https://example.com/cli/authz_cb',
7173
state='state'
72-
)
74+
)
75+
self.client.session_interface.store_item(auth_request, 'auth_request',
76+
'ABCDE')
77+
7378
auth_response = AuthorizationResponse(code='access_code')
74-
_state = State(auth_response=auth_response.to_json(),
75-
auth_request=auth_request.to_json())
76-
self.client.state_db.set('ABCDE', _state.to_json())
79+
self.client.session_interface.store_item(auth_response, 'auth_response',
80+
'ABCDE')
81+
7782
# Bind access code to state
7883
req_args = {}
7984
msg = self.client.service['accesstoken'].construct(
@@ -84,44 +89,52 @@ def test_construct_accesstoken_request(self):
8489
'client_secret': 'abcdefghijklmnop',
8590
'grant_type': 'authorization_code',
8691
'redirect_uri': 'https://example.com/cli/authz_cb',
87-
'state': 'state'}
92+
'state': 'state'
93+
}
8894

8995
def test_construct_refresh_token_request(self):
9096
auth_request = AuthorizationRequest(
9197
redirect_uri='https://example.com/cli/authz_cb',
9298
state='state'
93-
)
99+
)
100+
self.client.session_interface.store_item(auth_request, 'auth_request',
101+
'ABCDE')
94102
auth_response = AuthorizationResponse(code='access_code')
103+
self.client.session_interface.store_item(auth_response, 'auth_response',
104+
'ABCDE')
95105
token_response = AccessTokenResponse(refresh_token="refresh_with_me",
96106
access_token="access")
97-
_state = State(auth_response=auth_response.to_json(),
98-
auth_request=auth_request.to_json(),
99-
token_response=token_response.to_json())
100-
101-
self.client.state_db.set('ABCDE', _state.to_json())
107+
self.client.session_interface.store_item(token_response,
108+
'token_response', 'ABCDE')
102109

103110
req_args = {}
104111
msg = self.client.service['refresh_token'].construct(
105112
request_args=req_args, state='ABCDE')
106113
assert isinstance(msg, RefreshAccessTokenRequest)
107-
assert msg.to_dict() == {'client_id': 'client_1',
108-
'client_secret': 'abcdefghijklmnop',
109-
'grant_type': 'refresh_token',
110-
'refresh_token': 'refresh_with_me'}
114+
assert msg.to_dict() == {
115+
'client_id': 'client_1',
116+
'client_secret': 'abcdefghijklmnop',
117+
'grant_type': 'refresh_token',
118+
'refresh_token': 'refresh_with_me'
119+
}
111120

112121
def test_do_userinfo_request_init(self):
113122
auth_request = AuthorizationRequest(
114123
redirect_uri='https://example.com/cli/authz_cb',
115124
state='state'
116-
)
125+
)
126+
self.client.session_interface.store_item(auth_request, 'auth_request',
127+
'ABCDE')
117128
auth_response = AuthorizationResponse(code='access_code')
129+
130+
self.client.session_interface.store_item(auth_response, 'auth_response',
131+
'ABCDE')
132+
118133
token_response = AccessTokenResponse(refresh_token="refresh_with_me",
119134
access_token="access")
120-
_state = State(auth_response=auth_response.to_json(),
121-
auth_request=auth_request.to_json(),
122-
token_response=token_response.to_json())
123-
124-
self.client.state_db.set('ABCDE', _state.to_json())
135+
self.client.session_interface.store_item(token_response,
136+
'token_response',
137+
'ABCDE')
125138

126139
_srv = self.client.service['userinfo']
127140
_srv.endpoint = "https://example.com/userinfo"

0 commit comments

Comments
 (0)