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

Commit 79157ef

Browse files
committed
First cut as to how configure added post-/preconstruct functions.
1 parent c8c88bc commit 79157ef

2 files changed

Lines changed: 75 additions & 0 deletions

File tree

src/oidcservice/service.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,32 @@ def init_services(service_definitions, service_context, state_db,
542542
else:
543543
_srv = service_configuration['class'](**kwargs)
544544

545+
try:
546+
post_methods = service_configuration['post_functions']
547+
except KeyError:
548+
pass
549+
else:
550+
for meth in post_methods:
551+
try:
552+
func = meth['function']
553+
except KeyError:
554+
pass
555+
else:
556+
_srv.post_construct.append(util.importer(func))
557+
558+
try:
559+
post_methods = service_configuration['pre_functions']
560+
except KeyError:
561+
pass
562+
else:
563+
for meth in post_methods:
564+
try:
565+
func = meth['function']
566+
except KeyError:
567+
pass
568+
else:
569+
_srv.pre_construct.append(util.importer(func))
570+
545571
try:
546572
service[_srv.service_name] = _srv
547573
except AttributeError:

tests/test_14_pkce.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from oidcservice.oidc.pkce import add_code_verifier
77
from oidcservice.oidc.pkce import put_state_in_post_args
88
from oidcservice.service import Service
9+
from oidcservice.service import init_services
910
from oidcservice.service_context import ServiceContext
1011
from oidcservice.service_factory import service_factory
1112
from oidcservice.state_interface import InMemoryStateDataBase
@@ -118,6 +119,54 @@ def test_access_token_and_pkce():
118119
'client_secret', 'code_verifier', 'code',
119120
'state'}
120121

122+
123+
def test_pkce_config():
124+
client_config = {
125+
'client_id': 'client_id',
126+
'client_secret': 'password example one',
127+
'redirect_uris': ['https://example.com/cli/authz_cb'],
128+
'behaviour': {'response_types': ['code']}
129+
}
130+
service_context = ServiceContext(config=client_config)
131+
db = InMemoryStateDataBase()
132+
# Construct an authorization request.
133+
# Gives us a state value and stores code_verifier in state_db
134+
service_definitions = {
135+
'authorization': {
136+
'class': 'oidcservice.oidc.authorization.Authorization',
137+
'kwargs': {},
138+
'post_functions': [
139+
{
140+
'function': 'oidcservice.oidc.pkce.add_code_challenge'
141+
}
142+
]
143+
},
144+
'access_token': {
145+
'class': 'oidcservice.oidc.access_token.AccessToken',
146+
'kwargs': {},
147+
'pre_functions': [
148+
{
149+
'function': 'oidcservice.oidc.pkce.put_state_in_post_args'
150+
}
151+
],
152+
'post_functions': [
153+
{'function': 'oidcservice.oidc.pkce.add_code_verifier'}
154+
]
155+
}
156+
}
157+
service = init_services(service_definitions, service_context, db)
158+
159+
request = service['authorization'].construct_request()
160+
_state = request['state']
161+
162+
auth_response = AuthorizationResponse(code='access code')
163+
service['authorization'].store_item(auth_response, 'auth_response', _state)
164+
165+
request = service['accesstoken'].construct_request(state=_state)
166+
assert set(request.keys()) == {'client_id', 'redirect_uri', 'grant_type',
167+
'client_secret', 'code_verifier', 'code',
168+
'state'}
169+
121170
# class TestPKCE(object):
122171
# def test_pkce_create(self):
123172
# _cli = Client(

0 commit comments

Comments
 (0)