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

guangning 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 f696d2f  Feature support oauth2 for node client (#190)
f696d2f is described below

commit f696d2fd958b702122ca7a21f84642ea855ec25d
Author: Guangning E <[email protected]>
AuthorDate: Mon Jan 17 16:43:17 2022 +0800

    Feature support oauth2 for node client (#190)
    
    * The cpp client already supports oauth2 authentication, and some users 
want to support this feature in the node js client as well, so enable it in the 
node js client
---
 index.d.ts                              | 14 +++++++++++++-
 index.js                                |  2 ++
 src/Authentication.cc                   |  7 +++++++
 index.js => src/AuthenticationOauth2.js | 28 +++++++---------------------
 tstest.ts                               | 17 +++++++++++++++++
 5 files changed, 46 insertions(+), 22 deletions(-)

diff --git a/index.d.ts b/index.d.ts
index c8186c8..ac498ff 100644
--- a/index.d.ts
+++ b/index.d.ts
@@ -20,7 +20,7 @@
 
 export interface ClientConfig {
   serviceUrl: string;
-  authentication?: AuthenticationTls | AuthenticationAthenz | 
AuthenticationToken;
+  authentication?: AuthenticationTls | AuthenticationAthenz | 
AuthenticationToken | AuthenticationOauth2;
   operationTimeoutSeconds?: number;
   ioThreads?: number;
   messageListenerThreads?: number;
@@ -175,6 +175,18 @@ export class AuthenticationToken {
   constructor(params: { token: string });
 }
 
+export class AuthenticationOauth2 {
+  constructor(params: {
+    type: string;
+    issuer_url: string;
+    client_id?: string;
+    client_secret?: string;
+    private_key?: string;
+    audience?: string;
+    scope?: string;
+  });
+}
+
 export enum LogLevel {
   DEBUG = 0,
   INFO = 1,
diff --git a/index.js b/index.js
index 461a853..d3e3b94 100644
--- a/index.js
+++ b/index.js
@@ -21,6 +21,7 @@ const PulsarBinding = require('bindings')('Pulsar');
 const AuthenticationTls = require('./src/AuthenticationTls.js');
 const AuthenticationAthenz = require('./src/AuthenticationAthenz.js');
 const AuthenticationToken = require('./src/AuthenticationToken.js');
+const AuthenticationOauth2 = require('./src/AuthenticationOauth2.js');
 
 const LogLevel = {
   DEBUG: 0,
@@ -36,6 +37,7 @@ const Pulsar = {
   AuthenticationTls,
   AuthenticationAthenz,
   AuthenticationToken,
+  AuthenticationOauth2,
   LogLevel,
 };
 
diff --git a/src/Authentication.cc b/src/Authentication.cc
index 226fd0a..35de2d9 100644
--- a/src/Authentication.cc
+++ b/src/Authentication.cc
@@ -80,6 +80,13 @@ Authentication::Authentication(const Napi::CallbackInfo 
&info)
       return;
     }
     this->cAuthentication = 
pulsar_authentication_athenz_create(info[1].ToString().Utf8Value().c_str());
+  } else if (authMethod == "oauth2") {
+    if (info.Length() < 2 || !info[1].IsString()) {
+      Napi::Error::New(env, "Authentication parameter must be a JSON string 
for oauth2")
+          .ThrowAsJavaScriptException();
+      return;
+    }
+    this->cAuthentication = 
pulsar_authentication_oauth2_create(info[1].ToString().Utf8Value().c_str());
   } else {
     Napi::Error::New(env, "Unsupported authentication 
method").ThrowAsJavaScriptException();
     return;
diff --git a/index.js b/src/AuthenticationOauth2.js
similarity index 63%
copy from index.js
copy to src/AuthenticationOauth2.js
index 461a853..cd5b328 100644
--- a/index.js
+++ b/src/AuthenticationOauth2.js
@@ -16,27 +16,13 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-
 const PulsarBinding = require('bindings')('Pulsar');
-const AuthenticationTls = require('./src/AuthenticationTls.js');
-const AuthenticationAthenz = require('./src/AuthenticationAthenz.js');
-const AuthenticationToken = require('./src/AuthenticationToken.js');
-
-const LogLevel = {
-  DEBUG: 0,
-  INFO: 1,
-  WARN: 2,
-  ERROR: 3,
-};
 
-const Pulsar = {
-  Client: PulsarBinding.Client,
-  Message: PulsarBinding.Message,
-  MessageId: PulsarBinding.MessageId,
-  AuthenticationTls,
-  AuthenticationAthenz,
-  AuthenticationToken,
-  LogLevel,
-};
+class AuthenticationOauth2 {
+  constructor(params) {
+    const paramsStr = (typeof params === 'object') ? JSON.stringify(params) : 
params;
+    this.binding = new PulsarBinding.Authentication('oauth2', paramsStr);
+  }
+}
 
-module.exports = Pulsar;
+module.exports = AuthenticationOauth2;
diff --git a/tstest.ts b/tstest.ts
index 43042bc..220854d 100644
--- a/tstest.ts
+++ b/tstest.ts
@@ -39,6 +39,23 @@ import Pulsar = require('./index');
     tokenExpirationTime: '3600',
   });
 
+  const authOauth2PrivateKey: Pulsar.AuthenticationOauth2 = new 
Pulsar.AuthenticationOauth2({
+    type: "client_credentials",
+    issuer_url: "issuer-url",
+    private_key: "credentials-file-path",
+    audience: "audience",
+    scope: "your-scope",
+  });
+
+  const authOauth2ClientId: Pulsar.AuthenticationOauth2 = new 
Pulsar.AuthenticationOauth2({
+    type: "client_credentials",
+    issuer_url: "issuer-url",
+    client_id: "client-id",
+    client_secret: "client-secret",
+    audience: "audience",
+    scope: "scope"
+  });
+
   const authToken: Pulsar.AuthenticationToken = new 
Pulsar.AuthenticationToken({
     token: 'foobar',
   });

Reply via email to