From db479a7ae9a49d0ae70d8cd83d3581438ef71ab3 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalpati Date: Wed, 9 Aug 2017 17:17:27 +0530 Subject: [PATCH] SSO: Use Login and Logout view classes instead of methods - Closes #965 Signed-off-by: Joseph Nuthalpati Reviewed-by: James Valleroy --- plinth/modules/sso/urls.py | 4 ++-- plinth/modules/sso/views.py | 35 ++++++++++++++++++++++------------- plinth/modules/users/urls.py | 9 +++------ 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/plinth/modules/sso/urls.py b/plinth/modules/sso/urls.py index 9e9cc8863..8cd6227a4 100644 --- a/plinth/modules/sso/urls.py +++ b/plinth/modules/sso/urls.py @@ -20,10 +20,10 @@ URLs for the Single Sign On module. from django.conf.urls import url -from .views import login, refresh +from .views import SSOLoginView, refresh from stronghold.decorators import public urlpatterns = [ - url(r'^accounts/sso/login/$', public(login), name='sso-login'), + url(r'^accounts/sso/login/$', public(SSOLoginView.as_view()), name='sso-login'), url(r'^accounts/sso/refresh/$', refresh, name='sso-refresh'), ] diff --git a/plinth/modules/sso/views.py b/plinth/modules/sso/views.py index 8b0b220bf..a3de34ef8 100644 --- a/plinth/modules/sso/views.py +++ b/plinth/modules/sso/views.py @@ -26,8 +26,7 @@ from plinth import actions from django.http import HttpResponseRedirect from django.contrib.auth import REDIRECT_FIELD_NAME from django.contrib.auth.decorators import login_required -from django.contrib.auth.views import (login as auth_login, logout as - auth_logout) +from django.contrib.auth.views import LoginView, LogoutView PRIVATE_KEY_FILE_NAME = 'privkey.pem' SSO_COOKIE_NAME = 'auth_pubtkt' @@ -48,21 +47,31 @@ def set_ticket_cookie(user, response): return response -def login(request): - """Login to Plinth and set a auth_pubtkt cookie which will be +class SSOLoginView(LoginView): + """View to login to Plinth and set a auth_pubtkt cookie which will be used to provide Single Sign On for some other applications """ - response = auth_login( - request, template_name='login.html', redirect_authenticated_user=True) - return set_ticket_cookie( - request.user, response) if request.user.is_authenticated else response + + redirect_authenticated_user = True + template_name = 'login.html' + + def dispatch(self, request, *args, **kwargs): + response = super(SSOLoginView, self).dispatch(request, *args, **kwargs) + return set_ticket_cookie( + request.user, + response) if request.user.is_authenticated else response -def logout(request, next_page): - """Log out of Plinth and remove auth_pubtkt cookie""" - response = auth_logout(request, next_page=next_page) - response.delete_cookie(SSO_COOKIE_NAME) - return response +class SSOLogoutView(LogoutView): + """View to log out of Plinth and remove the auth_pubtkt cookie""" + + template_name = 'index.html' + + def dispatch(self, request, *args, **kwargs): + response = super(SSOLogoutView, self).dispatch(request, *args, + **kwargs) + response.delete_cookie(SSO_COOKIE_NAME) + return response @login_required diff --git a/plinth/modules/users/urls.py b/plinth/modules/users/urls.py index 0f548a2c1..5d00293d2 100644 --- a/plinth/modules/users/urls.py +++ b/plinth/modules/users/urls.py @@ -24,10 +24,7 @@ from django.urls import reverse_lazy from stronghold.decorators import public from plinth.utils import non_admin_view -from plinth.modules.sso.views import ( - login as sso_login, - logout as sso_logout -) +from plinth.modules.sso.views import SSOLoginView, SSOLogoutView from . import views @@ -42,8 +39,8 @@ urlpatterns = [ non_admin_view(views.UserChangePassword.as_view()), name='change_password'), # Add Django's login/logout urls - url(r'^accounts/login/$', public(sso_login), name='login'), - url(r'^accounts/logout/$', public(sso_logout), + url(r'^accounts/login/$', public(SSOLoginView.as_view()), name='login'), + url(r'^accounts/logout/$', SSOLogoutView.as_view(), {'next_page': reverse_lazy('index')}, name='logout'), url(r'^users/firstboot/$', public(views.FirstBootView.as_view()), name='firstboot'),