22from urllib .parse import urlparse
33
44from cryptojwt .jwt import JWT
5+ from oidcmsg .message import Message
6+ from oidcmsg .oauth2 import ResponseMessage
7+ from oidcmsg .oauth2 import is_error_message
58
9+ from oidcservice import util
610from oidcservice .client_auth import factory as ca_factory
711from oidcservice .exception import ResponseError
812from oidcservice .state_interface import StateInterface
9- from oidcservice .util import get_http_body
10- from oidcservice .util import get_http_url
1113from oidcservice .util import JOSE_ENCODED
1214from oidcservice .util import JSON_ENCODED
1315from oidcservice .util import URL_ENCODED
14-
15- from oidcmsg .oauth2 import is_error_message
16- from oidcmsg .oauth2 import ResponseMessage
17- from oidcmsg .message import Message
18-
16+ from oidcservice .util import get_http_body
17+ from oidcservice .util import get_http_url
18+ from oidcservice .util import importer
1919
2020__author__ = 'Roland Hedberg'
2121
@@ -46,9 +46,9 @@ def __init__(self, service_context, state_db, conf=None,
4646 StateInterface .__init__ (self , state_db )
4747
4848 if client_authn_factory is None :
49- self .client_authn_factory = ca_factory
49+ self .client_authn_factory = ca_factory
5050 else :
51- self .client_authn_factory = client_authn_factory
51+ self .client_authn_factory = client_authn_factory
5252
5353 self .service_context = service_context
5454 self .default_request_args = {}
@@ -94,7 +94,8 @@ def gather_request_args(self, **kwargs):
9494 ar_args [prop ] = self .conf ['request_args' ][prop ]
9595 except KeyError :
9696 try :
97- ar_args [prop ] = self .service_context .register_args [prop ]
97+ ar_args [prop ] = self .service_context .register_args [
98+ prop ]
9899 except KeyError :
99100 try :
100101 ar_args [prop ] = self .default_request_args [prop ]
@@ -337,11 +338,11 @@ def get_request_parameters(self, request_body_type="", method="",
337338 _info ['url' ] = get_http_url (endpoint_url , request , method = method )
338339
339340 # If there is to be a body part
340- if method == 'POST' :
341+ if method == 'POST' :
341342 # How should it be serialized
342343 if request_body_type == 'urlencoded' :
343344 content_type = URL_ENCODED
344- elif request_body_type in ['jws' ,'jwe' , 'jose' ]:
345+ elif request_body_type in ['jws' , 'jwe' , 'jose' ]:
345346 content_type = JOSE_ENCODED
346347 else : # request_body_type == 'json'
347348 content_type = JSON_ENCODED
@@ -424,7 +425,7 @@ def parse_response(self, info, sformat="", state="", **kwargs):
424425
425426 logger .debug ('response format: {}' .format (sformat ))
426427
427- if sformat in ['jose' ,'jws' ,'jwe' ]:
428+ if sformat in ['jose' , 'jws' , 'jwe' ]:
428429 resp = self .post_parse_response (info , state = state )
429430
430431 if not resp :
@@ -511,19 +512,50 @@ def get_conf_attr(self, attr, default=None):
511512 return default
512513
513514
514- def build_services (service_definitions , service_factory , module_dirs ,
515- service_context , state_db , client_authn_factory = None ):
515+ # def build_services(service_definitions, service_factory, module_dirs,
516+ # service_context, state_db, client_authn_factory=None):
517+ # """
518+ # This function will build a number of :py:class:`oidcservice.service.Service`
519+ # instances based on the service definitions provided.
520+ #
521+ # :param service_definitions: A dictionary of service definitions. The keys
522+ # are the names of the subclasses. The values are configurations.
523+ # :param service_factory: A factory that can initiate a service class
524+ # :param module_dirs: The directories in which to search for service modules.
525+ # Directory names are expected to be relative to the oidcservice package.
526+ # e.g. ['oidc', 'oauth2]. The directories will be search in order until
527+ # a matching module is found.
528+ # :param service_context: A reference to the service context, this is the same
529+ # for all service instances.
530+ # :param state_db: A reference to the state database. Shared by all the
531+ # services.
532+ # :param client_authn_factory: A list of methods the services can use to
533+ # authenticate the client to a service.
534+ # :return: A dictionary, with service name as key and the service instance as
535+ # value.
536+ # """
537+ # service = {}
538+ # for service_name, service_configuration in service_definitions.items():
539+ # _srv = service_factory(service_name,
540+ # module_dirs,
541+ # service_context=service_context,
542+ # state_db=state_db,
543+ # client_authn_factory=client_authn_factory,
544+ # conf=service_configuration)
545+ # try:
546+ # service[_srv.service_name] = _srv
547+ # except AttributeError:
548+ # raise ValueError("Could not load '{}'".format(service_name))
549+ #
550+ # return service
551+
552+
553+ def init_services (service_definitions , service_context , state_db ,
554+ client_authn_factory = None ):
516555 """
517- This function will build a number of :py:class:`oidcservice.service.Service`
518- instances based on the service definitions provided.
519-
520- :param service_definitions: A dictionary of service definitions. The keys
521- are the names of the subclasses. The values are configurations.
522- :param service_factory: A factory that can initiate a service class
523- :param module_dirs: The directories in which to search for service modules.
524- Directory names are expected to be relative to the oidcservice package.
525- e.g. ['oidc', 'oauth2]. The directories will be search in order until
526- a matching module is found.
556+ Initiates a set of services
557+
558+ :param service_definitions: A dictionary cotaining service definitions
527559 :param service_context: A reference to the service context, this is the same
528560 for all service instances.
529561 :param state_db: A reference to the state database. Shared by all the
@@ -535,12 +567,20 @@ def build_services(service_definitions, service_factory, module_dirs,
535567 """
536568 service = {}
537569 for service_name , service_configuration in service_definitions .items ():
538- _srv = service_factory (service_name ,
539- module_dirs ,
540- service_context = service_context ,
541- state_db = state_db ,
542- client_authn_factory = client_authn_factory ,
543- conf = service_configuration )
570+ try :
571+ kwargs = service_configuration ['kwargs' ]
572+ except KeyError :
573+ kwargs = {}
574+
575+ kwargs .update ({'service_context' : service_context ,
576+ 'state_db' : state_db ,
577+ 'client_authn_factory' : client_authn_factory })
578+
579+ if isinstance (service_configuration ['class' ], str ):
580+ _srv = util .importer (service_configuration ['class' ])(** kwargs )
581+ else :
582+ _srv = service_configuration ['class' ](** kwargs )
583+
544584 try :
545585 service [_srv .service_name ] = _srv
546586 except AttributeError :
0 commit comments