-
-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Added view to collect email in SAML registration
If the IdP does not include email address in SAML response, then the user will be asked for email during sign-up.
- Loading branch information
Showing
5 changed files
with
140 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
openwisp_radius/templates/djangosaml2/login_additional_info.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{% extends "account/base_manage.html" %} | ||
{% load i18n %} | ||
|
||
{% block content %} | ||
<h1>{% trans "Add Your Email to Complete Your Account Setup" %}</h1> | ||
<p>{% blocktrans %}It looks like we didn't receive your email from your identity provider.</br>Please enter your email address below to complete setting up your account.{% endblocktrans %}</p> | ||
<form method="post"> | ||
{% csrf_token %} | ||
{{ form.as_p }} | ||
<input type="submit" class="button" value="{% trans "Save Email Address and Continue" %}"> | ||
</form> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,9 +3,11 @@ | |
from urllib.parse import parse_qs, urlparse | ||
|
||
import swapper | ||
from django.conf import settings | ||
from django.contrib.auth import SESSION_KEY, get_user_model | ||
from django.core import mail | ||
from django.test import TestCase, override_settings | ||
from django.urls import reverse | ||
from django.urls import reverse, reverse_lazy | ||
from djangosaml2.tests import auth_response, conf | ||
from djangosaml2.utils import get_session_id_from_saml2, saml2_from_httpredirect_request | ||
from rest_framework.authtoken.models import Token | ||
|
@@ -74,15 +76,18 @@ def _post_successful_auth_assertions(self, query_params, org_slug): | |
org_user = OrganizationUser.objects.get(user_id=user_id) | ||
self.assertEqual(org_user.organization.slug, org_slug) | ||
expected_query_params = { | ||
'username': ['[email protected]'], | ||
'token': [Token.objects.get(user_id=user_id).key], | ||
'login_method': ['saml'], | ||
'next': [ | ||
'/radius/saml2/additional-info/' | ||
'?username=org_user%40example.com' | ||
f'&token={Token.objects.get(user_id=user_id).key}' | ||
'&login_method=saml' | ||
] | ||
} | ||
self.assertDictEqual(query_params, expected_query_params) | ||
|
||
@capture_any_output() | ||
def test_organization_slug_present(self): | ||
expected_redirect_url = 'https://captive-portal.example.com' | ||
expected_redirect_url = '/radius/saml2/additional-info/' | ||
org_slug = 'default' | ||
relay_state = self._get_relay_state( | ||
redirect_url=expected_redirect_url, org_slug=org_slug | ||
|
@@ -102,7 +107,7 @@ def test_organization_slug_present(self): | |
|
||
@capture_any_output() | ||
def test_relay_state_relative_path(self): | ||
expected_redirect_path = '/captive/portal/page' | ||
expected_redirect_path = '/radius/saml2/additional-info/' | ||
org_slug = 'default' | ||
relay_state = self._get_relay_state( | ||
redirect_url=expected_redirect_path, org_slug=org_slug | ||
|
@@ -165,6 +170,51 @@ def test_user_registered_with_non_saml_method(self): | |
self.assertEqual(user.username, '[email protected]') | ||
|
||
|
||
@override_settings(SAML_ALLOWED_HOSTS=['captive-portal.example.com']) | ||
class TestAdditionInfoView(TestSamlMixin, TestCase): | ||
view_path = reverse_lazy('radius:saml2_additional_info') | ||
|
||
def test_unauthenticated_user(self): | ||
response = self.client.get(self.view_path) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(response.url, f'{settings.LOGIN_URL}?next={self.view_path}') | ||
|
||
def test_user_has_email(self): | ||
user = self._create_user() | ||
self.client.force_login(user) | ||
response = self.client.get( | ||
f'{self.view_path}?next=https://captive-portal.example.com/login/' | ||
) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(response.url, 'https://captive-portal.example.com/login/') | ||
|
||
def test_user_has_no_email(self): | ||
user = self._create_user() | ||
User.objects.filter(pk=user.pk).update(email='') | ||
self.client.force_login(user) | ||
url = f'{self.view_path}?next=https://captive-portal.example.com/login/' | ||
response = self.client.get(url) | ||
# Verify that a form is displayed to collect email | ||
self.assertEqual(response.status_code, 200) | ||
self.assertContains( | ||
response, | ||
'<input type="email" name="email" maxlength="254" required id="id_email">', | ||
) | ||
|
||
# Make post request to set email | ||
response = self.client.post(url, data={'email': '[email protected]'}) | ||
self.assertEqual(response.status_code, 302) | ||
self.assertEqual(response.url, 'https://captive-portal.example.com/login/') | ||
user.refresh_from_db() | ||
self.assertEqual(user.email, '[email protected]') | ||
self.assertEqual( | ||
user.emailaddress_set.filter(email='[email protected]').count(), 1 | ||
) | ||
self.assertEqual( | ||
mail.outbox[0].subject, '[example.com] Please Confirm Your Email Address' | ||
) | ||
|
||
|
||
@override_settings( | ||
SAML_CONFIG=conf.create_conf( | ||
sp_host='sp.example.com', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters