Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
184 additions
and
1 deletion.
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
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,53 @@ | ||
from django import forms | ||
from django.utils.crypto import get_random_string | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
from ....admin.forms import YesNoSwitch | ||
from .base import ChangeSettingsForm | ||
|
||
|
||
class ChangeSSOSettingsForm(ChangeSettingsForm): | ||
settings = ["enable_sso", "sso_public_key", "sso_private_key", "sso_url"] | ||
|
||
enable_sso = YesNoSwitch( | ||
label=_("Enable Single Sign-On"), | ||
help_text=_( | ||
"Enabling SSO will make login option redirect users to the server URL " | ||
"configured below. It will also disable option to register on forum, " | ||
"change username, email or passward, as those features will be delegated " | ||
"to the 3rd party site." | ||
), | ||
) | ||
sso_public_key = forms.CharField( | ||
label=_("Public key"), | ||
help_text=_( | ||
"Leave this field empty for Misago to generate this key on form submission." | ||
), | ||
max_length=64, | ||
required=False, | ||
) | ||
sso_private_key = forms.CharField( | ||
label=_("Private key"), | ||
help_text=_( | ||
"Leave this field empty for Misago to generate this key on form submission." | ||
), | ||
max_length=64, | ||
required=False, | ||
) | ||
sso_url = forms.URLField(label=_("Server URL"), max_length=255, required=False) | ||
|
||
def clean(self): | ||
cleaned_data = super().clean() | ||
|
||
if cleaned_data.get("enable_sso"): | ||
if not cleaned_data.get("sso_public_key"): | ||
cleaned_data["sso_public_key"] = get_random_string(64) | ||
if not cleaned_data.get("sso_private_key"): | ||
cleaned_data["sso_private_key"] = get_random_string(64) | ||
|
||
if not cleaned_data.get("sso_url"): | ||
self.add_error( | ||
"sso_url", _("You need to enter server URL to enable SSO.") | ||
) | ||
|
||
return cleaned_data |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from django.urls import reverse | ||
|
||
from ...models import Setting | ||
|
||
admin_link = reverse("misago:admin:settings:sso:index") | ||
|
||
|
||
def test_sso_form_generates_public_key_when_enabling_sso(admin_client): | ||
response = admin_client.post( | ||
admin_link, {"enable_sso": "1", "sso_url": "https://test.com"} | ||
) | ||
setting = Setting.objects.get(setting="sso_public_key") | ||
assert setting.value | ||
|
||
|
||
def test_sso_form_generates_private_key_when_enabling_sso(admin_client): | ||
response = admin_client.post( | ||
admin_link, {"enable_sso": "1", "sso_url": "https://test.com"} | ||
) | ||
setting = Setting.objects.get(setting="sso_private_key") | ||
assert setting.value | ||
|
||
|
||
def test_sso_public_key_can_be_set_explicitly_when_enabling_sso(admin_client): | ||
response = admin_client.post( | ||
admin_link, | ||
{"enable_sso": "1", "sso_public_key": "custom", "sso_url": "https://test.com"}, | ||
) | ||
setting = Setting.objects.get(setting="sso_public_key") | ||
assert setting.value == "custom" | ||
|
||
|
||
def test_sso_private_key_can_be_set_explicitly_when_enabling_sso(admin_client): | ||
response = admin_client.post( | ||
admin_link, | ||
{"enable_sso": "1", "sso_private_key": "custom", "sso_url": "https://test.com"}, | ||
) | ||
setting = Setting.objects.get(setting="sso_private_key") | ||
assert setting.value == "custom" | ||
|
||
|
||
def test_form_requires_sso_url_when_enabling_sso(admin_client): | ||
response = admin_client.post( | ||
admin_link, {"enable_sso": "1", "sso_private_key": "custom", "sso_url": ""} | ||
) | ||
setting = Setting.objects.get(setting="enable_sso") | ||
assert not setting.value |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generated by Django 2.2.1 on 2019-05-19 00:16 | ||
|
||
from django.conf import settings | ||
from django.db import migrations | ||
|
||
from ..hydrators import dehydrate_value | ||
|
||
settings = [ | ||
{ | ||
"setting": "enable_sso", | ||
"python_type": "bool", | ||
"dry_value": False, | ||
"is_public": True, | ||
}, | ||
{"setting": "sso_public_key", "is_public": False}, | ||
{"setting": "sso_private_key", "is_public": False}, | ||
{"setting": "sso_url", "is_public": False}, | ||
] | ||
|
||
|
||
def create_settings(apps, _): | ||
Setting = apps.get_model("misago_conf", "Setting") | ||
for setting in settings: | ||
data = setting.copy() | ||
if "python_type" in data and "dry_value" in data: | ||
data["dry_value"] = dehydrate_value(data["python_type"], data["dry_value"]) | ||
|
||
Setting.objects.create(**setting) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("misago_conf", "0004_create_settings")] | ||
|
||
operations = [migrations.RunPython(create_settings)] |
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,10 @@ | ||
{% extends "misago/admin/conf/form.html" %} | ||
{% load i18n misago_admin_form %} | ||
|
||
|
||
{% block form-body %} | ||
{% form_row form.enable_sso %} | ||
{% form_row form.sso_public_key %} | ||
{% form_row form.sso_private_key %} | ||
{% form_row form.sso_url %} | ||
{% endblock form-body %} |
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