- Tool- Insomnia API Testing tool
- Creating a custom plugin to get the authentication token before each request.
I have already disabled Both the security validation
- Security
- Request/Response.
With the same url request I can get the token on the postman and Insomnia API UI as well but not through the plugin.
The hosted the application also show the certification issue on chrome when loading the home page but we click on the continue with certificate.
code :
const fetch = require('fetch-min');
module.exports = async function (context) {
console.log('authToken plugin executing ...');
const authData = await authDataProvider(context);
console.log(authData);
if (context.request.hasHeader('Authorization')) {
console.log(`[header] Skip setting default header ${name}. Already set to ${value}`);
return;
}
context.request.setHeader('Authorization', `bearer ${authData.access_token}`);
console.log(`[header] Set Authorization header: bearer ${authData.access_token}`);
};
async function authDataProvider(context) {
const url = context.request.getEnvironmentVariable('url');
const username = context.request.getEnvironmentVariable('user');
const password = context.request.getEnvironmentVariable('password');
const accUserName = context.request.getEnvironmentVariable('accUser');
const accUserPassword = context.request.getEnvironmentVariable('accUserPassword');
const client_id = context.request.getEnvironmentVariable('client_id');
const client_secret = context.request.getEnvironmentVariable('client_secret');
const asm_url="https://"+url+"/asmServices/services/v1.0/auth/tokens";
console.log(asm_url);
const authorizationBasic = "Basic "+window.btoa(username + ':' + password);
const updatedBody={
"auth": {
"dialects": [
"A3S_Core_Public"
],
"token-type": "JWT",
"token-expiration": null,
"on-behalf-of": {
"type": "USER_PASSWORD",
"user-credentials": {
"username": `${accUserName}`,
"password": `${accUserPassword}`
}
}
}
};
const options = {
method: 'POST',
// mode: 'no-cors',
// credentials: 'include',
headers: {
"Content-type": "application/json",
// "Access-Control-Allow-Headers":"Content-Type,Authorization,true",
// "Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
'Access-Control-Allow-Origin' : '*',
"Authorization":`${authorizationBasic}`
},
body: JSON.stringify(updatedBody),
};
let responseData = await window.fetch(`${asm_url}`,options)
.then((response) => {
console.log("response => ",response);
return response;
}).catch((error) => {
console.error("error => ",error);
});
let response = await responseData.json();
console.log(response);
return response;
}
error : message I am getting are
net::ERR_CERT_AUTHORITY_INVALID error => TypeError: Failed to fetch
POST URL : https://asm-services-cloud.xxx.com/asmServices/services/v1.0/auth/tokens
Request payload :
{"auth":{"dialects":["A3S_Core_Public"],"token-type":"JWT","token-expiration":null,"on-behalf-of":{"type":"USER_PASSWORD","user-credentials":{"username":"accUser15","password":"Acc100"}}}}
Could someone pin point the issue?.
I will not be able to create a new SSL certificates.