Add SSO using auth_pubtkt for 3 web apps

- Install mod_auth_pubtkt and generate public/private key-pair.

- Redirect user to login page if no cookie is presented.

- Add check for authenticated user for login page.

- Temporarily switched to DSA because of a bug in mod_auth_pubtkt
  which causes it to accept only DSA and not RSA. Also had to use SHA1
  instead of SHA256.

- Enabled SSO for Syncthing, Repro and TT-RSS.

- Using tokens to authorize by user groups.

- Generate keys during first boot.
This commit is contained in:
Joseph Nuthalpati authored and James Valleroy committed 2017-06-03 10:29:42 -04:00
1 parent 0cc8d3ec1d
commit 995365f3df
15 files changed
+324 -17

No files matched your search

+45
View File
@@ -0,0 +1,45 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Plinth module to configure Single Sign On services.
"""
from plinth import actions, action_utils
from django.utils.translation import ugettext_lazy as _
version = 1
is_essential = True
depends = ['security']
title = _('Single Sign On')
managed_packages = ['libapache2-mod-auth-pubtkt', 'openssl', 'python3-openssl']
first_boot_steps = [
{
'id': 'sso_firstboot',
'url': 'sso:firstboot',
'order': 1
},
]
def setup(helper, old_version=None):
"""Install the required packages"""
helper.install(managed_packages)
+49
View File
@@ -0,0 +1,49 @@
{% extends "base.html" %}
{% comment %}
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
{% endcomment %}
{% load bootstrap %}
{% load i18n %}
{% block content %}
<form class="form" method="post">
<div class="row">
{% csrf_token %}
<div class="col-sm-3 col-md-offset-3">
{{ form|bootstrap }}
<div class="text-center">
<input type="submit" class="btn btn-primary"
value="{% trans "Login" %}" />
</div>
</div>
</div>
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
{% block page_js %}
<script>
$('#id_username').focus();
</script>
{% endblock %}
+30
View File
@@ -0,0 +1,30 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
URLs for the Single Sign On module.
"""
from django.conf.urls import url
from .views import login, refresh, FirstBootView
from stronghold.decorators import public
urlpatterns = [
url(r'^accounts/sso/login/$', public(login), name='sso-login'),
url(r'^accounts/sso/refresh/$', refresh, name='sso-refresh'),
url(r'^accounts/sso/firstboot/$', public(FirstBootView.as_view()), name='firstboot'),
]
+86
View File
@@ -0,0 +1,86 @@
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
Views for the Single Sign On module of Plinth
"""
import os
import urllib
from plinth import actions
from plinth.modules import first_boot
from django.urls import reverse
from django.http import HttpResponseRedirect
from django.views.generic.base import RedirectView
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)
PRIVATE_KEY_FILE_NAME = 'privkey.pem'
SSO_COOKIE_NAME = 'auth_pubtkt'
KEYS_DIRECTORY = '/etc/apache2/auth-pubtkt-keys'
def set_ticket_cookie(user, response):
"""Generate and set a mod_auth_pubtkt as a cookie in the provided
response.
"""
tokens = list(map(lambda g: g.name, user.groups.all()))
private_key_file = os.path.join(KEYS_DIRECTORY, PRIVATE_KEY_FILE_NAME)
ticket = actions.superuser_run('auth-pubtkt', [
'generate-ticket', '--uid', user.username, '--private-key-file',
private_key_file, '--tokens', ','.join(tokens)
])
response.set_cookie(SSO_COOKIE_NAME, urllib.parse.quote(ticket))
return response
def login(request):
"""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
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
@login_required
def refresh(request):
"""Simulate cookie refresh - redirect logged in user with a new cookie"""
redirect_url = request.GET.get(REDIRECT_FIELD_NAME, '')
response = HttpResponseRedirect(redirect_url)
response.delete_cookie(SSO_COOKIE_NAME)
return set_ticket_cookie(request.user, response)
class FirstBootView(RedirectView):
"""Create keys for Apache server during first boot"""
def get_redirect_url(self, *args, **kwargs):
actions.superuser_run('auth-pubtkt', ['create-key-pair'])
first_boot.mark_step_done('sso_firstboot')
return reverse(first_boot.next_step())
+1 -1
View File
@@ -38,7 +38,7 @@ first_boot_steps = [
{
'id': 'users_firstboot',
'url': 'users:firstboot',
'order': 1
'order': 2
},
]
+6 -4
View File
@@ -20,11 +20,14 @@ URLs for the Users module
"""
from django.conf.urls import url
from django.contrib.auth import views as auth_views
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 . import views
@@ -39,9 +42,8 @@ urlpatterns = [
non_admin_view(views.UserChangePassword.as_view()),
name='change_password'),
# Add Django's login/logout urls
url(r'^accounts/login/$', public(auth_views.login),
{'template_name': 'login.html'}, name='login'),
url(r'^accounts/logout/$', public(auth_views.logout),
url(r'^accounts/login/$', public(sso_login), name='login'),
url(r'^accounts/logout/$', public(sso_logout),
{'next_page': reverse_lazy('index')}, name='logout'),
url(r'^users/firstboot/$', public(views.FirstBootView.as_view()),
name='firstboot'),