Skip to content

Commit

Permalink
Add example for simple token auth; Improve example for token supplier…
Browse files Browse the repository at this point in the history
… auth; Clean code
  • Loading branch information
Bouk250 committed Oct 18, 2024
1 parent 276ccd1 commit 9826a79
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 18 deletions.
4 changes: 1 addition & 3 deletions examples/consummer_token_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
56 changes: 56 additions & 0 deletions examples/consummer_token_auth_supplier.js
Original file line number Diff line number Diff line change
@@ -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();
})();
4 changes: 1 addition & 3 deletions examples/producer_token_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
53 changes: 53 additions & 0 deletions examples/producer_token_auth_supplier.js
Original file line number Diff line number Diff line change
@@ -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();
})();
14 changes: 2 additions & 12 deletions tstest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
},
Expand All @@ -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,
Expand Down

0 comments on commit 9826a79

Please sign in to comment.