You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by pe...@apache.org on 2022/09/07 00:42:19 UTC

[pulsar] branch master updated: [feat][python] Add basic authentication (#17482)

This is an automated email from the ASF dual-hosted git repository.

penghui pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git


The following commit(s) were added to refs/heads/master by this push:
     new eae90f6a64f [feat][python] Add basic authentication (#17482)
eae90f6a64f is described below

commit eae90f6a64f68a6294c742cd864493856a3dded7
Author: Zixuan Liu <no...@gmail.com>
AuthorDate: Wed Sep 7 08:42:09 2022 +0800

    [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 +++++++++++++++++++++++
 pulsar-client-cpp/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 d3a5b19f1d5..8384fac5f6b 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 fb1086a4ce7..56f71239a36 100644
--- a/pulsar-client-cpp/python/pulsar/__init__.py
+++ b/pulsar-client-cpp/python/pulsar/__init__.py
@@ -344,6 +344,23 @@ class AuthenticationOauth2(Authentication):
         _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 f15fafedc10..582514fb923 100755
--- a/pulsar-client-cpp/python/pulsar_test.py
+++ b/pulsar-client-cpp/python/pulsar_test.py
@@ -31,6 +31,7 @@ from pulsar import (
     CompressionType,
     ConsumerType,
     PartitionsRoutingMode,
+    AuthenticationBasic,
     AuthenticationTLS,
     Authentication,
     AuthenticationToken,
@@ -1282,6 +1283,28 @@ class PulsarTest(TestCase):
         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 920a7174b47..791749819ba 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_<AuthenticationOauth2Wrapper, bases<AuthenticationWrapper> >("AuthenticationOauth2",
                                                                        init<const std::string&>());
+
+    class_<AuthenticationBasicWrapper, bases<AuthenticationWrapper> >(
+        "AuthenticationBasic", init<const std::string&, const std::string&>());
 }