From 73f9abd6a16dec2b89c2f5896107600dee3f5681 Mon Sep 17 00:00:00 2001 From: lipenghui Date: Fri, 20 Nov 2020 19:21:13 +0800 Subject: [PATCH] add oauth2 wrapper for python (#7813) (#8644) Motivation There was already cpp oauth2 client provided, this or tries to provide a wrapper around it for Python client. Modifications add wrapper on cpp to support python client oauth2. (cherry picked from commit 58704f9e82750bf2613131eccbe9fca4c530ec3c) Co-authored-by: Jia Zhai --- .../include/pulsar/Authentication.h | 1 + .../include/pulsar/c/authentication.h | 2 ++ pulsar-client-cpp/lib/c/c_Authentication.cc | 6 ++++++ pulsar-client-cpp/python/pulsar/__init__.py | 16 +++++++++++++++- pulsar-client-cpp/python/src/authentication.cc | 11 +++++++++++ 5 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pulsar-client-cpp/include/pulsar/Authentication.h b/pulsar-client-cpp/include/pulsar/Authentication.h index 57a3e705a4836..b02d72a79681b 100644 --- a/pulsar-client-cpp/include/pulsar/Authentication.h +++ b/pulsar-client-cpp/include/pulsar/Authentication.h @@ -249,6 +249,7 @@ typedef std::shared_ptr CachedTokenPtr; * "client_secret": "on1uJ...k6F6R", * "audience": "https://broker.example.com" * ``` + * If passed in as std::string, it should be in Json format. */ class PULSAR_PUBLIC AuthOauth2 : public Authentication { public: diff --git a/pulsar-client-cpp/include/pulsar/c/authentication.h b/pulsar-client-cpp/include/pulsar/c/authentication.h index 9d540d5e5a5ec..20247263031b7 100644 --- a/pulsar-client-cpp/include/pulsar/c/authentication.h +++ b/pulsar-client-cpp/include/pulsar/c/authentication.h @@ -41,6 +41,8 @@ PULSAR_PUBLIC pulsar_authentication_t *pulsar_authentication_token_create_with_s PULSAR_PUBLIC pulsar_authentication_t *pulsar_authentication_athenz_create(const char *authParamsString); +PULSAR_PUBLIC pulsar_authentication_t *pulsar_authentication_oauth2_create(const char *authParamsString); + PULSAR_PUBLIC void pulsar_authentication_free(pulsar_authentication_t *authentication); #ifdef __cplusplus diff --git a/pulsar-client-cpp/lib/c/c_Authentication.cc b/pulsar-client-cpp/lib/c/c_Authentication.cc index 0485a57ca0d4f..d3a5b19f1d56c 100644 --- a/pulsar-client-cpp/lib/c/c_Authentication.cc +++ b/pulsar-client-cpp/lib/c/c_Authentication.cc @@ -65,4 +65,10 @@ pulsar_authentication_t *pulsar_authentication_token_create_with_supplier(token_ pulsar_authentication_t *authentication = new pulsar_authentication_t; authentication->auth = pulsar::AuthToken::create(std::bind(&tokenSupplierWrapper, tokenSupplier, ctx)); return authentication; +} + +pulsar_authentication_t *pulsar_authentication_oauth2_create(const char *authParamsString) { + pulsar_authentication_t *authentication = new pulsar_authentication_t; + authentication->auth = pulsar::AuthOauth2::create(authParamsString); + return authentication; } \ No newline at end of file diff --git a/pulsar-client-cpp/python/pulsar/__init__.py b/pulsar-client-cpp/python/pulsar/__init__.py index 7a98315d6a9ab..12188b522cc33 100644 --- a/pulsar-client-cpp/python/pulsar/__init__.py +++ b/pulsar-client-cpp/python/pulsar/__init__.py @@ -325,6 +325,20 @@ def __init__(self, auth_params_string): _check_type(str, auth_params_string, 'auth_params_string') self.auth = _pulsar.AuthenticationAthenz(auth_params_string) +class AuthenticationOauth2(Authentication): + """ + Oauth2 Authentication implementation + """ + def __init__(self, auth_params_string): + """ + Create the Oauth2 authentication provider instance. + + **Args** + + * `auth_params_string`: JSON encoded configuration for Oauth2 client + """ + _check_type(str, auth_params_string, 'auth_params_string') + self.auth = _pulsar.AuthenticationOauth2(auth_params_string) class Client: """ @@ -358,7 +372,7 @@ def __init__(self, service_url, * `authentication`: Set the authentication provider to be used with the broker. For example: - `AuthenticationTls` or `AuthenticationAthenz` + `AuthenticationTls`, AuthenticaionToken, `AuthenticationAthenz`or `AuthenticationOauth2` * `operation_timeout_seconds`: Set timeout on client operations (subscribe, create producer, close, unsubscribe). diff --git a/pulsar-client-cpp/python/src/authentication.cc b/pulsar-client-cpp/python/src/authentication.cc index 9547c99971a62..00f5848cc9e2b 100644 --- a/pulsar-client-cpp/python/src/authentication.cc +++ b/pulsar-client-cpp/python/src/authentication.cc @@ -92,6 +92,13 @@ struct AuthenticationAthenzWrapper : public AuthenticationWrapper { } }; +struct AuthenticationOauth2Wrapper : public AuthenticationWrapper { + AuthenticationOauth2Wrapper(const std::string& authParamsString) : + AuthenticationWrapper() { + this->auth = AuthOauth2::create(authParamsString); + } +}; + void export_authentication() { using namespace boost::python; @@ -109,4 +116,8 @@ void export_authentication() { class_ >("AuthenticationAthenz", init()) ; + + class_ >("AuthenticationOauth2", + init()) + ; }