|
| 1 | +--- |
| 2 | +title: "Learn about Microsoft Authentication Extensions for Node | Azure" |
| 3 | +titleSuffix: Microsoft identity platform |
| 4 | +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). |
| 5 | +services: active-directory |
| 6 | +author: henrymbuguakiarie |
| 7 | +manager: CelesteDG |
| 8 | + |
| 9 | +ms.service: active-directory |
| 10 | +ms.subservice: develop |
| 11 | +ms.topic: conceptual |
| 12 | +ms.workload: identity |
| 13 | +ms.date: 02/04/2022 |
| 14 | +ms.reviewer: j-mantu, samuelkubai, Dickson-Mwendia |
| 15 | +ms.author: henrymbugua |
| 16 | +#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. |
| 17 | +--- |
| 18 | + |
| 19 | +# Microsoft Authentication Extensions for Node |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +The [MSAL for Node](tutorial-v2-nodejs-webapp-msal.md) supports an in-memory cache by default and provides the ICachePlugin interface to perform cache serialization, but doesn't provide a default way of storing the token cache to disk. The Microsoft Authentication Extensions for Node is the default implementation for persisting cache to disk across different platforms. |
| 24 | + |
| 25 | +The Microsoft Authentication Extensions for Node support the following platforms: |
| 26 | + |
| 27 | +- Windows - Data protection API (DPAPI) is used for protection. |
| 28 | +- Mac - The Mac Keychain is used. |
| 29 | +- Linux - LibSecret is used for storing to "Secret Service". |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +The `msal-node-extensions` package is available on Node Package Manager (NPM). |
| 34 | + |
| 35 | +```bash |
| 36 | +npm i @azure/msal-node-extensions --save |
| 37 | +``` |
| 38 | + |
| 39 | +## Configure the token cache |
| 40 | + |
| 41 | +Here's an example of code that uses Microsoft Authentication Extensions for Node to configure the token cache. |
| 42 | + |
| 43 | +```javascript |
| 44 | +const { |
| 45 | + DataProtectionScope, |
| 46 | + Environment, |
| 47 | + PersistenceCreator, |
| 48 | + PersistenceCachePlugin, |
| 49 | +} = require("@azure/msal-node-extensions"); |
| 50 | + |
| 51 | +// You can use the helper functions provided through the Environment class to construct your cache path |
| 52 | +// The helper functions provide consistent implementations across Windows, Mac and Linux. |
| 53 | +const cachePath = path.join(Environment.getUserRootDirectory(), "./cache.json"); |
| 54 | + |
| 55 | +const persistenceConfiguration = { |
| 56 | + cachePath, |
| 57 | + dataProtectionScope: DataProtectionScope.CurrentUser, |
| 58 | + serviceName: "<SERVICE-NAME>", |
| 59 | + accountName: "<ACCOUNT-NAME>", |
| 60 | + usePlaintextFileOnLinux: false, |
| 61 | +}; |
| 62 | + |
| 63 | +// The PersistenceCreator obfuscates a lot of the complexity by doing the following actions for you :- |
| 64 | +// 1. Detects the environment the application is running on and initializes the right persistence instance for the environment. |
| 65 | +// 2. Performs persistence validation for you. |
| 66 | +// 3. Performs any fallbacks if necessary. |
| 67 | +PersistenceCreator.createPersistence(persistenceConfiguration).then( |
| 68 | + async (persistence) => { |
| 69 | + const publicClientConfig = { |
| 70 | + auth: { |
| 71 | + clientId: "<CLIENT-ID>", |
| 72 | + authority: "<AUTHORITY>", |
| 73 | + }, |
| 74 | + |
| 75 | + // This hooks up the cross-platform cache into MSAL |
| 76 | + cache: { |
| 77 | + cachePlugin: new PersistenceCachePlugin(persistence), |
| 78 | + }, |
| 79 | + }; |
| 80 | + |
| 81 | + const pca = new msal.PublicClientApplication(publicClientConfig); |
| 82 | + |
| 83 | + // Use the public client application as required... |
| 84 | + } |
| 85 | +); |
| 86 | +``` |
| 87 | + |
| 88 | +The following table provides an explanation for all the arguments for the persistence configuration. |
| 89 | + |
| 90 | +| Field Name | Description | Required For | |
| 91 | +| ----------------------- | --------------------------------------------------------------------------------------------------- | ---------------------- | |
| 92 | +| cachePath | The path to the lock file the library uses to synchronize the reads and the writes | Windows, Mac, and Linux | |
| 93 | +| dataProtectionScope | Specifies the scope of the data protection on Windows either the current user or the local machine. | Windows | |
| 94 | +| serviceName | Specifies the service name to be used on Mac and/or Linux | Mac and Linux | |
| 95 | +| accountName | Specifies the account name to be used on Mac and/or Linux | Mac and Linux | |
| 96 | +| usePlaintextFileOnLinux | The flag to default to plain text on linux if LibSecret fails. Defaults to `false` | Linux | |
| 97 | + |
| 98 | +## Next steps |
| 99 | + |
| 100 | +For more information about Microsoft Authentication Extensions for Node and MSAL Node, see: |
| 101 | + |
| 102 | +- [Microsoft Authentication Extensions for Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/extensions/msal-node-extensions) |
| 103 | +- [Microsoft Authentication Library for Node](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-node) |
0 commit comments