Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 19 additions & 8 deletions cloud/cpp/ConnectByOAuth2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,32 @@
* specific language governing permissions and limitations
* under the License.
*/
#include <iostream>
#include <pulsar/Authentication.h>
#include <pulsar/Client.h>

using namespace pulsar;

int main() {
ClientConfiguration config;
std::string oauthParams = R"({
// C++ client provides two ways to construct an AuthOauth2 object
// 1. Create by ParamMap (std::map<std::string, std::string>), this way is more simple and efficient
ParamMap params;
params["issuer_url"] = "http://cloud/oauth/token";
params["private_key"] = "/resources/authentication/token/cpp_credentials_file.json";
params["audience"] = "https://cloud.auth0.com/api/v2/";
params["scope"] = "api://pulsar-cluster-1/.default"; // scope is optional

Client client1("pulsar+ssl://streamnative.cloud:6651",
ClientConfiguration().setAuth(AuthOauth2::create(params)));
client1.close();

// 2. Create by JSON string, it will be parsed to a ParamMap internally
std::string paramsJson = R"({
"issuer_url": "https://cloud/oauth/token",
"private_key": "/resources/authentication/token/cpp_credentials_file.json",
"audience": "https://cloud.auth0.com/api/v2/"})";

config.setAuth(pulsar::AuthOauth2::create(params));
"audience": "https://cloud.auth0.com/api/v2/",
"scope": "api://pulsar-cluster-1/.default"})"; // scope is optional

Client client("pulsar+ssl://streamnative.cloud:6651", config);
client.close();
Client client2("pulsar+ssl://streamnative.cloud:6651",
ClientConfiguration().setAuth(AuthOauth2::create(paramsJson)));
client2.close();
}
2 changes: 2 additions & 0 deletions cloud/cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Produce message to and consume message from a Pulsar cluster using [Apache pulsa

- CPP Client: 2.6.1+

Since 2.8.1.10, C++ client has supported configuring `scope` field for OAuth2 authentication.

## Linux

Since 2.1.0 release, Pulsar ships pre-built RPM and Debian packages. You can download and install those packages directly.
Expand Down
18 changes: 18 additions & 0 deletions cloud/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ sudo python setup.py install
Produce message 'message 9 from oauth2 producer' to the pulsar service successfully.
```

> *NOTE*
>
> Since 2.8.1.10, Python client has supported configuring `scope` field for OAuth2 authentication.
>
> For example,
>
> ```shell
> python3 OAuth2Producer.py \
> -su "pulsar+ssl://streamnative.cloud:6651 \
> -t my-topic -n 10 --auth-params '{
> "issuer_url": "https://auth.streamnative.cloud",
> "private_key": "/path/to/private.key",
> "audience": "urn:sn:pulsar:test-organization-name:test-pulsar-instance-name"}',
> "scope": "api://pulsar-cluster-1/.default"
> }'
> ```


## Connect to the Pulsar cluster with Token authentication plugin

In this example, the Python producer publishes data to the `my-topic` in your Pulsar cluster. The consumer receives the message from the `my-topic` and `acknowledges` each received message.
Expand Down