UNPKG

3.3 kBPlain TextView Raw
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
8import { TokenCredential } from "@azure/identity";
9
10import { GraphClientError } from "../../GraphClientError";
11import { AuthenticationProvider } from "../../IAuthenticationProvider";
12import { TokenCredentialAuthenticationProviderOptions } from "./ITokenCredentialAuthenticationProviderOptions";
13
14/**
15 * @module TokenCredentialAuthenticationProvider
16 */
17
18/**
19 * @class
20 * Class representing TokenCredentialAuthenticationProvider
21 * This feature is introduced in Version 3.0.0
22 * @extends AuthenticationProvider
23 * Reference - https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/identity/identity/README.md
24 */
25export class TokenCredentialAuthenticationProvider implements AuthenticationProvider {
26 /**
27 * @private
28 * A member holding an instance of @azure/identity TokenCredential
29 */
30 private tokenCredential: TokenCredential;
31
32 /**
33 * @private
34 * A member holding an instance of TokenCredentialAuthenticationProviderOptions
35 */
36 private authenticationProviderOptions: TokenCredentialAuthenticationProviderOptions;
37
38 /**
39 * @public
40 * @constructor
41 * Creates an instance of TokenCredentialAuthenticationProvider
42 * @param {TokenCredential} tokenCredential - An instance of @azure/identity TokenCredential
43 * @param {TokenCredentialAuthenticationProviderOptions} authenticationProviderOptions - An instance of TokenCredentialAuthenticationProviderOptions
44 * @returns An instance of TokenCredentialAuthenticationProvider
45 */
46 public constructor(tokenCredential: TokenCredential, authenticationProviderOptions: TokenCredentialAuthenticationProviderOptions) {
47 if (!tokenCredential) {
48 throw new GraphClientError("Please pass a token credential object to the TokenCredentialAuthenticationProvider class constructor");
49 }
50 if (!authenticationProviderOptions) {
51 throw new GraphClientError("Please pass the TokenCredentialAuthenticationProviderOptions with scopes to the TokenCredentialAuthenticationProvider class constructor");
52 }
53 this.authenticationProviderOptions = authenticationProviderOptions;
54 this.tokenCredential = tokenCredential;
55 }
56
57 /**
58 * @public
59 * @async
60 * To get the access token
61 * @param {TokenCredentialAuthenticationProviderOptions} authenticationProviderOptions - The authentication provider options object
62 * @returns The promise that resolves to an access token
63 */
64 public async getAccessToken(): Promise<string> {
65 const scopes = this.authenticationProviderOptions.scopes;
66 const error = new GraphClientError();
67
68 if (!scopes || scopes.length === 0) {
69 error.name = "Empty Scopes";
70 error.message = "Scopes cannot be empty, Please provide scopes";
71 throw error;
72 }
73 const response = await this.tokenCredential.getToken(scopes, this.authenticationProviderOptions.getTokenOptions);
74 if (response) {
75 return response.token;
76 }
77 error.message = "Cannot retrieve accessToken from the Token Credential object";
78 error.name = "Access token is undefined";
79 throw error;
80 }
81}