From 1d76d67c6bc63407921c5e13eea6403f9329b2f8 Mon Sep 17 00:00:00 2001 From: Oguzcan Kirmemis Date: Thu, 19 Sep 2019 15:41:38 +0200 Subject: [PATCH 1/7] Added refresh token endpoints Signed-off-by: Oguzcan Kirmemis --- api/rest/api.js | 4 +++- api/rest/auth.def.js | 33 +++++++++++++++++++++++++++++++++ api/rest/iot.auth.js | 35 ++++++++++++++++++++++++++++++++--- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/api/rest/api.js b/api/rest/api.js index c2fc850..bc04188 100644 --- a/api/rest/api.js +++ b/api/rest/api.js @@ -51,7 +51,9 @@ module.exports.alerts = { module.exports.auth = { TOKEN: '/v1/api/auth/token', TOKEN_INFO: '/v1/api/auth/tokenInfo', - USER_INFO: '/v1/api/auth/me' + USER_INFO: '/v1/api/auth/me', + REFRESH_TOKEN: '/v1/api/auth/refresh', + REVOKE_REFRESH_TOKEN: '/v1/api/auth/refresh/revoke' } module.exports.cmpcatalog = { diff --git a/api/rest/auth.def.js b/api/rest/auth.def.js index 49ce2ed..ebee96f 100644 --- a/api/rest/auth.def.js +++ b/api/rest/auth.def.js @@ -66,5 +66,38 @@ module.exports = function(config) { GetAuthUserInfoOption.prototype.constructor = GetAuthUserInfoOption; module.GetAuthUserInfoOption = GetAuthUserInfoOption; + function GetRefreshTokenOption(data) { + this.pathname = common.buildPath(api.auth.REFRESH_TOKEN); + this.token = data.token; + ConnectionOptions.call(this); + this.method = 'POST'; + this.body = null; + } + GetRefreshTokenOption.prototype = new ConnectionOptions(); + GetRefreshTokenOption.prototype.constructor = GetRefreshTokenOption; + module.GetRefreshTokenOption = GetRefreshTokenOption; + + function RefreshAuthTokenOption(data) { + this.pathname = common.buildPath(api.auth.REFRESH_TOKEN); + this.token = data.token; + ConnectionOptions.call(this); + this.method = 'PUT'; + this.body = JSON.stringify(data.body); + } + RefreshAuthTokenOption.prototype = new ConnectionOptions(); + RefreshAuthTokenOption.prototype.constructor = RefreshAuthTokenOption; + module.RefreshAuthTokenOption = RefreshAuthTokenOption; + + function RevokeRefreshTokenOption(data) { + this.pathname = common.buildPath(api.auth.REVOKE_REFRESH_TOKEN); + this.token = data.token; + ConnectionOptions.call(this); + this.method = 'DELETE'; + this.body = JSON.stringify(data.body); + } + RevokeRefreshTokenOption.prototype = new ConnectionOptions(); + RevokeRefreshTokenOption.prototype.constructor = RevokeRefreshTokenOption; + module.RevokeRefreshTokenOption = RevokeRefreshTokenOption; + return module; } diff --git a/api/rest/iot.auth.js b/api/rest/iot.auth.js index 35d7f9f..450b82f 100644 --- a/api/rest/iot.auth.js +++ b/api/rest/iot.auth.js @@ -26,13 +26,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. module.exports = function(config) { var module = {}; - + module.httpClient = require('../../lib/httpClient'); module.userAdminDef = require('./admin.def')(config); /** * @description Get user token through API:POST/v1/api/auth/token - * @param data.body.email the user email + * @param data.body.email the user email * @param data.body.password the user password */ module.getAuthToken = function(data, callback) { @@ -59,6 +59,35 @@ module.exports = function(config) { var getAuthUserInfoOpt = new module.userAdminDef.auth.GetAuthUserInfoOption(data); return module.httpClient.httpRequest(getAuthUserInfoOpt, callback); }; - + + /** + * @description Get refresh token through API:POST/v1/api/auth/refresh + * @param data.token the access token + */ + module.getRefreshToken = function(data, callback) { + var getRefreshTokenOpt = new module.userAdminDef.auth.GetRefreshTokenOption(data); + return module.httpClient.httpRequest(getRefreshTokenOpt, callback); + }; + + /** + * @description Refresh access token through API:PUT/v1/api/auth/refresh + * @param data.token the access token + * @param data.body.refreshToken the refresh token + */ + module.refreshAuthToken = function(data, callback) { + var refreshAuthTokenOpt = new module.userAdminDef.auth.RefreshAuthTokenOption(data); + return module.httpClient.httpRequest(refreshAuthTokenOpt, callback); + }; + + /** + * @description Refresh access token through API:PUT/v1/api/auth/refresh + * @param data.token the access token + * @param data.body.refreshToken the refresh token + */ + module.revokeRefreshToken = function(data, callback) { + var revokeRefreshTokenOpt = new module.userAdminDef.auth.RevokeRefreshTokenOption(data); + return module.httpClient.httpRequest(revokeRefreshTokenOpt, callback); + }; + return module; } From 5122c65e666ecaeee517f5306ab72aa0afd40b53 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Mon, 16 Dec 2019 10:26:42 +0000 Subject: [PATCH 2/7] Test: Add nodejs workflow --- .github/workflows/nodejs.yml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workflows/nodejs.yml diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml new file mode 100644 index 0000000..4561a42 --- /dev/null +++ b/.github/workflows/nodejs.yml @@ -0,0 +1,25 @@ +name: Node CI + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [8.x] + + steps: + - uses: actions/checkout@v1 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v1 + with: + node-version: ${{ matrix.node-version }} + - name: npm install, build, and test + run: | + npm ci + npm test + env: + CI: true From effa605167d59c4c429069a715637662c09d9fb5 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Mon, 16 Dec 2019 10:29:57 +0000 Subject: [PATCH 3/7] Test: Update nodejs workflow to use install --- .github/workflows/nodejs.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 4561a42..7845e08 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -17,9 +17,9 @@ jobs: uses: actions/setup-node@v1 with: node-version: ${{ matrix.node-version }} - - name: npm install, build, and test + - name: npm install and test run: | - npm ci + npm install npm test env: CI: true From a0526e786bee710ccd6147b726ea53c313f1f491 Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Mon, 16 Dec 2019 10:38:08 +0000 Subject: [PATCH 4/7] Test: Run workflow on PR and push --- .github/workflows/nodejs.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 7845e08..0c46d0e 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -1,6 +1,6 @@ name: Node CI -on: [push] +on: [push, pull_request] jobs: build: From 471318eb6dafb26608093b2d360884dfd5ee81ed Mon Sep 17 00:00:00 2001 From: Oguzcan Kirmemis Date: Fri, 13 Dec 2019 20:47:51 +0100 Subject: [PATCH 5/7] Keycloak changes for SDK - Data sending: Data sending with user tokens is implemented as a seperate function named submitDataAsUser which uses the endpoint: /v1/api/accounts/{accountId}/data/{deviceId} - Refresh tokens: No more seperate endpoint for getting and revoking them Signed-off-by: Oguzcan Kirmemis --- api/rest/api.js | 4 ++-- api/rest/auth.def.js | 23 +------------------- api/rest/data.def.js | 19 ++++++++++++++++ api/rest/iot.auth.js | 18 --------------- api/rest/iot.data.js | 23 +++++++++++++++----- test/api_iot.dataTests.js | 46 +++++++++++++++++++++++++++++++++++++-- 6 files changed, 84 insertions(+), 49 deletions(-) diff --git a/api/rest/api.js b/api/rest/api.js index bc04188..8c6330f 100644 --- a/api/rest/api.js +++ b/api/rest/api.js @@ -52,8 +52,7 @@ module.exports.auth = { TOKEN: '/v1/api/auth/token', TOKEN_INFO: '/v1/api/auth/tokenInfo', USER_INFO: '/v1/api/auth/me', - REFRESH_TOKEN: '/v1/api/auth/refresh', - REVOKE_REFRESH_TOKEN: '/v1/api/auth/refresh/revoke' + REFRESH_TOKEN: '/v1/api/auth/refresh' } module.exports.cmpcatalog = { @@ -76,6 +75,7 @@ module.exports.control = { module.exports.data = { SEND: '/v1/api/data/{deviceid}', + SEND_AS_USER: '/v1/api/accounts/{accountId}/data/{deviceId}', SEARCH: '/v1/api/accounts/{accountId}/data/search', SEARCH_ADVANCED: '/v1/api/accounts/{accountId}/data/search/advanced' } diff --git a/api/rest/auth.def.js b/api/rest/auth.def.js index ebee96f..800ceba 100644 --- a/api/rest/auth.def.js +++ b/api/rest/auth.def.js @@ -65,17 +65,7 @@ module.exports = function(config) { GetAuthUserInfoOption.prototype = new ConnectionOptions(); GetAuthUserInfoOption.prototype.constructor = GetAuthUserInfoOption; module.GetAuthUserInfoOption = GetAuthUserInfoOption; - - function GetRefreshTokenOption(data) { - this.pathname = common.buildPath(api.auth.REFRESH_TOKEN); - this.token = data.token; - ConnectionOptions.call(this); - this.method = 'POST'; - this.body = null; - } - GetRefreshTokenOption.prototype = new ConnectionOptions(); - GetRefreshTokenOption.prototype.constructor = GetRefreshTokenOption; - module.GetRefreshTokenOption = GetRefreshTokenOption; + function RefreshAuthTokenOption(data) { this.pathname = common.buildPath(api.auth.REFRESH_TOKEN); @@ -88,16 +78,5 @@ module.exports = function(config) { RefreshAuthTokenOption.prototype.constructor = RefreshAuthTokenOption; module.RefreshAuthTokenOption = RefreshAuthTokenOption; - function RevokeRefreshTokenOption(data) { - this.pathname = common.buildPath(api.auth.REVOKE_REFRESH_TOKEN); - this.token = data.token; - ConnectionOptions.call(this); - this.method = 'DELETE'; - this.body = JSON.stringify(data.body); - } - RevokeRefreshTokenOption.prototype = new ConnectionOptions(); - RevokeRefreshTokenOption.prototype.constructor = RevokeRefreshTokenOption; - module.RevokeRefreshTokenOption = RevokeRefreshTokenOption; - return module; } diff --git a/api/rest/data.def.js b/api/rest/data.def.js index 4c7dbc0..ad9ac68 100644 --- a/api/rest/data.def.js +++ b/api/rest/data.def.js @@ -52,6 +52,25 @@ module.exports = function(config) { module.SubmitDataOption = SubmitDataOption; + function SubmitDataAsUserOption(data) { + var isBinary = common.isBinary(data.body); + this.pathname = common.buildPath(api.data.SEND_AS_USER, [data.accountId, data.deviceId]); + this.token = data.userToken; + ConnectionOptions.call(this); + this.method = 'POST'; + if ( isBinary ) { + this.body = cbor.encode(data.body); + this.headers["Content-type"] = "application/cbor"; + } else { + this.body = JSON.stringify(data.body); + this.headers["Content-type"] = "application/json"; + } + } + SubmitDataAsUserOption.prototype = new ConnectionOptions(); + SubmitDataAsUserOption.prototype.constructor = SubmitDataAsUserOption; + module.SubmitDataAsUserOption = SubmitDataAsUserOption; + + function SearchDataOption(data) { this.pathname = common.buildPath(api.data.SEARCH, data.accountId); this.token = data.userToken; diff --git a/api/rest/iot.auth.js b/api/rest/iot.auth.js index 450b82f..05095ed 100644 --- a/api/rest/iot.auth.js +++ b/api/rest/iot.auth.js @@ -60,14 +60,6 @@ module.exports = function(config) { return module.httpClient.httpRequest(getAuthUserInfoOpt, callback); }; - /** - * @description Get refresh token through API:POST/v1/api/auth/refresh - * @param data.token the access token - */ - module.getRefreshToken = function(data, callback) { - var getRefreshTokenOpt = new module.userAdminDef.auth.GetRefreshTokenOption(data); - return module.httpClient.httpRequest(getRefreshTokenOpt, callback); - }; /** * @description Refresh access token through API:PUT/v1/api/auth/refresh @@ -79,15 +71,5 @@ module.exports = function(config) { return module.httpClient.httpRequest(refreshAuthTokenOpt, callback); }; - /** - * @description Refresh access token through API:PUT/v1/api/auth/refresh - * @param data.token the access token - * @param data.body.refreshToken the refresh token - */ - module.revokeRefreshToken = function(data, callback) { - var revokeRefreshTokenOpt = new module.userAdminDef.auth.RevokeRefreshTokenOption(data); - return module.httpClient.httpRequest(revokeRefreshTokenOpt, callback); - }; - return module; } diff --git a/api/rest/iot.data.js b/api/rest/iot.data.js index c44f98a..2bde4c2 100644 --- a/api/rest/iot.data.js +++ b/api/rest/iot.data.js @@ -26,13 +26,13 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. module.exports = function(config) { var module = {}; - + module.httpClient = require('../../lib/httpClient'); module.userAdminDef = require('./admin.def')(config); - + /** * @description Sends data to cloud through API:POST/v1/api/data/{deviceId} - * @param data.body the data JSON object according to the API spec for this call + * @param data.body the data JSON object according to the API spec for this call * @param data.userToken contains the access token * @param data.deviceId id of the device which sends the data */ @@ -42,9 +42,22 @@ module.exports = function(config) { }; + /** + * @description Sends data to cloud through API:POST/v1/api/data/{deviceId} + * @param data.body the data JSON object according to the API spec for this call + * @param data.userToken contains the access token + * @param data.deviceId id of the device which sends the data + * @param data.accountId id of account that owns the device + */ + module.submitDataAsUser = function(data, callback) { + var submitDataAsUserOpt = new module.userAdminDef.data.SubmitDataAsUserOption(data); + return module.httpClient.httpRequest(submitDataAsUserOpt, callback); + }; + + /** * @description Retrieve data from cloud through API:POST/v1/api/accounts/{accountId}/data/search - * @param data.body the detail of the search data JSON object according to the API spec for this call + * @param data.body the detail of the search data JSON object according to the API spec for this call * @param data.userToken contains the access token * @param data.accountId id of the account where the search is performed */ @@ -56,7 +69,7 @@ module.exports = function(config) { /** * @description Retrieve data from cloud through advanced API:POST/v1/api/accounts/{accountId}/data/search/advanced - * @param data.body the detail of the search data JSON object according to the API spec for this call + * @param data.body the detail of the search data JSON object according to the API spec for this call * @param data.userToken contains the access token * @param data.accountId id of the account where the search is performed */ diff --git a/test/api_iot.dataTests.js b/test/api_iot.dataTests.js index cf98a10..dd24927 100644 --- a/test/api_iot.dataTests.js +++ b/test/api_iot.dataTests.js @@ -44,7 +44,8 @@ describe(fileToTest, function() { }; var Option = { - SendDataOption: {}, + SubmitDataOption: {}, + SubmitDataAsUserOption: {}, SearchDataOption: {}, SearchDataAdvancedOption: {} }; @@ -76,7 +77,7 @@ describe(fileToTest, function() { }; Option.SubmitDataOption = function(alerts) { - assert.deepEqual(alerts, data, "The data is not oki"); + assert.deepEqual(alerts, data, "The data is not ok"); return optData; } httpClientMock.httpRequest = function (opt, cb) { @@ -92,6 +93,47 @@ describe(fileToTest, function() { toTest.userAdminDef.data = Option; toTest.submitData(data, callBack); }); + + it('Shall send submit data request as user for data submission', function(done) { + var optData = { + method: 'POST', + host: "myhost", + body: "mybody" + }; + + var data = { + accountid: "aid", + deviceid: "did", + body: {data: "message"} + }; + + var reData = { + x : 10, + y : 220, + ar : ["222", "333"] + }; + + Option.SubmitDataAsUserOption = function(alerts) { + assert.deepEqual(alerts, data, "The data is not ok"); + return optData; + }; + + httpClientMock.httpRequest = function (opt, cb) { + assert.deepEqual(opt, optData, "the option object was missing"); + cb(reData); + }; + + var callBack = function(response) { + assert.isNotNull(response, "The response was missing"); + assert.deepEqual(response, reData, "The Data was missing"); + done(); + }; + + toTest.httpClient = httpClientMock; + toTest.userAdminDef.data = Option; + toTest.submitDataAsUser(data, callBack); + }); + it('Shall send search data request for data', function(done) { var optData = { From 4c3d026770887d746bb6e21301cd5d6dc591952e Mon Sep 17 00:00:00 2001 From: Marcel Wagner Date: Sun, 22 Mar 2020 18:58:26 +0100 Subject: [PATCH 6/7] api/mqtt/connector.js: disable pingpong protocol in mqtt. It leads to unneccessary connection disruptions. Signed-off-by: Marcel Wagner --- api/mqtt/connector.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/mqtt/connector.js b/api/mqtt/connector.js index 3e7147e..7bb63ae 100644 --- a/api/mqtt/connector.js +++ b/api/mqtt/connector.js @@ -41,7 +41,7 @@ function Broker(conf) { }; me.iamHealthyTopic = "server/{hash}/healthy"; me.isLive = false; - me.pingActivate = true; + me.pingActivate = false; me.client = { connected: false, end: function() {} From 2aa2cc943ecb140268e59435ab1883485365e38e Mon Sep 17 00:00:00 2001 From: Scott Ware Date: Sat, 18 Apr 2020 08:02:36 +0100 Subject: [PATCH 7/7] Bump version for v1.1.2 release Signed-off-by: Scott Ware --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 6aad8af..ff8a252 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@open-iot-service-platform/oisp-sdk-js", - "version": "1.1.1", + "version": "1.1.2", "description": "OISP SDK for Node.js", "main": "index.js", "scripts": {