1- from json import JSONDecodeError
21import logging
2+ from json import JSONDecodeError
33from typing import Callable
44from typing import Optional
55from typing import Union
1212from idpyoidc .client .exception import OidcServiceError
1313from idpyoidc .client .exception import ParseError
1414from idpyoidc .client .service import REQUEST_INFO
15- from idpyoidc .client .service import SUCCESSFUL
1615from idpyoidc .client .service import Service
16+ from idpyoidc .client .service import SUCCESSFUL
1717from idpyoidc .client .util import do_add_ons
1818from idpyoidc .client .util import get_deserialization_method
1919from idpyoidc .configure import Configuration
2626
2727logger = logging .getLogger (__name__ )
2828
29- Version = "2.0"
30-
3129
3230class ExpiredToken (Exception ):
3331 pass
@@ -40,20 +38,20 @@ class Client(Entity):
4038 client_type = "oauth2"
4139
4240 def __init__ (
43- self ,
44- keyjar : Optional [KeyJar ] = None ,
45- config : Optional [Union [dict , Configuration ]] = None ,
46- services : Optional [dict ] = None ,
47- httpc : Optional [Callable ] = None ,
48- httpc_params : Optional [dict ] = None ,
49- context : Optional [OidcContext ] = None ,
50- upstream_get : Optional [Callable ] = None ,
51- key_conf : Optional [dict ] = None ,
52- entity_id : Optional [str ] = "" ,
53- verify_ssl : Optional [bool ] = True ,
54- jwks_uri : Optional [str ] = "" ,
55- client_type : Optional [str ] = "" ,
56- ** kwargs
41+ self ,
42+ keyjar : Optional [KeyJar ] = None ,
43+ config : Optional [Union [dict , Configuration ]] = None ,
44+ services : Optional [dict ] = None ,
45+ httpc : Optional [Callable ] = None ,
46+ httpc_params : Optional [dict ] = None ,
47+ context : Optional [OidcContext ] = None ,
48+ upstream_get : Optional [Callable ] = None ,
49+ key_conf : Optional [dict ] = None ,
50+ entity_id : Optional [str ] = "" ,
51+ verify_ssl : Optional [bool ] = True ,
52+ jwks_uri : Optional [str ] = "" ,
53+ client_type : Optional [str ] = "" ,
54+ ** kwargs
5755 ):
5856 """
5957
@@ -70,7 +68,11 @@ def __init__(
7068 :return: Client instance
7169 """
7270
73- if not client_type :
71+ if client_type :
72+ self .client_type = client_type
73+ elif config and 'client_type' in config :
74+ client_type = self .client_type = config ["client_type" ]
75+ else :
7476 client_type = self .client_type
7577
7678 if verify_ssl is False :
@@ -80,6 +82,8 @@ def __init__(
8082 else :
8183 httpc_params = {"verify" : False }
8284
85+ jwks_uri = jwks_uri or config .get ('jwks_uri' , '' )
86+
8387 Entity .__init__ (
8488 self ,
8589 keyjar = keyjar ,
@@ -106,12 +110,12 @@ def __init__(
106110 do_add_ons (_add_ons , self ._service )
107111
108112 def do_request (
109- self ,
110- request_type : str ,
111- response_body_type : Optional [str ] = "" ,
112- request_args : Optional [dict ] = None ,
113- behaviour_args : Optional [dict ] = None ,
114- ** kwargs
113+ self ,
114+ request_type : str ,
115+ response_body_type : Optional [str ] = "" ,
116+ request_args : Optional [dict ] = None ,
117+ behaviour_args : Optional [dict ] = None ,
118+ ** kwargs
115119 ):
116120 _srv = self ._service [request_type ]
117121
@@ -134,14 +138,14 @@ def set_client_id(self, client_id):
134138 self .get_context ().set ("client_id" , client_id )
135139
136140 def get_response (
137- self ,
138- service : Service ,
139- url : str ,
140- method : Optional [str ] = "GET" ,
141- body : Optional [dict ] = None ,
142- response_body_type : Optional [str ] = "" ,
143- headers : Optional [dict ] = None ,
144- ** kwargs
141+ self ,
142+ service : Service ,
143+ url : str ,
144+ method : Optional [str ] = "GET" ,
145+ body : Optional [dict ] = None ,
146+ response_body_type : Optional [str ] = "" ,
147+ headers : Optional [dict ] = None ,
148+ ** kwargs
145149 ):
146150 """
147151
@@ -177,14 +181,14 @@ def get_response(
177181 return self .parse_request_response (service , resp , response_body_type , ** kwargs )
178182
179183 def service_request (
180- self ,
181- service : Service ,
182- url : str ,
183- method : Optional [str ] = "GET" ,
184- body : Optional [dict ] = None ,
185- response_body_type : Optional [str ] = "" ,
186- headers : Optional [dict ] = None ,
187- ** kwargs
184+ self ,
185+ service : Service ,
186+ url : str ,
187+ method : Optional [str ] = "GET" ,
188+ body : Optional [dict ] = None ,
189+ response_body_type : Optional [str ] = "" ,
190+ headers : Optional [dict ] = None ,
191+ ** kwargs
188192 ) -> Message :
189193 """
190194 The method that sends the request and handles the response returned.
@@ -312,17 +316,20 @@ def dynamic_provider_info_discovery(client: Client, behaviour_args: Optional[dic
312316 :param behaviour_args:
313317 :param client: A :py:class:`idpyoidc.client.oidc.Client` instance
314318 """
319+
320+ if client .client_type == 'oidc' and client .get_service ("provider_info" ):
321+ service = 'provider_info'
322+ elif client .client_type == 'oauth2' and client .get_service ('server_metadata' ):
323+ service = 'server_metadata'
324+ else :
325+ raise ConfigurationError ("Can not do dynamic provider info discovery" )
326+
327+ _context = client .get_context ()
315328 try :
316- client . get_service ( "provider_info" )
329+ _context . set ( "issuer" , _context . config [ "srv_discovery_url" ] )
317330 except KeyError :
318- raise ConfigurationError ("Can not do dynamic provider info discovery" )
319- else :
320- _context = client .get_context ()
321- try :
322- _context .set ("issuer" , _context .config ["srv_discovery_url" ])
323- except KeyError :
324- pass
331+ pass
325332
326- response = client .do_request ("provider_info" , behaviour_args = behaviour_args )
327- if is_error_message (response ):
328- raise OidcServiceError (response ["error" ])
333+ response = client .do_request (service , behaviour_args = behaviour_args )
334+ if is_error_message (response ):
335+ raise OidcServiceError (response ["error" ])
0 commit comments