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

Commit 58f0ec4

Browse files
committed
Typing
1 parent 90c3a08 commit 58f0ec4

File tree

4 files changed

+72
-46
lines changed

4 files changed

+72
-46
lines changed

src/oidcrp/client_auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from oidcmsg.oauth2 import SINGLE_OPTIONAL_STRING
1313
from oidcmsg.oidc import AuthnToken
1414
from oidcmsg.time_util import utc_time_sans_frac
15+
from oidcmsg.util import rndstr
1516

16-
from oidcrp.util import rndstr
1717
from oidcrp.util import sanitize
1818
from .defaults import DEF_SIGN_ALG
1919
from .defaults import JWT_BEARER

src/oidcrp/oauth2/__init__.py

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
from json import JSONDecodeError
22
import logging
3+
from typing import Optional
34

45
from oidcmsg.exception import FormatError
6+
from oidcmsg.message import Message
7+
from oidcmsg.oauth2 import is_error_message
58

6-
from oidcrp.configure import URIS
79
from oidcrp.entity import Entity
10+
from oidcrp.exception import ConfigurationError
811
from oidcrp.exception import OidcServiceError
912
from oidcrp.exception import ParseError
1013
from oidcrp.http import HTTPLib
1114
from oidcrp.service import REQUEST_INFO
1215
from oidcrp.service import SUCCESSFUL
16+
from oidcrp.service import Service
1317
from oidcrp.util import do_add_ons
1418
from oidcrp.util import get_deserialization_method
1519

@@ -73,7 +77,11 @@ def __init__(self, client_authn_factory=None, keyjar=None, verify_ssl=True, conf
7377
# just ignore verify_ssl until it goes away
7478
self.verify_ssl = self.httpc_params.get("verify", True)
7579

76-
def do_request(self, request_type, response_body_type="", request_args=None, **kwargs):
80+
def do_request(self,
81+
request_type: str,
82+
response_body_type: Optional[str] = "",
83+
request_args: Optional[dict] = None,
84+
behaviour_args: Optional[dict] = None, **kwargs):
7785
_srv = self._service[request_type]
7886

7987
_info = _srv.get_request_parameters(request_args=request_args, **kwargs)
@@ -94,8 +102,13 @@ def set_client_id(self, client_id):
94102
self.client_id = client_id
95103
self._service_context.set('client_id', client_id)
96104

97-
def get_response(self, service, url, method="GET", body=None, response_body_type="",
98-
headers=None, **kwargs):
105+
def get_response(self,
106+
service: Service,
107+
url: str,
108+
method: Optional[str] = "GET",
109+
body: Optional[dict] = None,
110+
response_body_type: Optional[str] = "",
111+
headers: Optional[dict] = None, **kwargs):
99112
"""
100113
101114
:param url:
@@ -130,8 +143,13 @@ def get_response(self, service, url, method="GET", body=None, response_body_type
130143
return self.parse_request_response(service, resp,
131144
response_body_type, **kwargs)
132145

133-
def service_request(self, service, url, method="GET", body=None,
134-
response_body_type="", headers=None, **kwargs):
146+
def service_request(self,
147+
service: Service,
148+
url: str,
149+
method: Optional[str] = "GET",
150+
body: Optional[dict] = None,
151+
response_body_type: Optional[str] = "",
152+
headers: Optional[dict] = None, **kwargs) -> Message:
135153
"""
136154
The method that sends the request and handles the response returned.
137155
This assumes that the response arrives in the HTTP response.
@@ -250,3 +268,27 @@ def parse_request_response(self, service, reqresp, response_body_type='',
250268
reqresp.text))
251269
raise OidcServiceError("HTTP ERROR: %s [%s] on %s" % (
252270
reqresp.text, reqresp.status_code, reqresp.url))
271+
272+
273+
def dynamic_provider_info_discovery(client: Client, behaviour_args: Optional[dict]=None):
274+
"""
275+
This is about performing dynamic Provider Info discovery
276+
277+
:param behaviour_args:
278+
:param client: A :py:class:`oidcrp.oidc.Client` instance
279+
"""
280+
try:
281+
client.get_service('provider_info')
282+
except KeyError:
283+
raise ConfigurationError(
284+
'Can not do dynamic provider info discovery')
285+
else:
286+
_context = client.client_get("service_context")
287+
try:
288+
_context.set('issuer', _context.config['srv_discovery_url'])
289+
except KeyError:
290+
pass
291+
292+
response = client.do_request('provider_info', behaviour_args=behaviour_args)
293+
if is_error_message(response):
294+
raise OidcServiceError(response['error'])

src/oidcrp/rp_handler.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
from .defaults import DEFAULT_RP_KEY_DEFS
2929
from .exception import OidcServiceError
3030
from .oauth2 import Client
31+
from .oauth2 import dynamic_provider_info_discovery
3132
from .oauth2.utils import pick_redirect_uri
3233
from .util import add_path
33-
from .util import dynamic_provider_info_discovery
3434
from .util import load_registration_response
3535
from .util import rndstr
3636

@@ -185,11 +185,15 @@ def init_client(self, issuer):
185185
_context.jwks_uri = self.jwks_uri
186186
return client
187187

188-
def do_provider_info(self, client=None, state='', behaviour_args=None):
188+
def do_provider_info(self,
189+
client: Optional[Client]=None,
190+
state: Optional[str]='',
191+
behaviour_args: Optional[dict]=None) -> str:
189192
"""
190193
Either get the provider info from configuration or through dynamic
191194
discovery.
192195
196+
:param behaviour_args:
193197
:param client: A Client instance
194198
:param state: A key by which the state of the session can be
195199
retrieved
@@ -205,7 +209,7 @@ def do_provider_info(self, client=None, state='', behaviour_args=None):
205209

206210
_context = client.client_get("service_context")
207211
if not _context.get('provider_info'):
208-
dynamic_provider_info_discovery(client)
212+
dynamic_provider_info_discovery(client, behaviour_args=behaviour_args)
209213
return _context.get('provider_info')['issuer']
210214
else:
211215
_pi = _context.get('provider_info')
@@ -280,16 +284,9 @@ def do_client_registration(self, client=None,
280284
_params = RegistrationRequest().parameters()
281285
request_args.update({k: v for k, v in behaviour_args.items() if k in _params})
282286

283-
# _ignore = [k for k in list(request_args.keys()) if k in CALLBACK_URIS]
284-
# if _context.get('redirect_uris'):
285-
# if 'redirect_uris' not in _ignore:
286-
# _ignore.append('redirect_uris')
287-
#
288-
# add_callbacks(_context, _ignore)
289-
290287
load_registration_response(client, request_args=request_args)
291288

292-
def do_webfinger(self, user):
289+
def do_webfinger(self, user: str) -> Client:
293290
"""
294291
Does OpenID Provider Issuer discovery using webfinger.
295292
@@ -304,7 +301,10 @@ def do_webfinger(self, user):
304301
temporary_client.do_request('webfinger', resource=user)
305302
return temporary_client
306303

307-
def client_setup(self, iss_id='', user='', behaviour_args=None):
304+
def client_setup(self,
305+
iss_id: Optional[str] = '',
306+
user: Optional[str] = '',
307+
behaviour_args: Optional[dict] = None) -> Client:
308308
"""
309309
First if no issuer ID is given then the identifier for the user is
310310
used by the webfinger service to try to find the issuer ID.
@@ -358,11 +358,17 @@ def _get_response_type(self, context, req_args: Optional[dict] = None):
358358
else:
359359
return context.get('behaviour')['response_types'][0]
360360

361-
def init_authorization(self, client=None, state='', req_args=None, behaviour_args=None):
361+
def init_authorization(self,
362+
client: Optional[Client] = None,
363+
state: Optional[str] = '',
364+
req_args: Optional[dict] = None,
365+
behaviour_args: Optional[dict] = None) -> dict:
362366
"""
363367
Constructs the URL that will redirect the user to the authorization
364368
endpoint of the OP/AS.
365369
370+
:param behaviour_args:
371+
:param state:
366372
:param client: A Client instance
367373
:param req_args: Non-default Request arguments
368374
:return: A dictionary with 2 keys: **url** The authorization redirect
@@ -607,8 +613,7 @@ def userinfo_in_id_token(id_token):
607613
:param id_token: An :py:class:`oidcmsg.oidc.IDToken` instance
608614
:return: A dictionary with user information
609615
"""
610-
res = dict([(k, id_token[k]) for k in OpenIDSchema.c_param.keys() if
611-
k in id_token])
616+
res = dict([(k, id_token[k]) for k in OpenIDSchema.c_param.keys() if k in id_token])
612617
res.update(id_token.extra())
613618
return res
614619

@@ -629,7 +634,8 @@ def finalize_auth(self, client, issuer: str, response: dict,
629634

630635
_srv = client.get_service('authorization')
631636
try:
632-
authorization_response = _srv.parse_response(response, sformat='dict')
637+
authorization_response = _srv.parse_response(response, sformat='dict',
638+
behaviour_args=behaviour_args)
633639
except Exception as err:
634640
logger.error('Parsing authorization_response: {}'.format(err))
635641
message = traceback.format_exception(*sys.exc_info())

src/oidcrp/util.py

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import ssl
1010
import string
1111
import sys
12+
from typing import Optional
1213
from urllib.parse import parse_qs
1314
from urllib.parse import urlsplit
1415
from urllib.parse import urlunsplit
@@ -553,26 +554,3 @@ def load_registration_response(client, request_args=None):
553554
else:
554555
if 'error' in response:
555556
raise OidcServiceError(response.to_json())
556-
557-
558-
def dynamic_provider_info_discovery(client):
559-
"""
560-
This is about performing dynamic Provider Info discovery
561-
562-
:param client: A :py:class:`oidcrp.oidc.Client` instance
563-
"""
564-
try:
565-
client.get_service('provider_info')
566-
except KeyError:
567-
raise ConfigurationError(
568-
'Can not do dynamic provider info discovery')
569-
else:
570-
_context = client.client_get("service_context")
571-
try:
572-
_context.set('issuer', _context.config['srv_discovery_url'])
573-
except KeyError:
574-
pass
575-
576-
response = client.do_request('provider_info')
577-
if is_error_message(response):
578-
raise OidcServiceError(response['error'])

0 commit comments

Comments
 (0)