Skip to content

Support auth param string for Basic authentication #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 22 additions & 8 deletions pulsar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,32 @@ class AuthenticationBasic(Authentication):
"""
Basic Authentication implementation
"""
def __init__(self, username, password):
def __init__(self, username=None, password=None, auth_params_string=None):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also accept the method argument here, otherwise it might not be obvious that you can pass it in the json string

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agreed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. PTAL again @merlimat @nodece

"""
Create the Basic authentication provider instance.

**Args**
For example, if you want to create a basic authentication instance whose
username is "my-user" and password is "my-pass", there are two ways:

* `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)
```
auth = AuthenticationBasic('my-user', 'my-pass')
auth = AuthenticationBasic(auth_params_string='{"username": "my-user", "password": "my-pass"}')
```

**Args**
* username : str, optional
* password : str, optional
* auth_params_string : str, optional
The JSON presentation of username and password (default is None)
If it's not None, the parameters will be ignored
"""
if auth_params_string is not None:
_check_type(str, auth_params_string, 'auth_params_string')
self.auth = _pulsar.AuthenticationBasic('', '', auth_params_string)
else:
_check_type(str, username, 'username')
_check_type(str, password, 'password')
self.auth = _pulsar.AuthenticationBasic(username, password, '')

class Client:
"""
Expand Down
11 changes: 8 additions & 3 deletions src/authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,14 @@ struct AuthenticationOauth2Wrapper : public AuthenticationWrapper {
};

struct AuthenticationBasicWrapper : public AuthenticationWrapper {
AuthenticationBasicWrapper(const std::string& username, const std::string& password)
AuthenticationBasicWrapper(const std::string& username, const std::string& password,
const std::string& authParamsString)
: AuthenticationWrapper() {
this->auth = AuthBasic::create(username, password);
if (authParamsString.empty()) {
this->auth = AuthBasic::create(username, password);
} else {
this->auth = AuthBasic::create(authParamsString);
}
}
};

Expand All @@ -115,5 +120,5 @@ void export_authentication() {
init<const std::string&>());

class_<AuthenticationBasicWrapper, bases<AuthenticationWrapper> >(
"AuthenticationBasic", init<const std::string&, const std::string&>());
"AuthenticationBasic", init<const std::string&, const std::string&, const std::string&>());
}
23 changes: 18 additions & 5 deletions tests/pulsar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,12 +1301,10 @@ 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))
def _test_basic_auth(self, id, auth):
client = Client(self.adminUrl, authentication=auth)

topic = "persistent://private/auth/my-python-topic-basic-auth"
topic = "persistent://private/auth/my-python-topic-basic-auth-" + str(id)
consumer = client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared)
producer = client.create_producer(topic)
producer.send(b"hello")
Expand All @@ -1316,13 +1314,28 @@ def test_basic_auth(self):
self.assertEqual(msg.data(), b"hello")
client.close()

def test_basic_auth(self):
username = "admin"
password = "123456"
self._test_basic_auth(0, AuthenticationBasic(username, password))
self._test_basic_auth(1, AuthenticationBasic(
auth_params_string='{{"username": "{}","password": "{}"}}'.format(username, password)
))

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)
client = Client(self.adminUrl, authentication=AuthenticationBasic(
auth_params_string='{{"username": "{}","password": "{}"}}'.format(username, password)
))
with self.assertRaises(pulsar.ConnectError):
client.subscribe(topic, "my-sub", consumer_type=ConsumerType.Shared)
with self.assertRaises(RuntimeError):
AuthenticationBasic(auth_params_string='invalid auth params')

if __name__ == "__main__":
main()