UNPKG

1.98 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
8/**
9 * @module CustomAuthenticationProvider
10 */
11
12import { GraphClientError } from "./GraphClientError";
13import { AuthenticationProvider } from "./IAuthenticationProvider";
14import { AuthProvider } from "./IAuthProvider";
15
16/**
17 * @class
18 * Class representing CustomAuthenticationProvider
19 * @extends AuthenticationProvider
20 */
21export class CustomAuthenticationProvider implements AuthenticationProvider {
22 /**
23 * @private
24 * A member to hold authProvider callback
25 */
26 private provider: AuthProvider;
27
28 /**
29 * @public
30 * @constructor
31 * Creates an instance of CustomAuthenticationProvider
32 * @param {AuthProviderCallback} provider - An authProvider function
33 * @returns An instance of CustomAuthenticationProvider
34 */
35 public constructor(provider: AuthProvider) {
36 this.provider = provider;
37 }
38
39 /**
40 * @public
41 * @async
42 * To get the access token
43 * @returns The promise that resolves to an access token
44 */
45 public async getAccessToken(): Promise<any> {
46 return new Promise((resolve: (accessToken: string) => void, reject: (error: any) => void) => {
47 this.provider(async (error: any, accessToken: string | null) => {
48 if (accessToken) {
49 resolve(accessToken);
50 } else {
51 if (!error) {
52 const invalidTokenMessage = "Access token is undefined or empty.\
53 Please provide a valid token.\
54 For more help - https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CustomAuthenticationProvider.md";
55 error = new GraphClientError(invalidTokenMessage);
56 }
57 const err = await GraphClientError.setGraphClientError(error);
58 reject(err);
59 }
60 });
61 });
62 }
63}