| 1 |
import base64 |
| 2 |
from unittest.mock import patch |
| 3 |
|
| 4 |
from django.test.client import Client |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture |
| 10 |
def client(): |
| 11 |
client = Client(HTTP_HOST="example.com") |
| 12 |
return client |
| 13 |
|
| 14 |
|
| 15 |
@pytest.fixture |
| 16 |
def saml_settings(settings): |
| 17 |
settings.SOCIALACCOUNT_PROVIDERS = { |
| 18 |
"saml": { |
| 19 |
"APPS": [ |
| 20 |
{ |
| 21 |
"client_id": "org", |
| 22 |
"provider_id": "urn:dev-123.us.auth0.com", |
| 23 |
"settings": { |
| 24 |
"attribute_mapping": { |
| 25 |
"uid": "http://schemas.auth0.com/clientID", |
| 26 |
"email_verified": "http://schemas.auth0.com/email_verified", |
| 27 |
"email": "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress", |
| 28 |
}, |
| 29 |
"idp": { |
| 30 |
"name": "Test IdP", |
| 31 |
"entity_id": "urn:dev-123.us.auth0.com", |
| 32 |
"sso_url": "https://dev-123.us.auth0.com/samlp/456", |
| 33 |
"slo_url": "https://dev-123.us.auth0.com/samlp/456", |
| 34 |
"x509cert": "", |
| 35 |
}, |
| 36 |
"advanced": { |
| 37 |
"strict": False, |
| 38 |
}, |
| 39 |
}, |
| 40 |
} |
| 41 |
] |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture |
| 47 |
def acs_saml_response_factory(): |
| 48 |
def factory(in_response_to=None): |
| 49 |
xml = f"""<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" ID="id123" InResponseTo="{in_response_to or ''}" Version="2.0" IssueInstant="2023-07-08T08:24:14.141Z" Destination="http://example.com/accounts/saml/org/acs/"> |
| 50 |
<saml:Issuer xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">urn:dev-123.us.auth0.com</saml:Issuer> |
| 51 |
<samlp:Status> |
| 52 |
<samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/> |
| 53 |
</samlp:Status> |
| 54 |
<saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0" ID="id321" IssueInstant="2023-07-08T08:24:14.094Z"> |
| 55 |
<saml:Issuer>urn:dev-123.us.auth0.com</saml:Issuer> |
| 56 |
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> |
| 57 |
<SignedInfo> |
| 58 |
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> |
| 59 |
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> |
| 60 |
<Reference URI="#id321"> |
| 61 |
<Transforms> |
| 62 |
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/> |
| 63 |
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> |
| 64 |
</Transforms> |
| 65 |
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/> |
| 66 |
<DigestValue>A321 |
| 67 |
</DigestValue> |
| 68 |
</Reference> |
| 69 |
</SignedInfo> |
| 70 |
<SignatureValue>MTIz |
| 71 |
</SignatureValue> |
| 72 |
<KeyInfo> |
| 73 |
<X509Data> |
| 74 |
<X509Certificate>MIIDHTCC... |
| 75 |
</X509Certificate> |
| 76 |
</X509Data> |
| 77 |
</KeyInfo> |
| 78 |
</Signature> |
| 79 |
<saml:Subject> |
| 80 |
<saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified">google-oauth2|108204123456789 |
| 81 |
</saml:NameID> |
| 82 |
<saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"> |
| 83 |
<saml:SubjectConfirmationData NotOnOrAfter="2123-07-08T09:24:14.094Z" Recipient="http://example.com/accounts/saml/org/acs/" InResponseTo="id321"/> |
| 84 |
</saml:SubjectConfirmation> |
| 85 |
</saml:Subject> |
| 86 |
<saml:Conditions NotBefore="2023-07-08T08:24:14.094Z" NotOnOrAfter="2123-07-08T09:24:14.094Z"> |
| 87 |
<saml:AudienceRestriction> |
| 88 |
<saml:Audience>http://example.com/accounts/saml/org/metadata/</saml:Audience> |
| 89 |
</saml:AudienceRestriction> |
| 90 |
</saml:Conditions> |
| 91 |
<saml:AuthnStatement AuthnInstant="2023-07-08T08:24:14.094Z" SessionIndex="_qPrYdL0O8w3vdb8eCEY5ZtHe76LA8-JU"> |
| 92 |
<saml:AuthnContext> |
| 93 |
<saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified |
| 94 |
</saml:AuthnContextClassRef> |
| 95 |
</saml:AuthnContext> |
| 96 |
</saml:AuthnStatement> |
| 97 |
<saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> |
| 98 |
<saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 99 |
<saml:AttributeValue xsi:type="xs:string">google-oauth2|108204123456789 |
| 100 |
</saml:AttributeValue> |
| 101 |
</saml:Attribute> |
| 102 |
<saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 103 |
<saml:AttributeValue xsi:type="xs:string">[email protected] |
| 104 |
</saml:AttributeValue> |
| 105 |
</saml:Attribute> |
| 106 |
<saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 107 |
<saml:AttributeValue xsi:type="xs:string">John |
| 108 |
</saml:AttributeValue> |
| 109 |
</saml:Attribute> |
| 110 |
<saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 111 |
<saml:AttributeValue xsi:type="xs:string">John |
| 112 |
</saml:AttributeValue> |
| 113 |
</saml:Attribute> |
| 114 |
<saml:Attribute Name="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/upn" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 115 |
<saml:AttributeValue xsi:type="xs:string">[email protected] |
| 116 |
</saml:AttributeValue> |
| 117 |
</saml:Attribute> |
| 118 |
<saml:Attribute Name="http://schemas.auth0.com/identities/default/provider" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 119 |
<saml:AttributeValue xsi:type="xs:string">google-oauth2 |
| 120 |
</saml:AttributeValue> |
| 121 |
</saml:Attribute> |
| 122 |
<saml:Attribute Name="http://schemas.auth0.com/identities/default/connection" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 123 |
<saml:AttributeValue xsi:type="xs:string">google-oauth2 |
| 124 |
</saml:AttributeValue> |
| 125 |
</saml:Attribute> |
| 126 |
<saml:Attribute Name="http://schemas.auth0.com/identities/default/isSocial" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 127 |
<saml:AttributeValue xsi:type="xs:boolean">true |
| 128 |
</saml:AttributeValue> |
| 129 |
</saml:Attribute> |
| 130 |
<saml:Attribute Name="http://schemas.auth0.com/clientID" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 131 |
<saml:AttributeValue xsi:type="xs:string">dummysamluid |
| 132 |
</saml:AttributeValue> |
| 133 |
</saml:Attribute> |
| 134 |
<saml:Attribute Name="http://schemas.auth0.com/created_at" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 135 |
<saml:AttributeValue xsi:type="xs:anyType">Wed Jun 28 2023 17:53:49 GMT+0000 (Coordinated Universal Time) |
| 136 |
</saml:AttributeValue> |
| 137 |
</saml:Attribute> |
| 138 |
<saml:Attribute Name="http://schemas.auth0.com/email_verified" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 139 |
<saml:AttributeValue xsi:type="xs:boolean">true |
| 140 |
</saml:AttributeValue> |
| 141 |
</saml:Attribute> |
| 142 |
<saml:Attribute Name="http://schemas.auth0.com/locale" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 143 |
<saml:AttributeValue xsi:type="xs:string">en |
| 144 |
</saml:AttributeValue> |
| 145 |
</saml:Attribute> |
| 146 |
<saml:Attribute Name="http://schemas.auth0.com/nickname" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 147 |
<saml:AttributeValue xsi:type="xs:string">john.doe |
| 148 |
</saml:AttributeValue> |
| 149 |
</saml:Attribute> |
| 150 |
<saml:Attribute Name="http://schemas.auth0.com/picture" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 151 |
<saml:AttributeValue xsi:type="xs:string">https://lh3.googleusercontent.com/a/AAcHTtfZ0fEyL3BKP1Hk2v1bNwpJd6ckIeo6jSExlkVjMXaIpsY=s96-c |
| 152 |
</saml:AttributeValue> |
| 153 |
</saml:Attribute> |
| 154 |
<saml:Attribute Name="http://schemas.auth0.com/updated_at" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:uri"> |
| 155 |
<saml:AttributeValue xsi:type="xs:anyType">Sat Jul 08 2023 06:13:07 GMT+0000 (Coordinated Universal Time) |
| 156 |
</saml:AttributeValue> |
| 157 |
</saml:Attribute> |
| 158 |
<saml:Attribute Name="Role" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> |
| 159 |
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">view-profile</saml:AttributeValue> |
| 160 |
</saml:Attribute> |
| 161 |
<saml:Attribute Name="Role" NameFormat="urn:oasis:names:tc:SAML:2.0:attrname-format:basic"> |
| 162 |
<saml:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xs:string">manage-account-links</saml:AttributeValue> |
| 163 |
</saml:Attribute> |
| 164 |
</saml:AttributeStatement> |
| 165 |
</saml:Assertion> |
| 166 |
</samlp:Response> |
| 167 |
""" |
| 168 |
return base64.b64encode(xml.encode("utf8")).decode("utf8") |
| 169 |
|
| 170 |
return factory |
| 171 |
|
| 172 |
|
| 173 |
@pytest.fixture |
| 174 |
def sls_saml_request(): |
| 175 |
xml = "<dummy></dummy>" |
| 176 |
return base64.b64encode(xml.encode("utf8")).decode("utf8") |
| 177 |
|
| 178 |
|
| 179 |
@pytest.fixture |
| 180 |
def mocked_signature_validation(): |
| 181 |
with patch("onelogin.saml2.utils.OneLogin_Saml2_Utils.validate_sign") as mock: |
| 182 |
mock.return_value = True |
| 183 |
yield |