Skip to content

Commit 06773a2

Browse files
[feat] Support HTTP basic for node client (#391)
* 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>
1 parent 54e6ba3 commit 06773a2

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

index.d.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
export interface ClientConfig {
2222
serviceUrl: string;
23-
authentication?: AuthenticationTls | AuthenticationAthenz | AuthenticationToken | AuthenticationOauth2;
23+
authentication?: AuthenticationTls | AuthenticationAthenz | AuthenticationToken | AuthenticationOauth2 | AuthenticationBasic;
2424
operationTimeoutSeconds?: number;
2525
ioThreads?: number;
2626
messageListenerThreads?: number;
@@ -239,6 +239,13 @@ export class AuthenticationOauth2 {
239239
});
240240
}
241241

242+
export class AuthenticationBasic {
243+
constructor(params: {
244+
username: string;
245+
password: string;
246+
});
247+
}
248+
242249
export enum LogLevel {
243250
DEBUG = 0,
244251
INFO = 1,

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const AuthenticationTls = require('./src/AuthenticationTls');
2222
const AuthenticationAthenz = require('./src/AuthenticationAthenz');
2323
const AuthenticationToken = require('./src/AuthenticationToken');
2424
const AuthenticationOauth2 = require('./src/AuthenticationOauth2');
25+
const AuthenticationBasic = require('./src/AuthenticationBasic');
2526
const Client = require('./src/Client');
2627

2728
const LogLevel = {
@@ -40,6 +41,7 @@ const Pulsar = {
4041
AuthenticationAthenz,
4142
AuthenticationToken,
4243
AuthenticationOauth2,
44+
AuthenticationBasic,
4345
LogLevel,
4446
};
4547

src/Authentication.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
static const std::string PARAM_TLS_CERT = "certificatePath";
2323
static const std::string PARAM_TLS_KEY = "privateKeyPath";
2424
static const std::string PARAM_TOKEN = "token";
25+
static const std::string PARAM_USERNAME = "username";
26+
static const std::string PARAM_PASSWORD = "password";
2527

2628
Napi::FunctionReference Authentication::constructor;
2729

@@ -49,7 +51,7 @@ Authentication::Authentication(const Napi::CallbackInfo &info)
4951

5052
std::string authMethod = info[0].ToString().Utf8Value();
5153

52-
if (authMethod == "tls" || authMethod == "token") {
54+
if (authMethod == "tls" || authMethod == "token" || authMethod == "basic") {
5355
if (info.Length() < 2 || !info[1].IsObject()) {
5456
Napi::Error::New(env, "Authentication parameter must be a object").ThrowAsJavaScriptException();
5557
return;
@@ -73,6 +75,15 @@ Authentication::Authentication(const Napi::CallbackInfo &info)
7375
}
7476
this->cAuthentication =
7577
pulsar_authentication_token_create(obj.Get(PARAM_TOKEN).ToString().Utf8Value().c_str());
78+
} else if (authMethod == "basic") {
79+
if (!obj.Has(PARAM_USERNAME) || !obj.Get(PARAM_USERNAME).IsString() || !obj.Has(PARAM_PASSWORD) ||
80+
!obj.Get(PARAM_PASSWORD).IsString()) {
81+
Napi::Error::New(env, "Missing required parameter").ThrowAsJavaScriptException();
82+
return;
83+
}
84+
this->cAuthentication =
85+
pulsar_authentication_basic_create(obj.Get(PARAM_USERNAME).ToString().Utf8Value().c_str(),
86+
obj.Get(PARAM_PASSWORD).ToString().Utf8Value().c_str());
7687
}
7788
} else if (authMethod == "athenz") {
7889
if (info.Length() < 2 || !info[1].IsString()) {

src/AuthenticationBasic.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
const PulsarBinding = require('./pulsar-binding');
20+
21+
class AuthenticationBasic {
22+
constructor(params) {
23+
this.binding = new PulsarBinding.Authentication('basic', params);
24+
}
25+
}
26+
27+
module.exports = AuthenticationBasic;

tstest.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ import Pulsar = require('./index');
7070
token: 'foobar',
7171
});
7272

73+
const authBasic: Pulsar.AuthenticationBasic = new Pulsar.AuthenticationBasic({
74+
username: 'basic.username',
75+
password: 'basic.password',
76+
});
77+
7378
const client: Pulsar.Client = new Pulsar.Client({
7479
serviceUrl: 'pulsar://localhost:6650',
7580
authentication: authToken,

0 commit comments

Comments
 (0)