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

Commit aa71789

Browse files
committed
Added new argument to Configuration.
Reuse code in oidcmsg instead of duplicating it here. Fixed tests.
1 parent 6d08f11 commit aa71789

File tree

6 files changed

+94
-50
lines changed

6 files changed

+94
-50
lines changed

example/flask_rp/wsgi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
import os
44
import sys
55

6+
from oidcmsg.configure import create_from_config_file
7+
68
from oidcrp.configure import Configuration
79
from oidcrp.configure import RPConfiguration
8-
from oidcrp.configure import create_from_config_file
910
from oidcrp.util import create_context
1011

1112
try:

src/oidcrp/configure.py

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ def __init__(self,
6868
file_attributes: Optional[List[str]] = None,
6969
domain: Optional[str] = "",
7070
port: Optional[int] = 0,
71+
dir_attributes: Optional[List[str]] = None,
7172
):
72-
Base.__init__(self, conf, base_path=base_path, file_attributes=file_attributes)
73+
Base.__init__(self, conf, base_path=base_path, file_attributes=file_attributes,
74+
dir_attributes=dir_attributes)
7375

7476
log_conf = conf.get('logging')
7577
if log_conf:
@@ -81,32 +83,33 @@ def __init__(self,
8183

8284
if entity_conf:
8385
self.extend(entity_conf=entity_conf, conf=conf, base_path=base_path,
84-
file_attributes=file_attributes, domain=domain, port=port)
85-
86-
87-
def create_from_config_file(cls,
88-
filename: str,
89-
base_path: Optional[str] = '',
90-
entity_conf: Optional[List[dict]] = None,
91-
file_attributes: Optional[List[str]] = None,
92-
dir_attributes: Optional[List[str]] = None,
93-
domain: Optional[str] = "",
94-
port: Optional[int] = 0):
95-
if filename.endswith(".yaml"):
96-
"""Load configuration as YAML"""
97-
_cnf = load_yaml_config(filename)
98-
elif filename.endswith(".json"):
99-
_str = open(filename).read()
100-
_cnf = json.loads(_str)
101-
elif filename.endswith(".py"):
102-
head, tail = os.path.split(filename)
103-
tail = tail[:-3]
104-
module = importlib.import_module(tail)
105-
_cnf = getattr(module, "CONFIG")
106-
else:
107-
raise ValueError("Unknown file type")
108-
109-
return cls(_cnf,
110-
entity_conf=entity_conf,
111-
base_path=base_path, file_attributes=file_attributes,
112-
domain=domain, port=port, dir_attributes=dir_attributes)
86+
file_attributes=file_attributes, domain=domain, port=port,
87+
dir_attributes=dir_attributes)
88+
89+
90+
# def create_from_config_file(cls,
91+
# filename: str,
92+
# base_path: Optional[str] = '',
93+
# entity_conf: Optional[List[dict]] = None,
94+
# file_attributes: Optional[List[str]] = None,
95+
# dir_attributes: Optional[List[str]] = None,
96+
# domain: Optional[str] = "",
97+
# port: Optional[int] = 0):
98+
# if filename.endswith(".yaml"):
99+
# """Load configuration as YAML"""
100+
# _cnf = load_yaml_config(filename)
101+
# elif filename.endswith(".json"):
102+
# _str = open(filename).read()
103+
# _cnf = json.loads(_str)
104+
# elif filename.endswith(".py"):
105+
# head, tail = os.path.split(filename)
106+
# tail = tail[:-3]
107+
# module = importlib.import_module(tail)
108+
# _cnf = getattr(module, "CONFIG")
109+
# else:
110+
# raise ValueError("Unknown file type")
111+
#
112+
# return cls(_cnf,
113+
# entity_conf=entity_conf,
114+
# base_path=base_path, file_attributes=file_attributes,
115+
# domain=domain, port=port, dir_attributes=dir_attributes)

tests/test_11_oauth2.py

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from cryptojwt.jwk.rsa import import_private_rsa_key_from_file
66
from cryptojwt.key_bundle import KeyBundle
7+
from oidcmsg.configure import create_from_config_file
78
from oidcmsg.oauth2 import AccessTokenRequest
89
from oidcmsg.oauth2 import AccessTokenResponse
910
from oidcmsg.oauth2 import AuthorizationRequest
@@ -14,6 +15,7 @@
1415
from oidcmsg.time_util import utc_time_sans_frac
1516
import pytest
1617

18+
from oidcrp.configure import RPConfiguration
1719
from oidcrp.exception import OidcServiceError
1820
from oidcrp.exception import ParseError
1921
from oidcrp.oauth2 import Client
@@ -60,7 +62,7 @@ def test_construct_authorization_request(self):
6062
}
6163

6264
self.client.client_get("service_context").state.create_state('issuer', key='ABCDE')
63-
msg = self.client.client_get("service",'authorization').construct(request_args=req_args)
65+
msg = self.client.client_get("service", 'authorization').construct(request_args=req_args)
6466
assert isinstance(msg, AuthorizationRequest)
6567
assert msg['client_id'] == 'client_1'
6668
assert msg['redirect_uri'] == 'https://example.com/auth_cb'
@@ -81,9 +83,9 @@ def test_construct_accesstoken_request(self):
8183
auth_response = AuthorizationResponse(code='access_code')
8284

8385
self.client.client_get("service_context").state.store_item(auth_response,
84-
'auth_response', 'ABCDE')
86+
'auth_response', 'ABCDE')
8587

86-
msg = self.client.client_get("service",'accesstoken').construct(
88+
msg = self.client.client_get("service", 'accesstoken').construct(
8789
request_args=req_args, state='ABCDE')
8890

8991
assert isinstance(msg, AccessTokenRequest)
@@ -105,19 +107,19 @@ def test_construct_refresh_token_request(self):
105107
state='state'
106108
)
107109

108-
_context.state.store_item(auth_request, 'auth_request','ABCDE')
110+
_context.state.store_item(auth_request, 'auth_request', 'ABCDE')
109111

110112
auth_response = AuthorizationResponse(code='access_code')
111113

112-
_context.state.store_item(auth_response,'auth_response', 'ABCDE')
114+
_context.state.store_item(auth_response, 'auth_response', 'ABCDE')
113115

114116
token_response = AccessTokenResponse(refresh_token="refresh_with_me",
115117
access_token="access")
116118

117119
_context.state.store_item(token_response, 'token_response', 'ABCDE')
118120

119121
req_args = {}
120-
msg = self.client.client_get("service",'refresh_token').construct(
122+
msg = self.client.client_get("service", 'refresh_token').construct(
121123
request_args=req_args, state='ABCDE')
122124
assert isinstance(msg, RefreshAccessTokenRequest)
123125
assert msg.to_dict() == {
@@ -131,7 +133,7 @@ def test_error_response(self):
131133
err = ResponseMessage(error='Illegal')
132134
http_resp = MockResponse(400, err.to_urlencoded())
133135
resp = self.client.parse_request_response(
134-
self.client.client_get("service",'authorization'), http_resp)
136+
self.client.client_get("service", 'authorization'), http_resp)
135137

136138
assert resp['error'] == 'Illegal'
137139
assert resp['status_code'] == 400
@@ -141,7 +143,7 @@ def test_error_response_500(self):
141143
http_resp = MockResponse(500, err.to_urlencoded())
142144
with pytest.raises(ParseError):
143145
self.client.parse_request_response(
144-
self.client.client_get("service",'authorization'), http_resp)
146+
self.client.client_get("service", 'authorization'), http_resp)
145147

146148
def test_error_response_2(self):
147149
err = ResponseMessage(error='Illegal')
@@ -151,4 +153,42 @@ def test_error_response_2(self):
151153

152154
with pytest.raises(OidcServiceError):
153155
self.client.parse_request_response(
154-
self.client.client_get("service",'authorization'), http_resp)
156+
self.client.client_get("service", 'authorization'), http_resp)
157+
158+
159+
class TestClient2(object):
160+
@pytest.fixture(autouse=True)
161+
def create_client(self):
162+
self.redirect_uri = "http://example.com/redirect"
163+
KEYSPEC = [
164+
{"type": "RSA", "use": ["sig"]},
165+
{"type": "EC", "crv": "P-256", "use": ["sig"]},
166+
]
167+
168+
conf = {
169+
'redirect_uris': ['https://example.com/cli/authz_cb'],
170+
'client_id': 'client_1',
171+
'client_secret': 'abcdefghijklmnop',
172+
'rp_keys': {
173+
'private_path': 'private/jwks.json',
174+
'key_defs': KEYSPEC,
175+
'public_path': 'static/jwks.json',
176+
# this will create the jwks files if they are absent
177+
'read_only': False
178+
}
179+
}
180+
rp_conf = RPConfiguration(conf)
181+
self.client = Client(config=rp_conf)
182+
assert self.client
183+
184+
def test_keyjar(self):
185+
req_args = {
186+
'state': 'ABCDE',
187+
'redirect_uri': 'https://example.com/auth_cb',
188+
'response_type': ['code']
189+
}
190+
191+
_context = self.client.client_get("service_context")
192+
assert len(_context.keyjar) == 1 # one issuer
193+
assert len(_context.keyjar[""]) == 2
194+
assert len(_context.keyjar.get("sig")) == 2

tests/test_17_read_registration.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
import json
22
import time
33

4-
import pytest
5-
import responses
64
from cryptojwt.utils import as_bytes
75
from oidcmsg.oidc import RegistrationResponse
6+
import pytest
7+
import responses
88

99
from oidcrp.entity import Entity
1010
import requests
11-
from oidcrp.service_context import ServiceContext
12-
from oidcrp.service_factory import service_factory
1311

1412
ISS = "https://example.com"
1513
RP_BASEURL = "https://example.com/rp"
@@ -44,8 +42,8 @@ def create_request(self):
4442

4543
self.entity = Entity(config=client_config, services=services)
4644

47-
self.reg_service = self.entity.client_get("service",'registration')
48-
self.read_service = self.entity.client_get("service",'registration_read')
45+
self.reg_service = self.entity.client_get("service", 'registration')
46+
self.read_service = self.entity.client_get("service", 'registration_read')
4947

5048
def test_construct(self):
5149
self.reg_service.endpoint = "{}/registration".format(ISS)
@@ -70,7 +68,8 @@ def test_construct(self):
7068
})
7169

7270
with responses.RequestsMock() as rsps:
73-
rsps.add(_param["method"], _param["url"], body=_client_registration_response, status=200)
71+
rsps.add(_param["method"], _param["url"], body=_client_registration_response,
72+
status=200)
7473
_resp = requests.request(
7574
_param["method"], _param["url"],
7675
data=as_bytes(_param["body"]),

tests/test_20_rp_handler_oidc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from urllib.parse import urlparse
55
from urllib.parse import urlsplit
66

7-
from cryptojwt.key_jar import KeyJar
87
from cryptojwt.key_jar import init_key_jar
98
from oidcmsg.oidc import AccessTokenResponse
109
from oidcmsg.oidc import AuthorizationResponse
@@ -312,7 +311,8 @@ def test_do_client_registration(self):
312311
# only 2 things should have happened
313312

314313
assert self.rph.hash2issuer['github'] == issuer
315-
assert client.client_get("service_context").callback.get("post_logout_redirect_uris") is None
314+
assert client.client_get("service_context").callback.get(
315+
"post_logout_redirect_uris") is None
316316

317317
def test_do_client_setup(self):
318318
client = self.rph.client_setup('github')

tests/test_22_config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22

3+
from oidcmsg.configure import create_from_config_file
4+
35
from oidcrp.configure import Configuration
46
from oidcrp.configure import RPConfiguration
5-
from oidcrp.configure import create_from_config_file
67

78
_dirname = os.path.dirname(os.path.abspath(__file__))
89

0 commit comments

Comments
 (0)