From 94f3d5ca6ac3c372f32db358276cf0b1a159de70 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 18 Jun 2014 22:44:43 +0200 Subject: [PATCH] templates: add template autologin.html to provider autologin capability in any page It use the new JSONP logged-in web service from authentic. You must declare you website in the VALID_REFERERS settings of your authentic server. --- portail_citoyen2/settings.py | 6 +++++- .../static/portail_citoyen/js/autologin.js | 20 +++++++++++++++++++ portail_citoyen2/templates/base.html | 1 + .../templates/portail_citoyen/autologin.html | 7 +++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 portail_citoyen2/static/portail_citoyen/js/autologin.js create mode 100644 portail_citoyen2/templates/portail_citoyen/autologin.html diff --git a/portail_citoyen2/settings.py b/portail_citoyen2/settings.py index 9e50e41..9908cba 100644 --- a/portail_citoyen2/settings.py +++ b/portail_citoyen2/settings.py @@ -141,13 +141,17 @@ AUTHENTICATION_BACKENDS = ( # auth and allauth settings LOGIN_REDIRECT_URL = os.environ.get('LOGIN_REDIRECT_URL', '/') LOGOUT_URL = os.environ.get('LOGOUT_URL', '/accounts/logout/') +PORTAIL_CITOYEN_TEMPLATE_VARS = {} if 'AUTHENTIC2_URL' in os.environ: + AUTHENTIC2_URL = os.environ.get('AUTHENTIC2_URL', '') + PORTAIL_CITOYEN_TEMPLATE_VARS['AUTHENTIC2_URL'] = AUTHENTIC2_URL INSTALLED_APPS += ('portail_citoyen2.allauth_authentic2',) LOGIN_URL = os.environ.get('LOGIN_URL', '/accounts/authentic2/login/?process=login') + PORTAIL_CITOYEN_TEMPLATE_VARS['LOGIN_URL'] = LOGIN_URL SOCIALACCOUNT_QUERY_EMAIL = True SOCIALACCOUNT_PROVIDERS = { 'authentic2': { - 'URL': os.environ.get('AUTHENTIC2_URL', '') + '/idp/oauth2/', + 'URL': AUTHENTIC2_URL + '/idp/oauth2/', 'SCOPE': ['read', 'write'], }, } diff --git a/portail_citoyen2/static/portail_citoyen/js/autologin.js b/portail_citoyen2/static/portail_citoyen/js/autologin.js new file mode 100644 index 0000000..f04780f --- /dev/null +++ b/portail_citoyen2/static/portail_citoyen/js/autologin.js @@ -0,0 +1,20 @@ +function autologin(authentic_url, login_url, next_url) { + var document = window.document; + var callback_name = 'callback'+Math.floor(Math.random()*100000+1); + window[callback_name] = function(is_logged) { + var s; + if (is_logged) { + if (login_url.indexOf('?') == -1) { + s = '?'; + } else { + s = '&'; + } + window.location.href = login_url + s + 'next=' + encodeURI(next_url); + } + } + var script = document.createElement('script'); + script.type = 'text/javascript'; + script.async = true; + script.src = authentic_url + '/accounts/logged-in/?callback=' + callback_name; + document.getElementsByTagName('head')[0].appendChild(script); +} diff --git a/portail_citoyen2/templates/base.html b/portail_citoyen2/templates/base.html index 4e447a1..861597a 100644 --- a/portail_citoyen2/templates/base.html +++ b/portail_citoyen2/templates/base.html @@ -11,6 +11,7 @@ {% endblock %} + {% include 'portail_citoyen/autologin.html' %} {% cms_toolbar %}