Example #1
0
class DigestAuthenticatorTest(TestCase):

    def setUp(self):
        super(DigestAuthenticatorTest, self).setUp()
        self.uuid4_m = self.mocker.replace("uuid.uuid4")
        self.auth = DigestAuthenticator("jiminy", "cricket")

    def challenge(self, method="Digest", **kwargs):
        kwargs.setdefault("qop", "auth")
        details = ", ".join("%s=%s" % item for item in kwargs.items())
        return ("%s realm=loathing, nonce=blah, %s" % (method, details))

    def default_response(self):
        return ('Digest username="******", realm="loathing", nonce="blah", '
                'uri="http://somewhe.re/", algorithm="MD5", '
                'response="8891952040a96ba62cce585972bd3360", qop="auth", '
                'nc="00000001", cnonce="twiddle"')

    def assert_response(self, challenge, response):
        self.assertEquals(
            self.auth.authenticate("METH", "http://somewhe.re/", challenge),
            response)

    def assert_error(self, challenge, err_type, err_message):
        error = self.assertRaises(
            err_type,
            self.auth.authenticate, "METH", "http://somewhe.re/", challenge)
        self.assertEquals(str(error), err_message)

    def assert_missing_key(self, challenge, key):
        self.mocker.replay()
        message = "Authentication request missing required key: %r" % key
        self.assert_error(challenge, ProviderError, message)

    def test_normal(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.mocker.replay()
        self.assert_response(self.challenge(), self.default_response())

    def test_nc_increments(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.uuid4_m()
        self.mocker.result("twaddle")
        self.mocker.replay()
        self.assert_response(self.challenge(), self.default_response())
        self.assert_response(
            self.challenge(),
            'Digest username="******", realm="loathing", nonce="blah", '
            'uri="http://somewhe.re/", algorithm="MD5", '
            'response="c12e9ec2e0569d896b2f26b91c0e74c3", qop="auth", '
            'nc="00000002", cnonce="twaddle"')

    def test_qop_choices(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.mocker.replay()
        self.assert_response(self.challenge(qop='"auth,auth-int"'),
                             self.default_response())

    def test_specify_algorithm(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.mocker.replay()
        self.assert_response(self.challenge(algorithm="MD5"),
                             self.default_response())

    def test_bad_method(self):
        self.mocker.replay()
        self.assert_error(self.challenge("Masticate"), ProviderError,
                          "Unknown authentication method: Masticate")

    def test_bad_algorithm(self):
        self.mocker.replay()
        self.assert_error(self.challenge(algorithm="ROT13"), ProviderError,
                          "Unsupported digest algorithm: ROT13")

    def test_bad_qop(self):
        self.mocker.replay()
        self.assert_error(self.challenge(qop="auth-int"), ProviderError,
                          "Unsupported quality-of-protection: auth-int")

    def test_missing_keys(self):
        self.assert_missing_key("Digest realm=x, nonce=y", "qop")
        self.assert_missing_key("Digest realm=x, qop=y", "nonce")
        self.assert_missing_key("Digest qop=x, nonce=y", "realm")
Example #2
0
class DigestAuthenticatorTest(TestCase):
    def setUp(self):
        super(DigestAuthenticatorTest, self).setUp()
        self.uuid4_m = self.mocker.replace("uuid.uuid4")
        self.auth = DigestAuthenticator("jiminy", "cricket")

    def challenge(self, method="Digest", **kwargs):
        kwargs.setdefault("qop", "auth")
        details = ", ".join("%s=%s" % item for item in kwargs.items())
        return ("%s realm=loathing, nonce=blah, %s" % (method, details))

    def default_response(self):
        return ('Digest username="******", realm="loathing", nonce="blah", '
                'uri="http://somewhe.re/", algorithm="MD5", '
                'response="8891952040a96ba62cce585972bd3360", qop="auth", '
                'nc="00000001", cnonce="twiddle"')

    def assert_response(self, challenge, response):
        self.assertEquals(
            self.auth.authenticate("METH", "http://somewhe.re/", challenge),
            response)

    def assert_error(self, challenge, err_type, err_message):
        error = self.assertRaises(err_type, self.auth.authenticate, "METH",
                                  "http://somewhe.re/", challenge)
        self.assertEquals(str(error), err_message)

    def assert_missing_key(self, challenge, key):
        self.mocker.replay()
        message = "Authentication request missing required key: %r" % key
        self.assert_error(challenge, ProviderError, message)

    def test_normal(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.mocker.replay()
        self.assert_response(self.challenge(), self.default_response())

    def test_nc_increments(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.uuid4_m()
        self.mocker.result("twaddle")
        self.mocker.replay()
        self.assert_response(self.challenge(), self.default_response())
        self.assert_response(
            self.challenge(),
            'Digest username="******", realm="loathing", nonce="blah", '
            'uri="http://somewhe.re/", algorithm="MD5", '
            'response="c12e9ec2e0569d896b2f26b91c0e74c3", qop="auth", '
            'nc="00000002", cnonce="twaddle"')

    def test_qop_choices(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.mocker.replay()
        self.assert_response(self.challenge(qop='"auth,auth-int"'),
                             self.default_response())

    def test_specify_algorithm(self):
        self.uuid4_m()
        self.mocker.result("twiddle")
        self.mocker.replay()
        self.assert_response(self.challenge(algorithm="MD5"),
                             self.default_response())

    def test_bad_method(self):
        self.mocker.replay()
        self.assert_error(self.challenge("Masticate"), ProviderError,
                          "Unknown authentication method: Masticate")

    def test_bad_algorithm(self):
        self.mocker.replay()
        self.assert_error(self.challenge(algorithm="ROT13"), ProviderError,
                          "Unsupported digest algorithm: ROT13")

    def test_bad_qop(self):
        self.mocker.replay()
        self.assert_error(self.challenge(qop="auth-int"), ProviderError,
                          "Unsupported quality-of-protection: auth-int")

    def test_missing_keys(self):
        self.assert_missing_key("Digest realm=x, nonce=y", "qop")
        self.assert_missing_key("Digest realm=x, qop=y", "nonce")
        self.assert_missing_key("Digest qop=x, nonce=y", "realm")