|
| 1 | +from __future__ import absolute_import |
| 2 | + |
| 3 | +import pytest |
| 4 | +import base64 |
| 5 | +import mock |
| 6 | +from exam import fixture |
| 7 | +from six.moves.urllib.parse import urlencode, urlparse, parse_qs |
| 8 | + |
| 9 | +from django.conf import settings |
| 10 | +from django.core.urlresolvers import reverse |
| 11 | + |
| 12 | +from sentry.auth.providers.saml2 import SAML2Provider, Attributes, HAS_SAML2 |
| 13 | +from sentry.models import AuthProvider |
| 14 | +from sentry.testutils import AuthProviderTestCase |
| 15 | + |
| 16 | + |
| 17 | +dummy_provider_config = { |
| 18 | + 'idp': { |
| 19 | + 'entity_id': 'https://example.com/saml/metadata/1234', |
| 20 | + 'x509cert': 'foo_x509_cert', |
| 21 | + 'sso_url': 'http://example.com/sso_url', |
| 22 | + 'slo_url': 'http://example.com/slo_url', |
| 23 | + }, |
| 24 | + 'attribute_mapping': { |
| 25 | + Attributes.IDENTIFIER: 'user_id', |
| 26 | + Attributes.USER_EMAIL: 'email', |
| 27 | + Attributes.FIRST_NAME: 'first_name', |
| 28 | + Attributes.LAST_NAME: 'last_name', |
| 29 | + }, |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +class DummySAML2Provider(SAML2Provider): |
| 34 | + strict_mode = False |
| 35 | + |
| 36 | + def get_saml_setup_pipeline(self): |
| 37 | + return [] |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.skipif(not HAS_SAML2, reason='SAML2 library is not installed') |
| 41 | +class AuthSAML2Test(AuthProviderTestCase): |
| 42 | + provider = DummySAML2Provider |
| 43 | + provider_name = 'saml2_dummy' |
| 44 | + |
| 45 | + def setUp(self): |
| 46 | + self. user = self. create_user( '[email protected]') |
| 47 | + self.org = self.create_organization(owner=self.user, name='saml2-org') |
| 48 | + self.auth_provider = AuthProvider.objects.create( |
| 49 | + provider=self.provider_name, |
| 50 | + config=dummy_provider_config, |
| 51 | + organization=self.org, |
| 52 | + ) |
| 53 | + |
| 54 | + # The system.url-prefix, which is used to generate absolute URLs, must |
| 55 | + # have a TLD for the SAML2 library to consider the URL generated for |
| 56 | + # the ACS endpoint valid. |
| 57 | + self.url_prefix = settings.SENTRY_OPTIONS.get('system.url-prefix') |
| 58 | + |
| 59 | + settings.SENTRY_OPTIONS.update({ |
| 60 | + 'system.url-prefix': 'http://testserver.com', |
| 61 | + }) |
| 62 | + |
| 63 | + super(AuthSAML2Test, self).setUp() |
| 64 | + |
| 65 | + def tearDown(self): |
| 66 | + # restore url-prefix config |
| 67 | + settings.SENTRY_OPTIONS.update({ |
| 68 | + 'system.url-prefix': self.url_prefix, |
| 69 | + }) |
| 70 | + |
| 71 | + super(AuthSAML2Test, self).tearDown() |
| 72 | + |
| 73 | + @fixture |
| 74 | + def login_path(self): |
| 75 | + return reverse('sentry-auth-organization', args=['saml2-org']) |
| 76 | + |
| 77 | + @fixture |
| 78 | + def sso_path(self): |
| 79 | + return reverse('sentry-auth-sso') |
| 80 | + |
| 81 | + def test_redirects_to_idp(self): |
| 82 | + resp = self.client.post(self.login_path, {'init': True}) |
| 83 | + |
| 84 | + assert resp.status_code == 302 |
| 85 | + redirect = urlparse(resp.get('Location', '')) |
| 86 | + query = parse_qs(redirect.query) |
| 87 | + |
| 88 | + assert redirect.path == '/sso_url' |
| 89 | + assert 'SAMLRequest' in query |
| 90 | + |
| 91 | + def test_auth_from_idp(self): |
| 92 | + # Start auth process |
| 93 | + self.client.post(self.login_path, {'init': True}) |
| 94 | + |
| 95 | + saml_response = self.load_fixture('saml2_auth_response.xml') |
| 96 | + saml_response = base64.b64encode(saml_response) |
| 97 | + |
| 98 | + # Disable validation of the SAML2 mock response |
| 99 | + is_valid = 'onelogin.saml2.response.OneLogin_Saml2_Response.is_valid' |
| 100 | + |
| 101 | + with mock.patch(is_valid, return_value=True): |
| 102 | + resp = self.client.post(self.sso_path, {'SAMLResponse': saml_response}) |
| 103 | + |
| 104 | + assert resp.status_code == 200 |
| 105 | + assert resp.context['existing_user'] == self.user |
| 106 | + |
| 107 | + def test_saml_metadata(self): |
| 108 | + path = reverse('sentry-auth-organization-saml-metadata', args=['saml2-org']) |
| 109 | + resp = self.client.get(path) |
| 110 | + |
| 111 | + assert resp.status_code == 200 |
| 112 | + assert resp.get('content-type') == 'text/xml' |
| 113 | + |
| 114 | + def test_logout_request(self): |
| 115 | + saml_request = self.load_fixture('saml2_slo_request.xml') |
| 116 | + saml_request = base64.b64encode(saml_request) |
| 117 | + |
| 118 | + self.login_as(self.user) |
| 119 | + |
| 120 | + path = reverse('sentry-auth-organization-saml-sls', args=['saml2-org']) |
| 121 | + path = path + '?' + urlencode({'SAMLRequest': saml_request}) |
| 122 | + resp = self.client.get(path) |
| 123 | + |
| 124 | + assert resp.status_code == 302 |
| 125 | + |
| 126 | + redirect = urlparse(resp.get('Location', '')) |
| 127 | + query = parse_qs(redirect.query) |
| 128 | + |
| 129 | + assert redirect.path == '/slo_url' |
| 130 | + assert 'SAMLResponse' in query |
0 commit comments