1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | import { GraphClientError } from "./GraphClientError";
|
13 | import { AuthenticationProvider } from "./IAuthenticationProvider";
|
14 | import { AuthProvider } from "./IAuthProvider";
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export class CustomAuthenticationProvider implements AuthenticationProvider {
|
22 | |
23 |
|
24 |
|
25 |
|
26 | private provider: AuthProvider;
|
27 |
|
28 | |
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | public constructor(provider: AuthProvider) {
|
36 | this.provider = provider;
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
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 | }
|