-
Notifications
You must be signed in to change notification settings - Fork 11
/
test_wsgi_kerberos.py
executable file
·321 lines (281 loc) · 14.3 KB
/
test_wsgi_kerberos.py
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
313
314
315
316
317
318
319
320
321
from wsgi_kerberos import KerberosAuthMiddleware, ensure_bytestring, _DEFAULT_READ_MAX
from webtest import TestApp, TestRequest
import kerberos
import mock
import unittest
def index(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/plain')])
response_body = 'Hello %s' % environ.get('REMOTE_USER', 'ANONYMOUS')
return [ensure_bytestring(response_body)]
class BasicAppTestCase(unittest.TestCase):
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_authentication_missing_but_not_required(self, clean, name, response, step, init):
'''
Ensure that when a user's auth_required_callback returns False,
and the request is missing an auth token,
authentication is not performed.
'''
false = lambda x: False
app = TestApp(KerberosAuthMiddleware(index, auth_required_callback=false))
r = app.get('/', expect_errors=False)
self.assertEqual(r.status, '200 OK')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b'Hello ANONYMOUS')
self.assertEqual(r.headers.get('WWW-Authenticate'), None)
self.assertEqual(init.mock_calls, [])
self.assertEqual(step.mock_calls, [])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_authentication_invalid_but_not_required(self, clean, name, response, step, init):
'''
Ensure that when a user's auth_required_callback returns False,
and the request includes an invalid auth token,
the invalid auth is ignored and the request
is allowed through to the app.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
false = lambda x: False
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
auth_required_callback=false))
r = app.get('/', headers={'Authorization': 'Negotiate CTOKEN'})
self.assertEqual(r.status, '200 OK')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b'Hello ANONYMOUS')
self.assertEqual(r.headers.get('WWW-Authenticate'), None)
self.assertEqual(init.mock_calls, [mock.call('HTTP@example.org')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_authentication_valid_but_not_required(self, clean, name, response, step, init):
'''
Ensure that when a users auth_required_callback returns False,
but the request does include a valid auth token,
the authenticated user is passed through to the app.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.return_value = kerberos.AUTH_GSS_COMPLETE
name.return_value = "user@EXAMPLE.ORG"
response.return_value = "STOKEN"
false = lambda x: False
app = TestApp(KerberosAuthMiddleware(index,
hostname='example.org',
auth_required_callback=false))
r = app.get('/', headers={'Authorization': 'Negotiate CTOKEN'})
self.assertEqual(r.status, '200 OK')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b'Hello user@EXAMPLE.ORG')
self.assertEqual(r.headers.get('WWW-Authenticate'), 'negotiate STOKEN')
self.assertEqual(init.mock_calls, [mock.call('HTTP@example.org')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [mock.call(state)])
self.assertEqual(response.mock_calls, [mock.call(state)])
self.assertEqual(clean.mock_calls, [mock.call(state)])
def test_unauthorized(self):
'''
Ensure that when the client does not send an authorization token, they
receive a 401 Unauthorized response which includes a www-authenticate
header field which indicates the server supports Negotiate
authentication.
'''
app = TestApp(KerberosAuthMiddleware(index))
r = app.get('/', expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertEqual(r.body, b'Unauthorized')
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
def test_read_max_on_auth_fail(self):
'''
KerberosAuthMiddleware's ``read_max_on_auth_fail`` should allow
customizing reading of request bodies of unauthenticated requests.
'''
body = b'body of unauthenticated request'
for read_max in (0, 5, 100, _DEFAULT_READ_MAX, float('inf')):
# When we drop Py2, we can use `with self.subTest(read_max=read_max):` here.
app = TestApp(KerberosAuthMiddleware(index, read_max_on_auth_fail=read_max))
req = TestRequest.blank('/', method='POST', body=body)
resp = app.do_request(req, status=401)
if read_max < len(body):
expect_read = 0
else:
expect_read = min(read_max, len(body))
self.assertEqual(req.body_file.input.tell(), expect_read)
def test_unauthorized_when_missing_negotiate(self):
'''
Ensure that when the client sends an Authorization header that does
not start with "Negotiate ", they receive a 401 Unauthorized response
with a "WWW-Authenticate: Negotiate" header.
'''
app = TestApp(KerberosAuthMiddleware(index))
r = app.get('/', headers={'Authorization': 'foo'}, expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertTrue(r.body.startswith(b'Unauthorized'))
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
def test_unauthorized_custom(self):
'''
Ensure that when the client does not send an authorization token, they
receive a 401 Unauthorized response which includes a www-authenticate
header field which indicates the server supports Negotiate
authentication. If configured, they should also receive customized
content.
'''
app = TestApp(KerberosAuthMiddleware(index, unauthorized='CUSTOM'))
r = app.get('/', expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
def test_unauthorized_custom_content_type(self):
'''
Ensure that when the client does not send an authorization token, they
receive a 401 Unauthorized response which includes a www-authenticate
header field which indicates the server supports Negotiate
authentication. If configured, they should also receive customized
content and content type.
'''
app = TestApp(KerberosAuthMiddleware(index, unauthorized=('401!', 'text/html')))
r = app.get('/', expect_errors=True)
self.assertEqual(r.status, '401 Unauthorized')
self.assertEqual(r.status_int, 401)
self.assertEqual(r.body, b'401!')
self.assertEqual(r.headers['www-authenticate'], 'Negotiate')
self.assertEqual(r.headers['content-type'], 'text/html')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_authorized(self, clean, name, response, step, init):
'''
Ensure that when the client sends a correct authorization token,
they receive a 200 OK response and the user principal is extracted and
passed on to the routed method.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.return_value = kerberos.AUTH_GSS_COMPLETE
name.return_value = "user@EXAMPLE.ORG"
response.return_value = "STOKEN"
app = TestApp(KerberosAuthMiddleware(index))
r = app.get('/', headers={'Authorization': 'Negotiate CTOKEN'})
self.assertEqual(r.status, '200 OK')
self.assertEqual(r.status_int, 200)
self.assertEqual(r.body, b'Hello user@EXAMPLE.ORG')
self.assertEqual(r.headers['WWW-Authenticate'], 'negotiate STOKEN')
self.assertEqual(init.mock_calls, [mock.call('')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [mock.call(state)])
self.assertEqual(response.mock_calls, [mock.call(state)])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_forbidden(self, clean, name, response, step, init):
'''
Ensure that when the client sends an incorrect authorization token,
they receive a 403 Forbidden response.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
app = TestApp(KerberosAuthMiddleware(index))
r = app.get('/',
headers={'Authorization': 'Negotiate CTOKEN'},
expect_errors=True)
self.assertEqual(r.status, '403 Forbidden')
self.assertEqual(r.status_int, 403)
self.assertEqual(r.body, b'Forbidden')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
self.assertEqual(init.mock_calls, [mock.call('')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_forbidden_custom(self, clean, name, response, step, init):
'''
Ensure that when the client sends an incorrect authorization token,
they receive a 403 Forbidden response. If configured, they should
receive customized content.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
app = TestApp(KerberosAuthMiddleware(index, forbidden='CUSTOM'))
r = app.get('/',
headers={'Authorization': 'Negotiate CTOKEN'},
expect_errors=True)
self.assertEqual(r.status, '403 Forbidden')
self.assertEqual(r.status_int, 403)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['content-type'], 'text/plain')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
self.assertEqual(init.mock_calls, [mock.call('')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
@mock.patch('kerberos.authGSSServerInit')
@mock.patch('kerberos.authGSSServerStep')
@mock.patch('kerberos.authGSSServerResponse')
@mock.patch('kerberos.authGSSServerUserName')
@mock.patch('kerberos.authGSSServerClean')
def test_forbidden_custom_content_type(self, clean, name, response, step, init):
'''
Ensure that when the client sends an incorrect authorization token,
they receive a 403 Forbidden response. If configured, they should
receive customized content and content-type.
'''
state = object()
init.return_value = (kerberos.AUTH_GSS_COMPLETE, state)
step.side_effect = kerberos.GSSError("FAILURE")
app = TestApp(KerberosAuthMiddleware(index, forbidden=('CUSTOM', 'text/html')))
r = app.get('/',
headers={'Authorization': 'Negotiate CTOKEN'},
expect_errors=True)
self.assertEqual(r.status, '403 Forbidden')
self.assertEqual(r.status_int, 403)
self.assertEqual(r.body, b'CUSTOM')
self.assertEqual(r.headers['content-type'], 'text/html')
self.assertEqual(r.headers['content-length'], str(len(r.body)))
self.assertEqual(init.mock_calls, [mock.call('')])
self.assertEqual(step.mock_calls, [mock.call(state, 'CTOKEN')])
self.assertEqual(name.mock_calls, [])
self.assertEqual(response.mock_calls, [])
self.assertEqual(clean.mock_calls, [mock.call(state)])
if __name__ == '__main__':
unittest.main()