Skip to content

@azure/identity-TokenCredential #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
999b114
Adding token credential auth provider
nikithauc Sep 25, 2020
9e4db10
testing with samplenpm
nikithauc Sep 29, 2020
0dab297
Adding client secret cred flow
nikithauc Nov 13, 2020
6e74cda
Adding unit tests
nikithauc Nov 28, 2020
a56a103
package json change
nikithauc Nov 29, 2020
7f12ab6
Adding readme docs
nikithauc Nov 29, 2020
d70b08a
Adding project references from spec and aut
nikithauc Dec 16, 2020
ba0947d
temporarily resetting package json
nikithauc Dec 23, 2020
40e1a43
Merge with dev
nikithauc Dec 23, 2020
862081c
Updating to isomorphic-fetch 3.00, commenting some tests
nikithauc Dec 23, 2020
d89f994
Exporting authProviders from lib folder
nikithauc Dec 23, 2020
8c57c0a
Exporting authProviders from lib folder
nikithauc Dec 23, 2020
2cfaf69
Keywords, msal files path change
nikithauc Jan 3, 2021
0bcb659
Resetting file, removing empty references
nikithauc Jan 3, 2021
6f6c690
Merge branch 'dev' into Authentication
nikithauc Jan 4, 2021
39d242e
Create versionTest.yml
nikithauc Jan 5, 2021
1967788
Delete versionTest.yml
nikithauc Jan 5, 2021
e169e3c
Merging with 3.0.0
nikithauc Jan 7, 2021
48d5a01
Merge branch 'Authentication' of https://github.com/microsoftgraph/ms…
nikithauc Jan 7, 2021
a7e1a62
merge with 3.0.0
nikithauc Jan 7, 2021
7da0d9f
refactoring with error conditions and test merge
nikithauc Jan 12, 2021
c61af2e
Adding note and karma file path
nikithauc Jan 12, 2021
571bdaf
adding js sample
nikithauc Jan 13, 2021
dfce4fd
Correcting doc language errors
nikithauc Jan 13, 2021
7e39e1b
Update docs/TokenCredentialAuthenticationProvider.md
nikithauc Jan 13, 2021
fee04ec
Update docs/TokenCredentialAuthenticationProvider.md
nikithauc Jan 20, 2021
3a2fe1b
Update samples/tokenCredentialSamples/ClientCredentialFlow/README.md
nikithauc Jan 20, 2021
6c296a0
Re-adding peerdependency meta in package.json
nikithauc Jan 21, 2021
c9aff66
Merge branch 'Authentication' of https://github.com/microsoftgraph/ms…
nikithauc Jan 21, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ npm-debug.log
/lib/*
!/lib/.npmignore

/authProviders/*

src/**/*.js
src/**/*.js.map
src/**/*.d.ts

samples/**/*/secrets.ts
samples/node/secrets.json
samples/browser/src/secrets.js
samples/browser/src/graph-js-sdk.js
Expand Down
11 changes: 11 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@
"outFiles": [],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
"name": "Run auth tests",
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
"args": ["${workspaceRoot}/lib/spec/authentication/*.js"],
"cwd": "${workspaceRoot}",
"preLaunchTask": "Run Compile",
"outFiles": [],
"internalConsoleOptions": "openOnSessionStart"
},
{
"type": "node",
"request": "launch",
Expand Down
5 changes: 2 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ npm install

To edit files, open them in an editor of your choice and modify them. To create a new file, use the editor of your choice and save the new file in the appropriate location in your local copy of the repository. While working, save your work frequently.

*Note: Make sure to add unit tests to validate you changes.*
_Note: Make sure to add unit tests to validate you changes._

Once you have done with your changes, You have to build and test your changes
To build the library run,
Once you have done with your changes, You have to build and test your changes To build the library run,

```cmd
npm run build
Expand Down
60 changes: 4 additions & 56 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,65 +55,13 @@ Register your application to use Microsoft Graph API using one of the following

### 2. Authenticate for the Microsoft Graph service

The Microsoft Graph JavaScript Client Library has an adapter implementation ([ImplicitMSALAuthenticationProvider](src/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)).
The Microsoft Graph JavaScript Client Library has an adapter implementation for the following - \* ([TokenCredentialAuthenticationProvider](src/authentication/TokenCredentialAuthenticationProvider.ts)) to support [Azure Identity TokenCredential](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md) (Azure Identity client library for JavaScript) which takes care of getting the `accessToken`. @azure/identity library does not ship with this library, user has to include it externally (For including @azure/identity, refer [this](https://www.npmjs.com/package/@azure/identity)). > Learn how to [create an instance of TokenCredentialAuthenticationProvider](./docs/TokenCredentialAuthenticationProvider.md)

> **Important Note:** MSAL is supported only for frontend applications, for server-side authentication you have to implement your own AuthenticationProvider. Learn how you can create a [Custom Authentication Provider](./docs/CustomAuthenticationProvider.md).
* ([ImplicitMSALAuthenticationProvider](src/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)).

#### Creating an instance of ImplicitMSALAuthenticationProvider in browser environment
> Learn how to [create an instance of ImplicitMSALAuthenticationProvider](./docs/ImplicitMSALAuthenticationProvider.md)

Refer devDependencies in [package.json](./package.json) for the compatible msal version and update that version in below.

```html
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/<version>/js/msal.min.js"></script>
```

```typescript

// Configuration options for MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL.js-1.0.0-api-release#configuration-options
const msalConfig = {
auth: {
clientId: "your_client_id", // Client Id of the registered application
redirectUri: "your_redirect_uri",
},
};
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes

// Important Note: This library implements loginPopup and acquireTokenPopup flow, remember this while initializing the msal
// Initialize the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js#1-instantiate-the-useragentapplication
const msalApplication = new Msal.UserAgentApplication(msalConfig);
const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
```

#### Creating an instance of ImplicitMSALAuthenticationProvider in node environment

Refer devDependencies in [package.json](./package.json) for the compatible msal version and update that version in below.

```cmd
npm install msal@<version>
```

```typescript
import { UserAgentApplication } from "msal";

import { ImplicitMSALAuthenticationProvider } from "@microsoft/microsoft-graph-client/lib/src/ImplicitMSALAuthenticationProvider";
import { MSALAuthenticationProviderOptions } from '@microsoft/microsoft-graph-client/lib/src/MSALAuthenticationProviderOptions';

// An Optional options for initializing the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics#configuration-options
const msalConfig = {
auth: {
clientId: "your_client_id", // Client Id of the registered application
redirectUri: "your_redirect_uri",
},
};
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes

// Important Note: This library implements loginPopup and acquireTokenPopup flow, remember this while initializing the msal
// Initialize the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js#1-instantiate-the-useragentapplication
const msalApplication = new UserAgentApplication(msalConfig);
const options = new MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new ImplicitMSALAuthenticationProvider(msalApplication, options);
```
> **Important Note:** MSAL is supported only for frontend applications, for server-side authentication you have to implement your own AuthenticationProvider. Learn how you can create a [Custom Authentication Provider](./docs/CustomAuthenticationProvider.md).

User can integrate own preferred authentication library by implementing `IAuthenticationProvider` interface. Refer implementing [Custom Authentication Provider](./docs/CustomAuthenticationProvider.md).

Expand Down
5 changes: 5 additions & 0 deletions authProviderOptions/ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
The authProviderOptions/ folders contains barrels for exporting Authentication Provider options such as MSAL and Azure Identity Token Credentials.

[tsconfig-sub-cjs.json](../tsconfig-sub-es.json) and [tsconfig-sub-cjs.json](../tsconfig-sub-es.json) contains the config for transpiling the files to authProviders/ output folder.

This approach is used because of the limitations of creating a submodule structure - References - https://github.com/microsoft/TypeScript/issues/8305 https://github.com/microsoft/TypeScript/issues/33079
8 changes: 8 additions & 0 deletions authProviderOptions/azureTokenCredentials/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
export * from "../../lib/src/authentication/azureTokenCredentials/TokenCredentialAuthenticationProvider";
export * from "../../lib/src/authentication/azureTokenCredentials/ITokenCredentialAuthenticationProviderOptions";
8 changes: 8 additions & 0 deletions authProviderOptions/es/azureTokenCredentials/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
export * from "../../../lib/es/src/authentication/azureTokenCredentials/TokenCredentialAuthenticationProvider";
export * from "../../../lib/es/src/authentication/azureTokenCredentials/ITokenCredentialAuthenticationProviderOptions";
9 changes: 9 additions & 0 deletions authProviderOptions/es/msal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/

export * from "../../../lib/es/src/authentication/msal/ImplicitMSALAuthenticationProvider";
export * from "../../../lib/es/src/authentication/msal/MSALAuthenticationProviderOptions";
8 changes: 8 additions & 0 deletions authProviderOptions/msal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
export * from "../../lib/src/authentication/msal/MSALAuthenticationProviderOptions";
export * from "../../lib/src/authentication/msal/ImplicitMSALAuthenticationProvider";
54 changes: 54 additions & 0 deletions docs/ImplicitMSALAuthenticationProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#### Creating an instance of ImplicitMSALAuthenticationProvider in browser environment

Refer devDependencies in [package.json](../package.json) for the compatible msal version and update that version in below.

```html
<script src="https://secure.aadcdn.microsoftonline-p.com/lib/<version>/js/msal.min.js"></script>
```

```typescript
// Configuration options for MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL.js-1.0.0-api-release#configuration-options
const msalConfig = {
auth: {
clientId: "your_client_id", // Client Id of the registered application
redirectUri: "your_redirect_uri",
},
};
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes

// Important Note: This library implements loginPopup and acquireTokenPopup flow, remember this while initializing the msal
// Initialize the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js#1-instantiate-the-useragentapplication
const msalApplication = new Msal.UserAgentApplication(msalConfig);
const options = new MicrosoftGraph.MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new MicrosoftGraph.ImplicitMSALAuthenticationProvider(msalApplication, options);
```

#### Creating an instance of ImplicitMSALAuthenticationProvider in node environment

Refer devDependencies in [package.json](./package.json) for the compatible msal version and update that version in below.

```cmd
npm install msal@<version>
```

```typescript
import { UserAgentApplication } from "msal";

import { ImplicitMSALAuthenticationProvider } from "@microsoft/microsoft-graph-client/lib/src/ImplicitMSALAuthenticationProvider";
import { MSALAuthenticationProviderOptions } from "@microsoft/microsoft-graph-client/lib/src/MSALAuthenticationProviderOptions";

// An Optional options for initializing the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js/wiki/MSAL-basics#configuration-options
const msalConfig = {
auth: {
clientId: "your_client_id", // Client Id of the registered application
redirectUri: "your_redirect_uri",
},
};
const graphScopes = ["user.read", "mail.send"]; // An array of graph scopes

// Important Note: This library implements loginPopup and acquireTokenPopup flow, remember this while initializing the msal
// Initialize the MSAL @see https://github.com/AzureAD/microsoft-authentication-library-for-js#1-instantiate-the-useragentapplication
const msalApplication = new UserAgentApplication(msalConfig);
const options = new MSALAuthenticationProviderOptions(graphScopes);
const authProvider = new ImplicitMSALAuthenticationProvider(msalApplication, options);
```
30 changes: 30 additions & 0 deletions docs/TokenCredentialAuthenticationProvider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#### Creating an instance of TokenCredentialAuthentication

###### Links for more information -

- [GitHub - Azure Identity client library for JavaScript ](https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md)

- [npm - Azure Identity client library for JavaScript](https://www.npmjs.com/package/@azure/identity)

- Check the [tokenCredentialSamples folder][../samples/tokencredentialsamples].

###### Example of how to create and pass a token credential sample -

```typescript
// Import the TokenCredential class that you wish to use. This examples uses a Client SecretCredential
import { ClientSecretCredential } from "@azure/identity";

// Create an instance of the TokenCredential Class that is imported
const tokenCredential = new ClientSecretCredential("your_tenantId", "your_clientId", "your_clientSecret");

// Set your scopes and options for TokenCredential.getToken (Check the ` interface GetTokenOptions` in (TokenCredential Implementation)[https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/core/core-auth/src/tokenCredential.ts])
const options:TokenCredentialAuthenticationProviderOptions = { scopes: [scopes], getTokenoptions: <> }

// Create an instance of the TokenCredentialAuthenticationProvider by passing the tokenCredential instance and options to the constructor
const authProvider = new TokenCredentialAuthenticationProvider(tokenCredential, options);
const client = Client.initWithMiddleware({
debugLogging: true,
authProvider: authProvider,
});
const res = await client.api("/users/").get();
```
Loading