Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Implement real SAML authentication adapter (#5482)

Adrian Gruntkowski605447cd6114
diff --git a/Makefile b/Makefile
index fc8afaa64c..b496919e72 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,11 @@
.PHONY: help install server clickhouse clickhouse-prod clickhouse-stop postgres postgres-client postgres-prod postgres-stop
+require = \
+ $(foreach 1,$1,$(__require))
+__require = \
+ $(if $(value $1),, \
+ $(error Provide required parameter: $1$(if $(value 2), ($(strip $2)))))
+
help:
@perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@@ -57,6 +63,36 @@ minio: ## Start a transient container with a recent version of minio (s3)
minio-stop: ## Stop and remove the minio container
docker stop plausible_minio
+sso:
+ $(call require, integration_id)
+ @echo "Setting up local IdP service..."
+ @docker run --name=idp \
+ -p 8080:8080 \
+ -e SIMPLESAMLPHP_SP_ENTITY_ID=http://localhost:8000/sso/$(integration_id) \
+ -e SIMPLESAMLPHP_SP_ASSERTION_CONSUMER_SERVICE=http://localhost:8000/sso/saml/consume/$(integration_id) \
+ -v $$PWD/extra/fixture/authsources.php:/var/www/simplesamlphp/config/authsources.php -d kenchan0130/simplesamlphp
+
+ @sleep 2
+
+ @echo "Use the following IdP configuration:"
+ @echo ""
+ @echo "Sign-in URL: http://localhost:8080/simplesaml/saml2/idp/SSOService.php"
+ @echo ""
+ @echo "Entity ID: http://localhost:8080/simplesaml/saml2/idp/metadata.php"
+ @echo ""
+ @echo "PEM Certificate:"
+ @curl http://localhost:8080/simplesaml/module.php/saml/idp/certs.php/idp.crt 2>/dev/null
+ @echo ""
+ @echo ""
+ @echo "Following accounts are configured:"
+ @echo "- [email protected] / plausible"
+ @echo "- [email protected] / plausible"
+ @echo "- [email protected] / plausible"
+
+sso-stop:
+ docker stop idp
+ docker remove idp
+
loadtest-server:
@echo "Ensure your OTP installation is built with --enable-lock-counter"
MIX_ENV=load ERL_FLAGS="-emu_type lcnt +Mdai max" iex -S mix do phx.digest + phx.server
diff --git a/extra/fixture/assertion.xml b/extra/fixture/assertion.xml
new file mode 100644
index 0000000000..860e969592
--- /dev/null
+++ b/extra/fixture/assertion.xml
@@ -0,0 +1,9 @@
+<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_eb26ecdc448c13e513b3a39f8eddb8bb77dfa01e25" Version="2.0" IssueInstant="2025-06-12T07:24:17Z" Destination="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_nbnG6j3cYsOUJSuyOnlGjjUBCrB4BrcH"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_eb26ecdc448c13e513b3a39f8eddb8bb77dfa01e25"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>UQLQNaShMv0BjLvgqJNJWz61XAHf+v5LFDUqvMgfWdU=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>R+oEKdanohpq7mKN3AyTDITBXGMjqxL3wnMciHuYo+WRrPOdL+fvNP8knTZuvRlrsQRg+K7bVUd+5V5t+H1RkC+hsYXcyRK0pIbX4GXSMmNhl9XS04R9sErFQ/Yuj3azXgv4YicUVORjG7ti0eqxUN3kzfiS1tcYz0G7lH0MCAx7AwbfLQX6GfoYG949sc5gj1AcC+J5Ql+tk7JT7PrWaqDRl2GpBlnDyAi+/dulgD9Vu41e//XAHkkwe6ZcpdA5rzrRlToeS6C6UbM9Mzx8mQZazFLVSHkzeWR/BmlnK6GfjJXNg/apCK/1PiUef9FFjxHe5KLHVY1/cbzjmlxteQ==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><saml:Assertion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="_9af7ae440c54adc52b2301a0bf628828b4c4372662" Version="2.0" IssueInstant="2025-06-12T07:24:17Z"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_9af7ae440c54adc52b2301a0bf628828b4c4372662"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>TcSdGz0TtFf6X/r3IYj7/95AFBPcas7TIXPTW8hNBRs=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>aXlzemjbwl1UmcBKXNOtHv+TFU8tWQGrJOGg3OUOeDvgBz1YCSq3iMSNGMadMU8WwlJjiZbGww6Jz+zAwNmhHKNvQpoMoV+v0S0Kwpy4uO5VynrD+M4PM0y8xTn8S3izEKaVQiTFHcgUYx9QBYOprBD2U2hIv4Y6hNgwIbuLg+D9qlkWo70xwn5uL1DZel4FxZOuerZfS7tSu5d48s1PcP4ZDWqsO5TUmJ4m6n7/XK17ROIQOccPLvwkVHWXB8g/r/R+lpFedsIh6cFO7Rx6urIjqz5k+ue7jFpuS+eVy/8Oq43q6XVvcOfb3Y8Qu41GkWEHSZp2wKWQVDY/jr12Uw==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><saml:Subject><saml:NameID SPNameQualifier="http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">_ddd0b91c6b00942964e35238a4c9d482e9bdb9b4ad</saml:NameID><saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml:SubjectConfirmationData NotOnOrAfter="2025-06-12T07:29:17Z" Recipient="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_nbnG6j3cYsOUJSuyOnlGjjUBCrB4BrcH"/></saml:SubjectConfirmation></saml:Subject><saml:Conditions NotBefore="2025-06-12T07:23:47Z" NotOnOrAfter="2025-06-12T07:29:17Z"><saml:AudienceRestriction><saml:Audience>http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8</saml:Audience></saml:AudienceRestriction></saml:Conditions><saml:AuthnStatement AuthnInstant="2025-06-12T07:24:17Z" SessionNotOnOrAfter="2025-06-12T15:24:17Z" SessionIndex="_ff4c0c34bb44fa87de72d114c68ee7786278461175"><saml:AuthnContext><saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef></saml:AuthnContext></saml:AuthnStatement><saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">[email protected]</saml:AttributeValue></saml:Attribute><saml:Attribute Name="first_name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">Jane</saml:AttributeValue></saml:Attribute><saml:Attribute Name="last_name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">Smith</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion></samlp:Response>
\ No newline at end of file
diff --git a/extra/fixture/assertion_invalid_email.xml b/extra/fixture/assertion_invalid_email.xml
new file mode 100644
index 0000000000..01e19fbf2f
--- /dev/null
+++ b/extra/fixture/assertion_invalid_email.xml
@@ -0,0 +1,9 @@
+<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_0b2094a6fdb9727ed5be59301f6b6888d17409e9cd" Version="2.0" IssueInstant="2025-06-12T10:28:24Z" Destination="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_gZDvdO2QMKiQ5xo0Oj34ydFV8XpOelDN"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_0b2094a6fdb9727ed5be59301f6b6888d17409e9cd"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>kx1GcbtUDzT+NUl/1cuwCpQqhQGRYaFN4YBUzhwqOb4=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>DVimuduQ1ghcpmEa+yWFDQ7CZBkhYa1omrbFeseap8fh98hIipB5xOcHUhNBWu4mZ2rV4GzFTAmBUJtJxElDTtatUiKzLWlfxRyALcH0rQMFnq9wpU6IKTkA9MNX6BbhBPyP8SiLtE5dW8PUeA2QrUz4bheqOOphAuXCHu2xix31CnsOpJRlttaxyq+ZJhdX8XzN1M6O7uTYNpkBU0oSG4/JWC8qjvWgg45WnB97f1+YbfiVPkB+gHjjY7tQ/2UkI8LVtQ9YD6a6sdikaAp9IyX742z4GCVtjgH6EqChOj5pP0QkZTNEOPice+kX1NVwYuzSDhUfik4UGGVYWU8A5Q==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><saml:Assertion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="_4c1ee740424b669f5e3bf3e65841b09465b0b6e1f7" Version="2.0" IssueInstant="2025-06-12T10:28:24Z"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_4c1ee740424b669f5e3bf3e65841b09465b0b6e1f7"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>KEWH5FrqiOeaNEc2o92eL7izusA68qCAt9b8Ge/qAuM=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>ZiuXxxrxxaD0bSD0QyPPVvvW+arVZEH2TMl/rF7F/xwJHbrXLRkEKCFuY6iQJpOAk+3t5i1edzhQt4YVddWg3TKnr8IMVumUFITd0qdPlcRpyEELyAjfBVBrVYe7GAuI602zAkmiZGiEqUp7HK9y+XWC97bk/QFRGxxgiFL5zh6iN1MU8GpEaFh2OUyEQKGj3Bq7lvuNOatILMkcwmkWfg9BzzQ2/ZcjwalKdd7Iv1oxBmlfrUlD0OYOBwu3rUtYutWcOsUrbRc3lhkS6E0BZ2tVzHIR+qoS9zcA25uqQ+SCQMQUDG1MiUMv06XscPp3Vr/4a+5PDIft9eX19PSAEA==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><saml:Subject><saml:NameID SPNameQualifier="http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">_9f26cf8c1e7fe26bc432975d3689dc53f3d82ca92a</saml:NameID><saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml:SubjectConfirmationData NotOnOrAfter="2025-06-12T10:33:24Z" Recipient="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_gZDvdO2QMKiQ5xo0Oj34ydFV8XpOelDN"/></saml:SubjectConfirmation></saml:Subject><saml:Conditions NotBefore="2025-06-12T10:27:54Z" NotOnOrAfter="2025-06-12T10:33:24Z"><saml:AudienceRestriction><saml:Audience>http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8</saml:Audience></saml:AudienceRestriction></saml:Conditions><saml:AuthnStatement AuthnInstant="2025-06-12T10:28:24Z" SessionNotOnOrAfter="2025-06-12T18:28:24Z" SessionIndex="_6c8da1613927d530a80350506fddc14ac44829d73a"><saml:AuthnContext><saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef></saml:AuthnContext></saml:AuthnStatement><saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">user1</saml:AttributeValue></saml:Attribute><saml:Attribute Name="first_name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">Lenny</saml:AttributeValue></saml:Attribute><saml:Attribute Name="last_name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">Carr</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion></samlp:Response>
\ No newline at end of file
diff --git a/extra/fixture/assertion_missing_email.xml b/extra/fixture/assertion_missing_email.xml
new file mode 100644
index 0000000000..9ab4c8d240
--- /dev/null
+++ b/extra/fixture/assertion_missing_email.xml
@@ -0,0 +1,9 @@
+<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_64a3c0ab4c95376ec797c25f6fa3ed0ca925a33229" Version="2.0" IssueInstant="2025-06-12T10:02:48Z" Destination="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_Gl369hlZLc4r5O7d5fcAmzArJmL29XIX"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_64a3c0ab4c95376ec797c25f6fa3ed0ca925a33229"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>fYVp1zcpNUpQasg8YpE+0SaLpNtWieUZd7AH2LfEMjo=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>u69XMTKduXFPY7eWEnZi1rUBRNgCruSdAwiXDJ9/kut5pUMiL/J7VZ7wsQSj10aePxN/tH36gJFTs2SClEeO1liej6Ef/rvvQFctKKZW0F7Aw9f1EFd11V5RunVaGfhnLDI9l6HSK+39xmA3Jkw4jGU46lfV1ljAt8nuoXosfstos4rloSMAFtDLey2MOBIio7j6iHjsXRKHQsJsth2qtZI1oVBwmkl21u2i5aWujf8cD2Lw54VEi7l8nlWkCuH1tzcOqNBTyxyoMbIuARHUlek8hy19bI4GOdGlgesLDk1sxlBgYEOeZxabn98AuRxB0kUCk0qkJkcoY41qASWEyA==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><saml:Assertion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="_386283e484581f9197bec9b4793c3019dc346c72cf" Version="2.0" IssueInstant="2025-06-12T10:02:48Z"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_386283e484581f9197bec9b4793c3019dc346c72cf"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>RlRAKhymDH0xAYJyF7X/txY+sZr6PHexWbMN7ctSC20=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>NfLq5ZWY4r0hpK7LOigqcDjSK2K5wxl6AHRCP0F3rstUPCsp8YWuC0PBGjGm4z8m47y2mdbWyDI+YcrN283obdAagFJcpPAROM8Ectw63VXpzMl44vIgSjJipU3fOzIdYkaJSuJG2FzT1xiByKzlth/U90W32h5yUkaSCjZUKflbXAtkZM7FOzF7QnZNXhNxZqPx398qt+21n/5mcMwSf55m5Q9igTYH3D36qflrJgD5R0W1cuks3l6181EJSYPnmPzxULk0erJ94CfjOOUHtGc70jQ4ux76XRym20dmRnPvC4RG+F2cDBkCJYvgHYWxDsW3HiNET+8FEc2FO9Bqhg==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><saml:Subject><saml:NameID SPNameQualifier="http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">_1d89d8faa809ed9d2b547362e98fb23cabff28aa8f</saml:NameID><saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml:SubjectConfirmationData NotOnOrAfter="2025-06-12T10:07:48Z" Recipient="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_Gl369hlZLc4r5O7d5fcAmzArJmL29XIX"/></saml:SubjectConfirmation></saml:Subject><saml:Conditions NotBefore="2025-06-12T10:02:18Z" NotOnOrAfter="2025-06-12T10:07:48Z"><saml:AudienceRestriction><saml:Audience>http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8</saml:Audience></saml:AudienceRestriction></saml:Conditions><saml:AuthnStatement AuthnInstant="2025-06-12T10:02:48Z" SessionNotOnOrAfter="2025-06-12T18:02:48Z" SessionIndex="_036e7fb323f70d3ad629d038e49cb9e601ad7315e4"><saml:AuthnContext><saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef></saml:AuthnContext></saml:AuthnStatement><saml:AttributeStatement><saml:Attribute Name="first_name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">Lenny</saml:AttributeValue></saml:Attribute><saml:Attribute Name="last_name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">Carr</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion></samlp:Response>
\ No newline at end of file
diff --git a/extra/fixture/assertion_missing_name.xml b/extra/fixture/assertion_missing_name.xml
new file mode 100644
index 0000000000..7034a52d53
--- /dev/null
+++ b/extra/fixture/assertion_missing_name.xml
@@ -0,0 +1,9 @@
+<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" ID="_c65f0f2a991d2c31385cb8906e0312a2006691a8c8" Version="2.0" IssueInstant="2025-06-12T10:04:07Z" Destination="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_r3I8eRQwA7wUXZyWjHX5jC5NdExpW2Va"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_c65f0f2a991d2c31385cb8906e0312a2006691a8c8"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>Ug1YcZ+jIMiYCixQMD5QPzLY2ZAy7czkRduM40NHtzU=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>AcKB/i3IQnhttssZh7VW56jv9jFml5aq1/CTEioKU7nZl+WeaD4gdzDbIlnxb+onmJRMCdsz5brez5hoHa56cI+a00fFZXENQZdegao04q5hKweXlJ7Rz6XuvaIdmnNJIhVasDS45t3pKVlEwTEQ3Zl0mwM4Tq2dl8sTVTihJOgr4pMtsAcmVOBU0Zb1JYakpoFXoJpa20VuiNuxVZsEhBbJpMCRBaMfEMDufQwywWXna7CbJqxCt8T5ibxERELVY42PAYh7GFSFG/aB3NGq4TwplNuLwKRN3oAsUYprHY/HHYoyOF79ULYCRfqfa3uPdH8eAAnUH4jYYGOF6PqNIQ==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><saml:Assertion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" ID="_ecfa6eaf2723f859273cba029754a33c57d6c3ed20" Version="2.0" IssueInstant="2025-06-12T10:04:07Z"><saml:Issuer>http://localhost:8080/simplesaml/saml2/idp/metadata.php</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
+ <ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
+ <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
+ <ds:Reference URI="#_ecfa6eaf2723f859273cba029754a33c57d6c3ed20"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>X0lSSqiuayRcxeOLi+dpwpXKLDnJWw0d3akUHWaeizU=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>iBVTipxjKTT338EnWeA9y6SOMT2VTmTpi0Mft5SQaA4bM6U3pBHJ3FYhEIvZbH4qc6jtDJVo3c0Otm2vJw6sDAslWmdNIsOePRDaMi5sgya6sqm9VXef3NIu71lbUEdhE7iwR8y9qBzd7Cm9IYxR+wvFXnIh45WFXfuHaSHcu+k9ZzpE75CvK7A2cgXfpv7sMUGG0Kz9IhkolkbuKDxkhU3sKGlDF7FByz2TJiiuyOqz6O+4rMBgmRZ94TCpkwy2X7fdz7kvWPLyfyrp24l/qHBj1ObN+dwLu8GRYPXUlsQgwMzQLMRNfDfH5PULsB7YN5swMvmCPc9bswhcsZfoQA==</ds:SignatureValue>
+<ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwEdGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLfsdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7YS+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xTL/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyhuF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0ifXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><saml:Subject><saml:NameID SPNameQualifier="http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient">_12466d6da890e3a8c5616b8b65c07eb58de7365a28</saml:NameID><saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml:SubjectConfirmationData NotOnOrAfter="2025-06-12T10:09:07Z" Recipient="http://localhost:8000/sso/saml/consume/d0a0ff3d-0754-498c-a90b-f3a20e9371f8" InResponseTo="saml_flow_r3I8eRQwA7wUXZyWjHX5jC5NdExpW2Va"/></saml:SubjectConfirmation></saml:Subject><saml:Conditions NotBefore="2025-06-12T10:03:37Z" NotOnOrAfter="2025-06-12T10:09:07Z"><saml:AudienceRestriction><saml:Audience>http://localhost:8000/sso/d0a0ff3d-0754-498c-a90b-f3a20e9371f8</saml:Audience></saml:AudienceRestriction></saml:Conditions><saml:AuthnStatement AuthnInstant="2025-06-12T10:04:07Z" SessionNotOnOrAfter="2025-06-12T18:04:07Z" SessionIndex="_6e020c7c8e9a32238e059023260a4fcc4fa4754ca8"><saml:AuthnContext><saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:Password</saml:AuthnContextClassRef></saml:AuthnContext></saml:AuthnStatement><saml:AttributeStatement><saml:Attribute Name="email" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"><saml:AttributeValue xsi:type="xs:string">[email protected]</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion></samlp:Response>
\ No newline at end of file
diff --git a/extra/fixture/authsources.php b/extra/fixture/authsources.php
new file mode 100644
index 0000000000..35d8649e1b
--- /dev/null
+++ b/extra/fixture/authsources.php
@@ -0,0 +1,25 @@
+<?php
+
+$config = [
+ 'admin' => [
+ 'core:AdminPassword',
+ ],
+ 'example-userpass' => [
+ 'exampleauth:UserPass',
+ '[email protected]:plausible' => [
+ 'email' => '[email protected]',
+ 'first_name' => 'Jane',
+ 'last_name' => 'Smith'
+ ],
+ '[email protected]:plausible' => [
+ 'email' => '[email protected]',
+ 'first_name' => 'Lenny',
+ 'last_name' => 'Carr'
+ ],
+ '[email protected]:plausible' => [
+ 'email' => '[email protected]',
+ 'first_name' => 'Jane',
+ 'last_name' => 'Doorwell'
+ ],
+ ],
+];
diff --git a/extra/lib/plausible_web/sso/real_saml_adapter.ex b/extra/lib/plausible_web/sso/real_saml_adapter.ex
new file mode 100644
index 0000000000..4579e962f9
--- /dev/null
+++ b/extra/lib/plausible_web/sso/real_saml_adapter.ex
@@ -0,0 +1,239 @@
+defmodule PlausibleWeb.SSO.RealSAMLAdapter do
+ @moduledoc """
+ Real implementation of SAML authentication interface.
+ """
+ alias Plausible.Auth.SSO
+ alias SimpleXml.XmlNode
+
+ alias PlausibleWeb.Router.Helpers, as: Routes
+
+ @deflate "urn:oasis:names:tc:SAML:2.0:bindings:URL-Encoding:DEFLATE"
+
+ @cookie_name "session_saml"
+ @cookie_seconds 10 * 60
+
+ def signin(conn, %{"integration_id" => integration_id} = params) do
+ email = params["email"]
+ return_to = params["return_to"]
+
+ case SSO.get_integration(integration_id) do
+ {:ok, integration} ->
+ sp_entity_id = SSO.SAMLConfig.entity_id(integration)
+ relay_state = gen_id()
+ id = "saml_flow_#{gen_id()}"
+
+ auth_xml = generate_auth_request(sp_entity_id, id, DateTime.utc_now())
+
+ params = %{
+ "SAMLEncoding" => @deflate,
+ "SAMLRequest" => Base.encode64(:zlib.zip(auth_xml)),
+ "RelayState" => relay_state,
+ "login_hint" => email
+ }
+
+ url = %URI{} = URI.parse(integration.config.idp_signin_url)
+
+ query_string =
+ (url.query || "")
+ |> URI.decode_query()
+ |> Map.merge(params)
+ |> URI.encode_query()
+
+ url = URI.to_string(%{url | query: query_string})
+
+ conn
+ |> Plug.Conn.configure_session(renew: true)
+ |> set_cookie(
+ relay_state: relay_state,
+ return_to: return_to
+ )
+ |> Phoenix.Controller.redirect(external: url)
+
+ {:error, :not_found} ->
+ Phoenix.Controller.redirect(conn,
+ to:
+ Routes.sso_path(conn, :login_form,
+ error: "Wrong email.",
+ return_to: return_to
+ )
+ )
+ end
+ end
+
+ def consume(conn, _params) do
+ integration_id = conn.path_params["integration_id"]
+ saml_response = conn.body_params["SAMLResponse"]
+ relay_state = conn.body_params["RelayState"] |> safe_decode_www_form()
+
+ case get_cookie(conn) do
+ {:ok, cookie} ->
+ conn
+ |> clear_cookie()
+ |> consume(integration_id, cookie, saml_response, relay_state)
+
+ {:error, :session_expired} ->
+ Phoenix.Controller.redirect(conn,
+ to: Routes.sso_path(conn, :login_form, error: "Session expired.")
+ )
+ end
+ end
+
+ @verify_opts if Mix.env() == :test, do: [skip_time_conditions?: true], else: []
+
+ defp consume(conn, integration_id, cookie, saml_response, relay_state) do
+ with {:ok, integration} <- SSO.get_integration(integration_id),
+ :ok <- validate_authresp(cookie, relay_state),
+ {:ok, {root, assertion}} <- SimpleSaml.parse_response(saml_response),
+ {:ok, cert} <- convert_pem_cert(integration.config.idp_cert_pem),
+ public_key = X509.Certificate.public_key(cert),
+ :ok <-
+ SimpleSaml.verify_and_validate_response(root, assertion, public_key, @verify_opts),
+ {:ok, attributes} <- extract_attributes(root) do
+ session_timeout_minutes = integration.team.policy.sso_session_timeout_minutes
+
+ expires_at =
+ NaiveDateTime.add(NaiveDateTime.utc_now(:second), session_timeout_minutes, :minute)
+
+ identity =
+ %SSO.Identity{
+ id: assertion.name_id,
+ name: name_from_attributes(attributes),
+ email: attributes.email,
+ expires_at: expires_at
+ }
+
+ PlausibleWeb.UserAuth.log_in_user(conn, identity, cookie.return_to)
+ else
+ {:error, reason} ->
+ Phoenix.Controller.redirect(conn,
+ to:
+ Routes.sso_path(conn, :login_form,
+ error: error_by_reason(reason),
+ return_to: cookie.return_to
+ )
+ )
+ end
+ end
+
+ defp error_by_reason(:not_found), do: "Wrong email."
+ defp error_by_reason(reason), do: "Authentication failed (reason: #{inspect(reason)})."
+
+ defp convert_pem_cert(cert) do
+ case X509.Certificate.from_pem(cert) do
+ {:ok, cert} -> {:ok, cert}
+ {:error, _} -> {:error, :malformed_certificate}
+ end
+ end
+
+ defp name_from_attributes(attributes) do
+ [attributes.first_name, attributes.last_name]
+ |> Enum.reject(&is_nil/1)
+ |> Enum.join(" ")
+ |> String.trim()
+ end
+
+ defp extract_attributes(root_node) do
+ with {:ok, assertion_node} <- XmlNode.first_child(root_node, ~r/.*:?Assertion$/),
+ {:ok, attributes_node} <-
+ XmlNode.first_child(assertion_node, ~r/.*:?AttributeStatement$/),
+ {:ok, attribute_nodes} <- XmlNode.children(attributes_node) do
+ found = get_attributes(attribute_nodes)
+
+ attributes = %{
+ email: String.trim(found["email"] || ""),
+ first_name: String.trim(found["first_name"] || ""),
+ last_name: String.trim(found["last_name"] || "")
+ }
+
+ cond do
+ attributes.email == "" ->
+ {:error, :missing_email_attribute}
+
+ # very rudimentary way to check if the attribute is at least email-like
+ not String.contains?(attributes.email, "@") or String.length(attributes.email) < 3 ->
+ {:error, :invalid_email_attribute}
+
+ attributes.first_name == "" and attributes.last_name == "" ->
+ {:error, :missing_name_attributes}
+
+ true ->
+ {:ok, attributes}
+ end
+ end
+ end
+
+ defp get_attributes(nodes) do
+ Enum.reduce(nodes, %{}, fn node, attributes ->
+ with {:ok, name} <- XmlNode.attribute(node, "Name"),
+ {:ok, value_node} <- XmlNode.first_child(node, ~r/.*:?AttributeValue$/),
+ {:ok, value} <- XmlNode.text(value_node) do
+ Map.put(attributes, name, value)
+ else
+ _ ->
+ attributes
+ end
+ end)
+ end
+
+ defp safe_decode_www_form(nil), do: ""
+ defp safe_decode_www_form(data), do: URI.decode_www_form(data)
+
+ defp generate_auth_request(issuer_id, id, timestamp) do
+ XmlBuilder.generate(
+ {:"samlp:AuthnRequest",
+ [
+ "xmlns:samlp": "urn:oasis:names:tc:SAML:2.0:protocol",
+ ID: id,
+ Version: "2.0",
+ IssueInstant: DateTime.to_iso8601(timestamp)
+ ], [{:"saml:Issuer", ["xmlns:saml": "urn:oasis:names:tc:SAML:2.0:assertion"], issuer_id}]}
+ )
+ end
+
+ defp validate_authresp(%{relay_state: relay_state}, relay_state)
+ when byte_size(relay_state) == 32 do
+ :ok
+ end
+
+ defp validate_authresp(_, _), do: {:error, :invalid_relay_state}
+
+ defp gen_id() do
+ 24 |> :crypto.strong_rand_bytes() |> Base.url_encode64()
+ end
+
+ @doc false
+ def set_cookie(conn, attrs) do
+ attrs = %{
+ relay_state: Keyword.fetch!(attrs, :relay_state),
+ return_to: Keyword.fetch!(attrs, :return_to)
+ }
+
+ Plug.Conn.put_resp_cookie(conn, @cookie_name, attrs,
+ domain: conn.private.phoenix_endpoint.host(),
+ secure: true,
+ encrypt: true,
+ max_age: @cookie_seconds,
+ same_site: "None"
+ )
+ end
+
+ defp get_cookie(conn) do
+ conn = Plug.Conn.fetch_cookies(conn, encrypted: [@cookie_name])
+
+ if cookie = conn.cookies[@cookie_name] do
+ {:ok, cookie}
+ else
+ {:error, :session_expired}
+ end
+ end
+
+ defp clear_cookie(conn) do
+ Plug.Conn.delete_resp_cookie(conn, @cookie_name,
+ domain: conn.private.phoenix_endpoint.host(),
+ secure: true,
+ encrypt: true,
+ max_age: @cookie_seconds,
+ same_site: "None"
+ )
+ end
+end
diff --git a/mix.exs b/mix.exs
index 9cc65eba7f..d6e8527a09 100644
--- a/mix.exs
+++ b/mix.exs
@@ -122,6 +122,8 @@ defmodule Plausible.MixProject do
{:ref_inspector, "~> 2.0"},
{:referrer_blocklist, git: "https://github.com/plausible/referrer-blocklist.git"},
{:sentry, "~> 10.8.1"},
+ {:simple_saml, git: "https://github.com/MBXSystems/simple_saml.git", branch: "main"},
+ {:xml_builder, "~> 2.1"},
{:siphash, "~> 3.2"},
{:timex, "~> 3.7"},
{:ua_inspector,
diff --git a/mix.lock b/mix.lock
index 5802b44b36..8ec0f17f2d 100644
--- a/mix.lock
+++ b/mix.lock
@@ -139,9 +139,12 @@
"referrer_blocklist": {:git, "https://github.com/plausible/referrer-blocklist.git", "d6f52c225cccb4f04b80e3a5d588868ec234139d", []},
"req": {:hex, :req, "0.5.6", "8fe1eead4a085510fe3d51ad854ca8f20a622aae46e97b302f499dfb84f726ac", [:mix], [{:brotli, "~> 0.3.1", [hex: :brotli, repo: "hexpm", optional: true]}, {:ezstd, "~> 1.0", [hex: :ezstd, repo: "hexpm", optional: true]}, {:finch, "~> 0.17", [hex: :finch, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mime, "~> 2.0.6 or ~> 2.1", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_csv, "~> 1.0", [hex: :nimble_csv, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "cfaa8e720945d46654853de39d368f40362c2641c4b2153c886418914b372185"},
"rustler_precompiled": {:hex, :rustler_precompiled, "0.7.3", "42cb9449785cd86c87453e39afdd27a0bdfa5c77a4ec5dc5ce45112e06b9f89b", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: false]}, {:rustler, "~> 0.23", [hex: :rustler, repo: "hexpm", optional: true]}], "hexpm", "cbc4b3777682e5f6f43ed39b0e0b4a42dccde8053aba91b4514e8f5ff9a5ac6d"},
+ "saxy": {:hex, :saxy, "1.5.0", "0141127f2d042856f135fb2d94e0beecda7a2306f47546dbc6411fc5b07e28bf", [:mix], [], "hexpm", "ea7bb6328fbd1f2aceffa3ec6090bfb18c85aadf0f8e5030905e84235861cf89"},
"scrivener": {:hex, :scrivener, "2.7.2", "1d913c965ec352650a7f864ad7fd8d80462f76a32f33d57d1e48bc5e9d40aba2", [:mix], [], "hexpm", "7866a0ec4d40274efbee1db8bead13a995ea4926ecd8203345af8f90d2b620d9"},
"scrivener_ecto": {:hex, :scrivener_ecto, "2.7.0", "cf64b8cb8a96cd131cdbcecf64e7fd395e21aaa1cb0236c42a7c2e34b0dca580", [:mix], [{:ecto, "~> 3.3", [hex: :ecto, repo: "hexpm", optional: false]}, {:scrivener, "~> 2.4", [hex: :scrivener, repo: "hexpm", optional: false]}], "hexpm", "e809f171687806b0031129034352f5ae44849720c48dd839200adeaf0ac3e260"},
"sentry": {:hex, :sentry, "10.8.1", "aa45309785e1521416225adb16e0b4d8b957578804527f3c7babb6fefbc5e456", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:nimble_options, "~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_ownership, "~> 0.3.0 or ~> 1.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.6", [hex: :phoenix, repo: "hexpm", optional: true]}, {:phoenix_live_view, "~> 0.20 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.6", [hex: :plug, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "495b3cdadad90ba72eef973aa3dec39b3b8b2a362fe87e2f4ef32133ac3b4097"},
+ "simple_saml": {:git, "https://github.com/MBXSystems/simple_saml.git", "d161dbc6cc059bddccdc986a0e1b29823e4348fd", [branch: "main"]},
+ "simple_xml": {:hex, :simple_xml, "1.3.1", "66813a84f650c4ac041ac9b0497d35448cadc5826a6d71a4dd2a33f015f48197", [:mix], [{:saxy, "~> 1.5", [hex: :saxy, repo: "hexpm", optional: false]}, {:x509, "~> 0.8.7", [hex: :x509, repo: "hexpm", optional: false]}], "hexpm", "67dc14276a7f71bdb868f345e0d63e52db49da54521cb43eb2c7aac67ef7147d"},
"siphash": {:hex, :siphash, "3.2.0", "ec03fd4066259218c85e2a4b8eec4bb9663bc02b127ea8a0836db376ba73f2ed", [:make, :mix], [], "hexpm", "ba3810701c6e95637a745e186e8a4899087c3b079ba88fb8f33df054c3b0b7c3"},
"site_encrypt": {:git, "https://github.com/sasa1977/site_encrypt.git", "046fbeca11b889604dafd2df6a71001f8abe5e2c", []},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
@@ -159,6 +162,7 @@
"websock": {:hex, :websock, "0.5.3", "2f69a6ebe810328555b6fe5c831a851f485e303a7c8ce6c5f675abeb20ebdadc", [:mix], [], "hexpm", "6105453d7fac22c712ad66fab1d45abdf049868f253cf719b625151460b8b453"},
"websock_adapter": {:hex, :websock_adapter, "0.5.8", "3b97dc94e407e2d1fc666b2fb9acf6be81a1798a2602294aac000260a7c4a47d", [:mix], [{:bandit, ">= 0.6.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.6", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:websock, "~> 0.5", [hex: :websock, repo: "hexpm", optional: false]}], "hexpm", "315b9a1865552212b5f35140ad194e67ce31af45bcee443d4ecb96b5fd3f3782"},
"x509": {:hex, :x509, "0.8.10", "5d1ec6d5f4db31982f9dc34e6a1eebd631d04599e0b6c1c259f1dadd4495e11f", [:mix], [], "hexpm", "a191221665af28b9bdfff0c986ef55f80e126d8ce751bbdf6cefa846410140c0"},
+ "xml_builder": {:hex, :xml_builder, "2.4.0", "b20d23077266c81f593360dc037ea398461dddb6638a329743da6c73afa56725", [:mix], [], "hexpm", "833e325bb997f032b5a1b740d2fd6feed3c18ca74627f9f5f30513a9ae1a232d"},
"yamerl": {:hex, :yamerl, "0.10.0", "4ff81fee2f1f6a46f1700c0d880b24d193ddb74bd14ef42cb0bcf46e81ef2f8e", [:rebar3], [], "hexpm", "346adb2963f1051dc837a2364e4acf6eb7d80097c0f53cbdc3046ec8ec4b4e6e"},
"zstream": {:hex, :zstream, "0.6.5", "ad4c5631f0da986ddcf8b23d0d2c696c09cedb1c3ace027fb2312a809df6f8b8", [:mix], [], "hexpm", "de908d8a050f6aabf5f4931aa41dab49419e3c8b95cfee01461a8c84fdde005e"},
"zxcvbn": {:git, "https://github.com/techgaun/zxcvbn-elixir.git", "5c9684590f4ecf00af2d391f90db9ce93bd4e996", []},
diff --git a/test/plausible_web/controllers/sso_controller_sync_test.exs b/test/plausible_web/controllers/sso_controller_sync_test.exs
index 9132f4a64e..3c690ed38d 100644
--- a/test/plausible_web/controllers/sso_controller_sync_test.exs
+++ b/test/plausible_web/controllers/sso_controller_sync_test.exs
@@ -1,11 +1,15 @@
defmodule PlausibleWeb.SSOControllerSyncTest do
use PlausibleWeb.ConnCase
- use Plausible.Teams.Test
-
@moduletag :ee_only
on_ee do
+ use Plausible.Teams.Test
+
+ alias Plausible.Auth
+ alias Plausible.Auth.SSO
+ alias Plausible.Repo
+
describe "sso_enabled = false" do
setup do
patch_env(:sso_enabled, false)
@@ -87,5 +91,422 @@ defmodule PlausibleWeb.SSOControllerSyncTest do
assert redirected_to(conn, 302) == "/"
end
end
+
+ @cert_pem """
+ -----BEGIN CERTIFICATE-----
+ MIICmjCCAYICCQDX5sKPsYV3+jANBgkqhkiG9w0BAQsFADAPMQ0wCwYDVQQDDAR0
+ ZXN0MB4XDTE5MTIyMzA5MDI1MVoXDTIwMDEyMjA5MDI1MVowDzENMAsGA1UEAwwE
+ dGVzdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMdtDJ278DQTp84O
+ 5Nq5F8s5YOR34GFOGI2Swb/3pU7X7918lVljiKv7WVM65S59nJSyXV+fa15qoXLf
+ sdRnq3yw0hTSTs2YDX+jl98kK3ksk3rROfYh1LIgByj4/4NeNpExgeB6rQk5Ay7Y
+ S+ARmMzEjXa0favHxu5BOdB2y6WvRQyjPS2lirT/PKWBZc04QZepsZ56+W7bd557
+ tdedcYdY/nKI1qmSQClG2qgslzgqFOv1KCOw43a3mcK/TiiD8IXyLMJNC6OFW3xT
+ L/BG6SOZ3dQ9rjQOBga+6GIaQsDjC4Xp7Kx+FkSvgaw0sJV8gt1mlZy+27Sza6d+
+ hHD2pWECAwEAATANBgkqhkiG9w0BAQsFAAOCAQEAm2fk1+gd08FQxK7TL04O8EK1
+ f0bzaGGUxWzlh98a3Dm8+OPhVQRi/KLsFHliLC86lsZQKunYdDB+qd0KUk2oqDG6
+ tstG/htmRYD/S/jNmt8gyPAVi11dHUqW3IvQgJLwxZtoAv6PNs188hvT1WK3VWJ4
+ YgFKYi5XQYnR5sv69Vsr91lYAxyrIlMKahjSW1jTD3ByRfAQghsSLk6fV0OyJHyh
+ uF1TxOVBVf8XOdaqfmvD90JGIPGtfMLPUX4m35qaGAU48PwCL7L3cRHYs9wZWc0i
+ fXZcBENLtHYCLi5txR8c5lyHB9d3AQHzKHMFNjLswn5HsckKg83RH7+eVqHqGw==
+ -----END CERTIFICATE-----
+ """
+
+ @other_cert_pem """
+ -----BEGIN CERTIFICATE-----
+ MIIFdTCCA12gAwIBAgIUNcATm3CidmlEMMsZa9KBZpWYCVcwDQYJKoZIhvcNAQEL
+ BQAwYzELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
+ GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEcMBoGA1UEAwwTc29tZWlkcC5leGFt
+ cGxlLmNvbTAeFw0yNTA1MjExMjI5MzVaFw0yNjA1MjExMjI5MzVaMGMxCzAJBgNV
+ BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
+ aWRnaXRzIFB0eSBMdGQxHDAaBgNVBAMME3NvbWVpZHAuZXhhbXBsZS5jb20wggIi
+ MA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQC1N6Drbjed+lFXpOYvE6Efgndy
+ W7kYiO8LqQTr4UwVrp9ArxgYuK4TrcNRh2rhS08xAzNTo+NqnJOm95baG97ADYk1
+ TqVIKxzaFurv+L/Na0wVXyeNUtxIVKF59uElsg2YLm5eQhL9fmN8jVINCvwDPzxc
+ Ihm6mQOaL/i/0DGINOqwHG9MGMZ11AeOM0wKMuXJ2+aKjHOCedhMYVuOaHZgLkcX
+ Zzgiv7itm3+JpCjL474MMfibiqKHR0e3QRNcsEC13f/LD8BAGOwsKLznFC8Uctms
+ 48EDNbxxLG01jVbnJSxRrcDN3RUDjtCdHyaTCCFJAgmldHKKua3VQEynOwJIkFMC
+ fpL1LpLvATzIt0cT1ESb1RHIlgacmESVn/TW2QjO5tp4FAu7GJK+5xY7jPvI6saG
+ oUHsk0zo9obLK8WYneF19ln+Ea5ZCl9PcTi559AKGpYzpL/9uxoPT1zxxTn6c2lt
+ 4xkxkuHtYqi/ENHGdo4CLBL93GDZEilSVmZjD/9N9990yWbPXXQ0eNoFckYSZuls
+ HaWz8W5c046/ob8mASI6wzAUCkO9Zz4WbIj9A+mNZB32hMZbMA02gU//ffvNkFjL
+ DGlNbROCg2DX64rvGs/RuqhuDVCnVfid9B36Cgs76GWI8dCInEfyZMtiqUb7E8Oe
+ BPVwtTscz1StlF/0cQIDAQABoyEwHzAdBgNVHQ4EFgQU9lvXH4X04v99rrwKNzsw
+ pNQP/dUwDQYJKoZIhvcNAQELBQADggIBAJD0MD+OK58vlP2HEoKLQYAKYM/4NsBz
+ vSK1PtZsEj0fqiuu66ceH0wlKlGquRad4Z+LXMptu1DzMNmJsf0zSQfleGFks3xI
+ 86hgkQ7f0qjs+YJzjJUxF9H8zX4jJk5poOqOJwStHBCDLsUmIxnT7/il3jlT0Nj4
+ cVs4946pCg7rP1kR9jojFD5yvzKoRBJG3/qvFnzAi8cDv9CRjSgoDTZyzZmwdCgu
+ NioW7YeFCtvYxvY7HDXinwq/w8Gn3n8zdISoAqSpYrt5Y5ygJGiEYVDWdA50a6PC
+ gq5xt8RCizz1L7a5BUJFMCQ0pyAUuODTndPUGLT8i7jFgzhamFPD72zFMk2+IabE
+ Dutyt2GFeTQ75wL8QvfsKm29Vd5EjAsdfmup3hCpLGqF3g8Sh0aXDrj8KPqIecuS
+ gkL69M9iXfnwZhTo23zUuFjBNoAIPXkNKXiJS7p9IEpYRVnlPYLToSEnnzptoPPQ
+ zMBb8x/UMMtNYkyehSLhuIPrRLvv3eth7Hq3hA7tOCRyyf78tReVm+VoRx6AK68v
+ 5ufxMKBFRTNoLIN3sD+DmSUNY+CaHxRMDhSESy0Ac/95J2yKi+Y1Kml2GV53pSlT
+ 6FPm8B0R9YXM7lHhTLyL7DYqnvklkLh2bUqCLyBowynPyGqdYV4DbFSiST14fGXR
+ mNEYF78kg0IA
+ -----END CERTIFICATE-----
+ """
+
+ describe "saml_signin/2 (real SAML)" do
+ setup do
+ patch_env(:sso_saml_adapter, PlausibleWeb.SSO.RealSAMLAdapter)
+
+ team = new_site().team |> Plausible.Teams.complete_setup()
+ integration = SSO.initiate_saml_integration(team)
+
+ {:ok, integration} =
+ SSO.update_integration(integration, %{
+ idp_signin_url: "http://localhost:8080/simplesaml/saml2/idp/SSOService.php",
+ idp_entity_id: "http://localhost:8080/simplesaml/saml2/idp/metadata.php",
+ idp_cert_pem: @cert_pem
+ })
+
+ domain = "example-#{Enum.random(1..10_000)}.com"
+
+ {:ok, sso_domain} = SSO.Domains.add(integration, domain)
+ sso_domain = SSO.Domains.verify(sso_domain, skip_checks?: true)
+
+ {:ok, team: team, integration: integration, domain: domain, sso_domain: sso_domain}
+ end
+
+ test "redirects to IdP", %{conn: conn, domain: domain, integration: integration} do
+ email = "paul@" <> domain
+
+ conn =
+ get(
+ conn,
+ Routes.sso_path(conn, :saml_signin, integration.identifier,
+ email: email,
+ return_to: "/sites"
+ )
+ )
+
+ session = fetch_cookies(conn, encrypted: ["session_saml"]).cookies["session_saml"]
+
+ assert String.length(session.relay_state) > 0
+ assert session.return_to == "/sites"
+
+ assert url = redirected_to(conn, 302)
+
+ assert {:ok, uri} = URI.new(url)
+
+ assert uri.host == "localhost"
+ assert uri.port == 8080
+ assert uri.scheme == "http"
+
+ assert %{
+ "RelayState" => relay_state,
+ "SAMLEncoding" => "urn:oasis:names:tc:SAML:2.0:bindings:URL-Encoding:DEFLATE",
+ "SAMLRequest" => saml_request,
+ "login_hint" => ^email
+ } = URI.decode_query(uri.query)
+
+ assert relay_state == session.relay_state
+
+ xml = saml_request |> Base.decode64!() |> :zlib.unzip()
+
+ assert {:ok, root_node} = SimpleXml.parse(xml)
+ assert {:ok, issuer_node} = SimpleXml.XmlNode.first_child(root_node, "saml:Issuer")
+ assert {:ok, acs_url} = SimpleXml.XmlNode.text(issuer_node)
+
+ assert acs_url == SSO.SAMLConfig.entity_id(integration)
+ end
+
+ test "redirects to login if integration not found", %{conn: conn, domain: domain} do
+ email = "paul@" <> domain
+
+ conn =
+ get(
+ conn,
+ Routes.sso_path(conn, :saml_signin, Ecto.UUID.generate(),
+ email: email,
+ return_to: "/sites"
+ )
+ )
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form, error: "Wrong email.", return_to: "/sites")
+ end
+ end
+
+ @assertion File.read!("extra/fixture/assertion.xml")
+ @assertion_missing_email File.read!("extra/fixture/assertion_missing_email.xml")
+ @assertion_invalid_email File.read!("extra/fixture/assertion_invalid_email.xml")
+ @assertion_missing_name File.read!("extra/fixture/assertion_missing_name.xml")
+
+ describe "saml_consume/2 (real SAML)" do
+ setup %{conn: conn} do
+ patch_env(:sso_saml_adapter, PlausibleWeb.SSO.RealSAMLAdapter)
+
+ team = new_site().team |> Plausible.Teams.complete_setup()
+ integration = SSO.initiate_saml_integration(team)
+
+ {:ok, integration} =
+ SSO.update_integration(integration, %{
+ idp_signin_url: "http://localhost:8080/simplesaml/saml2/idp/SSOService.php",
+ idp_entity_id: "http://localhost:8080/simplesaml/saml2/idp/metadata.php",
+ idp_cert_pem: @cert_pem
+ })
+
+ domain = "plausible.test"
+
+ {:ok, sso_domain} = SSO.Domains.add(integration, domain)
+ sso_domain = SSO.Domains.verify(sso_domain, skip_checks?: true)
+
+ {:ok, root_node} = @assertion |> SimpleXml.parse()
+ {:ok, assertion_node} = SimpleXml.XmlNode.first_child(root_node, "*:Assertion")
+ {:ok, subject_node} = SimpleXml.XmlNode.first_child(assertion_node, "*:Subject")
+ {:ok, name_id_node} = SimpleXml.XmlNode.first_child(subject_node, "*:NameID")
+ {:ok, name_id} = SimpleXml.XmlNode.text(name_id_node)
+
+ email = "[email protected]"
+
+ conn =
+ get(
+ conn,
+ Routes.sso_path(conn, :saml_signin, integration.identifier,
+ email: email,
+ return_to: "/sites"
+ )
+ )
+
+ saml_session = fetch_cookies(conn, encrypted: ["session_saml"]).cookies["session_saml"]
+ relay_state = saml_session.relay_state
+
+ conn =
+ conn
+ |> recycle()
+ |> Map.put(:secret_key_base, secret_key_base())
+
+ {:ok,
+ team: team,
+ integration: integration,
+ domain: domain,
+ sso_domain: sso_domain,
+ email: email,
+ conn: conn,
+ name_id: name_id,
+ relay_state: relay_state}
+ end
+
+ test "provisions identity and logs in", %{
+ conn: conn,
+ team: team,
+ integration: integration,
+ sso_domain: sso_domain,
+ relay_state: relay_state,
+ name_id: name_id
+ } do
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) == "/sites"
+
+ session = get_session(conn)
+
+ assert session["current_team_id"] == team.identifier
+
+ assert {:ok, user_session} = Auth.UserSessions.get_by_token(session["user_token"])
+
+ assert user_session.user.email == "[email protected]"
+ assert user_session.user.type == :sso
+ assert user_session.user.name == "Jane Smith"
+ assert user_session.user.sso_integration_id == integration.id
+ assert user_session.user.sso_domain_id == sso_domain.id
+ assert user_session.user.sso_identity_id == name_id
+
+ timeout_minutes = team.policy.sso_session_timeout_minutes
+
+ timeout_threshold_lower =
+ NaiveDateTime.add(NaiveDateTime.utc_now(:second), timeout_minutes - 2, :minute)
+
+ timeout_threshold_upper =
+ NaiveDateTime.add(NaiveDateTime.utc_now(:second), timeout_minutes + 2, :minute)
+
+ assert NaiveDateTime.compare(user_session.timeout_at, timeout_threshold_lower) == :gt
+ assert NaiveDateTime.compare(user_session.timeout_at, timeout_threshold_upper) == :lt
+ end
+
+ test "redirects with error when no matching integration found", %{
+ conn: conn,
+ relay_state: relay_state
+ } do
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, Ecto.UUID.generate()), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form, error: "Wrong email.", return_to: "/sites")
+ end
+
+ test "redirects with error on mismatch of RelayState", %{
+ conn: conn,
+ integration: integration
+ } do
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion),
+ "RelayState" => Ecto.UUID.generate()
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :invalid_relay_state).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on missing relay state", %{
+ conn: conn,
+ integration: integration
+ } do
+ params = %{"SAMLResponse" => Base.encode64(@assertion)}
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :invalid_relay_state).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on malformed assertion", %{
+ conn: conn,
+ integration: integration,
+ relay_state: relay_state
+ } do
+ params = %{
+ "SAMLResponse" => "malformed",
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :base64_decoding_failed).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on malformed certificate in config (should not happen)", %{
+ conn: conn,
+ integration: integration,
+ relay_state: relay_state
+ } do
+ Repo.query!(
+ """
+ UPDATE sso_integrations SET config = jsonb_set(config, '{idp_cert_pem}', '"invalid"')
+ WHERE id = $1
+ """,
+ [integration.id]
+ )
+
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :malformed_certificate).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on mismatched certificate in config", %{
+ conn: conn,
+ integration: integration,
+ relay_state: relay_state
+ } do
+ {:ok, integration} = SSO.update_integration(integration, %{idp_cert_pem: @other_cert_pem})
+
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :digest_verification_failed).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on missing email attribute in assertion", %{
+ conn: conn,
+ integration: integration,
+ relay_state: relay_state
+ } do
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion_missing_email),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :missing_email_attribute).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on invalid email attribute in assertion", %{
+ conn: conn,
+ integration: integration,
+ relay_state: relay_state
+ } do
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion_invalid_email),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :invalid_email_attribute).",
+ return_to: "/sites"
+ )
+ end
+
+ test "redirects with error on missing name attributes in assertion", %{
+ conn: conn,
+ integration: integration,
+ relay_state: relay_state
+ } do
+ params = %{
+ "SAMLResponse" => Base.encode64(@assertion_missing_name),
+ "RelayState" => relay_state
+ }
+
+ conn = post(conn, Routes.sso_path(conn, :saml_consume, integration.identifier), params)
+
+ assert redirected_to(conn, 302) ==
+ Routes.sso_path(conn, :login_form,
+ error: "Authentication failed (reason: :missing_name_attributes).",
+ return_to: "/sites"
+ )
+ end
+ end
+
+ defp secret_key_base() do
+ :plausible
+ |> Application.fetch_env!(PlausibleWeb.Endpoint)
+ |> Keyword.fetch!(:secret_key_base)
+ end
end
end