From 89e4a6588f9cd94a0cc35c500d0059b825227af0 Mon Sep 17 00:00:00 2001 From: Raymond Bourges Date: Wed, 14 Aug 2024 22:37:03 +0200 Subject: [PATCH 1/2] Feature support HTTP basic for node client --- index.d.ts | 9 ++++++++- index.js | 2 ++ src/Authentication.cc | 13 ++++++++++++- src/AuthenticationBasic.js | 27 +++++++++++++++++++++++++++ tstest.ts | 5 +++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 src/AuthenticationBasic.js diff --git a/index.d.ts b/index.d.ts index 4680d81a..29c1967d 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 f745932f..ddbb997b 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 35de2d97..0b4dd7eb 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/src/AuthenticationBasic.js b/src/AuthenticationBasic.js new file mode 100644 index 00000000..7f787873 --- /dev/null +++ b/src/AuthenticationBasic.js @@ -0,0 +1,27 @@ +/** + * 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 PulsarBinding = require('./pulsar-binding'); + +class AuthenticationBasic { + constructor(params) { + this.binding = new PulsarBinding.Authentication('basic', params); + } +} + +module.exports = AuthenticationBasic; diff --git a/tstest.ts b/tstest.ts index c1b81a3f..b8b700d1 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, From 956e3c1a884b64df2bbe50fd17f558f26a5d7c95 Mon Sep 17 00:00:00 2001 From: Raymond Bourges Date: Mon, 2 Sep 2024 17:50:00 +0200 Subject: [PATCH 2/2] Update tstest.ts Co-authored-by: Masahiro Sakamoto --- tstest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tstest.ts b/tstest.ts index b8b700d1..611f4f22 100644 --- a/tstest.ts +++ b/tstest.ts @@ -73,7 +73,7 @@ import Pulsar = require('./index'); const authBasic: Pulsar.AuthenticationBasic = new Pulsar.AuthenticationBasic({ username: 'basic.username', password: 'basic.password', - }); + }); const client: Pulsar.Client = new Pulsar.Client({ serviceUrl: 'pulsar://localhost:6650',