-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trigger): add custom get metadata hook (#342)
* feat(trigger): add custom get metadata hook * use custom trigger to override metadata retrieval * update README * update tests with new functions * Remove prints * Ignore types * Remove unused import * Update Django patch versions --------- Co-authored-by: Mostafa Moradian <[email protected]> Co-authored-by: Mostafa Moradian <[email protected]>
- Loading branch information
1 parent
2fecbec
commit 8429830
Showing
5 changed files
with
101 additions
and
8 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
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,27 @@ | ||
<md:EntityDescriptor entityID="https://testserver2.com/entity" validUntil="2025-08-30T19:10:29Z" | ||
xmlns:md="urn:oasis:names:tc:SAML:2.0:METADATA1" | ||
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" | ||
xmlns:mdrpi="urn:oasis:names:tc:SAML:METADATA1:rpi" | ||
xmlns:mdattr="urn:oasis:names:tc:SAML:METADATA1:attribute" | ||
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> | ||
<!-- insert ds:Signature element (omitted) --> | ||
<md:Extensions> | ||
<mdrpi:RegistrationInfo registrationAuthority="https://testserver2.com/"/> | ||
<mdrpi:PublicationInfo creationInstant="2025-08-16T19:10:29Z" publisher="https://testserver2.com/"/> | ||
<mdattr:EntityAttributes> | ||
<saml:Attribute Name="https://testserver2.com/entity-category" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> | ||
<saml:AttributeValue>https://testserver2.com/category/self-certified</saml:AttributeValue> | ||
</saml:Attribute> | ||
</mdattr:EntityAttributes> | ||
</md:Extensions> | ||
<!-- insert one or more concrete instances of the md:RoleDescriptor abstract type (see below) --> | ||
<md:Organization> | ||
<md:OrganizationName xml:lang="en">...</md:OrganizationName> | ||
<md:OrganizationDisplayName xml:lang="en">...</md:OrganizationDisplayName> | ||
<md:OrganizationURL xml:lang="en">https://testserver2.com/</md:OrganizationURL> | ||
</md:Organization> | ||
<md:ContactPerson contactType="technical"> | ||
<md:SurName>SAML Technical Support</md:SurName> | ||
<md:EmailAddress>mailto:[email protected]</md:EmailAddress> | ||
</md:ContactPerson> | ||
</md:EntityDescriptor> |
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 |
---|---|---|
|
@@ -88,6 +88,11 @@ | |
<md:EmailAddress>mailto:[email protected]</md:EmailAddress> | ||
</md:ContactPerson> | ||
</md:EntityDescriptor>""" | ||
DOMAIN_PATH_MAP = { | ||
"example.org": "django_saml2_auth/tests/metadata.xml", | ||
"example.com": "django_saml2_auth/tests/metadata2.xml", | ||
"api.example.com": "django_saml2_auth/tests/metadata.xml", | ||
} | ||
|
||
|
||
def get_metadata_auto_conf_urls( | ||
|
@@ -587,3 +592,51 @@ def test_acs_view_when_redirection_state_is_passed_in_relay_state( | |
|
||
result = acs(post_request) | ||
assert result["Location"] == "/admin/logs" | ||
|
||
|
||
def get_custom_metadata_example( | ||
user_id: Optional[str] = None, | ||
domain: Optional[str] = None, | ||
saml_response: Optional[str] = None, | ||
): | ||
""" | ||
Get metadata file locally depending on current SP domain | ||
""" | ||
metadata_file_path = "/absolute/path/to/metadata.xml" | ||
if domain: | ||
protocol_idx = domain.find("https://") | ||
if protocol_idx > -1: | ||
domain = domain[protocol_idx + 8:] | ||
if domain in DOMAIN_PATH_MAP: | ||
print('metadata domain', domain) | ||
metadata_file_path = DOMAIN_PATH_MAP[domain] | ||
print('metadata path', metadata_file_path) | ||
else: | ||
raise SAMLAuthError(f"Domain {domain} not mapped!") | ||
else: | ||
# Fallback to local path | ||
metadata_file_path = "/absolute/path/to/metadata.xml" | ||
return {"local": [metadata_file_path]} | ||
|
||
|
||
# WARNING: leave this test at the end or add | ||
# settings.SAML2_AUTH["TRIGGER"]["GET_CUSTOM_METADATA"] = None | ||
# to following tests that uses settings, otherwise the TRIGGER.GET_CUSTOM_METADATA is always set | ||
# and used in the get_metadata function | ||
|
||
def test_get_metadata_success_with_custom_trigger(settings: SettingsWrapper): | ||
"""Test get_metadata function to verify if correctly returns path to local metadata file. | ||
Args: | ||
settings (SettingsWrapper): Fixture for django settings | ||
""" | ||
settings.SAML2_AUTH["TRIGGER"]["GET_METADATA_AUTO_CONF_URLS"] = None | ||
settings.SAML2_AUTH["TRIGGER"]["GET_CUSTOM_METADATA"] = "django_saml2_auth.tests.test_saml.get_custom_metadata_example" | ||
|
||
result = get_metadata(domain="https://example.com") | ||
assert result == {"local": ["django_saml2_auth/tests/metadata2.xml"]} | ||
|
||
with pytest.raises(SAMLAuthError) as exc_info: | ||
get_metadata(domain="not-mapped-example.com") | ||
|
||
assert str(exc_info.value) == "Domain not-mapped-example.com not mapped!" |