Skip to content

Commit ed3698e

Browse files
authored
[feat][python] Add basic authentication (#17482)
1 parent 2041140 commit ed3698e

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

pulsar/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,23 @@ def __init__(self, auth_params_string):
344344
_check_type(str, auth_params_string, 'auth_params_string')
345345
self.auth = _pulsar.AuthenticationOauth2(auth_params_string)
346346

347+
class AuthenticationBasic(Authentication):
348+
"""
349+
Basic Authentication implementation
350+
"""
351+
def __init__(self, username, password):
352+
"""
353+
Create the Basic authentication provider instance.
354+
355+
**Args**
356+
357+
* `username`: Used to authentication as username
358+
* `password`: Used to authentication as password
359+
"""
360+
_check_type(str, username, 'username')
361+
_check_type(str, password, 'password')
362+
self.auth = _pulsar.AuthenticationBasic(username, password)
363+
347364
class Client:
348365
"""
349366
The Pulsar client. A single client instance can be used to create producers

pulsar_test.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
CompressionType,
3232
ConsumerType,
3333
PartitionsRoutingMode,
34+
AuthenticationBasic,
3435
AuthenticationTLS,
3536
Authentication,
3637
AuthenticationToken,
@@ -1282,6 +1283,28 @@ def _check_type_error(self, fun):
12821283
with self.assertRaises(TypeError):
12831284
fun()
12841285

1286+
def test_basic_auth(self):
1287+
username = "admin"
1288+
password = "123456"
1289+
client = Client(self.adminUrl, authentication=AuthenticationBasic(username, password))
1290+
1291+
topic = "persistent://private/auth/my-python-topic-basic-auth"
1292+
consumer = client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared)
1293+
producer = client.create_producer(topic)
1294+
producer.send(b"hello")
1295+
1296+
msg = consumer.receive(TM)
1297+
self.assertTrue(msg)
1298+
self.assertEqual(msg.data(), b"hello")
1299+
client.close()
1300+
1301+
def test_invalid_basic_auth(self):
1302+
username = "invalid"
1303+
password = "123456"
1304+
client = Client(self.adminUrl, authentication=AuthenticationBasic(username, password))
1305+
topic = "persistent://private/auth/my-python-topic-invalid-basic-auth"
1306+
with self.assertRaises(pulsar.ConnectError):
1307+
client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared)
12851308

12861309
if __name__ == "__main__":
12871310
main()

src/authentication.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ struct AuthenticationOauth2Wrapper : public AuthenticationWrapper {
9090
}
9191
};
9292

93+
struct AuthenticationBasicWrapper : public AuthenticationWrapper {
94+
AuthenticationBasicWrapper(const std::string& username, const std::string& password)
95+
: AuthenticationWrapper() {
96+
this->auth = AuthBasic::create(username, password);
97+
}
98+
};
99+
93100
void export_authentication() {
94101
using namespace boost::python;
95102

@@ -106,4 +113,7 @@ void export_authentication() {
106113

107114
class_<AuthenticationOauth2Wrapper, bases<AuthenticationWrapper> >("AuthenticationOauth2",
108115
init<const std::string&>());
116+
117+
class_<AuthenticationBasicWrapper, bases<AuthenticationWrapper> >(
118+
"AuthenticationBasic", init<const std::string&, const std::string&>());
109119
}

0 commit comments

Comments
 (0)