Debian Codegraph @

keystoneclient/tests/unit/v3/test_auth_oidc.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import urllib.parse
14 import uuid
15
16 from oslo_config import fixture as config
17
18 import testtools
19
20 from keystoneclient.auth import conf
21 from keystoneclient.contrib.auth.v3 import oidc
22 from keystoneclient import session
23 from keystoneclient.tests.unit.v3 import utils
24
25
26 ACCESS_TOKEN_ENDPOINT_RESP = {"access_token": "z5H1ITZLlJVDHQXqJun",
27 "token_type": "bearer",
28 "expires_in": 3599,
29 "scope": "profile",
30 "refresh_token": "DCERsh83IAhu9bhavrp"}
31
32 KEYSTONE_TOKEN_VALUE = uuid.uuid4().hex
33 UNSCOPED_TOKEN = {
34 "token": {
35 "issued_at": "2014-06-09T09:48:59.643406Z",
36 "extras": {},
37 "methods": ["oidc"],
38 "expires_at": "2014-06-09T10:48:59.643375Z",
39 "user": {
40 "OS-FEDERATION": {
41 "identity_provider": {
42 "id": "bluepages"
43 },
44 "protocol": {
45 "id": "oidc"
46 },
47 "groups": [
48 {"id": "1764fa5cf69a49a4918131de5ce4af9a"}
49 ]
50 },
51 "id": "oidc_user%40example.com",
52 "name": "[email protected]"
53 }
54 }
55 }
56
57
58 class AuthenticateOIDCTests(utils.TestCase):
59
60 GROUP = 'auth'
61
62 def setUp(self):
63 super(AuthenticateOIDCTests, self).setUp()
64
65 self.deprecations.expect_deprecations()
66
67 self.conf_fixture = self.useFixture(config.Config())
68 conf.register_conf_options(self.conf_fixture.conf, group=self.GROUP)
69
70 self.session = session.Session()
71
72 self.IDENTITY_PROVIDER = 'bluepages'
73 self.PROTOCOL = 'oidc'
74 self.USER_NAME = '[email protected]'
75 self.PASSWORD = uuid.uuid4().hex
76 self.CLIENT_ID = uuid.uuid4().hex
77 self.CLIENT_SECRET = uuid.uuid4().hex
78 self.ACCESS_TOKEN_ENDPOINT = 'https://localhost:8020/oidc/token'
79 self.FEDERATION_AUTH_URL = '%s/%s' % (
80 self.TEST_URL,
81 'OS-FEDERATION/identity_providers/bluepages/protocols/oidc/auth')
82
83 self.oidcplugin = oidc.OidcPassword(
84 self.TEST_URL,
85 self.IDENTITY_PROVIDER,
86 self.PROTOCOL,
87 username=self.USER_NAME,
88 password=self.PASSWORD,
89 client_id=self.CLIENT_ID,
90 client_secret=self.CLIENT_SECRET,
91 access_token_endpoint=self.ACCESS_TOKEN_ENDPOINT)
92
93 @testtools.skip("TypeError: __init__() got an unexpected keyword"
94 " argument 'project_name'")
95 def test_conf_params(self):
96 """Ensure OpenID Connect config options work."""
97 section = uuid.uuid4().hex
98 identity_provider = uuid.uuid4().hex
99 protocol = uuid.uuid4().hex
100 username = uuid.uuid4().hex
101 password = uuid.uuid4().hex
102 client_id = uuid.uuid4().hex
103 client_secret = uuid.uuid4().hex
104 access_token_endpoint = uuid.uuid4().hex
105
106 self.conf_fixture.config(auth_section=section, group=self.GROUP)
107 conf.register_conf_options(self.conf_fixture.conf, group=self.GROUP)
108
109 self.conf_fixture.register_opts(oidc.OidcPassword.get_options(),
110 group=section)
111 self.conf_fixture.config(auth_plugin='v3oidcpassword',
112 identity_provider=identity_provider,
113 protocol=protocol,
114 username=username,
115 password=password,
116 client_id=client_id,
117 client_secret=client_secret,
118 access_token_endpoint=access_token_endpoint,
119 group=section)
120
121 a = conf.load_from_conf_options(self.conf_fixture.conf, self.GROUP)
122 self.assertEqual(identity_provider, a.identity_provider)
123 self.assertEqual(protocol, a.protocol)
124 self.assertEqual(username, a.username)
125 self.assertEqual(password, a.password)
126 self.assertEqual(client_id, a.client_id)
127 self.assertEqual(client_secret, a.client_secret)
128 self.assertEqual(access_token_endpoint, a.access_token_endpoint)
129
130 def test_initial_call_to_get_access_token(self):
131 """Test initial call, expect JSON access token."""
132 # Mock the output that creates the access token
133 self.requests_mock.post(
134 self.ACCESS_TOKEN_ENDPOINT,
135 json=ACCESS_TOKEN_ENDPOINT_RESP)
136
137 # Prep all the values and send the request
138 grant_type = 'password'
139 scope = 'profile email'
140 client_auth = (self.CLIENT_ID, self.CLIENT_SECRET)
141 payload = {'grant_type': grant_type, 'username': self.USER_NAME,
142 'password': self.PASSWORD, 'scope': scope}
143 res = self.oidcplugin._get_access_token(self.session,
144 client_auth,
145 payload,
146 self.ACCESS_TOKEN_ENDPOINT)
147
148 # Verify the request matches the expected structure
149 self.assertEqual(self.ACCESS_TOKEN_ENDPOINT, res.request.url)
150 self.assertEqual('POST', res.request.method)
151 encoded_payload = urllib.parse.urlencode(payload)
152 self.assertEqual(encoded_payload, res.request.body)
153
154 def test_second_call_to_protected_url(self):
155 """Test subsequent call, expect Keystone token."""
156 # Mock the output that creates the keystone token
157 self.requests_mock.post(
158 self.FEDERATION_AUTH_URL,
159 json=UNSCOPED_TOKEN,
160 headers={'X-Subject-Token': KEYSTONE_TOKEN_VALUE})
161
162 # Prep all the values and send the request
163 access_token = uuid.uuid4().hex
164 headers = {'Authorization': 'Bearer ' + access_token}
165 res = self.oidcplugin._get_keystone_token(self.session,
166 headers,
167 self.FEDERATION_AUTH_URL)
168
169 # Verify the request matches the expected structure
170 self.assertEqual(self.FEDERATION_AUTH_URL, res.request.url)
171 self.assertEqual('POST', res.request.method)
172 self.assertEqual(headers['Authorization'],
173 res.request.headers['Authorization'])
174
175 def test_end_to_end_workflow(self):
176 """Test full OpenID Connect workflow."""
177 # Mock the output that creates the access token
178 self.requests_mock.post(
179 self.ACCESS_TOKEN_ENDPOINT,
180 json=ACCESS_TOKEN_ENDPOINT_RESP)
181
182 # Mock the output that creates the keystone token
183 self.requests_mock.post(
184 self.FEDERATION_AUTH_URL,
185 json=UNSCOPED_TOKEN,
186 headers={'X-Subject-Token': KEYSTONE_TOKEN_VALUE})
187
188 response = self.oidcplugin.get_unscoped_auth_ref(self.session)
189 self.assertEqual(KEYSTONE_TOKEN_VALUE, response.auth_token)