Skip to content

Commit

Permalink
feat: Use Tuya Auth with node client
Browse files Browse the repository at this point in the history
  • Loading branch information
lna-certi committed Nov 18, 2019
1 parent 0c33334 commit d218ac5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/consumer_tls_auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

const Pulsar = require('pulsar-client');
const Pulsar = require('../index.js');

(async () => {
const auth = new Pulsar.AuthenticationTls({
Expand Down
51 changes: 51 additions & 0 deletions examples/consumer_tuya.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* 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 Pulsar = require('pulsar-client');
const Pulsar = require('../index.js');

(async () => {
// Create a client

const auth = new Pulsar.AuthenticationTuya({
accessId: '84dj9ppwvvdyrf4e4sxq',
accessKey: 'yfcmuthtghmvhscj8redgp9r3wwsnr5w',
});
const client = new Pulsar.Client({
serviceUrl: "pulsar+ssl://mqe.tuyaus.com:7285/",
authentication: auth,
tlsAllowInsecureConnection: true,
});
// Create a consumer
const consumer = await client.subscribe({
topic: '84dj9ppwvvdyrf4e4sxq/out/event',
subscription: '84dj9ppwvvdyrf4e4sxq-sub',
ackTimeoutMs: 10000,
});
// Receive messages

for (let i = 0; i < 10000; i += 1) {
const msg = await consumer.receive();
console.log(msg.getData().toString());
consumer.acknowledge(msg);
}
await consumer.close();
await client.close();

})();
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 AuthenticationTuya = require('./src/AuthenticationTuya.js');

const Pulsar = {
Client: PulsarBinding.Client,
Expand All @@ -29,6 +30,7 @@ const Pulsar = {
AuthenticationTls,
AuthenticationAthenz,
AuthenticationToken,
AuthenticationTuya,
};

module.exports = Pulsar;
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@
"lodash": "^4.17.15"
},
"dependencies": {
"bindings": "^1.3.1",
"bindings": "^1.5.0",
"node-addon-api": "^1.6.2",
"node-gyp": "^3.8.0",
"node-pre-gyp": "^0.12.0"
"node-pre-gyp": "^0.12.0",
"pulsar-client": "^1.0.0"
},
"binary": {
"module_name": "libpulsar",
Expand Down
12 changes: 12 additions & 0 deletions 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_ACCESS_ID = "accessId";
static const std::string PARAM_ACCESS_KEY = "accessKey";

Napi::FunctionReference Authentication::constructor;

Expand Down Expand Up @@ -80,6 +82,16 @@ Authentication::Authentication(const Napi::CallbackInfo &info)
return;
}
this->cAuthentication = pulsar_authentication_athenz_create(info[1].ToString().Utf8Value().c_str());
} else if (authMethod == "auth1") {
Napi::Object obj = info[1].ToObject();

if (!obj.Has(PARAM_ACCESS_ID) || !obj.Get(PARAM_ACCESS_KEY).IsString()) {
Napi::Error::New(env, "Missing required parameter").ThrowAsJavaScriptException();
return;
}
this->cAuthentication =
pulsar_authentication_tuya_create(obj.Get(PARAM_ACCESS_ID).ToString().Utf8Value().c_str(),
obj.Get(PARAM_ACCESS_KEY).ToString().Utf8Value().c_str());
} else {
Napi::Error::New(env, "Unsupported authentication method").ThrowAsJavaScriptException();
return;
Expand Down
28 changes: 28 additions & 0 deletions src/AuthenticationTuya.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 AuthenticationTuya {
constructor(params) {
this.binding = new PulsarBinding.Authentication('auth1', params);
}
}

module.exports = AuthenticationTuya;

0 comments on commit d218ac5

Please sign in to comment.