Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Nodejs SSO] remove home account caching #793

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,84 @@

const myMSALObj = new msal.PublicClientApplication(msalConfig);

let homeAccountId = null; // The home account ID of the user that signs in.

/**
* Gets an access token to the REST API server by using MSAL (Microsoft Authentication Library for the browser.)
* @returns A promise which if successful, returns the access token.
*/
async function getAccessTokenMSAL() {
// Attempt to acquire token silently if user is already signed in.
if (homeAccountId !== null) {
const result = await myMSALObj.acquireTokenSilent(loginRequest);
if (result !== null && result.accessToken !== null) {
return result.accessToken;
} else return null;
} else {
// Create a promise to wrap the dialog callback we need to process later in this function.
let promise = await new Promise((resolve, reject) => {
const url = '/dialog.html';
var fullUrl =
location.protocol +
'//' +
location.hostname +
(location.port ? ':' + location.port : '') +
url;
// Create a promise to wrap the dialog callback we need to process later in this function.
let promise = await new Promise((resolve, reject) => {
const url = '/dialog.html';
var fullUrl =
location.protocol +
'//' +
location.hostname +
(location.port ? ':' + location.port : '') +
url;

// height and width are percentages of the size of the parent Office application, e.g., Outlook, PowerPoint, Excel, Word, etc.
Office.context.ui.displayDialogAsync(
fullUrl,
{ height: 60, width: 30 },
function (result) {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(
(result.error.code = ': ' + result.error.message)
);
reject(result.error.message);
} else {
console.log('Dialog has initialized. Wiring up events');
let loginDialog = result.value;
// height and width are percentages of the size of the parent Office application, e.g., Outlook, PowerPoint, Excel, Word, etc.
Office.context.ui.displayDialogAsync(
fullUrl,
{ height: 60, width: 30 },
function (result) {
if (result.status === Office.AsyncResultStatus.Failed) {
console.log(
(result.error.code = ': ' + result.error.message)
);
reject(result.error.message);
} else {
console.log('Dialog has initialized. Wiring up events');
let loginDialog = result.value;

// Handler for the dialog box closing unexpectedly.
loginDialog.addEventHandler(
Office.EventType.DialogEventReceived,
(arg) => {
console.log(
'DialogEventReceived: ' + arg.error
);
loginDialog.close();
// For more dialog codes, see https://learn.microsoft.com/office/dev/add-ins/develop/dialog-handle-errors-events#errors-and-events-in-the-dialog-box
switch (arg.error) {
case 12002:
reject('The auth dialog box has been directed to a page that it cannot find or load, or the URL syntax is invalid.');
break;
case 12003:
reject('The auth dialog box has been directed to a URL with the HTTP protocol. HTTPS is required.');
break;
case 12006:
reject('The auth dialog box was closed before the user signed in.');
break;
default:
reject('Unknown error in auth dialog box.');
break;
}
// Handler for the dialog box closing unexpectedly.
loginDialog.addEventHandler(
Office.EventType.DialogEventReceived,
(arg) => {
console.log(
'DialogEventReceived: ' + arg.error
);
loginDialog.close();
// For more dialog codes, see https://learn.microsoft.com/office/dev/add-ins/develop/dialog-handle-errors-events#errors-and-events-in-the-dialog-box
switch (arg.error) {
case 12002:
reject('The auth dialog box has been directed to a page that it cannot find or load, or the URL syntax is invalid.');
break;
case 12003:
reject('The auth dialog box has been directed to a URL with the HTTP protocol. HTTPS is required.');
break;
case 12006:
reject('The auth dialog box was closed before the user signed in.');
break;
default:
reject('Unknown error in auth dialog box.');
break;
}
);
loginDialog.addEventHandler(
Office.EventType.DialogMessageReceived,
function processMessage2(arg) {
console.log(
'Message received in processMessage'
);
let messageFromDialog = JSON.parse(arg.message);

if (messageFromDialog.status === 'success') {
// We now have a valid access token.
loginDialog.close();
homeAccountId = messageFromDialog.accountId;
}
);
loginDialog.addEventHandler(
Office.EventType.DialogMessageReceived,
function processMessage2(arg) {
console.log(
'Message received in processMessage'
);
let messageFromDialog = JSON.parse(arg.message);

// Set the active account so future token requests can be silent.
myMSALObj.setActiveAccount(
myMSALObj.getAccountByHomeId(
homeAccountId
)
);
if (messageFromDialog.status === 'success') {
// We now have a valid access token.
loginDialog.close();

// Return the token.
resolve(messageFromDialog.result);
} else {
// Something went wrong with authentication or the authorization of the web application.
loginDialog.close();
reject(messageFromDialog.error);
}
// Return the token.
resolve(messageFromDialog.result);
} else {
// Something went wrong with authentication or the authorization of the web application.
loginDialog.close();
reject(messageFromDialog.error);
}
);
}
}
);
}
);
});
return promise;
}
}
}
);
});
return promise;
}