-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #854 from OfficeDev/davech-naa-fb-simple
[All hosts] NAA - add fallback support
- Loading branch information
Showing
14 changed files
with
522 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,11 +26,11 @@ This sample shows how to use MSAL.js nested app authentication (NAA) in an Outlo | |
## Features | ||
|
||
- Use MSAL.js NAA to get an access token to call Microsoft Graph APIs. | ||
- Use MSAL.js NAA to get information about the user signed in to Office. | ||
- Fall back to using the Office dialog API for auth when NAA unavailable. | ||
|
||
## Applies to | ||
|
||
- Outlook (Current Channel (previeiw) for classic Outlook only, new Outlook coming soon). | ||
- Outlook (Current Channel (preview) for classic Outlook only, new Outlook coming soon). | ||
- Outlook on the web. | ||
|
||
For more information on supported platforms, see [NAA supported accounts and hosts](https://learn.microsoft.com/office/dev/add-ins/develop/enable-nested-app-authentication-in-your-add-in#naa-supported-accounts-and-hosts). | ||
|
@@ -47,15 +47,20 @@ For more information on supported platforms, see [NAA supported accounts and hos | |
### Create an application registration | ||
|
||
1. Go to the [Azure portal - App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page to register your app. | ||
1. Sign in with the ***admin*** credentials to your Microsoft 365 tenancy. For example, **[email protected]**. | ||
1. Sign in with the **_admin_** credentials to your Microsoft 365 tenancy. For example, **[email protected]**. | ||
1. Select **New registration**. On the **Register an application** page, set the values as follows. | ||
|
||
- Set **Name** to `Outlook-Add-in-SSO-NAA`. | ||
- Set **Supported account types** to **Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)**. | ||
- In the **Redirect URI** section, ensure that **Single-page application (SPA)** is selected in the drop down and then set the URI to `brk-multihub://localhost:3000`. | ||
- Select **Register**. | ||
- Set **Name** to `Outlook-Add-in-SSO-NAA`. | ||
- Set **Supported account types** to **Accounts in any organizational directory (Any Microsoft Entra ID tenant - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)**. | ||
- In the **Redirect URI** section, ensure that **Single-page application (SPA)** is selected in the drop down and then set the URI to `brk-multihub://localhost:3000`. This allows Office to broker the auth request. | ||
- Select **Register**. | ||
|
||
1. On the **Outlook-Add-in-SSO-NAA** page, copy and save the value for the **Application (client) ID**. You'll use it in the next section. | ||
1. Under **Manage** select **Authentication**. | ||
1. In the **Single-page application** pane, select **Add URI**. | ||
1. Enter the value `https://localhost:3000/auth.html` and select **Save**. This redirect handles the fallback scenario when browser auth is used from add-in. | ||
1. In the **Single-page application** pane, select **Add URI**. | ||
1. Enter the value `https://localhost:3000/dialog.html` and select **Save**. This redirect handles the fallback scenario when the Office dialog API is used. | ||
|
||
For more information on how to register your application, see [Register an application with the Microsoft Identity Platform](https://learn.microsoft.com/graph/auth-register-app-v2). | ||
|
||
|
@@ -71,18 +76,18 @@ For more information on how to register your application, see [Register an appli | |
|
||
1. Run the following commands. | ||
|
||
`npm install` | ||
`npm run start` | ||
`npm install` | ||
`npm run start` | ||
|
||
This will start the web server and sideload the add-in to Outlook. | ||
This will start the web server and sideload the add-in to Outlook. | ||
|
||
1. In Outlook, compose a new email message. | ||
1. On the ribbon for the message, look for the **Show task pane** button and select it. | ||
1. When the task pane opens, there are two buttons: **Get user data** and **Get user files**. | ||
1. To see the signed in user's name and email, select **Get user data**. | ||
1. To insert the first 10 filenames from the signed in user's Microsoft OneDrive, select **Get user files**. | ||
|
||
You will be prompted to consent to the scopes the sample needs when you select the buttons. | ||
You will be prompted to consent to the scopes the sample needs when you select the buttons. | ||
|
||
## Debugging steps | ||
|
||
|
@@ -102,24 +107,38 @@ The `src/taskpane/authConfig.ts` file contains the MSAL code for configuring and | |
|
||
- The `initialize` function is called from Office.onReady to configure and intitialize MSAL to use NAA. | ||
- The `ssoGetAccessToken` function gets an access token for the signed in user to call Microsoft Graph APIs. | ||
- The `ssoGetUserAccount` function gets the account information of the signed in user. This can be used to get user details such as name and email. | ||
- The `getTokenWithDialogApi` function uses the Office dialog API to support a fallback option if NAA fails. | ||
|
||
The `src/taskpane/taskpane.ts` file contains code that runs when the user chooses buttons in the task pane. They use the AccountManager class to get tokens or user information depending on which button is chosen. | ||
The `src/taskpane/taskpane.ts` file contains code that runs when the user chooses buttons in the task pane. It uses the AccountManager class to get tokens or user information depending on which button is chosen. | ||
|
||
The `src/taskpane/msgraph-helper.ts` file contains code to construct and make a REST call to the Microsoft Graph API. | ||
|
||
### Fallback code | ||
|
||
The `fallback` folder contains files to fall back to an alternate authentication method if NAA is unavailable and fails. When your code calls `acquireTokenSilent`, and NAA is unavailable, an error is thrown. The next step is the code calls `acquireTokenPopup`. MSAL then attempts to sign in the user by opening a dialog box with `window.open` and `about:blank`. Some older Outlook clients don't support the `about:blank` dialog box and cause the `aquireTokenPopup` method to fail. You can catch this error and fall back to using the Office dialog API to open the auth dialog instead. | ||
|
||
- the `src/taskpane/authconfig.ts` file contains the following code to detect the error and fall back to using the Office dialog API. | ||
|
||
```typescript | ||
// Optional fallback if about:blank popup should not be shown | ||
if (popupError instanceof BrowserAuthError && popupError.errorCode === "popup_window_error") { | ||
const accessToken = await this.getTokenWithDialogApi(); | ||
return accessToken; | ||
``` | ||
- The `src/taskpane/fallback/fallbackauthdialog.ts` file contains code to initialize MSAL and acquire an access token. It sends the access token back to the task pane. | ||
## Security reporting | ||
If you find a security issue with our libraries or services, report the issue to [[email protected]](mailto:[email protected]) with as much detail as you can provide. Your submission may be eligible for a bounty through the [Microsoft Bounty](https://aka.ms/bugbounty) program. Don't post security issues to [GitHub Issues](https://github.com/AzureAD/microsoft-authentication-library-for-android/issues) or any other public site. We'll contact you shortly after receiving your issue report. We encourage you to get new security incident notifications by visiting [Microsoft technical security notifications](https://technet.microsoft.com/security/dd252948) to subscribe to Security Advisory Alerts. | ||
## More resources | ||
- NAA public preview blog: https://aka.ms/NAApreviewblog | ||
- NAA public preview blog: https://aka.ms/NAApreviewblog | ||
- [Updates on deprecating legacy Exchange Online tokens for Outlook add-ins](https://devblogs.microsoft.com/microsoft365dev/updates-on-deprecating-legacy-exchange-online-tokens-for-outlook-add-ins/?commentid=1131) | ||
- NAA docs to get started: https://aka.ms/NAAdocs | ||
- NAA FAQ: https://aka.ms/NAAFAQ | ||
- NAA Outlook sample: https://aka.ms/NAAsampleOutlook | ||
- NAA Word, Excel, and PowerPoint sample: https://aka.ms/NAAsampleOffice | ||
- NAA docs to get started: https://aka.ms/NAAdocs | ||
- NAA FAQ: https://aka.ms/NAAFAQ | ||
- NAA Word, Excel, and PowerPoint sample: https://aka.ms/NAAsampleOffice | ||
## Questions and feedback | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.