You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
18
24
## Usage
19
25
20
26
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
25
31
26
32
The following are usage examples for each API:
27
33
28
-
### getAllAccounts
34
+
### `getAllAccounts`
29
35
30
36
For a multiple accounts scenario:
31
37
@@ -57,7 +63,7 @@ function callAcquireTokenSilent()
57
63
});
58
64
```
59
65
60
-
### getAccountByHomeId and getAccountByLocalId
66
+
### `getAccountByHomeId` and `getAccountByLocalId`
61
67
62
68
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.
63
69
@@ -113,7 +119,8 @@ async function getResource() {
113
119
}
114
120
```
115
121
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
117
125
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 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.
23
19
24
20
## Prerequisites
25
21
@@ -48,7 +44,7 @@ const pca = new PublicClientApplication(msalConfig);
48
44
49
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 (refer to the [Supported Environment](#supported-environment) section above).
50
46
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)
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:
19
19
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.
21
21
22
-
### Using secrets securely
22
+
##Use client secrets securely
23
23
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.
25
25
26
26
```javascript
27
27
constmsal=require('@azure/msal-node');
@@ -39,7 +39,6 @@ const cca = new msal.ConfidentialClientApplication({
39
39
/**
40
40
* acquireToken* APIs return an account object containing the "homeAccountId"
41
41
* 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
* 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
65
64
* Web apps, web APIs, daemon apps (confidential client apps (CCA)):
66
65
* 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)
67
66
@@ -71,7 +70,7 @@ MSAL maintains an in-memory cache. The in-memory cache is representative of the
71
70
72
71
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.
73
72
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.
75
74
76
75
## Persistent cache
77
76
@@ -112,10 +111,10 @@ class MyCachePlugin implements ICachePlugin {
112
111
}
113
112
```
114
113
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.
116
115
* 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.
117
116
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](../../../extensions/msal-node-extensions/README.md). For confidential clients however, you are responsible for devising an appropriate encryption solution.
119
118
120
119
## Performance and security
121
120
@@ -131,19 +130,19 @@ On confidential client apps that handle users (web apps that sign in users and c
131
130
132
131
### Web apps
133
132
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_common.icacheplugin.html). An instance of `DistributedCachePlugin` requires:
135
134
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**.
138
137
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.
140
139
141
-
## More information
140
+
## See also
142
141
143
142
See the samples below for more about how to handle caching in MSAL Node apps:
144
143
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)
147
+
-[Microsoft Authentication Extensions for Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/master/extensions/msal-node-extensions)
148
+
-[Microsoft Authentication Library for Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/master/lib/msal-node)
0 commit comments