This is an automated email from the ASF dual-hosted git repository.

baodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar-client-node.git


The following commit(s) were added to refs/heads/master by this push:
     new 06773a2  [feat] Support HTTP basic for node client (#391)
06773a2 is described below

commit 06773a2e5882d71b20938b9c849269b6a5860112
Author: Raymond Bourges <[email protected]>
AuthorDate: Tue Sep 3 05:16:14 2024 +0200

    [feat] Support HTTP basic for node client (#391)
    
    * Feature support HTTP basic for node client
    
    * Update tstest.ts
    
    Co-authored-by: Masahiro Sakamoto <[email protected]>
    
    ---------
    
    Co-authored-by: Masahiro Sakamoto <[email protected]>
---
 index.d.ts                             |  9 ++++++++-
 index.js                               |  2 ++
 src/Authentication.cc                  | 13 ++++++++++++-
 index.js => src/AuthenticationBasic.js | 33 +++++++--------------------------
 tstest.ts                              |  5 +++++
 5 files changed, 34 insertions(+), 28 deletions(-)

diff --git a/index.d.ts b/index.d.ts
index 4680d81..29c1967 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -20,7 +20,7 @@
 
 export interface ClientConfig {
   serviceUrl: string;
-  authentication?: AuthenticationTls | AuthenticationAthenz | 
AuthenticationToken | AuthenticationOauth2;
+  authentication?: AuthenticationTls | AuthenticationAthenz | 
AuthenticationToken | AuthenticationOauth2 | AuthenticationBasic;
   operationTimeoutSeconds?: number;
   ioThreads?: number;
   messageListenerThreads?: number;
@@ -239,6 +239,13 @@ export class AuthenticationOauth2 {
   });
 }
 
+export class AuthenticationBasic {
+  constructor(params: {
+    username: string;
+    password: string;
+  });
+}
+
 export enum LogLevel {
   DEBUG = 0,
   INFO = 1,
diff --git a/index.js b/index.js
index f745932..ddbb997 100644
--- a/index.js
+++ b/index.js
@@ -22,6 +22,7 @@ const AuthenticationTls = require('./src/AuthenticationTls');
 const AuthenticationAthenz = require('./src/AuthenticationAthenz');
 const AuthenticationToken = require('./src/AuthenticationToken');
 const AuthenticationOauth2 = require('./src/AuthenticationOauth2');
+const AuthenticationBasic = require('./src/AuthenticationBasic');
 const Client = require('./src/Client');
 
 const LogLevel = {
@@ -40,6 +41,7 @@ const Pulsar = {
   AuthenticationAthenz,
   AuthenticationToken,
   AuthenticationOauth2,
+  AuthenticationBasic,
   LogLevel,
 };
 
diff --git a/src/Authentication.cc b/src/Authentication.cc
index 35de2d9..0b4dd7e 100644
--- a/src/Authentication.cc
+++ b/src/Authentication.cc
@@ -22,6 +22,8 @@
 static const std::string PARAM_TLS_CERT = "certificatePath";
 static const std::string PARAM_TLS_KEY = "privateKeyPath";
 static const std::string PARAM_TOKEN = "token";
+static const std::string PARAM_USERNAME = "username";
+static const std::string PARAM_PASSWORD = "password";
 
 Napi::FunctionReference Authentication::constructor;
 
@@ -49,7 +51,7 @@ Authentication::Authentication(const Napi::CallbackInfo &info)
 
   std::string authMethod = info[0].ToString().Utf8Value();
 
-  if (authMethod == "tls" || authMethod == "token") {
+  if (authMethod == "tls" || authMethod == "token" || authMethod == "basic") {
     if (info.Length() < 2 || !info[1].IsObject()) {
       Napi::Error::New(env, "Authentication parameter must be a 
object").ThrowAsJavaScriptException();
       return;
@@ -73,6 +75,15 @@ Authentication::Authentication(const Napi::CallbackInfo 
&info)
       }
       this->cAuthentication =
           
pulsar_authentication_token_create(obj.Get(PARAM_TOKEN).ToString().Utf8Value().c_str());
+    } else if (authMethod == "basic") {
+      if (!obj.Has(PARAM_USERNAME) || !obj.Get(PARAM_USERNAME).IsString() || 
!obj.Has(PARAM_PASSWORD) ||
+          !obj.Get(PARAM_PASSWORD).IsString()) {
+        Napi::Error::New(env, "Missing required 
parameter").ThrowAsJavaScriptException();
+        return;
+      }
+      this->cAuthentication =
+          
pulsar_authentication_basic_create(obj.Get(PARAM_USERNAME).ToString().Utf8Value().c_str(),
+                                             
obj.Get(PARAM_PASSWORD).ToString().Utf8Value().c_str());
     }
   } else if (authMethod == "athenz") {
     if (info.Length() < 2 || !info[1].IsString()) {
diff --git a/index.js b/src/AuthenticationBasic.js
similarity index 52%
copy from index.js
copy to src/AuthenticationBasic.js
index f745932..7f78787 100644
--- a/index.js
+++ b/src/AuthenticationBasic.js
@@ -16,31 +16,12 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+const PulsarBinding = require('./pulsar-binding');
 
-const PulsarBinding = require('./src/pulsar-binding');
-const AuthenticationTls = require('./src/AuthenticationTls');
-const AuthenticationAthenz = require('./src/AuthenticationAthenz');
-const AuthenticationToken = require('./src/AuthenticationToken');
-const AuthenticationOauth2 = require('./src/AuthenticationOauth2');
-const Client = require('./src/Client');
+class AuthenticationBasic {
+  constructor(params) {
+    this.binding = new PulsarBinding.Authentication('basic', params);
+  }
+}
 
-const LogLevel = {
-  DEBUG: 0,
-  INFO: 1,
-  WARN: 2,
-  ERROR: 3,
-  toString: (level) => Object.keys(LogLevel).find((key) => LogLevel[key] === 
level),
-};
-
-const Pulsar = {
-  Client,
-  Message: PulsarBinding.Message,
-  MessageId: PulsarBinding.MessageId,
-  AuthenticationTls,
-  AuthenticationAthenz,
-  AuthenticationToken,
-  AuthenticationOauth2,
-  LogLevel,
-};
-
-module.exports = Pulsar;
+module.exports = AuthenticationBasic;
diff --git a/tstest.ts b/tstest.ts
index c1b81a3..611f4f2 100644
--- a/tstest.ts
+++ b/tstest.ts
@@ -70,6 +70,11 @@ import Pulsar = require('./index');
     token: 'foobar',
   });
 
+  const authBasic: Pulsar.AuthenticationBasic = new 
Pulsar.AuthenticationBasic({
+    username: 'basic.username',
+    password: 'basic.password',
+  });
+
   const client: Pulsar.Client = new Pulsar.Client({
     serviceUrl: 'pulsar://localhost:6650',
     authentication: authToken,

Reply via email to