diff --git a/README.md b/README.md index d1a5341..98501de 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ This project was created using NodeJS 18.18. This application aims to demonstrat 3. Finally, run the application using the following command ```bash - node embed.js + node tokengeneration.js ``` 4. After the application has started, it will display a URL in the `command line interface`, typically something like (e.g., ). Copy this URL and paste it into your default web browser. @@ -67,7 +67,7 @@ This project was created using NodeJS 18.18. This application aims to demonstrat * Finally, run the application using the following command. ```bash - node embed.js + node tokengeneration.js ``` * After the application has started, it will display a URL in the `command line interface`, typically something like (e.g., ). Copy this URL and paste it into your default web browser. @@ -86,4 +86,4 @@ Look at the Bold BI Embedding sample to live demo [here](https://samples.boldbi. ## Documentation -A complete Bold BI Embedding documentation can be found on [Bold BI Embedding Help](https://help.boldbi.com/embedded-bi/javascript-based/?utm_source=github&utm_medium=backlinks). +A complete Bold BI Embedding documentation can be found on [Bold BI Embedding Help](https://help.boldbi.com/embedded-bi/javascript-based/?utm_source=github&utm_medium=backlinks). \ No newline at end of file diff --git a/embed.js b/embed.js deleted file mode 100644 index 9c9c7c8..0000000 --- a/embed.js +++ /dev/null @@ -1,75 +0,0 @@ -var fs = require("fs"); -var http = require("http"); -var https = require("https"); -var url = require("url"); -var express = require('express'); -var cors = require('cors'); -var app = express(); -var bytes = require('utf8-bytes'); -var crypto = require('crypto'); - -app.use(cors()); -// Parse JSON bodies (as sent by API clients) -app.use(express.json()); - -var appconfig = JSON.parse(fs.readFileSync('embedConfig.json')); - -// Get the embedSecret key from Bold BI -var embedSecret = appconfig.EmbedSecret; - -var configjson ={"DashboardId": appconfig.DashboardId, "ServerUrl":appconfig.ServerUrl, "SiteIdentifier": appconfig.SiteIdentifier, "Environment": appconfig.Environment, "EmbedType": appconfig.EmbedType}; - -//Enter your BoldBI credentials here -var userEmail = appconfig.UserEmail; - -app.post('/embeddetail/get', function (req, response) { - var embedQuerString = req.body.embedQuerString; - var dashboardServerApiUrl = req.body.dashboardServerApiUrl; - - embedQuerString += "&embed_user_email=" + userEmail; - embedQuerString += "&embed_server_timestamp=" + Math.round((new Date()).getTime() / 1000); - var embedSignature = "&embed_signature=" + GetSignatureUrl(embedQuerString); - var embedDetailsUrl = "/embed/authorize?" + embedQuerString+embedSignature; - - var serverProtocol = url.parse(dashboardServerApiUrl).protocol == 'https:' ? https : http; - serverProtocol.get(dashboardServerApiUrl+embedDetailsUrl, function(res){ - var str = ''; - res.on('data', function (chunk) { - str += chunk; - }); - res.on('end', function () { - response.send(str); - }); - }); -}) - -function GetSignatureUrl(queryString) -{ - var keyBytes = Buffer.from(embedSecret); - var hmac = crypto.createHmac('sha256', keyBytes); - data = hmac.update(queryString); - gen_hmac= data.digest().toString('base64'); - -return gen_hmac; -} - -app.get("/",function (request, response) { - - var pathname = url.parse(request.url).pathname; - console.log("Request for " + pathname + " received."); - - response.writeHead(200); - - if(pathname == "/") { - html = fs.readFileSync("index.html", "utf8"); - html = html.replace(" - - -
- + - //Item id of the dashboard in the Bold BI server` - var dashboardId = configjson.DashboardId; + +
+ - - \ No newline at end of file + }).catch(error => { + console.error("Failed to get embed token:", error); + }); + } + + function getEmbedToken() { + return fetch(tokenGenerationUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + body: JSON.stringify({}) + }) + .then(response => { + if (!response.ok) throw new Error("Token fetch failed"); + return response.text(); + }); + } + + + + \ No newline at end of file diff --git a/package.json b/package.json index cbe92b6..83d1145 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "NodeJS", "version": "1.0.0", "description": "", - "main": "embed.js", + "main": "tokengeneration.js", "dependencies": { "cors": "^2.8.5", "express": "^4.18.2", diff --git a/tokengeneration.js b/tokengeneration.js new file mode 100644 index 0000000..eb25525 --- /dev/null +++ b/tokengeneration.js @@ -0,0 +1,80 @@ +var fs = require("fs"); +var http = require("http"); +var https = require("https"); +var url = require("url"); +var express = require('express'); +var cors = require('cors'); +var app = express(); + +app.use(cors()); +// Parse JSON bodies (as sent by API clients) +app.use(express.json()); + +// Read JSON as UTF-8 and strip BOM if present to avoid parse errors +var appconfig = JSON.parse(fs.readFileSync('embedConfig.json', 'utf8').replace(/^\uFEFF/, '')); + +var configjson ={"DashboardId": appconfig.DashboardId, "ServerUrl": appconfig.ServerUrl, "SiteIdentifier": appconfig.SiteIdentifier, "Environment": appconfig.Environment, "EmbedType": appconfig.EmbedType}; + +app.post('/TokenGeneration', function (req, response) { + const embedDetails = { + email: appconfig.UserEmail, + serverurl: appconfig.ServerUrl, + siteidentifier: appconfig.SiteIdentifier, + embedsecret: appconfig.EmbedSecret, + dashboard: { // Dashboard ID property is mandatory only when using BoldBI version 14.1.11. + id: appconfig.DashboardId + } + } + + const parsedUrl = new URL(embedDetails.serverurl); + const postData = JSON.stringify(embedDetails); + const client = parsedUrl.protocol === 'https:' ? https : http; + const options = { + hostname: parsedUrl.hostname, + port: parsedUrl.port || (parsedUrl.protocol === 'https:' ? 443 : 80), + path: `${parsedUrl.pathname}/api/${embedDetails.siteidentifier}/embed/authorize`, + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Content-Length': Buffer.byteLength(postData) + } + }; + + const requ = client.request(options, res => { + let result = ''; + res.setEncoding('utf8'); + res.on('data', chunk => result += chunk); + res.on('end', () => { + const resultparse = JSON.parse(result); // Parse the response + response.send(resultparse?.Data?.access_token); + }); + }); + + requ.on('error', (e) => { + console.error("Error fetching embed token:", e.message); + }); + + requ.write(postData); + requ.end(); +}) + +app.get("/",function (request, response) { + + var pathname = url.parse(request.url).pathname; + console.log("Request for " + pathname + " received."); + + response.writeHead(200); + + if(pathname == "/") { + html = fs.readFileSync("index.html", "utf8"); + html = html.replace("