Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pulsar-client-cpp/lib/c/c_Authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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;
}
17 changes: 17 additions & 0 deletions pulsar-client-cpp/python/pulsar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions pulsar-client-cpp/python/pulsar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
CompressionType,
ConsumerType,
PartitionsRoutingMode,
AuthenticationBasic,
AuthenticationTLS,
Authentication,
AuthenticationToken,
Expand Down Expand Up @@ -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()
10 changes: 10 additions & 0 deletions pulsar-client-cpp/python/src/authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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&>());
}