Skip to content

Commit

Permalink
Feature support oauth2 for node client (#190)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tuteng authored Jan 17, 2022
1 parent 2fcf6a4 commit f696d2f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
14 changes: 13 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;
authentication?: AuthenticationTls | AuthenticationAthenz | AuthenticationToken | AuthenticationOauth2;
operationTimeoutSeconds?: number;
ioThreads?: number;
messageListenerThreads?: number;
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -36,6 +37,7 @@ const Pulsar = {
AuthenticationTls,
AuthenticationAthenz,
AuthenticationToken,
AuthenticationOauth2,
LogLevel,
};

Expand Down
7 changes: 7 additions & 0 deletions src/Authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
28 changes: 28 additions & 0 deletions src/AuthenticationOauth2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* 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('bindings')('Pulsar');

class AuthenticationOauth2 {
constructor(params) {
const paramsStr = (typeof params === 'object') ? JSON.stringify(params) : params;
this.binding = new PulsarBinding.Authentication('oauth2', paramsStr);
}
}

module.exports = AuthenticationOauth2;
17 changes: 17 additions & 0 deletions tstest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand Down

0 comments on commit f696d2f

Please sign in to comment.