2015-06-19 18:19:46 +00:00
|
|
|
import importlib
|
2017-04-18 09:25:16 +00:00
|
|
|
import random
|
|
|
|
import string
|
2015-06-19 18:19:46 +00:00
|
|
|
|
2015-01-28 17:50:14 +00:00
|
|
|
from django.conf import settings
|
2015-03-02 20:37:54 +00:00
|
|
|
|
2015-01-28 17:50:14 +00:00
|
|
|
|
2015-03-04 20:17:37 +00:00
|
|
|
class DefaultSettings(object):
|
2017-03-31 20:41:26 +00:00
|
|
|
required_attrs = ()
|
2015-01-28 17:50:14 +00:00
|
|
|
|
2017-04-18 09:25:16 +00:00
|
|
|
def __init__(self):
|
|
|
|
self._unauthenticated_session_management_key = None
|
|
|
|
|
2015-03-04 20:17:37 +00:00
|
|
|
@property
|
2017-03-31 20:41:26 +00:00
|
|
|
def OIDC_LOGIN_URL(self):
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
2017-03-31 20:41:26 +00:00
|
|
|
REQUIRED. Used to log the user in. By default Django's LOGIN_URL will be used.
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
2017-03-31 20:41:26 +00:00
|
|
|
return settings.LOGIN_URL
|
2015-03-02 20:37:54 +00:00
|
|
|
|
2015-03-04 20:17:37 +00:00
|
|
|
@property
|
|
|
|
def SITE_URL(self):
|
|
|
|
"""
|
2016-05-25 21:58:58 +00:00
|
|
|
OPTIONAL. The OP server url.
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
|
|
|
return None
|
2015-03-02 20:37:54 +00:00
|
|
|
|
2015-03-19 17:04:32 +00:00
|
|
|
@property
|
|
|
|
def OIDC_AFTER_USERLOGIN_HOOK(self):
|
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. Provide a way to plug into the process after
|
|
|
|
the user has logged in, typically to perform some business logic.
|
2015-03-19 17:04:32 +00:00
|
|
|
"""
|
2016-01-19 20:37:32 +00:00
|
|
|
return 'oidc_provider.lib.utils.common.default_after_userlogin_hook'
|
2015-03-19 17:04:32 +00:00
|
|
|
|
2017-01-27 08:05:25 +00:00
|
|
|
@property
|
2017-03-31 12:00:24 +00:00
|
|
|
def OIDC_AFTER_END_SESSION_HOOK(self):
|
2017-01-27 08:05:25 +00:00
|
|
|
"""
|
|
|
|
OPTIONAL. Provide a way to plug into the end session process just before calling
|
|
|
|
Django's logout function, typically to perform some business logic.
|
|
|
|
"""
|
2017-03-31 12:00:24 +00:00
|
|
|
return 'oidc_provider.lib.utils.common.default_after_end_session_hook'
|
2017-01-27 08:05:25 +00:00
|
|
|
|
2015-03-04 20:17:37 +00:00
|
|
|
@property
|
|
|
|
def OIDC_CODE_EXPIRE(self):
|
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. Code expiration time expressed in seconds.
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
|
|
|
return 60*10
|
2015-02-26 19:14:36 +00:00
|
|
|
|
2015-03-04 20:17:37 +00:00
|
|
|
@property
|
|
|
|
def OIDC_EXTRA_SCOPE_CLAIMS(self):
|
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. A string with the location of your class.
|
2016-07-07 15:50:27 +00:00
|
|
|
Used to add extra scopes specific for your app.
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
2016-07-07 15:50:27 +00:00
|
|
|
return None
|
2015-03-04 20:17:37 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def OIDC_IDTOKEN_EXPIRE(self):
|
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. Id token expiration time expressed in seconds.
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
|
|
|
return 60*10
|
|
|
|
|
|
|
|
@property
|
|
|
|
def OIDC_IDTOKEN_SUB_GENERATOR(self):
|
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. Subject Identifier. A locally unique and never
|
|
|
|
reassigned identifier within the Issuer for the End-User,
|
|
|
|
which is intended to be consumed by the Client.
|
2015-03-04 20:17:37 +00:00
|
|
|
"""
|
2016-01-12 18:17:22 +00:00
|
|
|
return 'oidc_provider.lib.utils.common.default_sub_generator'
|
2015-03-04 20:17:37 +00:00
|
|
|
|
2016-10-28 18:25:52 +00:00
|
|
|
@property
|
|
|
|
def OIDC_SESSION_MANAGEMENT_ENABLE(self):
|
|
|
|
"""
|
|
|
|
OPTIONAL. If enabled, the Server will support Session Management 1.0 specification.
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2017-04-18 09:25:16 +00:00
|
|
|
@property
|
|
|
|
def OIDC_UNAUTHENTICATED_SESSION_MANAGEMENT_KEY(self):
|
|
|
|
"""
|
|
|
|
OPTIONAL. Supply a fixed string to use as browser-state key for unauthenticated clients.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Memoize generated value
|
|
|
|
if not self._unauthenticated_session_management_key:
|
|
|
|
self._unauthenticated_session_management_key = ''.join(
|
|
|
|
random.choice(string.ascii_uppercase + string.digits) for _ in range(100))
|
|
|
|
return self._unauthenticated_session_management_key
|
|
|
|
|
2015-06-24 15:40:00 +00:00
|
|
|
def OIDC_SKIP_CONSENT_EXPIRE(self):
|
2015-06-22 21:41:42 +00:00
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. User consent expiration after been granted.
|
2015-06-22 21:41:42 +00:00
|
|
|
"""
|
2015-06-24 15:40:00 +00:00
|
|
|
return 30*3
|
2015-06-22 21:41:42 +00:00
|
|
|
|
|
|
|
@property
|
2015-06-24 15:40:00 +00:00
|
|
|
def OIDC_TOKEN_EXPIRE(self):
|
2015-06-22 21:41:42 +00:00
|
|
|
"""
|
2015-07-14 16:01:29 +00:00
|
|
|
OPTIONAL. Token object expiration after been created.
|
|
|
|
Expressed in seconds.
|
2015-06-22 21:41:42 +00:00
|
|
|
"""
|
2015-06-24 15:40:00 +00:00
|
|
|
return 60*60
|
2015-06-19 18:19:46 +00:00
|
|
|
|
2015-08-11 18:58:52 +00:00
|
|
|
@property
|
|
|
|
def OIDC_USERINFO(self):
|
|
|
|
"""
|
2016-07-07 15:50:27 +00:00
|
|
|
OPTIONAL. A string with the location of your function.
|
|
|
|
Used to populate standard claims with your user information.
|
2015-08-11 18:58:52 +00:00
|
|
|
"""
|
2016-07-07 15:50:27 +00:00
|
|
|
return 'oidc_provider.lib.utils.common.default_userinfo'
|
2015-08-11 18:58:52 +00:00
|
|
|
|
2016-02-12 16:02:35 +00:00
|
|
|
@property
|
2016-02-12 17:51:43 +00:00
|
|
|
def OIDC_IDTOKEN_PROCESSING_HOOK(self):
|
2016-02-12 16:02:35 +00:00
|
|
|
"""
|
|
|
|
OPTIONAL. A string with the location of your hook.
|
|
|
|
Used to add extra dictionary values specific for your app into id_token.
|
|
|
|
"""
|
2016-02-12 17:51:43 +00:00
|
|
|
return 'oidc_provider.lib.utils.common.default_idtoken_processing_hook'
|
2016-02-12 16:02:35 +00:00
|
|
|
|
2016-12-13 12:40:14 +00:00
|
|
|
@property
|
|
|
|
def OIDC_GRANT_TYPE_PASSWORD_ENABLE(self):
|
|
|
|
"""
|
|
|
|
OPTIONAL. A boolean to set whether to allow the Resource Owner Password
|
|
|
|
Credentials Grant. https://tools.ietf.org/html/rfc6749#section-4.3
|
|
|
|
|
|
|
|
From the specification:
|
|
|
|
Since this access token request utilizes the resource owner's
|
|
|
|
password, the authorization server MUST protect the endpoint
|
|
|
|
against brute force attacks (e.g., using rate-limitation or
|
|
|
|
generating alerts).
|
|
|
|
|
|
|
|
How you do this, is up to you.
|
|
|
|
"""
|
|
|
|
return False
|
|
|
|
|
2017-03-31 21:25:42 +00:00
|
|
|
@property
|
|
|
|
def OIDC_TEMPLATES(self):
|
|
|
|
return {
|
|
|
|
'authorize': 'oidc_provider/authorize.html',
|
|
|
|
'error': 'oidc_provider/error.html'
|
|
|
|
}
|
|
|
|
|
2015-03-06 15:55:50 +00:00
|
|
|
default_settings = DefaultSettings()
|
2015-03-02 20:37:54 +00:00
|
|
|
|
2015-06-19 18:19:46 +00:00
|
|
|
|
|
|
|
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):
|
|
|
|
"""
|
2015-03-02 20:37:54 +00:00
|
|
|
Helper function to use inside the package.
|
2015-06-19 18:19:46 +00:00
|
|
|
"""
|
2016-02-15 19:42:08 +00:00
|
|
|
value = None
|
2017-03-31 21:25:42 +00:00
|
|
|
default_value = getattr(default_settings, name)
|
|
|
|
|
2015-03-02 20:37:54 +00:00
|
|
|
try:
|
|
|
|
value = getattr(settings, name)
|
|
|
|
except AttributeError:
|
2017-03-31 21:25:42 +00:00
|
|
|
if name in default_settings.required_attrs:
|
2015-03-02 20:37:54 +00:00
|
|
|
raise Exception('You must set ' + name + ' in your settings.')
|
|
|
|
|
2017-04-07 23:59:40 +00:00
|
|
|
if isinstance(default_value, dict) and value:
|
|
|
|
default_value.update(value)
|
|
|
|
value = default_value
|
|
|
|
else:
|
2017-03-31 21:25:42 +00:00
|
|
|
value = value or default_value
|
2017-04-07 23:59:40 +00:00
|
|
|
value = import_from_str(value) if import_str else value
|
2015-06-19 18:37:42 +00:00
|
|
|
|
2015-06-19 18:19:46 +00:00
|
|
|
return value
|