From c323b0829b9e10f48289668f677854189369c299 Mon Sep 17 00:00:00 2001 From: juanifioren Date: Wed, 28 Jan 2015 14:50:14 -0300 Subject: [PATCH] Add default package settings. Edit README. --- README.rst | 15 ++++++++++++--- openid_provider/lib/endpoints/token.py | 3 ++- openid_provider/settings.py | 23 +++++++++++++++++++++++ openid_provider/views.py | 6 +++--- 4 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 openid_provider/settings.py diff --git a/README.rst b/README.rst index 7707fa6..f613a93 100644 --- a/README.rst +++ b/README.rst @@ -46,9 +46,18 @@ Add the provider urls. # ... ) -Finally, add a login view and ensure that has the same url defined in `LOGIN_URL` setting. +******** +Settings +******** -See: https://docs.djangoproject.com/en/1.7/ref/settings/#login-url +.. code:: python + + # REQUIRED. Your server provider url. + SITE_URL = 'http://localhost:8000' + + # REQUIRED. + # See: https://docs.djangoproject.com/en/1.7/ref/settings/#login-url + LOGIN_URL = '/accounts/login/' ******************** Create User & Client @@ -61,7 +70,7 @@ Then let's create a Client. Start django shell: ``python manage.py shell``. .. code:: python >>> from openid_provider.models import Client - >>> c = Client(name='Some Client', client_id='123', client_secret='456', client_type='confidential', response_type='code', redirect_uris=['http://example.com/']) + >>> c = Client(name='Some Client', client_id='123', client_secret='456', response_type='code', redirect_uris=['http://example.com/']) >>> c.save() ******************* diff --git a/openid_provider/lib/endpoints/token.py b/openid_provider/lib/endpoints/token.py index 59661e6..ff1fa58 100644 --- a/openid_provider/lib/endpoints/token.py +++ b/openid_provider/lib/endpoints/token.py @@ -3,6 +3,7 @@ from openid_provider.lib.errors import * from openid_provider.lib.utils.params import * from openid_provider.lib.utils.token import * from openid_provider.models import * +from openid_provider import settings import urllib @@ -54,7 +55,7 @@ class TokenEndpoint(object): id_token_dic = create_id_token_dic( self.code.user, - 'http://localhost:8000', # TODO: Add this into settings. + settings.get('SITE_URL'), self.client.client_id) token = create_token( diff --git a/openid_provider/settings.py b/openid_provider/settings.py new file mode 100644 index 0000000..caa7a4d --- /dev/null +++ b/openid_provider/settings.py @@ -0,0 +1,23 @@ +from django.conf import settings + + +class default_settings(object): + + # Here goes all the package default settings. + + LOGIN_URL = None + + SITE_URL = None + +def get(name): + ''' + Helper function to use inside the package. + ''' + try: + value = getattr(default_settings, name) + value = getattr(settings, name) + except AttributeError: + if value == None: + raise Exception('You must set ' + name + ' in your settings.') + + return value diff --git a/openid_provider/views.py b/openid_provider/views.py index d76c5ba..ee23629 100644 --- a/openid_provider/views.py +++ b/openid_provider/views.py @@ -1,4 +1,3 @@ -from django.conf import settings from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.views import redirect_to_login from django.core.urlresolvers import reverse @@ -11,6 +10,7 @@ from openid_provider.lib.errors import * from openid_provider.lib.endpoints.authorize import * from openid_provider.lib.endpoints.token import * from openid_provider.lib.endpoints.userinfo import * +from openid_provider import settings class AuthorizeView(View): @@ -24,7 +24,7 @@ class AuthorizeView(View): if request.user.is_authenticated(): - # This is for printing scopes in the form. + # This is for printing scopes in form. authorize.params.scope_str = ' '.join(authorize.params.scope) context = { @@ -36,7 +36,7 @@ class AuthorizeView(View): else: path = request.get_full_path() return redirect_to_login( - path, settings.LOGIN_URL, REDIRECT_FIELD_NAME) + path, settings.get('LOGIN_URL'), REDIRECT_FIELD_NAME) except (ClientIdError, RedirectUriError) as error: context = {