Skip to content

Commit 91ef52b

Browse files
authored
MsalBrowserAuthenticationProvider (#484)
* creating msal browser auth provider * unit test, msal browser options * error handling unit test, doc updates * rename auth provider * doc and folder name update * throw error, msal cdn reference
1 parent 2cdf1f7 commit 91ef52b

23 files changed

+1327
-266
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* -------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4+
* See License in the project root for license information.
5+
* -------------------------------------------------------------------------------------------
6+
*/
7+
export * from "../../lib/src/authentication/msal-browser/AuthCodeMSALBrowserAuthenticationProvider";
8+
export * from "../../lib/src/authentication/msalOptions/MSALAuthenticationProviderOptions";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* -------------------------------------------------------------------------------------------
3+
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
4+
* See License in the project root for license information.
5+
* -------------------------------------------------------------------------------------------
6+
*/
7+
export * from "../../../lib/es/src/authentication/msal-browser/AuthCodeMSALBrowserAuthenticationProvider";
8+
export * from "../../../lib/es/src/authentication/msalOptions/MSALAuthenticationProviderOptions";

authProviderOptions/es/msal/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
*/
77

88
export * from "../../../lib/es/src/authentication/msal/ImplicitMSALAuthenticationProvider";
9-
export * from "../../../lib/es/src/authentication/msal/MSALAuthenticationProviderOptions";
9+
export * from "../../../lib/es/src/authentication/msalOptions/MSALAuthenticationProviderOptions";

authProviderOptions/msal/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
* See License in the project root for license information.
55
* -------------------------------------------------------------------------------------------
66
*/
7-
export * from "../../lib/src/authentication/msal/MSALAuthenticationProviderOptions";
7+
export * from "../../lib/src/authentication/msalOptions/MSALAuthenticationProviderOptions";
88
export * from "../../lib/src/authentication/msal/ImplicitMSALAuthenticationProvider";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#### Creating an instance of AuthCodeMSALBrowserAuthenticationProvider for a browser application
2+
3+
**Note**: The `AuthCodeMSALBrowserAuthenticationProvider` is introduced in version 3.0.0 of Microsoft Graph Client Library
4+
5+
###### Links for more information -
6+
7+
- [npm - @azure/msal-browser](https://www.npmjs.com/package/@azure/msal-browser)
8+
- [github - @azure/msal-browser](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/README.md)
9+
10+
Steps to use `AuthCodeMSALBrowserAuthenticationProvider`;
11+
12+
1. Using npm: `npm install @azure/msal-browser @microsoft/microsoft-graph-client`
13+
14+
Using html:
15+
16+
```html
17+
<!--Using script tag to include the bundled file or the CDN source-->
18+
<script type="text/javascript" src="https://alcdn.msauth.net/browser/2.15.0/js/msal-browser.min.js"></script>
19+
<script src="graph-js-sdk.js"></script>
20+
<script src="graph-client-msalBrowserAuthProvider.js"></script>
21+
```
22+
23+
Reference : [MSAL Browser CDN usage](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/cdn-usage.md)
24+
25+
2. Initialize the `msal-browser` `PublicClientApplication` instance: Learn more [how to initialize msal](https://github.com/AzureAD/microsoft-authentication-library-for-js/blob/dev/lib/msal-browser/docs/initialization.md)
26+
27+
3. Following sample shows how to initialize a Microsoft Graph SDK `Client` instance,
28+
29+
Using npm:
30+
31+
```typescript
32+
import { PublicClientApplication, InteractionType, AccountInfo } from "@azure/msal-browser";
33+
34+
import { AuthCodeMSALBrowserAuthenticationProvider, AuthCodeMSALBrowserAuthenticationProviderOptions } from "@microsoft/microsoft-graph-client/authProviders/authCodeMsalBrowser";
35+
36+
const options:AuthCodeMSALBrowserAuthenticationProviderOptions: {
37+
account: account, // the AccountInfo instance to acquire the token for.
38+
interactionType: InteractionType.PopUp , // msal-browser InteractionType
39+
scopes: ["user.read", "mail.send"] // example of the scopes to be passed
40+
}
41+
42+
// Pass the PublicClientApplication instance from step 2 to create AuthCodeMSALBrowserAuthenticationProvider instance
43+
const authProvider: new AuthCodeMSALBrowserAuthenticationProvider(publicClientApplication, options),
44+
45+
46+
// Initialize the Graph client
47+
const graphClient = Client.initWithMiddleware({
48+
authprovider
49+
});
50+
51+
```
52+
53+
Using CDN or script:
54+
55+
```javascript
56+
const msalClient = new msal.PublicClientApplication(msalConfig);
57+
58+
const authProvider = new MSGraphAuthCodeMSALBrowserAuthProvider.AuthCodeMSALBrowserAuthenticationProvider(msalClient, {
59+
account, // the AccountInfo instance to acquire the token for
60+
scopes: ["user.read", "mail.send"],
61+
interactionType: msal.InteractionType.Popup,
62+
});
63+
64+
// Initialize the Graph client
65+
const graphClient = MicrosoftGraph.Client.initWithMiddleware({ authProvider });
66+
```

docs/CreatingClientInstance.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ In order to instantiate a Client object, one has to pass in the `authProvider` o
1111
### Option A. Default Middleware chain
1212

1313
The default middleware chain contains consecutively chained instances of the following:
14-
- [AuthenticationHandler](../src/middleware/AuthenticationHandler.ts)
15-
- [RetryHandler](../src/middleware/RetryHandler.ts)
16-
- [RedirectHandler](../src/middleware/RedirectHandler.ts)
17-
- [TelemetryHandler](../src/middleware/TelemetryHandler.ts)
18-
- [HTTPMessageHandler](../src/middleware/HTTPMessageHandler.ts)
14+
15+
- [AuthenticationHandler](../src/middleware/AuthenticationHandler.ts)
16+
- [RetryHandler](../src/middleware/RetryHandler.ts)
17+
- [RedirectHandler](../src/middleware/RedirectHandler.ts)
18+
- [TelemetryHandler](../src/middleware/TelemetryHandler.ts)
19+
- [HTTPMessageHandler](../src/middleware/HTTPMessageHandler.ts)
1920

2021
To create a client instance with the default middleware chain:
2122

@@ -36,9 +37,9 @@ The Microsoft Graph JavaScript Client Library has an adapter implementation for
3637

3738
> Learn how to [create an instance of TokenCredentialAuthenticationProvider](./TokenCredentialAuthenticationProvider.md).
3839
39-
- **DEPRECATED** ([ImplicitMSALAuthenticationProvider](../src/authentication/msal/ImplicitMSALAuthenticationProvider.ts)) for [MSAL](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-core) (Microsoft Authentication Library) which takes care of getting the `accessToken`. MSAL library does not ship with this library, user has to include it externally (For including MSAL, refer [this](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-core#installation)).
40+
- ([AuthCodeMSALBrowserAuthenticationProvider](../src/authentication/msal/ImplicitMSALAuthenticationProvider.ts)) for [msal-browser](https://github.com/AzureAD/microsoft-authentication-library-for-js/tree/dev/lib/msal-browser) (Microsoft Authentication Library) which takes care of getting the `accessToken`. `msal-browser` library does not ship with this library, user has to include it externally.
4041

41-
> Learn how to [create an instance of ImplicitMSALAuthenticationProvider](./ImplicitMSALAuthenticationProvider.md).
42+
> Learn how to [create an instance of AuthCodeMSALBrowserAuthenticationProvider](./AuthCodeMSALBrowserAuthenticationProvider.md).
4243
4344
**User can integrate any preferred authentication library by implementing `IAuthenticationProvider` interface**. Refer implementing [Custom Authentication Provider](./CustomAuthenticationProvider.md) for more detailed information.
4445

docs/TokenCredentialAuthenticationProvider.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#### Creating an instance of TokenCredentialAuthentication
22

3+
**Note**: The `TokenCredentialAuthentication` is introduced in version 3.0.0 of Microsoft Graph Client Library
4+
35
###### Links for more information -
46

57
- [GitHub - Azure Identity client library for JavaScript ](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md)
@@ -60,5 +62,5 @@ The browser use of the file is as follows:
6062
```html
6163
<!-- include the script -->
6264
<script type="text/javascript" src="<PATH_TO_SCRIPT>/graph-client-tokenCredentialAuthProvider.js"></script>
63-
; // create an authProvider var authProvider = new MicrosoftGraph.TokenCredentialAuthProvider.TokenCredentialAuthenticationProvider(tokenCred, { scopes: scopes }); client = MicrosoftGraph.Client.initWithMiddleware({ authProvider: authProvider, });
65+
; // create an authProvider var authProvider = new MicrosoftGraphTokenCredentialAuthProvider.TokenCredentialAuthenticationProvider(tokenCred, { scopes: scopes }); client = MicrosoftGraph.Client.initWithMiddleware({ authProvider: authProvider, });
6466
```

lib/.npmignore

-4
This file was deleted.

0 commit comments

Comments
 (0)