I am trying to run this code in Modified JavaScript value step from PENTAHO to generate a token which will be used to access information by API. I am new with JS and also pentaho so idk why it keeps me shooting the error: syntax error (script#16). Furthermore, if i let just the code until const request, it says ReferenceError: “require” is not defined. (script#2). So, i think even the jsonwebtoken library is not being called.
Everything is fine if you run this code outside pentaho, i did it in Vs Code and i got the token, but when i put in the step from pentaho, it does not run.
const fs = require('fs');
const path = require('path');
const jwt = require('jsonwebtoken');
const request = require('request');
// settings
const basePath = 'https://identityhomolog.acesso.io';
// entry point
const options = {
serviceAccount: pemfile,
tenant: x-tenant
};
requestAnAccessToken(createServiceAccountToken(options), (err, accessToken) => {
const payload = jwt.decode(accessToken.access_token);
console.log('Access Token:', accessToken.access_token);
console.log('expires_in:', accessToken.expires_in);
});
// functions
function createServiceAccountToken({ tenant, serviceAccount, account = '' }) {
// Reads the service account private key
const privateKey = serviceAccount;
// Prepare the request
const payload = {
iss: '[email protected]',
aud: 'https://identity.acesso.io',
scope: '*',
exp: Math.floor(Date.now() / 1000) + 3600,
iat: Math.floor(Date.now() / 1000),
};
// Service account is requesting an access token for another user?
if (account) {
payload.sub = account;
}
// Create JWS
return jwt.sign(payload, privateKey, { algorithm: 'RS256' });
}
function requestAnAccessToken(serviceToken, callback) {
// Prepare the request
const options = {
method: 'POST',
url: 'https://identity.acesso.io/oauth2/token',
headers: { 'content-type': content-type },
form: {
grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer',
assertion: serviceToken,
},
};
console.log('Requesting Access Token with self created token: ', serviceToken);
// Ask identity and authorization server for an access token
request(options, (error, response, body) => {
if (error) {
callback(new Error(error));
}
body = JSON.parse(body);
if (body.error) {
callback(new Error(`${body.error}: ${body.error_description}`));
}
callback(null, body);
});
}