GitHub user tncyclk edited a discussion: Support for passing custom provider
auth credentials from Python client
I want to write a Python client for a pulsar server written with BasicAuth mean
custom auth (authenticationMethods=customAuth) that authenticates with userId
and password params. I wrote the following code for this, but it did not work.
```
import pulsar
from pulsar import Authentication
import logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s -
%(message)s')
class CustomAuthProvider(Authentication):
def __init__(self, username, password):
self.userId = username
self.password = password
self.auth_type = "customAuth"
self.auth = self.get_auth_data()
def get_auth_data(self):
return pulsar.BasicAuthAuthentication(self.userId, self.password)
def auth_method_name(self):
return self.auth_type
class Messenger():
def __init__(self) -> None:
self.pulsar_server_url = "pulsar://192.168.56.1:6650"
self.client = None
self.username = "user"
self.password = "95ce64b2-69a9-3c4e-8e2e-ab244acfdd99"
def connect_to_server(self):
auth_provider = CustomAuthProvider(self.username, self.password)
try:
logging.info("Trying to connect to Pulsar...")
self.client = pulsar.Client(
self.pulsar_server_url,
authentication=auth_provider
)
logging.info("Connected to Pulsar server!")
except Exception as e:
logging.error(f"Failed to connect to Pulsar: {str(e)}")
if self.client:
logging.debug(f"Client object: {self.client}")
if __name__ == '__main__':
app = Messenger()
app.connect_to_server()
```
Also, the broker conf I made for custom CustomAuthProvider is as follows.
```
### --- Authentication --- ###
authenticationEnabled=true
# Specify the authentication providers to use
authenticationProviders=com.liderahenkpulsar.auth.BasicAuthProvider
# Define the authentication method name that matches your
AuthenticationBasicAuth class
# This should match the value returned by getAuthMethodName()
authenticationMethods=customAuth
# Authentication settings of the broker itself. Used when the broker connects
to other brokers,
# either in same or other clusters
brokerClientAuthenticationPlugin=com.liderahenkpulsar.auth.AuthenticationBasicAuth
brokerClientAuthenticationParameters=
```
Please give support in Python client for sending basic auth credentials while
connecting to server with userId and password.
GitHub link: https://github.com/apache/pulsar/discussions/23287
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]