Skip to content

Commit 9ffd1d9

Browse files
authored
Merge pull request MicrosoftDocs#30 from cilwerner/consolidated-msal.js-docs
MSAL Node documentation updates
2 parents 1575696 + c78a767 commit 9ffd1d9

17 files changed

+503
-211
lines changed

msal-javascript-conceptual/node/accounts.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: EmLauber
55
manager: CelesteDG
66
ms.author: emilylauber
77

8-
ms.date: 04/26/2021
8+
ms.date: 10/26/2023
99
ms.service: active-directory
1010
ms.subservice: develop
1111
ms.topic: conceptual
@@ -15,6 +15,12 @@ ms.reviewer: dmwendia,cwerner, owenrichards, kengaderdus
1515

1616
# Accounts in MSAL Node
1717

18+
This article shows you how to use the `msal-node` library to access cached accounts in your Node.js application. The APIs, `getAllAccounts()`, `getAccountByHomeId()`, and `getAccountByLocalId()` are covered
19+
20+
## Prerequisites
21+
22+
- [Node.js](https://nodejs.org/en/download/)
23+
1824
## Usage
1925

2026
The `msal-node` library provides the following different APIs to access cached accounts:
@@ -25,7 +31,7 @@ The `msal-node` library provides the following different APIs to access cached a
2531

2632
The following are usage examples for each API:
2733

28-
### getAllAccounts
34+
### `getAllAccounts`
2935

3036
For a multiple accounts scenario:
3137

@@ -57,7 +63,7 @@ function callAcquireTokenSilent()
5763
});
5864
```
5965

60-
### getAccountByHomeId and getAccountByLocalId
66+
### `getAccountByHomeId` and `getAccountByLocalId`
6167

6268
For a single account scenario, the `homeAccountId` or `localAccountId` must be obtained from the initial `AuthResponse` object received from a non-silent authorization flow, such as the `Auth Code` flow.
6369

@@ -113,7 +119,8 @@ async function getResource() {
113119
}
114120
```
115121

116-
## Notes
122+
For a multiple accounts scenario, please should modify the [sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/samples/msal-node-samples/silent-flow/index.js) (in `/graphCall` route) to list all cached accounts and choose a specific account. You may also need to customize the related view templates and `handlebars` template params.
123+
124+
## See also
117125

118-
* The current msal-node silent-flow [sample](../../../samples/msal-node-samples/silent-flow) has a working single account scenario that uses `getAccountByHomeId()`.
119-
* If you have a multiple accounts scenario, please modify the [sample](../../../samples/msal-node-samples/silent-flow/index.js) (in `/graphCall` route) to list all cached accounts and choose a specific account. You may also need to customize the related view templates and `handlebars` template params.
126+
* The current msal-node silent-flow [sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/silent-flow) has a working single account scenario that uses `getAccountByHomeId()`.

msal-javascript-conceptual/node/brokering.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: "Acquiring Device Bound Tokens"
3-
description: Learn how to acquire tokens from the native token broker.
3+
description: Learn how to acquire device bound tokens from the native token broker.
44
author: EmLauber
55
manager: CelesteDG
66
ms.author: emilylauber
77

8-
ms.date: 04/26/2021
8+
ms.date: 10/26/2023
99
ms.service: active-directory
1010
ms.subservice: develop
1111
ms.topic: conceptual
@@ -15,11 +15,7 @@ ms.reviewer: dmwendia,cwerner, owenrichards, kengaderdus
1515

1616
# Acquiring Device Bound Tokens
1717

18-
MSAL Node supports acquiring tokens from the native token broker. When using the native broker refresh tokens are bound to the device on which they are acquired on and are not accessible by `msal-node` or the application. This provides a higher level of security that cannot be achieved by `msal-node` alone.
19-
20-
## Supported Environment
21-
22-
This feature is currently only supported on Windows.
18+
MSAL Node supports acquiring tokens from the native token broker. When using the native broker refresh tokens are bound to the device on which they are acquired on and are not accessible by `msal-node` or the application. This provides a higher level of security that cannot be achieved by `msal-node` alone. This feature is currently only supported on Windows.
2319

2420
## Prerequisites
2521

@@ -46,9 +42,9 @@ const msalConfig: Configuration = {
4642
const pca = new PublicClientApplication(msalConfig);
4743
```
4844

49-
Please note that `msal-node` will _not_ fallback to the non-brokered flow in the event of a failure. In order to avoid unexpected failures in the future, only enable the broker flow in environments which support it (refer to the [Supported Environment](#supported-environment) section above).
45+
Please note that `msal-node` will _not_ fallback to the non-brokered flow in the event of a failure. In order to avoid unexpected failures in the future, only enable the broker flow in environments which support it.
5046

51-
A working sample can be found [here](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/auth-code-cli-brokered-app)
47+
A working sample can be found in the [auth-code-cli-brokered-app sample](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/auth-code-cli-brokered-app)
5248

5349
## Window Parenting
5450

msal-javascript-conceptual/node/MIP-caching.md renamed to msal-javascript-conceptual/node/caching-in-extensions.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
2-
title: "Microsoft Authentication Extensions for Node"
2+
title: Caching in Microsoft Authentication Extensions for Node
33
description: The Microsoft Authentication Extensions for Node enables application developers to perform cross-platform token cache serialization and persistence. It gives extra support to the Microsoft Authentication Library for Node (MSAL Node).
44
author: henrymbuguakiarie
55
manager: CelesteDG
66
ms.author: henrymbugua
7-
ms.date: 02/04/2022
7+
ms.date: 10/04/2023
88
ms.reviewer: j-mantu, samuelkubai, Dickson-Mwendia
99
ms.service: active-directory
1010
ms.subservice: develop
1111
ms.topic: conceptual
1212
#Customer intent: As an application developer, I want to learn how to use the Microsoft Authentication Extensions for Node to perform cross-platform token cache serialization and persistence.
1313
---
1414

15-
# Microsoft Authentication Extensions for Node
15+
# Caching in Microsoft Authentication Extensions for Node
1616

1717
The Microsoft Authentication Extensions for Node enables developers to perform cross-platform token cache serialization and persistence to disk. It gives extra support to the Microsoft Authentication Library (MSAL) for Node.
1818

@@ -91,9 +91,3 @@ The following table provides an explanation for all the arguments for the persis
9191
| accountName | Specifies the account name to be used on Mac and/or Linux | Mac and Linux |
9292
| usePlaintextFileOnLinux | The flag to default to plain text on linux if LibSecret fails. Defaults to `false` | Linux |
9393

94-
## Next steps
95-
96-
For more information about Microsoft Authentication Extensions for Node and MSAL Node, see:
97-
98-
- [Microsoft Authentication Extensions for Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/extensions/msal-node-extensions)
99-
- [Microsoft Authentication Library for Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node)

msal-javascript-conceptual/node/caching.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ author: EmLauber
55
manager: CelesteDG
66
ms.author: emilylauber
77

8-
ms.date: 04/26/2021
8+
ms.date: 04/26/2023
99
ms.service: active-directory
1010
ms.subservice: develop
1111
ms.topic: conceptual
@@ -17,11 +17,11 @@ ms.reviewer: dmwendia, cwerner, owenrichards, kengaderdus
1717

1818
When MSAL Node acquires a token, it caches it in memory for future usage. MSAL Node manages the token lifetime and refreshing for you. APIs like `acquireTokenSilent()` retrieves access tokens from the cache for a given account:
1919

20-
> :information_source: MSAL does not expose refresh tokens for security reasons. See [FAQ: How do I get the Refresh Token](./faq.md#how-do-i-get-the-refresh-token) for more.
20+
MSAL does not expose refresh tokens for security reasons. Refer to the [FAQ](./faq.md#how-do-i-get-the-refresh-token) for how to get the refresh tokens.
2121

22-
### Using secrets securely
22+
## Use client secrets securely
2323

24-
Secrets should never be hardcoded. The dotenv npm package can be used to store secrets in a .env file (located in project's root directory) that should be included in .gitignore to prevent accidental uploads of the secrets.
24+
Client secrets should never be hardcoded. The dotenv npm package can be used to store secrets in a *.env* file (located in project's root directory) that should be included in *.gitignore* to prevent accidental uploads of the secrets.
2525

2626
```javascript
2727
const msal = require('@azure/msal-node');
@@ -39,7 +39,6 @@ const cca = new msal.ConfidentialClientApplication({
3939
/**
4040
* acquireToken* APIs return an account object containing the "homeAccountId"
4141
* you should keep a record of this in your app and use it later on when calling acquireTokenSilent
42-
* For more, see: https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-node/docs/accounts.md
4342
*/
4443
const someUserHomeAccountId = "Enter_User_Home_Account_Id";
4544

@@ -58,10 +57,10 @@ cca.acquireTokenSilent(silentTokenRequest).then((response) => {
5857
});
5958
```
6059

61-
In production, you would most likely want to serialize and persist the token cache. Depending on the type of application, you can:
60+
In production, you'd most likely want to serialize and persist the token cache. Depending on the type of application, you can:
6261

6362
* Desktop apps, console apps (public clients apps (PCA)):
64-
* Use [MSAL Node Extensions](../../../extensions/msal-node-extensions/README.md), which provides persistence and encryption at rest solutions on Windows, Linux and Mac OS
63+
* Use [MSAL Node Extensions](./extensions.md), which provides persistence and encryption at rest solutions on Windows, Linux and Mac OS
6564
* Web apps, web APIs, daemon apps (confidential client apps (CCA)):
6665
* MSAL's in-memory token cache does not scale for production. Use the [distributed token caching](#performance-and-security) pattern to persist the cache in your choice of storage environment (Redis, MongoDB, SQL databases etc. -keep in mind that you can use these in tandem *e.g.* a Redis-like memory cache as a first layer of persistence, and a SQL database as a second, more stable persistence layer)
6766

@@ -71,16 +70,16 @@ MSAL maintains an in-memory cache. The in-memory cache is representative of the
7170

7271
Note that the in-memory cache is not scalable for server-side applications and performance will degrade after holding a few 100 tokens in cache. For web app and web API scenarios, this approximates to serving a few 100 users. For daemon app scenarios using client credentials grant to call other apps, this means a few 100 tenants. See [performance](#performance-and-security) below for more.
7372

74-
> :warning: We recommend **persisting** the cache with **encryption** for all production applications both for security and desired cache longevity. If you choose not to persist the cache, the [TokenCache](https://azuread.github.io/microsoft-authentication-library-for-js/ref/classes/_azure_msal_node.tokencache.html) interface is still available to access the cached entities.
73+
> :warning: We recommend **persisting** the cache with **encryption** for all production applications both for security and desired cache longevity. If you choose not to persist the cache, the [TokenCache](/javascript/api/@azure/msal-node/tokencache) interface is still available to access the cached entities.
7574
7675
## Persistent cache
7776

78-
MSAL Node fires events when the in-memory cache is accessed and apps can choose whether to persist the cache (see: [TokenCacheContext](https://azuread.github.io/microsoft-authentication-library-for-js/ref/classes/_azure_msal_common.tokencachecontext.html)) (e.g. to a file, a SQL database and etc.). This constitutes two actions:
77+
MSAL Node fires events when the in-memory cache is accessed and apps can choose whether to persist the cache (see: [TokenCacheContext](https://azuread.github.io/microsoft-authentication-library-for-js/ref/classes/_azure_msal_node.TokenCacheContext.html)) (e.g. to a file, a SQL database and etc.). This constitutes two actions:
7978

8079
1. Load the cache from persistence to MSAL's memory before accessing the cache
8180
2. If the in-memory cache has changed since last access, save the cache back to persistence
8281

83-
For persisting the cache, MSAL accepts a custom cache plugin in [configuration](./configuration.md). This plugin should implement the [ICachePlugin](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_common.icacheplugin.html) interface:
82+
For persisting the cache, MSAL accepts a custom cache plugin in [configuration](./configuration.md). This plugin should implement the [ICachePlugin](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_node.ICachePlugin.html) interface:
8483

8584
```typescript
8685
interface ICachePlugin {
@@ -112,14 +111,14 @@ class MyCachePlugin implements ICachePlugin {
112111
}
113112
```
114113

115-
* If you are developing a public client app, [MSAL Node Extensions](../../../extensions/msal-node-extensions/README.md) handles this for you.
114+
* If you are developing a public client app, [MSAL Node Extensions](./extensions.md) handles this for you.
116115
* If you are developing a confidential client app, you should persist the cache via a separate service, since a single, *per-server* cache instance isn't suitable for a cloud environment with many servers and app instances.
117116

118-
> :warning: We strongly recommend to encrypt the token cache when persisting it on disk. For public client apps, this is offered out-of-box with [MSAL Node Extensions](../../../extensions/msal-node-extensions/README.md). For confidential clients however, you are responsible for devising an appropriate encryption solution.
117+
We strongly recommend to encrypt the token cache when persisting it on disk. For public client apps, this is offered out-of-box with MSAL Node Extensions. For confidential clients however, you are responsible for devising an appropriate encryption solution.
119118

120119
## Performance and security
121120

122-
On public client apps, [MSAL Node Extensions](../../../extensions/msal-node-extensions/README.md) ensures performance and security for you.
121+
On public client apps, MSAL Node Extensions ensures performance and security for you.
123122

124123
On confidential client apps that handle users (web apps that sign in users and call web APIs, and web APIs calling downstream web APIs), there can be many users active concurrently for a given application. Our recommendation is to serialize one cache blob (see [CacheRecord](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-common/src/cache/entities/CacheRecord.ts)) per user. This would help with scaling the cache across a distributed system. Use a key for partitioning the cache (*i.e.* **partition key**), such as:
125124

@@ -131,19 +130,17 @@ On confidential client apps that handle users (web apps that sign in users and c
131130
132131
### Web apps
133132

134-
Since web apps are user-facing and often rely on sessions to keep track of each user, the appropriate partition key for caching is often stored within the session data, and needs to be retrieved before the cache lookup can take place. To help with this, MSAL Node provides the [DistributedCachePlugin](https://azuread.github.io/microsoft-authentication-library-for-js/ref/classes/_azure_msal_node.distributedcacheplugin.html) class, which implements the [ICachePlugin](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_common.icacheplugin.html). An instance of `DistributedCachePlugin` requires:
133+
Since web apps are user-facing and often rely on sessions to keep track of each user, the appropriate partition key for caching is often stored within the session data, and needs to be retrieved before the cache lookup can take place. To help with this, MSAL Node provides the [DistributedCachePlugin](/javascript/api/@azure/msal-node/distributedcacheplugin) class, which implements the [ICachePlugin](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_node.ICachePlugin.html). An instance of `DistributedCachePlugin` requires:
135134

136-
* a **client interface** ([ICacheClient](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_node.icacheclient.html)), which implements `get` and `set` operations on the persistence server (Redis, MySQL etc.).
137-
* a **partition manager** ([IPartitionManager](https://azuread.github.io/microsoft-authentication-library-for-js/ref/interfaces/_azure_msal_node.ipartitionmanager.html)), for reading from and writing to cache with respect to a given **session ID**.
135+
* a **client interface** ([ICacheClient](/javascript/api/@azure/msal-node/icacheclient)), which implements `get` and `set` operations on the persistence server (Redis, MySQL etc.).
136+
* a **partition manager** ([IPartitionManager](/javascript/api/@azure/msal-node/ipartitionmanager)), for reading from and writing to cache with respect to a given **session ID**.
138137

139-
Please refer to the [Web app using DistributedCachePlugin](../../../samples/msal-node-samples/auth-code-distributed-cache/README.md) for a sample implementation.
138+
Please refer to the [Web app using DistributedCachePlugin](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/auth-code-distributed-cache) for a sample implementation.
140139

141-
## More information
140+
## See also
142141

143142
See the samples below for more about how to handle caching in MSAL Node apps:
144143

145-
* [(PCA) Console app using MSAL Node Extensions](../../../extensions/samples/msal-node-extensions/index.js)
146-
* [(PCA) Dektop app using MSAL Node Extensions](../../../extensions/samples/electron-webpack/README.md)
147-
* [(CCA) Web app using DistributedCachePlugin](../../../samples/msal-node-samples/auth-code-distributed-cache/README.md)
148-
* [(CCA) Web API using a custom distributed cache plugin](../../../samples/msal-node-samples/auth-code-distributed-cache/README.md)
149-
* [(CCA) Daemon app using a custom distributed cache plugin](../../../samples/msal-node-samples/auth-code-distributed-cache/README.md)
144+
* [(PCA) Console app using MSAL Node Extensions](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/extensions/samples/msal-node-extensions/index.js)
145+
* [(PCA) Dektop app using MSAL Node Extensions](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/extensions/samples/electron-webpack)
146+
* [(CCA) Web app using DistributedCachePlugin](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/samples/msal-node-samples/auth-code-distributed-cache)

0 commit comments

Comments
 (0)