This file is indexed.

/usr/lib/python2.7/dist-packages/keystoneauth1/tests/unit/extras/saml2/test_auth_saml2.py is in python-keystoneauth1 3.4.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import base64
import uuid

import requests

from keystoneauth1 import exceptions
from keystoneauth1.extras import _saml2 as saml2
from keystoneauth1 import fixture as ksa_fixtures
from keystoneauth1 import session
from keystoneauth1.tests.unit.extras.saml2 import fixtures as saml2_fixtures
from keystoneauth1.tests.unit.extras.saml2 import utils
from keystoneauth1.tests.unit import matchers

PAOS_HEADER = 'application/vnd.paos+xml'
CONTENT_TYPE_PAOS_HEADER = {'Content-Type': PAOS_HEADER}
InvalidResponse = saml2.v3.saml2.InvalidResponse


class SamlAuth2PluginTests(utils.TestCase):
    """These test ONLY the standalone requests auth plugin.

    Tests for the auth plugin are later so that hopefully these can be
    extracted into it's own module.
    """

    HEADER_MEDIA_TYPE_SEPARATOR = ','

    TEST_USER = 'user'
    TEST_PASS = 'pass'
    TEST_SP_URL = 'http://sp.test'
    TEST_IDP_URL = 'http://idp.test'
    TEST_CONSUMER_URL = "https://openstack4.local/Shibboleth.sso/SAML2/ECP"

    def get_plugin(self, **kwargs):
        kwargs.setdefault('identity_provider_url', self.TEST_IDP_URL)
        kwargs.setdefault('requests_auth', (self.TEST_USER, self.TEST_PASS))
        return saml2.v3.saml2._SamlAuth(**kwargs)

    @property
    def calls(self):
        return [r.url.strip('/') for r in self.requests_mock.request_history]

    def basic_header(self, username=TEST_USER, password=TEST_PASS):
        user_pass = ('%s:%s' % (username, password)).encode('utf-8')
        return 'Basic %s' % base64.b64encode(user_pass).decode('utf-8')

    def test_request_accept_headers(self):
        # Include some random Accept header
        random_header = uuid.uuid4().hex
        headers = {'Accept': random_header}
        req = requests.Request('GET', 'http://another.test', headers=headers)

        plugin = self.get_plugin()
        plugin_headers = plugin(req).headers
        self.assertIn('Accept', plugin_headers)

        # Since we have included a random Accept header, the plugin should have
        # added the PAOS_HEADER to it using the correct media type separator
        accept_header = plugin_headers['Accept']
        self.assertIn(self.HEADER_MEDIA_TYPE_SEPARATOR, accept_header)
        self.assertIn(random_header,
                      accept_header.split(self.HEADER_MEDIA_TYPE_SEPARATOR))
        self.assertIn(PAOS_HEADER,
                      accept_header.split(self.HEADER_MEDIA_TYPE_SEPARATOR))

    def test_passed_when_not_200(self):
        text = uuid.uuid4().hex
        test_url = 'http://another.test'
        self.requests_mock.get(test_url,
                               status_code=201,
                               headers=CONTENT_TYPE_PAOS_HEADER,
                               text=text)

        resp = requests.get(test_url, auth=self.get_plugin())
        self.assertEqual(201, resp.status_code)
        self.assertEqual(text, resp.text)

    def test_200_without_paos_header(self):
        text = uuid.uuid4().hex
        test_url = 'http://another.test'
        self.requests_mock.get(test_url, status_code=200, text=text)

        resp = requests.get(test_url, auth=self.get_plugin())
        self.assertEqual(200, resp.status_code)
        self.assertEqual(text, resp.text)

    def test_standard_workflow_302_redirect(self):
        text = uuid.uuid4().hex

        self.requests_mock.get(self.TEST_SP_URL, response_list=[
            dict(headers=CONTENT_TYPE_PAOS_HEADER,
                 content=utils.make_oneline(saml2_fixtures.SP_SOAP_RESPONSE)),
            dict(text=text)
        ])

        authm = self.requests_mock.post(self.TEST_IDP_URL,
                                        content=saml2_fixtures.SAML2_ASSERTION)

        self.requests_mock.post(
            self.TEST_CONSUMER_URL,
            status_code=302,
            headers={'Location': self.TEST_SP_URL})

        resp = requests.get(self.TEST_SP_URL, auth=self.get_plugin())
        self.assertEqual(200, resp.status_code)
        self.assertEqual(text, resp.text)

        self.assertEqual(self.calls, [self.TEST_SP_URL,
                                      self.TEST_IDP_URL,
                                      self.TEST_CONSUMER_URL,
                                      self.TEST_SP_URL])

        self.assertEqual(self.basic_header(),
                         authm.last_request.headers['Authorization'])

        authn_request = self.requests_mock.request_history[1].text
        self.assertThat(saml2_fixtures.AUTHN_REQUEST,
                        matchers.XMLEquals(authn_request))

    def test_standard_workflow_303_redirect(self):
        text = uuid.uuid4().hex

        self.requests_mock.get(self.TEST_SP_URL, response_list=[
            dict(headers=CONTENT_TYPE_PAOS_HEADER,
                 content=utils.make_oneline(saml2_fixtures.SP_SOAP_RESPONSE)),
            dict(text=text)
        ])

        authm = self.requests_mock.post(self.TEST_IDP_URL,
                                        content=saml2_fixtures.SAML2_ASSERTION)

        self.requests_mock.post(
            self.TEST_CONSUMER_URL,
            status_code=303,
            headers={'Location': self.TEST_SP_URL})

        resp = requests.get(self.TEST_SP_URL, auth=self.get_plugin())
        self.assertEqual(200, resp.status_code)
        self.assertEqual(text, resp.text)

        url_flow = [self.TEST_SP_URL,
                    self.TEST_IDP_URL,
                    self.TEST_CONSUMER_URL,
                    self.TEST_SP_URL]

        self.assertEqual(url_flow, [r.url.rstrip('/') for r in resp.history])
        self.assertEqual(url_flow, self.calls)

        self.assertEqual(self.basic_header(),
                         authm.last_request.headers['Authorization'])

        authn_request = self.requests_mock.request_history[1].text
        self.assertThat(saml2_fixtures.AUTHN_REQUEST,
                        matchers.XMLEquals(authn_request))

    def test_initial_sp_call_invalid_response(self):
        """Send initial SP HTTP request and receive wrong server response."""
        self.requests_mock.get(self.TEST_SP_URL,
                               headers=CONTENT_TYPE_PAOS_HEADER,
                               text='NON XML RESPONSE')

        self.assertRaises(InvalidResponse,
                          requests.get,
                          self.TEST_SP_URL,
                          auth=self.get_plugin())

        self.assertEqual(self.calls, [self.TEST_SP_URL])

    def test_consumer_mismatch_error_workflow(self):
        consumer1 = 'http://consumer1/Shibboleth.sso/SAML2/ECP'
        consumer2 = 'http://consumer2/Shibboleth.sso/SAML2/ECP'
        soap_response = saml2_fixtures.soap_response(consumer=consumer1)
        saml_assertion = saml2_fixtures.saml_assertion(destination=consumer2)

        self.requests_mock.get(self.TEST_SP_URL,
                               headers=CONTENT_TYPE_PAOS_HEADER,
                               content=soap_response)

        self.requests_mock.post(self.TEST_IDP_URL, content=saml_assertion)

        # receive the SAML error, body unchecked
        saml_error = self.requests_mock.post(consumer1)

        self.assertRaises(saml2.v3.saml2.ConsumerMismatch,
                          requests.get,
                          self.TEST_SP_URL,
                          auth=self.get_plugin())

        self.assertTrue(saml_error.called)


class AuthenticateviaSAML2Tests(utils.TestCase):

    TEST_USER = 'user'
    TEST_PASS = 'pass'
    TEST_IDP = 'tester'
    TEST_PROTOCOL = 'saml2'
    TEST_AUTH_URL = 'http://keystone.test:5000/v3/'

    TEST_IDP_URL = 'https://idp.test'
    TEST_CONSUMER_URL = "https://openstack4.local/Shibboleth.sso/SAML2/ECP"

    def get_plugin(self, **kwargs):
        kwargs.setdefault('auth_url', self.TEST_AUTH_URL)
        kwargs.setdefault('username', self.TEST_USER)
        kwargs.setdefault('password', self.TEST_PASS)
        kwargs.setdefault('identity_provider', self.TEST_IDP)
        kwargs.setdefault('identity_provider_url', self.TEST_IDP_URL)
        kwargs.setdefault('protocol', self.TEST_PROTOCOL)
        return saml2.V3Saml2Password(**kwargs)

    def sp_url(self, **kwargs):
        kwargs.setdefault('base', self.TEST_AUTH_URL.rstrip('/'))
        kwargs.setdefault('identity_provider', self.TEST_IDP)
        kwargs.setdefault('protocol', self.TEST_PROTOCOL)

        templ = ('%(base)s/OS-FEDERATION/identity_providers/'
                 '%(identity_provider)s/protocols/%(protocol)s/auth')
        return templ % kwargs

    @property
    def calls(self):
        return [r.url.strip('/') for r in self.requests_mock.request_history]

    def basic_header(self, username=TEST_USER, password=TEST_PASS):
        user_pass = ('%s:%s' % (username, password)).encode('utf-8')
        return 'Basic %s' % base64.b64encode(user_pass).decode('utf-8')

    def setUp(self):
        super(AuthenticateviaSAML2Tests, self).setUp()
        self.session = session.Session()
        self.default_sp_url = self.sp_url()

    def test_workflow(self):
        token_id = uuid.uuid4().hex
        token = ksa_fixtures.V3Token()

        self.requests_mock.get(self.default_sp_url, response_list=[
            dict(headers=CONTENT_TYPE_PAOS_HEADER,
                 content=utils.make_oneline(saml2_fixtures.SP_SOAP_RESPONSE)),
            dict(headers={'X-Subject-Token': token_id}, json=token)
        ])

        authm = self.requests_mock.post(self.TEST_IDP_URL,
                                        content=saml2_fixtures.SAML2_ASSERTION)

        self.requests_mock.post(
            self.TEST_CONSUMER_URL,
            status_code=302,
            headers={'Location': self.sp_url()})

        auth_ref = self.get_plugin().get_auth_ref(self.session)

        self.assertEqual(token_id, auth_ref.auth_token)

        self.assertEqual(self.calls, [self.default_sp_url,
                                      self.TEST_IDP_URL,
                                      self.TEST_CONSUMER_URL,
                                      self.default_sp_url])

        self.assertEqual(self.basic_header(),
                         authm.last_request.headers['Authorization'])

        authn_request = self.requests_mock.request_history[1].text
        self.assertThat(saml2_fixtures.AUTHN_REQUEST,
                        matchers.XMLEquals(authn_request))

    def test_consumer_mismatch_error_workflow(self):
        consumer1 = 'http://keystone.test/Shibboleth.sso/SAML2/ECP'
        consumer2 = 'http://consumer2/Shibboleth.sso/SAML2/ECP'

        soap_response = saml2_fixtures.soap_response(consumer=consumer1)
        saml_assertion = saml2_fixtures.saml_assertion(destination=consumer2)

        self.requests_mock.get(self.default_sp_url,
                               headers=CONTENT_TYPE_PAOS_HEADER,
                               content=soap_response)

        self.requests_mock.post(self.TEST_IDP_URL, content=saml_assertion)

        # receive the SAML error, body unchecked
        saml_error = self.requests_mock.post(consumer1)

        self.assertRaises(exceptions.AuthorizationFailure,
                          self.get_plugin().get_auth_ref,
                          self.session)

        self.assertTrue(saml_error.called)

    def test_initial_sp_call_invalid_response(self):
        """Send initial SP HTTP request and receive wrong server response."""
        self.requests_mock.get(self.default_sp_url,
                               headers=CONTENT_TYPE_PAOS_HEADER,
                               text='NON XML RESPONSE')

        self.assertRaises(exceptions.AuthorizationFailure,
                          self.get_plugin().get_auth_ref,
                          self.session)

        self.assertEqual(self.calls, [self.default_sp_url])