Skip to content

Commit

Permalink
add oauth2 wrapper for python (apache#7813)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jiazhai authored Aug 17, 2020
1 parent ff1780c commit 58704f9
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions pulsar-client-cpp/include/pulsar/Authentication.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ typedef std::shared_ptr<CachedToken> 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:
Expand Down
2 changes: 2 additions & 0 deletions pulsar-client-cpp/include/pulsar/c/authentication.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pulsar-client-cpp/lib/c/c_Authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
16 changes: 15 additions & 1 deletion pulsar-client-cpp/python/pulsar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,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:
"""
Expand Down Expand Up @@ -352,7 +366,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).
Expand Down
11 changes: 11 additions & 0 deletions pulsar-client-cpp/python/src/authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -109,4 +116,8 @@ void export_authentication() {
class_<AuthenticationAthenzWrapper, bases<AuthenticationWrapper> >("AuthenticationAthenz",
init<const std::string&>())
;

class_<AuthenticationOauth2Wrapper, bases<AuthenticationWrapper> >("AuthenticationOauth2",
init<const std::string&>())
;
}

0 comments on commit 58704f9

Please sign in to comment.