forked from miguelgrinberg/Flask-HTTPAuth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_httpauth.py
336 lines (286 loc) · 13.7 KB
/
test_httpauth.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import unittest
import base64
import re
from hashlib import md5 as basic_md5
from flask import Flask
from flask.ext.httpauth import HTTPBasicAuth, HTTPDigestAuth
from werkzeug.http import parse_dict_header
def md5(str):
if type(str).__name__ == 'str':
str = str.encode('utf-8')
return basic_md5(str)
class HTTPAuthTestCase(unittest.TestCase):
def setUp(self):
app = Flask(__name__)
app.config['SECRET_KEY'] = 'my secret'
basic_auth = HTTPBasicAuth()
basic_auth_my_realm = HTTPBasicAuth()
basic_auth_my_realm.realm = 'My Realm'
basic_custom_auth = HTTPBasicAuth()
basic_verify_auth = HTTPBasicAuth()
digest_auth = HTTPDigestAuth()
digest_auth_my_realm = HTTPDigestAuth()
digest_auth_my_realm.realm = 'My Realm'
@basic_auth.get_password
def get_basic_password(username):
if username == 'john':
return 'hello'
elif username == 'susan':
return 'bye'
else:
return None
@basic_auth_my_realm.get_password
def get_basic_password_2(username):
if username == 'john':
return 'johnhello'
elif username == 'susan':
return 'susanbye'
else:
return None
@basic_auth_my_realm.hash_password
def basic_auth_my_realm_hash_password(username, password):
return username + password
@basic_auth_my_realm.error_handler
def basic_auth_my_realm_error():
return 'custom error'
@basic_custom_auth.get_password
def get_basic_custom_auth_get_password(username):
if username == 'john':
return md5('hello').hexdigest()
elif username == 'susan':
return md5('bye').hexdigest()
else:
return None
@basic_custom_auth.hash_password
def basic_custom_auth_hash_password(password):
return md5(password).hexdigest()
@basic_verify_auth.verify_password
def basic_verify_auth_verify_password(username, password):
if username == 'john':
return password == 'hello'
elif username == 'susan':
return password == 'bye'
return False
@digest_auth.get_password
def get_digest_password(username):
if username == 'susan':
return 'hello'
elif username == 'john':
return 'bye'
else:
return None
@digest_auth_my_realm.get_password
def get_digest_password_2(username):
if username == 'susan':
return 'hello'
elif username == 'john':
return 'bye'
else:
return None
@app.route('/')
def index():
return 'index'
@app.route('/basic')
@basic_auth.login_required
def basic_auth_route():
return 'basic_auth:' + basic_auth.username()
@app.route('/basic-with-realm')
@basic_auth_my_realm.login_required
def basic_auth_my_realm_route():
return 'basic_auth_my_realm:' + basic_auth_my_realm.username()
@app.route('/basic-custom')
@basic_custom_auth.login_required
def basic_custom_auth_route():
return 'basic_custom_auth:' + basic_custom_auth.username()
@app.route('/basic-verify')
@basic_verify_auth.login_required
def basic_verify_auth_route():
return 'basic_verify_auth:' + basic_verify_auth.username()
@app.route('/digest')
@digest_auth.login_required
def digest_auth_route():
return 'digest_auth:' + digest_auth.username()
@app.route('/digest-with-realm')
@digest_auth_my_realm.login_required
def digest_auth_my_realm_route():
return 'digest_auth_my_realm:' + digest_auth_my_realm.username()
self.app = app
self.basic_auth = basic_auth
self.basic_auth_my_realm = basic_auth_my_realm
self.basic_custom_auth = basic_custom_auth
self.basic_verify_auth = basic_verify_auth
self.digest_auth = digest_auth
self.client = app.test_client()
def test_no_auth(self):
response = self.client.get('/')
self.assertEqual(response.data.decode('utf-8'), 'index')
def test_basic_auth_prompt(self):
response = self.client.get('/basic')
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertEqual(response.headers['WWW-Authenticate'],
'Basic realm="Authentication Required"')
def test_basic_auth_ignore_options(self):
response = self.client.options('/basic')
self.assertEqual(response.status_code, 200)
self.assertTrue('WWW-Authenticate' not in response.headers)
def test_basic_auth_prompt_with_custom_realm(self):
response = self.client.get('/basic-with-realm')
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertEqual(response.headers['WWW-Authenticate'],
'Basic realm="My Realm"')
self.assertEqual(response.data.decode('utf-8'), 'custom error')
def test_basic_auth_login_valid(self):
creds = base64.b64encode(b'john:hello').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.data.decode('utf-8'), 'basic_auth:john')
def test_basic_auth_login_valid_with_hash1(self):
creds = base64.b64encode(b'john:hello').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-custom', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.data.decode('utf-8'),
'basic_custom_auth:john')
def test_basic_auth_login_valid_with_hash2(self):
creds = base64.b64encode(b'john:hello').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-with-realm', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.data.decode('utf-8'),
'basic_auth_my_realm:john')
def test_basic_auth_login_invalid(self):
creds = base64.b64encode(b'john:bye').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-with-realm', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertEqual(response.headers['WWW-Authenticate'],
'Basic realm="My Realm"')
def test_basic_custom_auth_login_valid(self):
creds = base64.b64encode(b'john:hello').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-custom', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.data, b'basic_custom_auth:john')
def test_basic_custom_auth_login_invalid(self):
creds = base64.b64encode(b'john:bye').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-custom', headers={"Authorization": "Basic " + creds})
self.assertEqual(response.status_code, 401)
self.assertIn("WWW-Authenticate", response.headers)
def test_verify_auth_login_valid(self):
creds = base64.b64encode(b'susan:bye').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-verify', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.data, b'basic_verify_auth:susan')
def test_verify_auth_login_invalid(self):
creds = base64.b64encode(b'john:bye').decode('utf-8').strip('\r\n')
response = self.client.get(
'/basic-verify', headers={'Authorization': 'Basic ' + creds})
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
def test_digest_auth_prompt(self):
response = self.client.get('/digest')
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertTrue(re.match(r'^Digest realm="Authentication Required",'
r'nonce="[0-9a-f]+",opaque="[0-9a-f]+"$',
response.headers['WWW-Authenticate']))
def test_digest_auth_ignore_options(self):
response = self.client.options('/digest')
self.assertEqual(response.status_code, 200)
self.assertTrue('WWW-Authenticate' not in response.headers)
def test_digest_auth_prompt_with_custom_realm(self):
response = self.client.get('/digest-with-realm')
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertTrue(re.match(r'^Digest realm="My Realm",nonce="[0-9a-f]+",'
r'opaque="[0-9a-f]+"$',
response.headers['WWW-Authenticate']))
def test_digest_auth_login_valid(self):
response = self.client.get('/digest')
self.assertTrue(response.status_code == 401)
header = response.headers.get('WWW-Authenticate')
auth_type, auth_info = header.split(None, 1)
d = parse_dict_header(auth_info)
a1 = 'john:' + d['realm'] + ':bye'
ha1 = md5(a1).hexdigest()
a2 = 'GET:/digest'
ha2 = md5(a2).hexdigest()
a3 = ha1 + ':' + d['nonce'] + ':' + ha2
auth_response = md5(a3).hexdigest()
response = self.client.get(
'/digest', headers={
'Authorization': 'Digest username="john",realm="{0}",'
'nonce="{1}",uri="/digest",response="{2}",'
'opaque="{3}"'.format(d['realm'],
d['nonce'],
auth_response,
d['opaque'])})
self.assertEqual(response.data, b'digest_auth:john')
def test_digest_auth_login_bad_realm(self):
response = self.client.get('/digest')
self.assertTrue(response.status_code == 401)
header = response.headers.get('WWW-Authenticate')
auth_type, auth_info = header.split(None, 1)
d = parse_dict_header(auth_info)
a1 = 'john:' + 'Wrong Realm' + ':bye'
ha1 = md5(a1).hexdigest()
a2 = 'GET:/digest'
ha2 = md5(a2).hexdigest()
a3 = ha1 + ':' + d['nonce'] + ':' + ha2
auth_response = md5(a3).hexdigest()
response = self.client.get(
'/digest', headers={
'Authorization': 'Digest username="john",realm="{0}",'
'nonce="{1}",uri="/digest",response="{2}",'
'opaque="{3}"'.format(d['realm'],
d['nonce'],
auth_response,
d['opaque'])})
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertTrue(re.match(r'^Digest realm="Authentication Required",'
r'nonce="[0-9a-f]+",opaque="[0-9a-f]+"$',
response.headers['WWW-Authenticate']))
def test_digest_auth_login_invalid(self):
response = self.client.get(
'/digest-with-realm', headers={
"Authorization": 'Digest username="susan",realm="My Realm",'
'nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093",'
'uri="/digest-with-realm",'
'response="ca306c361a9055b968810067a37fb8cb",'
'opaque="5ccc069c403ebaf9f0171e9517f40e41"'})
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertTrue(re.match(r'^Digest realm="My Realm",nonce="[0-9a-f]+",'
r'opaque="[0-9a-f]+"$',
response.headers['WWW-Authenticate']))
def test_digest_auth_login_invalid2(self):
response = self.client.get('/digest')
self.assertEqual(response.status_code, 401)
header = response.headers.get('WWW-Authenticate')
auth_type, auth_info = header.split(None, 1)
d = parse_dict_header(auth_info)
a1 = 'david:' + 'Authentication Required' + ':bye'
ha1 = md5(a1).hexdigest()
a2 = 'GET:/digest'
ha2 = md5(a2).hexdigest()
a3 = ha1 + ':' + d['nonce'] + ':' + ha2
auth_response = md5(a3).hexdigest()
response = self.client.get(
'/digest', headers={
'Authorization': 'Digest username="david",realm="{0}",'
'nonce="{1}",uri="/digest",response="{2}",'
'opaque="{3}"'.format(d['realm'],
d['nonce'],
auth_response,
d['opaque'])})
self.assertEqual(response.status_code, 401)
self.assertIn('WWW-Authenticate', response.headers)
self.assertTrue(re.match(r'^Digest realm="Authentication Required",'
r'nonce="[0-9a-f]+",opaque="[0-9a-f]+"$',
response.headers['WWW-Authenticate']))
def suite():
return unittest.makeSuite(HTTPAuthTestCase)
if __name__ == '__main__':
unittest.main(defaultTest='suite')