SSO: Use Login and Logout view classes instead of methods

- Closes #965

Signed-off-by: Joseph Nuthalpati <njoseph@thoughtworks.com>

Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Joseph Nuthalpati 2017-08-09 17:17:27 +05:30 committed by James Valleroy
parent 08e84001a8
commit db479a7ae9
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
3 changed files with 27 additions and 21 deletions

View File

@ -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'),
]

View File

@ -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

View File

@ -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'),