I need some help to resolve this issue.
Codebeamer is a secured server (production server in my Project). I am creating an Interface between Codebeamer and MS Project to sync tasks data through Office add-ins.i have a html screen where user can enter Username and password in MS Project so that connection can be established to codebeamer. Once the connection is successful, fetching the Taskdata and displaying the data in appropriate fields in MS Project (can think of one-way communication. data flow from Codebeamer to MS Project). The problem I am facing now is: when the user enters wrong credentials of Codebeamer to configure connection in MS Project which returns a 401 response. then browser is throwing authentication pop-up instead of executing my java script code for handling 401 response.
I am supposed to use Basic authentication as per guidebook. I tried writing "removeHeader" : WWW-Authenticate in fetch()
API method, but this one didn't work. next solution I have read is: change the response from 401 to 400 from server side, so that my code gets executed instead of browsers popup. I am not sure if it is correct way or not (as codebeamer is production server, I am not supposed to affect it in any way) ?
So my queries:
Is using the above mentioned solution affects server may be in configuring connection to other 3rd party application?
Please suggest if any other good solutions for this issue?
Below is my current code:
async function clickBtnAuthenticationOk() {
const userName = htmlClassInputUserId.value;
const userPwd = htmlClassInputPassword.value;
serverName = htmlClassCbServer.value;
taskIDInput = htmlClassTaskID.value;
// make sure data is entered
if (userName === "" || userPwd === "" || serverName === "") {
logError(`authentification data missing\n`);
return;
}
// checking whether entered TaskID column name is valid or not.
if (taskIDInput === "") {
logError("Please enter field name of Task IDs.\n");
return;
}
if (!Object.keys(Office.ProjectTaskFields).includes(taskIDInput)) {
logError(
`'${taskIDInput}' is not a valid column name. Please enter appropriate Field name in which Task IDs are present.\n`
);
return;
}
const apiUrl = `${serverName}/cb/rest/user/${userName}`;
auth = "Basic " + btoa(`${userName}:${userPwd}`);
logText(`making authentification request to ${apiUrl}\n`);
try {
const response = await fetch(apiUrl, {
method: "GET",
mode: "cors",
cache: "no-store",
credentials: "include",
headers: {
"Content-Type": "application/json",
Authorization: auth,
},
redirect: "follow",
referrerPolicy: "no-referrer",
});
logText(`res staus is: ${response.status}`);
if (!response.ok) {
if (response.status === 401) {
logError("Please enter correct userName, password.");
return;
} else {
throw new Error("something went wrong.\n");
}
}
const responseData = await response.json();
if (responseData.status === "Activated" || responseData.status === "Aktiv") {
isAuthenticationSuccess = true;
}
} catch (error) {
logError(`Authentication failed: ${error}`);
isAuthenticationSuccess = false;
} finally {
logText(`isAuthenticationSuccess:: ${isAuthenticationSuccess}\n`);
if (isAuthenticationSuccess) {
// Invalidate password
htmlClassInputPassword.value = "";
showStandardUsageHtmlScreen();
}
}
}
I tried writing "removeHeader" : WWW-Authenticate in fetch()
API method, but this one didn't work.
One more thing: WWW-Authenticate: "xBasic"; this one also didn't work either.