From bcb144dcdb363750f5cf7ce4429549c10ec5d551 Mon Sep 17 00:00:00 2001 From: juanifioren Date: Fri, 19 Jun 2015 15:19:46 -0300 Subject: [PATCH] Now OIDC_EXTRA_SCOPE_CLAIMS must be a string (lazy imported). --- oidc_provider/lib/endpoints/userinfo.py | 2 +- oidc_provider/settings.py | 32 +++++++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/oidc_provider/lib/endpoints/userinfo.py b/oidc_provider/lib/endpoints/userinfo.py index 3be9d6d..77f5e7a 100644 --- a/oidc_provider/lib/endpoints/userinfo.py +++ b/oidc_provider/lib/endpoints/userinfo.py @@ -66,7 +66,7 @@ class UserInfoEndpoint(object): dic.update(standard_claims.create_response_dic()) - extra_claims = settings.get('OIDC_EXTRA_SCOPE_CLAIMS')( + extra_claims = settings.get('OIDC_EXTRA_SCOPE_CLAIMS', import_str=True)( self.token.user, self.token.scope) dic.update(extra_claims.create_response_dic()) diff --git a/oidc_provider/settings.py b/oidc_provider/settings.py index 26288b8..33d381a 100644 --- a/oidc_provider/settings.py +++ b/oidc_provider/settings.py @@ -1,3 +1,5 @@ +import importlib + from django.conf import settings @@ -39,9 +41,7 @@ class DefaultSettings(object): """ OPTIONAL. """ - from oidc_provider.lib.claims import AbstractScopeClaims - - return AbstractScopeClaims + return 'oidc_provider.lib.claims.AbstractScopeClaims' @property def OIDC_IDTOKEN_EXPIRE(self): @@ -67,17 +67,35 @@ class DefaultSettings(object): """ return 60*60 + default_settings = DefaultSettings() -def get(name): - ''' + +def import_from_str(value): + """ + Attempt to import a class from a string representation. + """ + try: + parts = value.split('.') + module_path, class_name = '.'.join(parts[:-1]), parts[-1] + module = importlib.import_module(module_path) + return getattr(module, class_name) + except ImportError as e: + msg = 'Could not import %s for settings. %s: %s.' % (value, e.__class__.__name__, e) + raise ImportError(msg) + + +def get(name, import_str=False): + """ Helper function to use inside the package. - ''' + """ try: value = getattr(default_settings, name) value = getattr(settings, name) + if import_str: + value = import_from_str(value) except AttributeError: if value == None: raise Exception('You must set ' + name + ' in your settings.') - return value \ No newline at end of file + return value