Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix saml login errors after rebase on a+ #95

Merged
merged 1 commit into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ lint-html-fix:
.PHONY: lint-html-files
lint-html-files:
EXIT_STATUS=0; \
$(VIRTUAL_ENV)/bin/djlint $(ARGUMENTS) --profile=django --ignore=H006,H030,H031 || EXIT_STATUS=$$?; \
$(VIRTUAL_ENV)/bin/djlint $(ARGUMENTS) --profile=django --ignore=T002,H006,H030,H031 || EXIT_STATUS=$$?; \
exit $${EXIT_STATUS}

.PHONY: lint-python-files
Expand Down
64 changes: 34 additions & 30 deletions adhocracy-plus/config/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,40 +49,45 @@
BASEDIR = path.dirname(path.abspath(__file__))

SAML_CONFIG = {
'entityid': 'http://app.example.com',
'allow_unknown_attributes': True,
'attribute_map_dir': path.join(BASEDIR, 'saml', 'attribute-maps'),
'service': {
'sp': {
'name': 'Federated Django sample SP',
'name_id_format': saml2.saml.NAMEID_FORMAT_PERSISTENT,
'endpoints': {
'single_logout_service': [
('http://localhost:8004/saml2/ls/', saml2.BINDING_HTTP_REDIRECT),
('http://localhost:8004/saml2/ls/post', saml2.BINDING_HTTP_POST),
],
'assertion_consumer_service': [
('http://localhost:8004/saml2/acs/', saml2.BINDING_HTTP_POST),
"entityid": "http://app.example.com",
"allow_unknown_attributes": True,
"attribute_map_dir": path.join(BASEDIR, "saml", "attribute-maps"),
"service": {
"sp": {
"name": "Federated Django sample SP",
"name_id_format": saml2.saml.NAMEID_FORMAT_PERSISTENT,
"endpoints": {
"single_logout_service": [
("http://localhost:8004/saml2/ls/", saml2.BINDING_HTTP_REDIRECT),
("http://localhost:8004/saml2/ls/post", saml2.BINDING_HTTP_POST),
],
"assertion_consumer_service": [
("http://localhost:8004/saml2/acs/", saml2.BINDING_HTTP_POST),
],
},
"required_attributes": ["mail"],
"allow_unsolicited": False,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only thing which changed here is adding this line, the rest was auto-formatting

},
},
"metadata": {
"remote": [
{"url": "http://localhost:8080/simplesaml/saml2/idp/metadata.php"},
],
},
'required_attributes': ['mail'],
},
},
'metadata': {
'remote': [{"url": "http://localhost:8080/simplesaml/saml2/idp/metadata.php"},],
},
'key_file': path.join(BASEDIR, 'saml', 'private.key'),
'cert_file': path.join(BASEDIR, 'saml', 'cert.pem'),
'encryption_keypairs': [{
'key_file': path.join(BASEDIR, 'saml', 'private.key'),
'cert_file': path.join(BASEDIR, 'saml', 'cert.pem'),
}],
'debug': 1,
"key_file": path.join(BASEDIR, "saml", "private.key"),
"cert_file": path.join(BASEDIR, "saml", "cert.pem"),
"encryption_keypairs": [
{
"key_file": path.join(BASEDIR, "saml", "private.key"),
"cert_file": path.join(BASEDIR, "saml", "cert.pem"),
}
],
"debug": 1,
}
SAML_DJANGO_USER_MAIN_ATTRIBUTE = 'email'
SAML_DJANGO_USER_MAIN_ATTRIBUTE = "email"
SAML_LOGOUT_REQUEST_PREFERRED_BINDING = saml2.BINDING_HTTP_REDIRECT
SAML_ATTRIBUTE_MAPPING = {
'mail': ['email', 'set_username_from_email'],
"mail": ["email", "set_username_from_email"],
}

# The local.py import happens at the end of this file so that it can overwrite
Expand All @@ -106,4 +111,3 @@
CKEDITOR_CONFIGS["video-editor"]["embed_provider"] = CKEDITOR_URL
except NameError:
pass

16 changes: 6 additions & 10 deletions apps/djangosaml2_overwrites/overwrites.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
from djangosaml2.urls import urlpatterns
from djangosaml2.views import AssertionConsumerServiceView

from apps.users.models import User

from .urls import urlpatterns as custom_urlpatterns


Expand All @@ -15,21 +13,19 @@ def apply_custom_overwrites():


def customize_session(self, user, session_info):
user_obj = User.objects.get(email=user)
if not EmailAddress.objects.filter(user=user_obj, email=user).exists():
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passing email=user doesn't work anymore, I'm not entirely sure why

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of the latest allauth, we changed I think the mandatory fields. did you try email=user.email?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this is what I did in this PR

email_address = EmailAddress(user=user_obj, email=user)
if not EmailAddress.objects.filter(user=user, email=user.email).exists():
email_address = EmailAddress(user=user, email=user.email)
email_address.save()
if not EmailAddress.objects.filter(user=user_obj, primary=True).exists():
email_address = EmailAddress.objects.get(user=user_obj, email=user)
if not EmailAddress.objects.filter(user=user, primary=True).exists():
email_address = EmailAddress.objects.get(user=user, email=user.email)
email_address.primary = True
email_address.save()


def custom_redirect(self, user, relay_state, session_info):
user_obj = User.objects.get(email=user)
email_address = EmailAddress.objects.get(user=user_obj, email=user)
email_address = EmailAddress.objects.get(user=user, email=user.email)
if not email_address.verified:
signup = reverse('saml2_signup')
signup = reverse("saml2_signup")
if relay_state:
signup += "?next={}".format(relay_state)
return signup
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{% extends "socialaccount/base.html" %}
{% extends "account/base.html" %}

{% load i18n %}

{% block head_title %}{% trans "Signup" %}{% endblock %}
{% block head_title %}{% trans "Signup" %}{% endblock head_title %}

{% block content %}
<h1>{% trans "Sign Up" %}</h1>
Expand Down Expand Up @@ -53,7 +53,7 @@ <h1>{% trans "Sign Up" %}</h1>
<div class="form-check">
<label class="form-check__label">
{{ form.terms_of_use_extra }}
{{ form.terms_of_use_extra.label|safe}}
{{ form.terms_of_use_extra.label|safe }}
{% if form.terms_of_use_extra.field.required %}<span role="presentation" title="{% trans 'This field is required' %}">*</span>{% endif %}
</label>
{{ form.terms_of_use.errors }}
Expand Down Expand Up @@ -87,4 +87,4 @@ <h1>{% trans "Sign Up" %}</h1>
</div>
</form>

{% endblock %}
{% endblock content %}
3 changes: 3 additions & 0 deletions changelog/_1111.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- fix saml login broken after rebase on latest a+