def test_auth_with_failed_password_lookup(self): policy = DigestAuthenticationPolicy("test", get_pwdhash=lambda u, r: None) request = make_request() params = get_challenge(policy, request) build_response(params, request, "tester", "testing") self.assertEquals(policy.unauthenticated_userid(request), "tester") self.assertEquals(policy.authenticated_userid(request), None)
def test_auth_with_different_realm(self): policy = DigestAuthenticationPolicy("test", get_password=lambda u: "testing") request = make_request() params = get_challenge(policy, request) params["realm"] = "other-realm" build_response(params, request, "tester", "testing") self.assertEquals(policy.authenticated_userid(request), None)
def test_auth_good_get_with_vars(self): pwdhash = calculate_pwdhash("tester", "testing", "test") policy = DigestAuthenticationPolicy("test", get_pwdhash=lambda u, r: pwdhash) request = make_request(REQUEST_METHOD="GET", PATH_INFO="/hi?who=me") params = get_challenge(policy, request) build_response(params, request, "tester", "testing") self.assertNotEquals(policy.authenticated_userid(request), None)
def test_auth_good_post(self): policy = DigestAuthenticationPolicy("test", get_password=lambda u: "testing") request = make_request(REQUEST_METHOD="POST", PATH_INFO="/do/stuff") params = get_challenge(policy, request) build_response(params, request, "tester", "testing") self.assertNotEquals(policy.authenticated_userid(request), None)
def test_auth_with_bad_digest_response(self): policy = DigestAuthenticationPolicy("test", get_password=lambda u: "testing") request = make_request() params = get_challenge(policy, request) params = build_response(params, request, "tester", "testing") authz = request.environ["HTTP_AUTHORIZATION"] authz = authz.replace(params["response"], "WRONG") request.environ["HTTP_AUTHORIZATION"] = authz params["response"] += "WRONG" self.assertEquals(policy.authenticated_userid(request), None)
def test_rfc2617_example(self): password = "******" params = { "username": "******", "realm": "*****@*****.**", "nonce": "dcd98b7102dd2f0e8b11d0f600bfb0c093", "uri": "/dir/index.html", "qop": "auth", "nc": "00000001", "cnonce": "0a4f113b", "opaque": "5ccc069c403ebaf9f0171e9517f40e41", } policy = DigestAuthenticationPolicy("*****@*****.**", EasyNonceManager(), get_password=lambda u: password) # Calculate the response according to the RFC example parameters. request = make_request(REQUEST_METHOD="GET", PATH_INFO="/dir/index.html") resp = calculate_digest_response(params, request, password=password) # Check that it's as expected from the RFC example section. self.assertEquals(resp, "6629fae49393a05397450978507c4ef1") # Check that we can auth using it. params["response"] = resp set_authz_header(request, params) self.assertEquals(policy.unauthenticated_userid(request), "Mufasa") self.assertEquals(policy.authenticated_userid(request), "Mufasa")
def test_rfc2617_example(self): password = "******" params = {"username": "******", "realm": "*****@*****.**", "nonce": "dcd98b7102dd2f0e8b11d0f600bfb0c093", "uri": "/dir/index.html", "qop": "auth", "nc": "00000001", "cnonce": "0a4f113b", "opaque": "5ccc069c403ebaf9f0171e9517f40e41"} policy = DigestAuthenticationPolicy("*****@*****.**", EasyNonceManager(), get_password=lambda u: password) # Calculate the response according to the RFC example parameters. request = make_request(REQUEST_METHOD="GET", PATH_INFO="/dir/index.html") resp = calculate_digest_response(params, request, password=password) # Check that it's as expected from the RFC example section. self.assertEquals(resp, "6629fae49393a05397450978507c4ef1") # Check that we can auth using it. params["response"] = resp set_authz_header(request, params) self.assertEquals(policy.unauthenticated_userid(request), "Mufasa") self.assertEquals(policy.authenticated_userid(request), "Mufasa")
def test_auth_with_no_password_callbacks(self): policy = DigestAuthenticationPolicy("test") request = make_request() params = get_challenge(policy, request) build_response(params, request, "tester", "testing") self.assertEquals(policy.authenticated_userid(request), None)
def test_auth_with_no_identity(self): policy = DigestAuthenticationPolicy("test", get_password=lambda u: "testing") request = make_request() self.assertEquals(policy.authenticated_userid(request), None)