Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
| File repozewho-tg-2114.diff,
15.7 KB
(added by Gustavo, 3 years ago) |
|
Patch to repoze.who v1.0.10
|
-
|
|
|
|
| 72 | 72 | class FormPlugin(FormPluginBase): |
| 73 | 73 | |
| 74 | 74 | implements(IChallenger, IIdentifier) |
| 75 | | |
| | 75 | |
| 76 | 76 | def __init__(self, login_form_qs, rememberer_name, formbody=None, |
| 77 | 77 | formcallable=None): |
| 78 | 78 | self.login_form_qs = login_form_qs |
| … |
… |
|
| 88 | 88 | query = parse_dict_querystring(environ) |
| 89 | 89 | # If the extractor finds a special query string on any request, |
| 90 | 90 | # it will attempt to find the values in the input body. |
| 91 | | if query.get(self.login_form_qs): |
| | 91 | if query.get(self.login_form_qs): |
| 92 | 92 | form = parse_formvars(environ) |
| 93 | 93 | from StringIO import StringIO |
| 94 | 94 | # XXX we need to replace wsgi.input because we've read it |
| … |
… |
|
| 115 | 115 | if location: |
| 116 | 116 | headers = list(app_headers) + list(forget_headers) |
| 117 | 117 | return HTTPFound(headers = headers) |
| 118 | | |
| | 118 | |
| 119 | 119 | form = self.formbody or _DEFAULT_FORM |
| 120 | 120 | if self.formcallable is not None: |
| 121 | 121 | form = self.formcallable(environ) |
| … |
… |
|
| 131 | 131 | class RedirectingFormPlugin(FormPluginBase): |
| 132 | 132 | |
| 133 | 133 | implements(IChallenger, IIdentifier) |
| 134 | | |
| | 134 | |
| 135 | 135 | def __init__(self, login_form_url, login_handler_path, logout_handler_path, |
| 136 | 136 | rememberer_name, reason_param='reason'): |
| 137 | 137 | self.login_form_url = login_form_url |
| … |
… |
|
| 146 | 146 | # IIdentifier |
| 147 | 147 | def identify(self, environ): |
| 148 | 148 | path_info = environ['PATH_INFO'] |
| | 149 | script_path = environ.get('SCRIPT_PATH') or '/' |
| 149 | 150 | query = parse_dict_querystring(environ) |
| 150 | 151 | |
| 151 | 152 | if path_info == self.logout_handler_path: |
| 152 | 153 | # we've been asked to perform a logout |
| 153 | 154 | form = parse_formvars(environ) |
| 154 | 155 | form.update(query) |
| 155 | | referer = environ.get('HTTP_REFERER', '/') |
| | 156 | referer = environ.get('HTTP_REFERER', script_path) |
| 156 | 157 | came_from = form.get('came_from', referer) |
| 157 | 158 | # set in environ for self.challenge() to find later |
| 158 | 159 | environ['came_from'] = came_from |
| … |
… |
|
| 172 | 173 | } |
| 173 | 174 | except KeyError: |
| 174 | 175 | credentials = None |
| 175 | | referer = environ.get('HTTP_REFERER', '/') |
| | 176 | referer = environ.get('HTTP_REFERER', script_path) |
| 176 | 177 | came_from = form.get('came_from', referer) |
| 177 | 178 | environ['repoze.who.application'] = HTTPFound(came_from) |
| 178 | 179 | return credentials |
| … |
… |
|
| 189 | 190 | query_elements[self.reason_param] = reason |
| 190 | 191 | url_parts[4] = urllib.urlencode(query_elements, doseq=True) |
| 191 | 192 | login_form_url = urlparse.urlunparse(url_parts) |
| | 193 | login_form_url = self._get_full_path(login_form_url, environ) |
| 192 | 194 | headers = [ ('Location', login_form_url) ] |
| 193 | 195 | cookies = [(h,v) for (h,v) in app_headers if h.lower() == 'set-cookie'] |
| 194 | 196 | headers = headers + forget_headers + cookies |
| 195 | 197 | return HTTPFound(headers=headers) |
| 196 | 198 | |
| | 199 | def _get_full_path(self, path, environ): |
| | 200 | """ |
| | 201 | Return the full path to ``path`` by prepending the SCRIPT_PATH. |
| | 202 | |
| | 203 | If ``path`` is a URL, do nothing. |
| | 204 | |
| | 205 | """ |
| | 206 | if path.startswith('/'): |
| | 207 | path = environ.get('SCRIPT_PATH', '') + path |
| | 208 | return path |
| | 209 | |
| 197 | 210 | def make_plugin(login_form_qs='__do_login', rememberer_name=None, |
| 198 | 211 | form=None): |
| 199 | 212 | if rememberer_name is None: |
-
|
|
|
|
| 21 | 21 | authenticators=None, |
| 22 | 22 | challengers=None, |
| 23 | 23 | classifier=None, |
| 24 | | mdproviders=None, |
| | 24 | mdproviders=None, |
| 25 | 25 | challenge_decider=None, |
| 26 | 26 | log_stream=None, |
| 27 | 27 | log_level=None, |
| … |
… |
|
| 53 | 53 | log_stream, |
| 54 | 54 | log_level=logging.DEBUG) |
| 55 | 55 | return mw |
| 56 | | |
| | 56 | |
| 57 | 57 | def test_accepts_logger(self): |
| 58 | 58 | import logging |
| 59 | 59 | logger = logging.Logger('something') |
| … |
… |
|
| 437 | 437 | self.assertEqual(environ['challenged'], app2) |
| 438 | 438 | self.assertEqual(identifier.forgotten, identity) |
| 439 | 439 | |
| 440 | | def test_add_metadata(self): |
| | 440 | def test_add_metadata(self): |
| 441 | 441 | environ = self._makeEnviron() |
| 442 | 442 | plugin1 = DummyMDProvider({'foo':'bar'}) |
| 443 | 443 | plugin2 = DummyMDProvider({'fuz':'baz'}) |
| … |
… |
|
| 449 | 449 | self.assertEqual(identity['foo'], 'bar') |
| 450 | 450 | self.assertEqual(identity['fuz'], 'baz') |
| 451 | 451 | |
| 452 | | def test_add_metadata_w_classification(self): |
| | 452 | def test_add_metadata_w_classification(self): |
| 453 | 453 | environ = self._makeEnviron() |
| 454 | 454 | plugin1 = DummyMDProvider({'foo':'bar'}) |
| 455 | 455 | plugin2 = DummyMDProvider({'fuz':'baz'}) |
| … |
… |
|
| 461 | 461 | identity = {} |
| 462 | 462 | mw.add_metadata(environ, classification, identity) |
| 463 | 463 | self.assertEqual(identity['foo'], 'bar') |
| 464 | | self.assertEqual(identity.get('fuz'), None) |
| | 464 | self.assertEqual(identity.get('fuz'), None) |
| 465 | 465 | |
| 466 | 466 | def test_call_remoteuser_already_set(self): |
| 467 | 467 | environ = self._makeEnviron({'REMOTE_USER':'admin'}) |
| … |
… |
|
| 504 | 504 | self.assertEqual(result, ['body']) |
| 505 | 505 | self.assertEqual(start_response.status, '200 OK') |
| 506 | 506 | self.assertEqual(start_response.headers, headers) |
| 507 | | |
| | 507 | |
| 508 | 508 | def test_call_401_no_identifiers(self): |
| 509 | 509 | environ = self._makeEnviron() |
| 510 | 510 | headers = [('a', '1')] |
| … |
… |
|
| 724 | 724 | self.assertEqual(wrapper.start_response, None) |
| 725 | 725 | self.assertEqual(wrapper.headers, []) |
| 726 | 726 | self.failUnless(wrapper.buffer) |
| 727 | | |
| | 727 | |
| 728 | 728 | def test_finish_response(self): |
| 729 | 729 | statuses = [] |
| 730 | 730 | headerses = [] |
| … |
… |
|
| 736 | 736 | def close(): |
| 737 | 737 | closededs.append(True) |
| 738 | 738 | write.close = close |
| 739 | | |
| | 739 | |
| 740 | 740 | def start_response(status, headers, exc_info=None): |
| 741 | 741 | statuses.append(status) |
| 742 | 742 | headerses.append(headers) |
| 743 | 743 | return write |
| 744 | | |
| | 744 | |
| 745 | 745 | wrapper = self._makeOne(start_response) |
| 746 | 746 | wrapper.status = '401 Unauthorized' |
| 747 | 747 | wrapper.headers = [('a', '1')] |
| … |
… |
|
| 782 | 782 | items.append(item) |
| 783 | 783 | response = ''.join(items) |
| 784 | 784 | self.failUnless(response.startswith('401 Unauthorized')) |
| 785 | | |
| | 785 | |
| 786 | 786 | def test_identify_noauthinfo(self): |
| 787 | 787 | plugin = self._makeOne('realm') |
| 788 | 788 | environ = self._makeEnviron() |
| … |
… |
|
| 836 | 836 | forget = plugin._get_wwwauth() |
| 837 | 837 | result = plugin.challenge(environ, '401 Unauthorized', [], forget) |
| 838 | 838 | self.assertEqual(result.headers, forget) |
| 839 | | |
| | 839 | |
| 840 | 840 | def test_challenge_forgetheaders_omits(self): |
| 841 | 841 | plugin = self._makeOne('realm') |
| 842 | 842 | creds = {'login':'foo', 'password':'password'} |
| … |
… |
|
| 874 | 874 | creds = {} |
| 875 | 875 | result = plugin.authenticate(environ, creds) |
| 876 | 876 | self.assertEqual(result, None) |
| 877 | | |
| | 877 | |
| 878 | 878 | def test_authenticate_nolines(self): |
| 879 | 879 | from StringIO import StringIO |
| 880 | 880 | io = StringIO() |
| … |
… |
|
| 883 | 883 | creds = {'login':'chrism', 'password':'pass'} |
| 884 | 884 | result = plugin.authenticate(environ, creds) |
| 885 | 885 | self.assertEqual(result, None) |
| 886 | | |
| | 886 | |
| 887 | 887 | def test_authenticate_nousermatch(self): |
| 888 | 888 | from StringIO import StringIO |
| 889 | 889 | io = StringIO('nobody:foo') |
| … |
… |
|
| 987 | 987 | environ = self._makeEnviron() |
| 988 | 988 | result = plugin.identify(environ) |
| 989 | 989 | self.assertEqual(result, None) |
| 990 | | |
| | 990 | |
| 991 | 991 | def test_identify_badcookies(self): |
| 992 | 992 | plugin = self._makeOne('oatmeal') |
| 993 | 993 | environ = self._makeEnviron({'HTTP_COOKIE':'oatmeal=a'}) |
| … |
… |
|
| 1072 | 1072 | extra['QUERY_STRING'] = '__do_login=true' |
| 1073 | 1073 | environ = self._makeEnviron(extra) |
| 1074 | 1074 | return environ |
| 1075 | | |
| | 1075 | |
| 1076 | 1076 | def test_implements(self): |
| 1077 | 1077 | from zope.interface.verify import verifyClass |
| 1078 | 1078 | from repoze.who.interfaces import IIdentifier |
| … |
… |
|
| 1086 | 1086 | environ = self._makeFormEnviron() |
| 1087 | 1087 | result = plugin.identify(environ) |
| 1088 | 1088 | self.assertEqual(result, None) |
| 1089 | | |
| | 1089 | |
| 1090 | 1090 | def test_identify_qs_no_values(self): |
| 1091 | 1091 | plugin = self._makeOne() |
| 1092 | 1092 | environ = self._makeFormEnviron(do_login=True) |
| … |
… |
|
| 1098 | 1098 | environ = self._makeFormEnviron(do_login=True, login='chris') |
| 1099 | 1099 | result = plugin.identify(environ) |
| 1100 | 1100 | self.assertEqual(result, None) |
| 1101 | | |
| | 1101 | |
| 1102 | 1102 | def test_identify_nopassword(self): |
| 1103 | 1103 | plugin = self._makeOne() |
| 1104 | 1104 | environ = self._makeFormEnviron(do_login=True, password='password') |
| … |
… |
|
| 1217 | 1217 | return plugin |
| 1218 | 1218 | |
| 1219 | 1219 | def _makeFormEnviron(self, login=None, password=None, came_from=None, |
| 1220 | | path_info='/', identifier=None): |
| | 1220 | path_info='/', identifier=None, script_path=''): |
| 1221 | 1221 | from StringIO import StringIO |
| 1222 | 1222 | fields = [] |
| 1223 | 1223 | if login: |
| … |
… |
|
| 1240 | 1240 | 'repoze.who.plugins': {'cookie':identifier}, |
| 1241 | 1241 | 'QUERY_STRING':'default=1', |
| 1242 | 1242 | 'PATH_INFO':path_info, |
| | 1243 | 'SCRIPT_PATH':script_path |
| 1243 | 1244 | } |
| 1244 | 1245 | environ = self._makeEnviron(extra) |
| 1245 | 1246 | return environ |
| 1246 | | |
| | 1247 | |
| 1247 | 1248 | def test_implements(self): |
| 1248 | 1249 | from zope.interface.verify import verifyClass |
| 1249 | 1250 | from repoze.who.interfaces import IIdentifier |
| … |
… |
|
| 1258 | 1259 | result = plugin.identify(environ) |
| 1259 | 1260 | self.assertEqual(result, None) |
| 1260 | 1261 | self.failIf(environ.get('repoze.who.application')) |
| 1261 | | |
| | 1262 | |
| 1262 | 1263 | def test_identify_via_login_handler(self): |
| 1263 | 1264 | plugin = self._makeOne() |
| 1264 | 1265 | environ = self._makeFormEnviron(path_info='/login_handler', |
| … |
… |
|
| 1315 | 1316 | self.assertEqual(value, 'http://foo.bar') |
| 1316 | 1317 | self.assertEqual(app.code, 302) |
| 1317 | 1318 | |
| | 1319 | def test_identify_via_login_handler_no_came_from_no_referer_spath(self): |
| | 1320 | plugin = self._makeOne() |
| | 1321 | environ = self._makeFormEnviron(path_info='/login_handler', |
| | 1322 | script_path='/my-app', |
| | 1323 | login='chris', |
| | 1324 | password='password') |
| | 1325 | plugin.identify(environ) |
| | 1326 | app = environ['repoze.who.application'] |
| | 1327 | self.assertEqual(app.location(), '/my-app') |
| | 1328 | |
| 1318 | 1329 | def test_identify_via_logout_handler(self): |
| 1319 | 1330 | plugin = self._makeOne() |
| 1320 | 1331 | environ = self._makeFormEnviron(path_info='/logout_handler', |
| … |
… |
|
| 1340 | 1351 | self.assertEqual(app.code, 401) |
| 1341 | 1352 | self.assertEqual(environ['came_from'], '/') |
| 1342 | 1353 | |
| | 1354 | def test_identify_via_logout_handler_no_came_from_no_referer_spath(self): |
| | 1355 | plugin = self._makeOne() |
| | 1356 | environ = self._makeFormEnviron(path_info='/logout_handler', |
| | 1357 | script_path='/my-app', |
| | 1358 | login='chris', |
| | 1359 | password='password') |
| | 1360 | plugin.identify(environ) |
| | 1361 | app = environ['repoze.who.application'] |
| | 1362 | self.assertEqual(environ['came_from'], '/my-app') |
| | 1363 | |
| 1343 | 1364 | def test_identify_via_logout_handler_no_came_from(self): |
| 1344 | 1365 | plugin = self._makeOne() |
| 1345 | 1366 | environ = self._makeFormEnviron(path_info='/logout_handler', |
| … |
… |
|
| 1506 | 1527 | self.assertEqual(sr.headers[2][0], 'set-cookie') |
| 1507 | 1528 | self.assertEqual(sr.headers[2][1], 'b') |
| 1508 | 1529 | |
| | 1530 | def test_challenge_with_non_root_script_path(self): |
| | 1531 | """The script path must be taken into account while redirecting.""" |
| | 1532 | plugin = self._makeOne(login_form_url='/login') |
| | 1533 | environ = self._makeFormEnviron(script_path='/app', |
| | 1534 | path_info='/admin') |
| | 1535 | came_from = 'http://www.example.com/app/admin?default=1' |
| | 1536 | environ['came_from'] = came_from |
| | 1537 | app = plugin.challenge(environ, '401 Unauthorized', [('app', '1')], |
| | 1538 | [('forget', '1')]) |
| | 1539 | from urllib import quote |
| | 1540 | login_url = '/app/login?came_from=%s' % quote(came_from, '') |
| | 1541 | self.assertEqual(app.location(), login_url) |
| | 1542 | |
| 1509 | 1543 | class TestAuthTktCookiePlugin(Base): |
| 1510 | 1544 | def _getTargetClass(self): |
| 1511 | 1545 | from repoze.who.plugins.auth_tkt import AuthTktCookiePlugin |
| … |
… |
|
| 1546 | 1580 | environ = self._makeEnviron() |
| 1547 | 1581 | result = plugin.identify(environ) |
| 1548 | 1582 | self.assertEqual(result, None) |
| 1549 | | |
| | 1583 | |
| 1550 | 1584 | def test_identify_good_cookie_include_ip(self): |
| 1551 | 1585 | plugin = self._makeOne('secret', include_ip=True) |
| 1552 | 1586 | val = self._makeTicket(remote_addr='1.1.1.1') |
| … |
… |
|
| 1608 | 1642 | environ = self._makeEnviron({'HTTP_COOKIE':'auth_tkt=bogus'}) |
| 1609 | 1643 | result = plugin.identify(environ) |
| 1610 | 1644 | self.assertEqual(result, None) |
| 1611 | | |
| | 1645 | |
| 1612 | 1646 | def test_remember_creds_same(self): |
| 1613 | 1647 | plugin = self._makeOne('secret') |
| 1614 | 1648 | val = self._makeTicket(userid='userid') |
| … |
… |
|
| 1642 | 1676 | new_val = self._makeTicket(userid='1', userdata='userid_type:int') |
| 1643 | 1677 | result = plugin.remember(environ, {'repoze.who.userid':1, |
| 1644 | 1678 | 'userdata':''}) |
| 1645 | | |
| | 1679 | |
| 1646 | 1680 | self.assertEqual(len(result), 3) |
| 1647 | 1681 | self.assertEqual(result[0], |
| 1648 | 1682 | ('Set-Cookie', |
| … |
… |
|
| 1717 | 1751 | environ = self._makeEnviron({'HTTP_USER_AGENT':'WebDrive'}) |
| 1718 | 1752 | result = classifier(environ) |
| 1719 | 1753 | self.assertEqual(result, 'dav') |
| 1720 | | |
| | 1754 | |
| 1721 | 1755 | def test_classify_xmlpost(self): |
| 1722 | 1756 | classifier = self._getFUT() |
| 1723 | 1757 | environ = self._makeEnviron({'CONTENT_TYPE':'text/xml', |
| … |
… |
|
| 1742 | 1776 | iface_reg, name_reg = fn([], [], [], []) |
| 1743 | 1777 | self.assertEqual(iface_reg, {}) |
| 1744 | 1778 | self.assertEqual(name_reg, {}) |
| 1745 | | |
| | 1779 | |
| 1746 | 1780 | def test_brokenimpl(self): |
| 1747 | 1781 | fn = self._getFUT() |
| 1748 | 1782 | self.assertRaises(ValueError, fn, [(None, DummyApp())], [], [], []) |
| … |
… |
|
| 2240 | 2274 | |
| 2241 | 2275 | IDENTIFIERS_ONLY = """\ |
| 2242 | 2276 | [identifiers] |
| 2243 | | plugins = |
| | 2277 | plugins = |
| 2244 | 2278 | repoze.who.tests:DummyPlugin;klass1 |
| 2245 | 2279 | repoze.who.tests:DummyPlugin |
| 2246 | 2280 | """ |
| 2247 | 2281 | |
| 2248 | 2282 | IDENTIFIERS_WITH_PLUGINS = """\ |
| 2249 | 2283 | [identifiers] |
| 2250 | | plugins = |
| | 2284 | plugins = |
| 2251 | 2285 | foo;klass1 |
| 2252 | 2286 | bar |
| 2253 | 2287 | |
| … |
… |
|
| 2260 | 2294 | |
| 2261 | 2295 | AUTHENTICATORS_ONLY = """\ |
| 2262 | 2296 | [authenticators] |
| 2263 | | plugins = |
| | 2297 | plugins = |
| 2264 | 2298 | repoze.who.tests:DummyPlugin;klass1 |
| 2265 | 2299 | repoze.who.tests:DummyPlugin |
| 2266 | 2300 | """ |
| 2267 | 2301 | |
| 2268 | 2302 | AUTHENTICATORS_WITH_PLUGINS = """\ |
| 2269 | 2303 | [authenticators] |
| 2270 | | plugins = |
| | 2304 | plugins = |
| 2271 | 2305 | foo;klass1 |
| 2272 | 2306 | bar |
| 2273 | 2307 | |
| … |
… |
|
| 2280 | 2314 | |
| 2281 | 2315 | CHALLENGERS_ONLY = """\ |
| 2282 | 2316 | [challengers] |
| 2283 | | plugins = |
| | 2317 | plugins = |
| 2284 | 2318 | repoze.who.tests:DummyPlugin;klass1 |
| 2285 | 2319 | repoze.who.tests:DummyPlugin |
| 2286 | 2320 | """ |
| 2287 | 2321 | |
| 2288 | 2322 | CHALLENGERS_WITH_PLUGINS = """\ |
| 2289 | 2323 | [challengers] |
| 2290 | | plugins = |
| | 2324 | plugins = |
| 2291 | 2325 | foo;klass1 |
| 2292 | 2326 | bar |
| 2293 | 2327 | |
| … |
… |
|
| 2300 | 2334 | |
| 2301 | 2335 | MDPROVIDERS_ONLY = """\ |
| 2302 | 2336 | [mdproviders] |
| 2303 | | plugins = |
| | 2337 | plugins = |
| 2304 | 2338 | repoze.who.tests:DummyPlugin;klass1 |
| 2305 | 2339 | repoze.who.tests:DummyPlugin |
| 2306 | 2340 | """ |
| 2307 | 2341 | |
| 2308 | 2342 | MDPROVIDERS_WITH_PLUGINS = """\ |
| 2309 | 2343 | [mdproviders] |
| 2310 | | plugins = |
| | 2344 | plugins = |
| 2311 | 2345 | foo;klass1 |
| 2312 | 2346 | bar |
| 2313 | 2347 | |
| … |
… |
|
| 2383 | 2417 | challenge_decider = repoze.who.classifiers:default_challenge_decider |
| 2384 | 2418 | |
| 2385 | 2419 | [identifiers] |
| 2386 | | plugins = |
| | 2420 | plugins = |
| 2387 | 2421 | form;browser |
| 2388 | 2422 | auth_tkt |
| 2389 | 2423 | basicauth |
| … |
… |
|
| 2606 | 2640 | environ['repoze.who.identity']['password'] = 'schooled' |
| 2607 | 2641 | start_response(self.status, self.headers) |
| 2608 | 2642 | return ['body'] |
| 2609 | | |
| | 2643 | |
| 2610 | 2644 | class DummyRequestClassifier: |
| 2611 | 2645 | def __call__(self, environ): |
| 2612 | 2646 | return 'browser' |
| … |
… |
|
| 2648 | 2682 | class DummyAuthenticator: |
| 2649 | 2683 | def __init__(self, userid=None): |
| 2650 | 2684 | self.userid = userid |
| 2651 | | |
| | 2685 | |
| 2652 | 2686 | def authenticate(self, environ, credentials): |
| 2653 | 2687 | if self.userid is None: |
| 2654 | 2688 | return credentials['login'] |
| … |
… |
|
| 2672 | 2706 | class DummyMDProvider: |
| 2673 | 2707 | def __init__(self, metadata=None): |
| 2674 | 2708 | self._metadata = metadata |
| 2675 | | |
| | 2709 | |
| 2676 | 2710 | def add_metadata(self, environ, identity): |
| 2677 | 2711 | return identity.update(self._metadata) |
| 2678 | 2712 | |
Download in other formats: