55from oic .oic .consumer import Consumer
66from oic .utils .authn .client import CLIENT_AUTHN_METHOD
77
8+ from django_pyoidc .exceptions import (
9+ InvalidOIDCConfigurationException ,
10+ InvalidSIDException ,
11+ )
812from django_pyoidc .session import OIDCCacheSessionBackendForDjango
913from django_pyoidc .settings import OIDCSettingsFactory
10- from django_pyoidc .utils import OIDCCacheBackendForDjango , get_setting_for_sso_op
14+ from django_pyoidc .utils import OIDCCacheBackendForDjango
1115
1216logger = logging .getLogger (__name__ )
1317
1418
1519class OIDCClient :
16- def __init__ (self , op_name , session_id = None ):
17- self ._op_name = op_name
18- self .settings = OIDCSettingsFactory .get (self .op_name )
20+ def __init__ (self , op_name : str , session_id = None ):
21+ self .opsettings = OIDCSettingsFactory .get (op_name )
1922
20- self .session_cache_backend = OIDCCacheSessionBackendForDjango (self ._op_name )
21- self .general_cache_backend = OIDCCacheBackendForDjango (self ._op_name )
22-
23- consumer_config = {
24- # "debug": True,
25- "response_type" : "code"
26- }
23+ self .session_cache_backend = OIDCCacheSessionBackendForDjango (self .opsettings )
24+ self .general_cache_backend = OIDCCacheBackendForDjango (self .opsettings )
25+ client_id = self .opsettings .get ("client_id" )
26+ client_secret = self .opsettings .get ("client_secret" , None )
27+ consumer_config = self .opsettings .get (
28+ "client_consumer_config_dict" ,
29+ {
30+ # "debug": True,
31+ "response_type" : "code"
32+ },
33+ )
2734
2835 client_config = {
29- "client_id" : get_setting_for_sso_op (op_name , "OIDC_CLIENT_ID" ),
30- "client_authn_method" : CLIENT_AUTHN_METHOD ,
36+ "client_id" : client_id ,
37+ "client_authn_method" : self .opsettings .get (
38+ "client_authn_method" , CLIENT_AUTHN_METHOD
39+ ),
3140 }
32-
3341 self .consumer = Consumer (
3442 session_db = self .session_cache_backend ,
3543 consumer_config = consumer_config ,
@@ -38,24 +46,40 @@ def __init__(self, op_name, session_id=None):
3846 # used in token introspection
3947 self .client_extension = ClientExtension (** client_config )
4048
41- provider_info_uri = get_setting_for_sso_op (
42- op_name , "OIDC_PROVIDER_DISCOVERY_URI"
43- )
44- client_secret = get_setting_for_sso_op (op_name , "OIDC_CLIENT_SECRET" )
49+ provider_discovery_uri = self .opsettings .get ("provider_discovery_uri" , None )
4550 self .client_extension .client_secret = client_secret
4651
4752 if session_id :
48- self .consumer .restore (session_id )
49- else :
50-
51- cache_key = self .general_cache_backend .generate_hashed_cache_key (
52- provider_info_uri
53- )
5453 try :
55- config = self .general_cache_backend [ cache_key ]
54+ self .consumer . restore ( session_id )
5655 except KeyError :
57- config = self .consumer .provider_config (provider_info_uri )
58- # shared microcache for provider config
59- # FIXME: Setting for duration
60- self .general_cache_backend .set (cache_key , config , 60 )
61- self .consumer .client_secret = client_secret
56+ # This is an error as for example during the first communication round trips between
57+ # the op and the client we'll have to find state elements in the oidc session
58+ raise InvalidSIDException (
59+ f"OIDC consumer failed to restore oidc session { session_id } ."
60+ )
61+ return
62+
63+ if not provider_discovery_uri :
64+ raise InvalidOIDCConfigurationException (
65+ "No provider discovery uri provided."
66+ )
67+ else :
68+ if self .opsettings .get ("oidc_cache_provider_metadata" , False ):
69+ cache_key = self .general_cache_backend .generate_hashed_cache_key (
70+ provider_discovery_uri
71+ )
72+ try :
73+ config = self .general_cache_backend [cache_key ]
74+ # this will for example register endpoints on the consumer object
75+ self .consumer .handle_provider_config (config , provider_discovery_uri )
76+ except KeyError :
77+ # This make an HTTP call on provider discovery uri
78+ config = self .consumer .provider_config (provider_discovery_uri )
79+ # shared microcache for provider config
80+ # FIXME: Setting for duration
81+ self .general_cache_backend .set (cache_key , config , 60 )
82+ else :
83+ # This make an HTTP call on provider discovery uri
84+ config = self .consumer .provider_config (provider_discovery_uri )
85+ self .consumer .client_secret = client_secret
0 commit comments