From 9826a79ee2b268f8cef2d4fef10c4674304bf07a Mon Sep 17 00:00:00 2001 From: Ariel Boukris Date: Fri, 18 Oct 2024 13:52:51 +0300 Subject: [PATCH] Add example for simple token auth; Improve example for token supplier auth; Clean code --- examples/consummer_token_auth.js | 4 +- examples/consummer_token_auth_supplier.js | 56 +++++++++++++++++++++++ examples/producer_token_auth.js | 4 +- examples/producer_token_auth_supplier.js | 53 +++++++++++++++++++++ tstest.ts | 14 +----- 5 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 examples/consummer_token_auth_supplier.js create mode 100644 examples/producer_token_auth_supplier.js diff --git a/examples/consummer_token_auth.js b/examples/consummer_token_auth.js index eac2b6e..e6a9019 100644 --- a/examples/consummer_token_auth.js +++ b/examples/consummer_token_auth.js @@ -22,9 +22,7 @@ const Pulsar = require('../'); console.log("Starting consumer"); (async () => { - const auth = new Pulsar.AuthenticationToken({token: async () => { - return "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY" - }}); + const auth = new Pulsar.AuthenticationToken({token: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"}); // Create a client const client = new Pulsar.Client({ diff --git a/examples/consummer_token_auth_supplier.js b/examples/consummer_token_auth_supplier.js new file mode 100644 index 0000000..a85b6a1 --- /dev/null +++ b/examples/consummer_token_auth_supplier.js @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const { exit } = require('process'); +const Pulsar = require('..'); +console.log("Starting consumer"); + +async function getToken() { + console.log("Get token"); + return "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"; +} + +(async () => { + + const auth = new Pulsar.AuthenticationToken({token: getToken}); + + // Create a client + const client = new Pulsar.Client({ + serviceUrl: 'pulsar://localhost:6650', + authentication: auth, + }); + + // Create a consumer + const consumer = await client.subscribe({ + topic: 'persistent://public/default/my-topic', + subscription: 'sub1', + subscriptionType: 'Shared', + ackTimeoutMs: 10000, + }); + + // Receive messages + for (let i = 0; i < 10; i += 1) { + const msg = await consumer.receive(); + console.log(msg.getData().toString()); + consumer.acknowledge(msg); + } + + await consumer.close(); + await client.close(); +})(); diff --git a/examples/producer_token_auth.js b/examples/producer_token_auth.js index 081265e..f0af460 100644 --- a/examples/producer_token_auth.js +++ b/examples/producer_token_auth.js @@ -20,9 +20,7 @@ const Pulsar = require('../'); (async () => { - const auth = new Pulsar.AuthenticationToken({token: async () => { - return "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY" - }}); + const auth = new Pulsar.AuthenticationToken({token: "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"}); // Create a client const client = new Pulsar.Client({ diff --git a/examples/producer_token_auth_supplier.js b/examples/producer_token_auth_supplier.js new file mode 100644 index 0000000..6b121e3 --- /dev/null +++ b/examples/producer_token_auth_supplier.js @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +const Pulsar = require('..'); + +async function getToken() { + console.log("Get token"); + return "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJKb2UifQ.ipevRNuRP6HflG8cFKnmUPtypruRC4fb1DWtoLL62SY"; +} + +(async () => { + const auth = new Pulsar.AuthenticationToken({token: getToken}); + + // Create a client + const client = new Pulsar.Client({ + serviceUrl: 'pulsar://localhost:6650', + authentication: auth, + }); + + // Create a producer + const producer = await client.createProducer({ + topic: 'persistent://public/default/my-topic', + }); + + // Send messages + for (let i = 0; i < 10; i += 1) { + const msg = `my-message-${i}`; + producer.send({ + data: Buffer.from(msg), + }); + console.log(`Sent message: ${msg}`); + } + await producer.flush(); + + await producer.close(); + await client.close(); +})(); diff --git a/tstest.ts b/tstest.ts index 0829be6..1ca89b7 100644 --- a/tstest.ts +++ b/tstest.ts @@ -66,17 +66,7 @@ import Pulsar = require('./index'); scope: "scope" }); - const authToken1: Pulsar.AuthenticationToken = new Pulsar.AuthenticationToken({ - token: 'foobar', - }); - - const authToken2: Pulsar.AuthenticationToken = new Pulsar.AuthenticationToken({ - token: () => { - return 'foobar'; - }, - }); - - const authToken3: Pulsar.AuthenticationToken = new Pulsar.AuthenticationToken({ + const authToken: Pulsar.AuthenticationToken = new Pulsar.AuthenticationToken({ token: async () => { return 'foobar'; }, @@ -89,7 +79,7 @@ import Pulsar = require('./index'); const client: Pulsar.Client = new Pulsar.Client({ serviceUrl: 'pulsar://localhost:6650', - authentication: authToken1, + authentication: authToken, operationTimeoutSeconds: 30, ioThreads: 4, messageListenerThreads: 4,