Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature support oauth2 for node client #190

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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