2018-11-26 21:40:10 +00:00
|
|
|
"""passbook SAML IDP Forms"""
|
|
|
|
|
|
|
|
from django import forms
|
2020-06-05 18:18:45 +00:00
|
|
|
from django.utils.html import mark_safe
|
2019-03-10 17:34:09 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2018-11-26 21:40:10 +00:00
|
|
|
|
2020-06-05 18:18:45 +00:00
|
|
|
from passbook.admin.fields import CodeMirrorWidget
|
2020-06-05 10:00:27 +00:00
|
|
|
from passbook.core.expression import PropertyMappingEvaluator
|
2020-11-08 21:27:28 +00:00
|
|
|
from passbook.crypto.models import CertificateKeyPair
|
2020-06-07 14:35:08 +00:00
|
|
|
from passbook.flows.models import Flow, FlowDesignation
|
2020-07-11 11:28:03 +00:00
|
|
|
from passbook.providers.saml.models import SAMLPropertyMapping, SAMLProvider
|
2018-11-26 21:40:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SAMLProviderForm(forms.ModelForm):
|
|
|
|
"""SAML Provider form"""
|
|
|
|
|
2020-08-01 18:00:20 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields["authorization_flow"].queryset = Flow.objects.filter(
|
|
|
|
designation=FlowDesignation.AUTHORIZATION
|
|
|
|
)
|
|
|
|
self.fields["property_mappings"].queryset = SAMLPropertyMapping.objects.all()
|
2020-11-08 21:27:28 +00:00
|
|
|
self.fields["signing_kp"].queryset = CertificateKeyPair.objects.exclude(
|
|
|
|
key_data__iexact=""
|
|
|
|
)
|
2018-12-09 22:06:14 +00:00
|
|
|
|
2018-11-26 21:40:10 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = SAMLProvider
|
2019-12-31 11:51:16 +00:00
|
|
|
fields = [
|
|
|
|
"name",
|
2020-06-07 14:35:08 +00:00
|
|
|
"authorization_flow",
|
2019-12-31 11:51:16 +00:00
|
|
|
"acs_url",
|
|
|
|
"audience",
|
|
|
|
"issuer",
|
2020-06-07 14:35:08 +00:00
|
|
|
"sp_binding",
|
2020-02-14 14:19:48 +00:00
|
|
|
"assertion_valid_not_before",
|
|
|
|
"assertion_valid_not_on_or_after",
|
|
|
|
"session_valid_not_on_or_after",
|
2020-02-17 15:28:18 +00:00
|
|
|
"digest_algorithm",
|
|
|
|
"signature_algorithm",
|
2020-03-05 16:09:08 +00:00
|
|
|
"signing_kp",
|
2020-11-08 21:24:50 +00:00
|
|
|
"verification_kp",
|
|
|
|
"property_mappings",
|
2019-12-31 11:51:16 +00:00
|
|
|
]
|
2018-12-09 22:06:14 +00:00
|
|
|
widgets = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"name": forms.TextInput(),
|
|
|
|
"audience": forms.TextInput(),
|
|
|
|
"issuer": forms.TextInput(),
|
2020-02-14 14:19:48 +00:00
|
|
|
"assertion_valid_not_before": forms.TextInput(),
|
|
|
|
"assertion_valid_not_on_or_after": forms.TextInput(),
|
|
|
|
"session_valid_not_on_or_after": forms.TextInput(),
|
2018-12-09 22:06:14 +00:00
|
|
|
}
|
2019-03-08 11:47:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SAMLPropertyMappingForm(forms.ModelForm):
|
|
|
|
"""SAML Property Mapping form"""
|
|
|
|
|
2020-06-20 19:49:16 +00:00
|
|
|
template_name = "providers/saml/property_mapping_form.html"
|
2020-02-17 19:30:14 +00:00
|
|
|
|
2020-06-05 10:00:27 +00:00
|
|
|
def clean_expression(self):
|
|
|
|
"""Test Syntax"""
|
|
|
|
expression = self.cleaned_data.get("expression")
|
|
|
|
evaluator = PropertyMappingEvaluator()
|
|
|
|
evaluator.validate(expression)
|
|
|
|
return expression
|
|
|
|
|
2019-03-08 11:47:50 +00:00
|
|
|
class Meta:
|
|
|
|
|
|
|
|
model = SAMLPropertyMapping
|
2020-02-17 19:38:14 +00:00
|
|
|
fields = ["name", "saml_name", "friendly_name", "expression"]
|
2019-03-08 11:47:50 +00:00
|
|
|
widgets = {
|
2019-12-31 11:51:16 +00:00
|
|
|
"name": forms.TextInput(),
|
|
|
|
"saml_name": forms.TextInput(),
|
|
|
|
"friendly_name": forms.TextInput(),
|
2020-06-05 18:18:45 +00:00
|
|
|
"expression": CodeMirrorWidget(mode="python"),
|
|
|
|
}
|
|
|
|
help_texts = {
|
|
|
|
"saml_name": mark_safe(
|
|
|
|
_(
|
|
|
|
"URN OID used by SAML. This is optional. "
|
|
|
|
'<a href="https://www.rfc-editor.org/rfc/rfc2798.html#section-2">Reference</a>'
|
|
|
|
)
|
|
|
|
),
|
2019-03-08 14:11:01 +00:00
|
|
|
}
|