From eae90f6a64f68a6294c742cd864493856a3dded7 Mon Sep 17 00:00:00 2001 From: Zixuan Liu Date: Wed, 7 Sep 2022 08:42:09 +0800 Subject: [PATCH] [feat][python] Add basic authentication (#17482) --- pulsar-client-cpp/lib/c/c_Authentication.cc | 8 ++++++- pulsar-client-cpp/python/pulsar/__init__.py | 17 ++++++++++++++ pulsar-client-cpp/python/pulsar_test.py | 23 +++++++++++++++++++ .../python/src/authentication.cc | 10 ++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/pulsar-client-cpp/lib/c/c_Authentication.cc b/pulsar-client-cpp/lib/c/c_Authentication.cc index d3a5b19f1d56c..8384fac5f6b3d 100644 --- a/pulsar-client-cpp/lib/c/c_Authentication.cc +++ b/pulsar-client-cpp/lib/c/c_Authentication.cc @@ -71,4 +71,10 @@ pulsar_authentication_t *pulsar_authentication_oauth2_create(const char *authPar pulsar_authentication_t *authentication = new pulsar_authentication_t; authentication->auth = pulsar::AuthOauth2::create(authParamsString); return authentication; -} \ No newline at end of file +} + +pulsar_authentication_t *pulsar_authentication_basic_create(const char *username, const char *password) { + pulsar_authentication_t *authentication = new pulsar_authentication_t; + authentication->auth = pulsar::AuthBasic::create(username, password); + return authentication; +} diff --git a/pulsar-client-cpp/python/pulsar/__init__.py b/pulsar-client-cpp/python/pulsar/__init__.py index fb1086a4ce701..56f71239a3622 100644 --- a/pulsar-client-cpp/python/pulsar/__init__.py +++ b/pulsar-client-cpp/python/pulsar/__init__.py @@ -344,6 +344,23 @@ def __init__(self, auth_params_string): _check_type(str, auth_params_string, 'auth_params_string') self.auth = _pulsar.AuthenticationOauth2(auth_params_string) +class AuthenticationBasic(Authentication): + """ + Basic Authentication implementation + """ + def __init__(self, username, password): + """ + Create the Basic authentication provider instance. + + **Args** + + * `username`: Used to authentication as username + * `password`: Used to authentication as password + """ + _check_type(str, username, 'username') + _check_type(str, password, 'password') + self.auth = _pulsar.AuthenticationBasic(username, password) + class Client: """ The Pulsar client. A single client instance can be used to create producers diff --git a/pulsar-client-cpp/python/pulsar_test.py b/pulsar-client-cpp/python/pulsar_test.py index f15fafedc10ae..582514fb923c9 100755 --- a/pulsar-client-cpp/python/pulsar_test.py +++ b/pulsar-client-cpp/python/pulsar_test.py @@ -31,6 +31,7 @@ CompressionType, ConsumerType, PartitionsRoutingMode, + AuthenticationBasic, AuthenticationTLS, Authentication, AuthenticationToken, @@ -1282,6 +1283,28 @@ def _check_type_error(self, fun): with self.assertRaises(TypeError): fun() + def test_basic_auth(self): + username = "admin" + password = "123456" + client = Client(self.adminUrl, authentication=AuthenticationBasic(username, password)) + + topic = "persistent://private/auth/my-python-topic-basic-auth" + consumer = client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared) + producer = client.create_producer(topic) + producer.send(b"hello") + + msg = consumer.receive(TM) + self.assertTrue(msg) + self.assertEqual(msg.data(), b"hello") + client.close() + + def test_invalid_basic_auth(self): + username = "invalid" + password = "123456" + client = Client(self.adminUrl, authentication=AuthenticationBasic(username, password)) + topic = "persistent://private/auth/my-python-topic-invalid-basic-auth" + with self.assertRaises(pulsar.ConnectError): + client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared) if __name__ == "__main__": main() diff --git a/pulsar-client-cpp/python/src/authentication.cc b/pulsar-client-cpp/python/src/authentication.cc index 920a7174b47bb..791749819ba46 100644 --- a/pulsar-client-cpp/python/src/authentication.cc +++ b/pulsar-client-cpp/python/src/authentication.cc @@ -90,6 +90,13 @@ struct AuthenticationOauth2Wrapper : public AuthenticationWrapper { } }; +struct AuthenticationBasicWrapper : public AuthenticationWrapper { + AuthenticationBasicWrapper(const std::string& username, const std::string& password) + : AuthenticationWrapper() { + this->auth = AuthBasic::create(username, password); + } +}; + void export_authentication() { using namespace boost::python; @@ -106,4 +113,7 @@ void export_authentication() { class_ >("AuthenticationOauth2", init()); + + class_ >( + "AuthenticationBasic", init()); }