diff --git a/web/documentserver-example/nodejs/app.js b/web/documentserver-example/nodejs/app.js index a42bca91..391bced9 100644 --- a/web/documentserver-example/nodejs/app.js +++ b/web/documentserver-example/nodejs/app.js @@ -469,25 +469,32 @@ app.post("/track", function (req, res) { //checkjwt if (cfgSignatureEnable && cfgSignatureUseForRequest) { - var checkJwtHeaderRes = documentService.checkJwtHeader(req); - if (checkJwtHeaderRes) { - var body; - if (checkJwtHeaderRes.payload) { - body = checkJwtHeaderRes.payload; - } - if (checkJwtHeaderRes.query) { - if (checkJwtHeaderRes.query.useraddress) { - userAddress = checkJwtHeaderRes.query.useraddress; - } - if (checkJwtHeaderRes.query.filename) { - fileName = fileUtility.getFileName(checkJwtHeaderRes.query.filename); - } - } - processTrack(res, body, fileName, userAddress); + var body = null; + if (req.body.hasOwnProperty("token")) { + body = documentService.readToken(req.body.token); } else { + var checkJwtHeaderRes = documentService.checkJwtHeader(req); + if (checkJwtHeaderRes) { + var body; + if (checkJwtHeaderRes.payload) { + body = checkJwtHeaderRes.payload; + } + if (checkJwtHeaderRes.query) { + if (checkJwtHeaderRes.query.useraddress) { + userAddress = checkJwtHeaderRes.query.useraddress; + } + if (checkJwtHeaderRes.query.filename) { + fileName = fileUtility.getFileName(checkJwtHeaderRes.query.filename); + } + } + } + } + if (body == null) { res.write("{\"error\":1}"); res.end(); + return; } + processTrack(res, body, fileName, userAddress); return; } diff --git a/web/documentserver-example/nodejs/helpers/documentService.js b/web/documentserver-example/nodejs/helpers/documentService.js index 09dec4ec..619cb9d3 100644 --- a/web/documentserver-example/nodejs/helpers/documentService.js +++ b/web/documentserver-example/nodejs/helpers/documentService.js @@ -82,6 +82,7 @@ documentService.getConvertedUri = function (documentUri, fromExtension, toExtens if (cfgSignatureEnable && cfgSignatureUseForRequest) { headers[cfgSignatureAuthorizationHeader] = cfgSignatureAuthorizationHeaderPrefix + this.fillJwtByUrl(uri, params); + params.token = documentService.getToken(params); } urllib.request(uri, @@ -185,6 +186,7 @@ documentService.commandRequest = function (method, documentRevisionId, callback) }; if (cfgSignatureEnable && cfgSignatureUseForRequest) { headers[cfgSignatureAuthorizationHeader] = cfgSignatureAuthorizationHeaderPrefix + this.fillJwtByUrl(uri, params); + params.token = documentService.getToken(params); } urllib.request(uri, @@ -218,4 +220,18 @@ documentService.fillJwtByUrl = function (uri, opt_dataObject, opt_iss, opt_paylo return jwt.sign(payload, cfgSignatureSecret, options); } +documentService.getToken = function (data) { + var options = {algorithm: cfgSignatureSecretAlgorithmRequest, expiresIn: cfgSignatureSecretExpiresIn}; + return jwt.sign(data, cfgSignatureSecret, options); +}; + +documentService.readToken = function (token) { + try { + return jwt.verify(token, cfgSignatureSecret); + } catch (err) { + console.log('checkJwtHeader error: name = ' + err.name + ' message = ' + err.message + ' token = ' + token) + } + return null; +}; + module.exports = documentService;