Skip to content

Commit

Permalink
[feat] Support HTTP basic for node client (#391)
Browse files Browse the repository at this point in the history
* Feature support HTTP basic for node client

* Update tstest.ts

Co-authored-by: Masahiro Sakamoto <massakam@lycorp.co.jp>

---------

Co-authored-by: Masahiro Sakamoto <massakam@lycorp.co.jp>
  • Loading branch information
raymondBourges and massakam authored Sep 3, 2024
1 parent 54e6ba3 commit 06773a2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -239,6 +239,13 @@ export class AuthenticationOauth2 {
});
}

export class AuthenticationBasic {
constructor(params: {
username: string;
password: string;
});
}

export enum LogLevel {
DEBUG = 0,
INFO = 1,
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -40,6 +41,7 @@ const Pulsar = {
AuthenticationAthenz,
AuthenticationToken,
AuthenticationOauth2,
AuthenticationBasic,
LogLevel,
};

Expand Down
13 changes: 12 additions & 1 deletion src/Authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down
27 changes: 27 additions & 0 deletions src/AuthenticationBasic.js
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions tstest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 06773a2

Please sign in to comment.