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

Commit c7b7499

Browse files
authored
Merge pull request #3 from openid/master
pkce support
2 parents 7b311c5 + 848cc0a commit c7b7499

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

flask_rp/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def init_oidc_rp_handler(app):
3737
client_configs=app.config.get('CLIENTS'),
3838
services=app.config.get('SERVICES'),
3939
verify_ssl=verify_ssl)
40+
4041
return rph
4142

4243

flask_rp/conf.yaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ RP_KEYS:
3131
# this will create the jwks files if they are absent
3232
'read_only': False
3333

34-
client_preferences: &id001
34+
CLIENT_PREFERENCES: &id001
3535
application_name: rphandler
3636
application_type: web
3737
contacts: [[email protected]]
3838
response_types: [code]
3939
scope: [openid, profile, email, address, phone]
4040
token_endpoint_auth_method: [client_secret_basic, client_secret_post]
4141

42-
services: &id002
42+
SERVICES: &id002
4343
discovery:
4444
class: oidcservice.oidc.provider_info_discovery.ProviderInfoDiscovery
4545
kwargs: {}
@@ -62,7 +62,6 @@ services: &id002
6262
class: oidcservice.oidc.end_session.EndSession
6363
kwargs: {}
6464

65-
6665
CLIENTS:
6766
flop:
6867
client_preferences: *id001
@@ -73,6 +72,12 @@ CLIENTS:
7372
jwks_uri: https://127.0.0.1:8090/static/jwks.json
7473
redirect_uris: ['https://127.0.0.1:8090/authz_cb/flop']
7574
services: *id002
75+
add_ons:
76+
pkce:
77+
function: oidcservice.oidc.add_on.pkce.add_pkce_support
78+
kwargs:
79+
code_challenge_length: 64
80+
code_challenge_method: S256
7681

7782
# Whether an attempt to fetch the userinfo should be made
7883
USERINFO: true

src/oidcrp/__init__.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,8 @@ def finalize_auth(self, client, issuer, response):
647647

648648
_srv.update_service_context(authorization_response,
649649
state=authorization_response['state'])
650+
self.session_interface.store_item(authorization_response, "auth_response",
651+
authorization_response['state'])
650652
return authorization_response
651653

652654
def get_access_and_id_token(self, authorization_response=None, state='',
@@ -805,7 +807,7 @@ def has_active_authentication(self, state):
805807

806808
def get_valid_access_token(self, state):
807809
"""
808-
Find me a valid access token
810+
Find a valid access token.
809811
810812
:param state:
811813
:return: An access token if a valid one exists and when it
@@ -825,20 +827,16 @@ def get_valid_access_token(self, state):
825827
except KeyError:
826828
pass
827829
else:
828-
try:
829-
access_token = response['access_token']
830-
except:
831-
continue
832-
else:
830+
if 'access_token' in response:
831+
access_token = response["access_token"]
833832
try:
834833
_exp = response['__expires_at']
835834
except KeyError: # No expiry date, lives for ever
836835
indefinite.append((access_token, 0))
837836
else:
838-
if _exp > now: # expires sometime in the future
839-
if _exp > exp:
840-
exp = _exp
841-
token = (access_token, _exp)
837+
if _exp > now and _exp > exp: # expires sometime in the future
838+
exp = _exp
839+
token = (access_token, _exp)
842840

843841
if indefinite:
844842
return indefinite[0]

src/oidcrp/oauth2/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from oidcservice.state_interface import StateInterface
1414

1515
from oidcrp.http import HTTPLib
16+
from oidcrp.util import do_add_ons
1617
from oidcrp.util import get_deserialization_method
1718

1819
__author__ = 'Roland Hedberg'
@@ -41,7 +42,7 @@ def __init__(self, state_db, ca_certs=None, client_authn_factory=None,
4142
:param keyjar: A py:class:`oidcmsg.key_jar.KeyJar` instance
4243
:param verify_ssl: Whether the SSL certificate should be verified.
4344
:param config: Configuration information passed on to the
44-
:py:class:`oidcservice.service_context.ServiceContext`
45+
:py:class:`oidcservice.service_context.ServiceContext`
4546
initialization
4647
:param client_cert: Certificate used by the HTTP client
4748
:param httplib: A HTTP client to use
@@ -72,6 +73,9 @@ def __init__(self, state_db, ca_certs=None, client_authn_factory=None,
7273
self.service = init_services(_srvs, self.service_context, state_db,
7374
_cam)
7475

76+
if 'add_ons' in config:
77+
do_add_ons(config['add_ons'], self.service)
78+
7579
self.service_context.service = self.service
7680

7781
self.verify_ssl = verify_ssl
@@ -142,6 +146,11 @@ def service_request(self, service, url, method="GET", body=None,
142146
if 'error' in response:
143147
pass
144148
else:
149+
try:
150+
kwargs['key'] = kwargs['state']
151+
except KeyError:
152+
pass
153+
145154
service.update_service_context(response, **kwargs)
146155
return response
147156

src/oidcrp/util.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from oidcservice import sanitize
1111
from oidcservice.exception import TimeFormatError
1212
from oidcservice.exception import WrongContentType
13+
from oidcservice.util import importer
1314

1415
logger = logging.getLogger(__name__)
1516

@@ -128,10 +129,10 @@ def set_cookie(cookiejar, kaka):
128129

129130
def verify_header(reqresp, body_type):
130131
"""
131-
132-
:param reqresp: Class instance with attributes: ['status', 'text',
133-
'headers', 'url']
134-
:param body_type: If information returned in the body part
132+
133+
:param reqresp: Class instance with attributes: ['status', 'text',
134+
'headers', 'url']
135+
:param body_type: If information returned in the body part
135136
:return: Verified body content type
136137
"""
137138
logger.debug("resp.headers: %s" % (sanitize(reqresp.headers),))
@@ -237,11 +238,17 @@ def get_value_type(http_response, body_type):
237238
def load_configuration(filename):
238239
if filename.endswith('.yaml'):
239240
with open(filename) as fp:
240-
conf = yaml.load(fp)
241+
conf = yaml.safe_load(fp)
241242
elif filename.endswith('.py'):
242243
sys.path.insert(0, ".")
243244
conf = importlib.import_module(filename[:-3])
244245
else:
245246
raise ValueError('Wrong file type')
246247

247-
return conf
248+
return conf
249+
250+
251+
def do_add_ons(add_ons, services):
252+
for key, spec in add_ons.items():
253+
_func = importer(spec['function'])
254+
_func(services, **spec['kwargs'])

0 commit comments

Comments
 (0)