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
|
# Copyright 2012 Canonical Ltd.
#
# This file is part of u1db.
#
# u1db is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# as published by the Free Software Foundation.
#
# u1db is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with u1db. If not, see <http://www.gnu.org/licenses/>.
"""Test OAuth wsgi middleware"""
import paste.fixture
from oauth import oauth
try:
import simplejson as json
except ImportError:
import json # noqa
import time
from u1db import tests
from u1db.remote.oauth_middleware import OAuthMiddleware
from u1db.remote.basic_auth_middleware import BasicAuthMiddleware, Unauthorized
BASE_URL = 'https://example.net'
class TestBasicAuthMiddleware(tests.TestCase):
def setUp(self):
super(TestBasicAuthMiddleware, self).setUp()
self.got = []
def witness_app(environ, start_response):
start_response("200 OK", [("content-type", "text/plain")])
self.got.append((
environ['user_id'], environ['PATH_INFO'],
environ['QUERY_STRING']))
return ["ok"]
class MyAuthMiddleware(BasicAuthMiddleware):
def verify_user(self, environ, user, password):
if user != "correct_user":
raise Unauthorized
if password != "correct_password":
raise Unauthorized
environ['user_id'] = user
self.auth_midw = MyAuthMiddleware(witness_app, prefix="/pfx/")
self.app = paste.fixture.TestApp(self.auth_midw)
def test_expect_prefix(self):
url = BASE_URL + '/foo/doc/doc-id'
resp = self.app.delete(url, expect_errors=True)
self.assertEqual(400, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
self.assertEqual('{"error": "bad request"}', resp.body)
def test_missing_auth(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
resp = self.app.delete(url, expect_errors=True)
self.assertEqual(401, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
self.assertEqual(
{"error": "unauthorized",
"message": "Missing Basic Authentication."},
json.loads(resp.body))
def test_correct_auth(self):
user = "correct_user"
password = "correct_password"
params = {'old_rev': 'old-rev'}
url = BASE_URL + '/pfx/foo/doc/doc-id?%s' % (
'&'.join("%s=%s" % (k, v) for k, v in params.items()))
auth = '%s:%s' % (user, password)
headers = {
'Authorization': 'Basic %s' % (auth.encode('base64'),)}
resp = self.app.delete(url, headers=headers)
self.assertEqual(200, resp.status)
self.assertEqual(
[('correct_user', '/foo/doc/doc-id', 'old_rev=old-rev')], self.got)
def test_incorrect_auth(self):
user = "correct_user"
password = "incorrect_password"
params = {'old_rev': 'old-rev'}
url = BASE_URL + '/pfx/foo/doc/doc-id?%s' % (
'&'.join("%s=%s" % (k, v) for k, v in params.items()))
auth = '%s:%s' % (user, password)
headers = {
'Authorization': 'Basic %s' % (auth.encode('base64'),)}
resp = self.app.delete(url, headers=headers, expect_errors=True)
self.assertEqual(401, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
self.assertEqual(
{"error": "unauthorized",
"message": "Incorrect password or login."},
json.loads(resp.body))
class TestOAuthMiddlewareDefaultPrefix(tests.TestCase):
def setUp(self):
super(TestOAuthMiddlewareDefaultPrefix, self).setUp()
self.got = []
def witness_app(environ, start_response):
start_response("200 OK", [("content-type", "text/plain")])
self.got.append((environ['token_key'], environ['PATH_INFO'],
environ['QUERY_STRING']))
return ["ok"]
class MyOAuthMiddleware(OAuthMiddleware):
get_oauth_data_store = lambda self: tests.testingOAuthStore
def verify(self, environ, oauth_req):
consumer, token = super(MyOAuthMiddleware, self).verify(
environ, oauth_req)
environ['token_key'] = token.key
self.oauth_midw = MyOAuthMiddleware(witness_app, BASE_URL)
self.app = paste.fixture.TestApp(self.oauth_midw)
def test_expect_tilde(self):
url = BASE_URL + '/foo/doc/doc-id'
resp = self.app.delete(url, expect_errors=True)
self.assertEqual(400, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
self.assertEqual('{"error": "bad request"}', resp.body)
def test_oauth_in_header(self):
url = BASE_URL + '/~/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer2,
tests.token2,
parameters=params,
http_url=url,
http_method='DELETE'
)
url = oauth_req.get_normalized_http_url() + '?' + (
'&'.join("%s=%s" % (k, v) for k, v in params.items()))
oauth_req.sign_request(tests.sign_meth_HMAC_SHA1,
tests.consumer2, tests.token2)
resp = self.app.delete(url, headers=oauth_req.to_header())
self.assertEqual(200, resp.status)
self.assertEqual([(tests.token2.key,
'/foo/doc/doc-id', 'old_rev=old-rev')], self.got)
def test_oauth_in_query_string(self):
url = BASE_URL + '/~/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer1,
tests.token1,
parameters=params,
http_url=url,
http_method='DELETE'
)
oauth_req.sign_request(tests.sign_meth_HMAC_SHA1,
tests.consumer1, tests.token1)
resp = self.app.delete(oauth_req.to_url())
self.assertEqual(200, resp.status)
self.assertEqual([(tests.token1.key,
'/foo/doc/doc-id', 'old_rev=old-rev')], self.got)
class TestOAuthMiddleware(tests.TestCase):
def setUp(self):
super(TestOAuthMiddleware, self).setUp()
self.got = []
def witness_app(environ, start_response):
start_response("200 OK", [("content-type", "text/plain")])
self.got.append((environ['token_key'], environ['PATH_INFO'],
environ['QUERY_STRING']))
return ["ok"]
class MyOAuthMiddleware(OAuthMiddleware):
get_oauth_data_store = lambda self: tests.testingOAuthStore
def verify(self, environ, oauth_req):
consumer, token = super(MyOAuthMiddleware, self).verify(
environ, oauth_req)
environ['token_key'] = token.key
self.oauth_midw = MyOAuthMiddleware(
witness_app, BASE_URL, prefix='/pfx/')
self.app = paste.fixture.TestApp(self.oauth_midw)
def test_expect_prefix(self):
url = BASE_URL + '/foo/doc/doc-id'
resp = self.app.delete(url, expect_errors=True)
self.assertEqual(400, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
self.assertEqual('{"error": "bad request"}', resp.body)
def test_missing_oauth(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
resp = self.app.delete(url, expect_errors=True)
self.assertEqual(401, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
self.assertEqual(
{"error": "unauthorized", "message": "Missing OAuth."},
json.loads(resp.body))
def test_oauth_in_query_string(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer1,
tests.token1,
parameters=params,
http_url=url,
http_method='DELETE'
)
oauth_req.sign_request(tests.sign_meth_HMAC_SHA1,
tests.consumer1, tests.token1)
resp = self.app.delete(oauth_req.to_url())
self.assertEqual(200, resp.status)
self.assertEqual([(tests.token1.key,
'/foo/doc/doc-id', 'old_rev=old-rev')], self.got)
def test_oauth_invalid(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer1,
tests.token3,
parameters=params,
http_url=url,
http_method='DELETE'
)
oauth_req.sign_request(tests.sign_meth_HMAC_SHA1,
tests.consumer1, tests.token3)
resp = self.app.delete(oauth_req.to_url(),
expect_errors=True)
self.assertEqual(401, resp.status)
self.assertEqual('application/json', resp.header('content-type'))
err = json.loads(resp.body)
self.assertEqual({"error": "unauthorized",
"message": err['message']},
err)
def test_oauth_in_header(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer2,
tests.token2,
parameters=params,
http_url=url,
http_method='DELETE'
)
url = oauth_req.get_normalized_http_url() + '?' + (
'&'.join("%s=%s" % (k, v) for k, v in params.items()))
oauth_req.sign_request(tests.sign_meth_HMAC_SHA1,
tests.consumer2, tests.token2)
resp = self.app.delete(url, headers=oauth_req.to_header())
self.assertEqual(200, resp.status)
self.assertEqual([(tests.token2.key,
'/foo/doc/doc-id', 'old_rev=old-rev')], self.got)
def test_oauth_plain_text(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer1,
tests.token1,
parameters=params,
http_url=url,
http_method='DELETE'
)
oauth_req.sign_request(tests.sign_meth_PLAINTEXT,
tests.consumer1, tests.token1)
resp = self.app.delete(oauth_req.to_url())
self.assertEqual(200, resp.status)
self.assertEqual([(tests.token1.key,
'/foo/doc/doc-id', 'old_rev=old-rev')], self.got)
def test_oauth_timestamp_threshold(self):
url = BASE_URL + '/pfx/foo/doc/doc-id'
params = {'old_rev': 'old-rev'}
oauth_req = oauth.OAuthRequest.from_consumer_and_token(
tests.consumer1,
tests.token1,
parameters=params,
http_url=url,
http_method='DELETE'
)
oauth_req.set_parameter('oauth_timestamp', int(time.time()) - 5)
oauth_req.sign_request(tests.sign_meth_PLAINTEXT,
tests.consumer1, tests.token1)
# tweak threshold
self.oauth_midw.timestamp_threshold = 1
resp = self.app.delete(oauth_req.to_url(), expect_errors=True)
self.assertEqual(401, resp.status)
err = json.loads(resp.body)
self.assertIn('Expired timestamp', err['message'])
self.assertIn('threshold 1', err['message'])
|