@microsoft/teams.client 0.2.13 → 2.0.0-preview.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -37
- package/dist/app.d.mts +48 -19
- package/dist/app.d.ts +48 -19
- package/dist/app.js +12 -8
- package/dist/app.js.map +1 -1
- package/dist/app.mjs +12 -8
- package/dist/app.mjs.map +1 -1
- package/dist/msal-utils.d.mts +2 -2
- package/dist/msal-utils.d.ts +2 -2
- package/dist/msal-utils.js +2 -2
- package/dist/msal-utils.js.map +1 -1
- package/dist/msal-utils.mjs +2 -2
- package/dist/msal-utils.mjs.map +1 -1
- package/package.json +6 -7
package/README.md
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
# Teams: Client
|
|
2
|
-
|
|
3
|
-
<p>
|
|
4
|
-
<a href="https://www.npmjs.com/package/@microsoft/teams.client" target="_blank">
|
|
5
|
-
<img src="https://img.shields.io/npm/v/@microsoft/teams.client" />
|
|
6
|
-
</a>
|
|
7
|
-
<a href="https://www.npmjs.com/package/@microsoft/teams.client?activeTab=code" target="_blank">
|
|
8
|
-
<img src="https://img.shields.io/bundlephobia/min/@microsoft/teams.client" />
|
|
9
|
-
</a>
|
|
10
|
-
<a href="https://www.npmjs.com/package/@microsoft/teams.client?activeTab=dependencies" target="_blank">
|
|
11
|
-
<img src="https://img.shields.io/librariesio/release/npm/@microsoft/teams.client" />
|
|
12
|
-
</a>
|
|
13
|
-
<a href="https://www.npmjs.com/package/@microsoft/teams.client" target="_blank">
|
|
14
|
-
<img src="https://img.shields.io/npm/dw/@microsoft/teams.client" />
|
|
15
|
-
</a>
|
|
16
|
-
<a href="https://microsoft.github.io/teams-ai" target="_blank">
|
|
17
|
-
<img src="https://img.shields.io/badge/📖 docs-open-blue" />
|
|
18
|
-
</a>
|
|
19
|
-
</p>
|
|
20
|
-
|
|
21
|
-
A client used to create app/bot surfaces such as tabs.
|
|
22
|
-
|
|
23
|
-
<a href="https://microsoft.github.io/teams-ai" target="_blank">
|
|
24
|
-
<img src="https://img.shields.io/badge/📖 Getting Started-blue?style=for-the-badge" />
|
|
25
|
-
</a>
|
|
26
|
-
|
|
27
|
-
## Install
|
|
28
|
-
|
|
29
|
-
```bash
|
|
30
|
-
$: npm install @microsoft/teams.client
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
## Dependencies
|
|
34
|
-
|
|
35
|
-
## Links
|
|
36
|
-
|
|
37
|
-
- https://learn.microsoft.com/en-us/microsoftteams/platform/tabs/what-are-tabs?tabs=personal
|
|
1
|
+
# Teams: Client
|
|
2
|
+
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://www.npmjs.com/package/@microsoft/teams.client" target="_blank">
|
|
5
|
+
<img src="https://img.shields.io/npm/v/@microsoft/teams.client" />
|
|
6
|
+
</a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/@microsoft/teams.client?activeTab=code" target="_blank">
|
|
8
|
+
<img src="https://img.shields.io/bundlephobia/min/@microsoft/teams.client" />
|
|
9
|
+
</a>
|
|
10
|
+
<a href="https://www.npmjs.com/package/@microsoft/teams.client?activeTab=dependencies" target="_blank">
|
|
11
|
+
<img src="https://img.shields.io/librariesio/release/npm/@microsoft/teams.client" />
|
|
12
|
+
</a>
|
|
13
|
+
<a href="https://www.npmjs.com/package/@microsoft/teams.client" target="_blank">
|
|
14
|
+
<img src="https://img.shields.io/npm/dw/@microsoft/teams.client" />
|
|
15
|
+
</a>
|
|
16
|
+
<a href="https://microsoft.github.io/teams-ai" target="_blank">
|
|
17
|
+
<img src="https://img.shields.io/badge/📖 docs-open-blue" />
|
|
18
|
+
</a>
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
A client used to create app/bot surfaces such as tabs.
|
|
22
|
+
|
|
23
|
+
<a href="https://microsoft.github.io/teams-ai" target="_blank">
|
|
24
|
+
<img src="https://img.shields.io/badge/📖 Getting Started-blue?style=for-the-badge" />
|
|
25
|
+
</a>
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
$: npm install @microsoft/teams.client
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Dependencies
|
|
34
|
+
|
|
35
|
+
## Links
|
|
36
|
+
|
|
37
|
+
- https://learn.microsoft.com/en-us/microsoftteams/platform/tabs/what-are-tabs?tabs=personal
|
package/dist/app.d.mts
CHANGED
|
@@ -4,12 +4,24 @@ import * as http from '@microsoft/teams.common/http';
|
|
|
4
4
|
import { ILogger } from '@microsoft/teams.common/logging';
|
|
5
5
|
import * as graph from '@microsoft/teams.graph';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Options to control how MSAL is initialized and used.
|
|
9
|
+
*/
|
|
10
|
+
type MsalOptions = ({
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
12
|
+
* MSAL instance to use when making authenticated function calls to remote endpoints.
|
|
13
|
+
* This is useful if you want to use a custom MSAL instance or if you want to share the
|
|
14
|
+
* same MSAL instance across multiple apps.
|
|
11
15
|
*/
|
|
16
|
+
readonly msalInstance?: msal.IPublicClientApplication;
|
|
17
|
+
readonly configuration?: never;
|
|
18
|
+
} | {
|
|
19
|
+
readonly msalInstance?: never;
|
|
20
|
+
/**
|
|
21
|
+
* MSAL configuration to use when constructing an MSAL instance used
|
|
22
|
+
* to make authenticated function calls to remote endpoints. */
|
|
12
23
|
readonly configuration?: msal.Configuration;
|
|
24
|
+
}) & {
|
|
13
25
|
/**
|
|
14
26
|
* Options to control scope consent pre-warming. If explicitly set to false, no pre-warming is performed.
|
|
15
27
|
* If no value is provided, the default scope (i.e. ".default") is pre-warmed. If a set of scopes is
|
|
@@ -18,13 +30,23 @@ type MsalOptions = {
|
|
|
18
30
|
*/
|
|
19
31
|
readonly prewarmScopes?: false | string[];
|
|
20
32
|
};
|
|
21
|
-
type
|
|
33
|
+
type RemoteApiOptions = {
|
|
22
34
|
/**
|
|
23
|
-
* The
|
|
35
|
+
* The remote API base URL. If omitted, it's assumed that the remote API is hosted in the same domain as the app.
|
|
24
36
|
*/
|
|
25
37
|
readonly baseUrl?: string;
|
|
26
|
-
/**
|
|
27
|
-
|
|
38
|
+
/**
|
|
39
|
+
* The default resource name when building a token request for the Entra token to include when invoking
|
|
40
|
+
* a remote function. This is useful when the API is hosted in an different AAD app from
|
|
41
|
+
* the caller and is typically in the format 'api://<remoteAppClientId>'.
|
|
42
|
+
* When invoking @see exec with default options or a permission name,
|
|
43
|
+
* this value is used to build a token request in the format of '<remoteAppResource>/<permission>'.
|
|
44
|
+
* If omitted, it's assumed that the API is hosted in the same AAD app as the caller, and the
|
|
45
|
+
* token request is made for 'api://<clientId>/permission'.
|
|
46
|
+
*/
|
|
47
|
+
readonly remoteAppResource?: string;
|
|
48
|
+
};
|
|
49
|
+
type AppOptions = {
|
|
28
50
|
/**
|
|
29
51
|
* Logger instance to use.
|
|
30
52
|
*/
|
|
@@ -33,6 +55,8 @@ type AppOptions = {
|
|
|
33
55
|
* Options to control how MSAL is initialized and used.
|
|
34
56
|
*/
|
|
35
57
|
readonly msalOptions?: MsalOptions;
|
|
58
|
+
/** Options to control how remote APIs are invoked. */
|
|
59
|
+
readonly remoteApiOptions?: RemoteApiOptions;
|
|
36
60
|
};
|
|
37
61
|
type AppState = {
|
|
38
62
|
phase: 'stopped' | 'starting';
|
|
@@ -45,18 +69,23 @@ type AppState = {
|
|
|
45
69
|
msalInstance: msal.IPublicClientApplication;
|
|
46
70
|
context: teamsJs.app.Context;
|
|
47
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* ExecOptions is used to specify options for the exec method.
|
|
74
|
+
*/
|
|
48
75
|
type ExecOptions = ({
|
|
49
|
-
msalTokenRequest
|
|
50
|
-
permission?: never;
|
|
51
|
-
} | {
|
|
52
|
-
msalTokenRequest?: never;
|
|
53
|
-
permission: string;
|
|
76
|
+
readonly msalTokenRequest?: msal.SilentRequest;
|
|
77
|
+
readonly permission?: never;
|
|
54
78
|
} | {
|
|
55
|
-
msalTokenRequest?: never;
|
|
56
|
-
permission?:
|
|
79
|
+
readonly msalTokenRequest?: never;
|
|
80
|
+
readonly permission?: string;
|
|
57
81
|
}) & {
|
|
58
|
-
requestHeaders?: Record<string, string>;
|
|
82
|
+
readonly requestHeaders?: Record<string, string>;
|
|
59
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* The main entry point for this library. This class streamlines Microsoft Teams app development
|
|
86
|
+
* by simplifying the process of managing authentication, interacting with Microsoft Graph APIs,
|
|
87
|
+
* and executing server-side functions.
|
|
88
|
+
*/
|
|
60
89
|
declare class App {
|
|
61
90
|
readonly options: AppOptions;
|
|
62
91
|
readonly http: http.Client;
|
|
@@ -64,15 +93,15 @@ declare class App {
|
|
|
64
93
|
readonly clientId: string;
|
|
65
94
|
protected _state: AppState;
|
|
66
95
|
/**
|
|
67
|
-
*
|
|
96
|
+
* The apps logger
|
|
68
97
|
*/
|
|
69
98
|
get log(): ILogger;
|
|
70
99
|
protected _log: ILogger;
|
|
71
100
|
/**
|
|
72
|
-
*
|
|
101
|
+
* The date/time when the app was successfully started.
|
|
73
102
|
*/
|
|
74
103
|
get startedAt(): Date | undefined;
|
|
75
|
-
/**
|
|
104
|
+
/** The msal instance used in this app. undefined until the app is started. */
|
|
76
105
|
get msalInstance(): msal.IPublicClientApplication | undefined;
|
|
77
106
|
constructor(clientId: string, options?: AppOptions);
|
|
78
107
|
/**
|
|
@@ -110,4 +139,4 @@ declare class App {
|
|
|
110
139
|
private appStateGuard;
|
|
111
140
|
}
|
|
112
141
|
|
|
113
|
-
export { App, type AppOptions, type ExecOptions, type MsalOptions };
|
|
142
|
+
export { App, type AppOptions, type ExecOptions, type MsalOptions, type RemoteApiOptions };
|
package/dist/app.d.ts
CHANGED
|
@@ -4,12 +4,24 @@ import * as http from '@microsoft/teams.common/http';
|
|
|
4
4
|
import { ILogger } from '@microsoft/teams.common/logging';
|
|
5
5
|
import * as graph from '@microsoft/teams.graph';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Options to control how MSAL is initialized and used.
|
|
9
|
+
*/
|
|
10
|
+
type MsalOptions = ({
|
|
8
11
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
12
|
+
* MSAL instance to use when making authenticated function calls to remote endpoints.
|
|
13
|
+
* This is useful if you want to use a custom MSAL instance or if you want to share the
|
|
14
|
+
* same MSAL instance across multiple apps.
|
|
11
15
|
*/
|
|
16
|
+
readonly msalInstance?: msal.IPublicClientApplication;
|
|
17
|
+
readonly configuration?: never;
|
|
18
|
+
} | {
|
|
19
|
+
readonly msalInstance?: never;
|
|
20
|
+
/**
|
|
21
|
+
* MSAL configuration to use when constructing an MSAL instance used
|
|
22
|
+
* to make authenticated function calls to remote endpoints. */
|
|
12
23
|
readonly configuration?: msal.Configuration;
|
|
24
|
+
}) & {
|
|
13
25
|
/**
|
|
14
26
|
* Options to control scope consent pre-warming. If explicitly set to false, no pre-warming is performed.
|
|
15
27
|
* If no value is provided, the default scope (i.e. ".default") is pre-warmed. If a set of scopes is
|
|
@@ -18,13 +30,23 @@ type MsalOptions = {
|
|
|
18
30
|
*/
|
|
19
31
|
readonly prewarmScopes?: false | string[];
|
|
20
32
|
};
|
|
21
|
-
type
|
|
33
|
+
type RemoteApiOptions = {
|
|
22
34
|
/**
|
|
23
|
-
* The
|
|
35
|
+
* The remote API base URL. If omitted, it's assumed that the remote API is hosted in the same domain as the app.
|
|
24
36
|
*/
|
|
25
37
|
readonly baseUrl?: string;
|
|
26
|
-
/**
|
|
27
|
-
|
|
38
|
+
/**
|
|
39
|
+
* The default resource name when building a token request for the Entra token to include when invoking
|
|
40
|
+
* a remote function. This is useful when the API is hosted in an different AAD app from
|
|
41
|
+
* the caller and is typically in the format 'api://<remoteAppClientId>'.
|
|
42
|
+
* When invoking @see exec with default options or a permission name,
|
|
43
|
+
* this value is used to build a token request in the format of '<remoteAppResource>/<permission>'.
|
|
44
|
+
* If omitted, it's assumed that the API is hosted in the same AAD app as the caller, and the
|
|
45
|
+
* token request is made for 'api://<clientId>/permission'.
|
|
46
|
+
*/
|
|
47
|
+
readonly remoteAppResource?: string;
|
|
48
|
+
};
|
|
49
|
+
type AppOptions = {
|
|
28
50
|
/**
|
|
29
51
|
* Logger instance to use.
|
|
30
52
|
*/
|
|
@@ -33,6 +55,8 @@ type AppOptions = {
|
|
|
33
55
|
* Options to control how MSAL is initialized and used.
|
|
34
56
|
*/
|
|
35
57
|
readonly msalOptions?: MsalOptions;
|
|
58
|
+
/** Options to control how remote APIs are invoked. */
|
|
59
|
+
readonly remoteApiOptions?: RemoteApiOptions;
|
|
36
60
|
};
|
|
37
61
|
type AppState = {
|
|
38
62
|
phase: 'stopped' | 'starting';
|
|
@@ -45,18 +69,23 @@ type AppState = {
|
|
|
45
69
|
msalInstance: msal.IPublicClientApplication;
|
|
46
70
|
context: teamsJs.app.Context;
|
|
47
71
|
};
|
|
72
|
+
/**
|
|
73
|
+
* ExecOptions is used to specify options for the exec method.
|
|
74
|
+
*/
|
|
48
75
|
type ExecOptions = ({
|
|
49
|
-
msalTokenRequest
|
|
50
|
-
permission?: never;
|
|
51
|
-
} | {
|
|
52
|
-
msalTokenRequest?: never;
|
|
53
|
-
permission: string;
|
|
76
|
+
readonly msalTokenRequest?: msal.SilentRequest;
|
|
77
|
+
readonly permission?: never;
|
|
54
78
|
} | {
|
|
55
|
-
msalTokenRequest?: never;
|
|
56
|
-
permission?:
|
|
79
|
+
readonly msalTokenRequest?: never;
|
|
80
|
+
readonly permission?: string;
|
|
57
81
|
}) & {
|
|
58
|
-
requestHeaders?: Record<string, string>;
|
|
82
|
+
readonly requestHeaders?: Record<string, string>;
|
|
59
83
|
};
|
|
84
|
+
/**
|
|
85
|
+
* The main entry point for this library. This class streamlines Microsoft Teams app development
|
|
86
|
+
* by simplifying the process of managing authentication, interacting with Microsoft Graph APIs,
|
|
87
|
+
* and executing server-side functions.
|
|
88
|
+
*/
|
|
60
89
|
declare class App {
|
|
61
90
|
readonly options: AppOptions;
|
|
62
91
|
readonly http: http.Client;
|
|
@@ -64,15 +93,15 @@ declare class App {
|
|
|
64
93
|
readonly clientId: string;
|
|
65
94
|
protected _state: AppState;
|
|
66
95
|
/**
|
|
67
|
-
*
|
|
96
|
+
* The apps logger
|
|
68
97
|
*/
|
|
69
98
|
get log(): ILogger;
|
|
70
99
|
protected _log: ILogger;
|
|
71
100
|
/**
|
|
72
|
-
*
|
|
101
|
+
* The date/time when the app was successfully started.
|
|
73
102
|
*/
|
|
74
103
|
get startedAt(): Date | undefined;
|
|
75
|
-
/**
|
|
104
|
+
/** The msal instance used in this app. undefined until the app is started. */
|
|
76
105
|
get msalInstance(): msal.IPublicClientApplication | undefined;
|
|
77
106
|
constructor(clientId: string, options?: AppOptions);
|
|
78
107
|
/**
|
|
@@ -110,4 +139,4 @@ declare class App {
|
|
|
110
139
|
private appStateGuard;
|
|
111
140
|
}
|
|
112
141
|
|
|
113
|
-
export { App, type AppOptions, type ExecOptions, type MsalOptions };
|
|
142
|
+
export { App, type AppOptions, type ExecOptions, type MsalOptions, type RemoteApiOptions };
|
package/dist/app.js
CHANGED
|
@@ -36,19 +36,19 @@ class App {
|
|
|
36
36
|
clientId;
|
|
37
37
|
_state = { phase: "stopped" };
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
39
|
+
* The apps logger
|
|
40
40
|
*/
|
|
41
41
|
get log() {
|
|
42
42
|
return this._log;
|
|
43
43
|
}
|
|
44
44
|
_log;
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
46
|
+
* The date/time when the app was successfully started.
|
|
47
47
|
*/
|
|
48
48
|
get startedAt() {
|
|
49
49
|
return this._state?.startedAt;
|
|
50
50
|
}
|
|
51
|
-
/**
|
|
51
|
+
/** The msal instance used in this app. undefined until the app is started. */
|
|
52
52
|
get msalInstance() {
|
|
53
53
|
return this._state.msalInstance;
|
|
54
54
|
}
|
|
@@ -59,7 +59,7 @@ class App {
|
|
|
59
59
|
this.clientId = clientId;
|
|
60
60
|
this.options = options;
|
|
61
61
|
this._log = options?.logger || new logging.ConsoleLogger("@teams/client");
|
|
62
|
-
this.http = new http__namespace.Client({ baseUrl: options?.baseUrl });
|
|
62
|
+
this.http = new http__namespace.Client({ baseUrl: options?.remoteApiOptions?.baseUrl });
|
|
63
63
|
this.graph = graphUtils.buildGraphClient(() => this.appStateGuard(), this._log);
|
|
64
64
|
}
|
|
65
65
|
/**
|
|
@@ -76,9 +76,12 @@ class App {
|
|
|
76
76
|
this._state = { phase: "starting" };
|
|
77
77
|
await teamsJs__namespace.app.initialize();
|
|
78
78
|
const context = await teamsJs__namespace.app.getContext();
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
let msalInstance = this.options.msalOptions?.msalInstance;
|
|
80
|
+
if (!msalInstance) {
|
|
81
|
+
const msalConfig = this.options.msalOptions?.configuration ?? msalUtils.buildMsalConfig(this.clientId, this._log);
|
|
82
|
+
msalInstance = await msal__namespace.createNestablePublicClientApplication(msalConfig);
|
|
83
|
+
await msalInstance.initialize();
|
|
84
|
+
}
|
|
82
85
|
this._state = { phase: "started", msalInstance, context, startedAt: /* @__PURE__ */ new Date() };
|
|
83
86
|
if (this.options.msalOptions?.prewarmScopes !== false) {
|
|
84
87
|
const scopes = this.options.msalOptions?.prewarmScopes ?? [".default"];
|
|
@@ -99,9 +102,10 @@ class App {
|
|
|
99
102
|
*/
|
|
100
103
|
async exec(name, data, options) {
|
|
101
104
|
const { msalInstance, context } = this.appStateGuard();
|
|
105
|
+
const remoteAppResource = this.options.remoteApiOptions?.remoteAppResource ?? `api://${this.clientId}`;
|
|
102
106
|
const accessToken = await msalUtils.acquireMsalAccessToken(
|
|
103
107
|
msalInstance,
|
|
104
|
-
options?.msalTokenRequest ?? msalUtils.getStandardExecSilentRequest(
|
|
108
|
+
options?.msalTokenRequest ?? msalUtils.getStandardExecSilentRequest(remoteAppResource, options?.permission),
|
|
105
109
|
this._log
|
|
106
110
|
);
|
|
107
111
|
const res = await this.http.post(`/api/functions/${name}`, data, {
|
package/dist/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/app.ts"],"names":["ConsoleLogger","http","buildGraphClient","teamsJs","buildMsalConfig","msal","acquireMsalAccessToken","getStandardExecSilentRequest","hasConsentForScopes"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEO,MAAM,GAAI,CAAA;AAAA,EACN,OAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,QAAA;AAAA,EACC,MAAA,GAAmB,EAAE,KAAA,EAAO,SAAU,EAAA;AAAA;AAAA;AAAA;AAAA,EAKhD,IAAI,GAAM,GAAA;AACR,IAAA,OAAO,IAAK,CAAA,IAAA;AAAA;AACd,EACU,IAAA;AAAA;AAAA;AAAA;AAAA,EAKV,IAAI,SAAY,GAAA;AACd,IAAA,OAAO,KAAK,MAAQ,EAAA,SAAA;AAAA;AACtB;AAAA,EAGA,IAAI,YAAe,GAAA;AACjB,IAAA,OAAO,KAAK,MAAO,CAAA,YAAA;AAAA;AACrB,EAEA,WAAY,CAAA,QAAA,EAAkB,OAAsB,GAAA,EAAI,EAAA;AACtD,IAAA,IAAI,CAAC,QAAU,EAAA;AACb,MAAM,MAAA,IAAI,MAAM,oBAAoB,CAAA;AAAA;AAGtC,IAAA,IAAA,CAAK,QAAW,GAAA,QAAA;AAChB,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA;AACf,IAAA,IAAA,CAAK,IAAO,GAAA,OAAA,EAAS,MAAU,IAAA,IAAIA,sBAAc,eAAe,CAAA;AAChE,IAAK,IAAA,CAAA,IAAA,GAAO,IAAIC,eAAK,CAAA,MAAA,CAAO,EAAE,OAAS,EAAA,OAAA,EAAS,SAAS,CAAA;AACzD,IAAA,IAAA,CAAK,QAAQC,2BAAiB,CAAA,MAAM,KAAK,aAAc,EAAA,EAAG,KAAK,IAAI,CAAA;AAAA;AACrE;AAAA;AAAA;A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A,IAAA,CAAK,aAAc,EAAA;AAE5C,IAAA,OAAO,MAAMC,6BAAA,CAAoB,YAAc,EAAA,MAAA,EAAQ,KAAK,GAAG,CAAA;AAAA;AACjE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,uBAAuB,MAAoC,EAAA;AAC/D,IAAA,MAAM,EAAE,YAAA,EAAiB,GAAA,IAAA,CAAK,aAAc,EAAA;AAE5C,IAAI,IAAA;AACF,MAAM,MAAA,KAAA,GAAQ,MAAMF,gCAAuB,CAAA,YAAA,EAAc,EAAE,MAAO,EAAA,EAAG,KAAK,GAAG,CAAA;AAC7E,MAAA,OAAO,CAAC,CAAC,KAAA;AAAA,aACF,EAAI,EAAA;AACX,MAAO,OAAA,KAAA;AAAA;AACT;AACF,EAEQ,aAAiD,GAAA;AACvD,IAAI,IAAA,IAAA,CAAK,MAAO,CAAA,KAAA,KAAU,SAAW,EAAA;AACnC,MAAM,MAAA,IAAI,MAAM,iBAAiB,CAAA;AAAA;AAEnC,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AAEhB","file":"app.js","sourcesContent":["import * as msal from '@azure/msal-browser';\r\n\r\nimport * as teamsJs from '@microsoft/teams-js';\r\nimport * as http from '@microsoft/teams.common/http';\r\nimport { ILogger, ConsoleLogger } from '@microsoft/teams.common/logging';\r\nimport * as graph from '@microsoft/teams.graph';\r\n\r\nimport { buildGraphClient } from './graph-utils';\r\nimport {\r\n acquireMsalAccessToken,\r\n buildMsalConfig,\r\n getStandardExecSilentRequest,\r\n hasConsentForScopes,\r\n} from './msal-utils';\r\n\r\nexport type MsalOptions = {\r\n /**\r\n * Optional MSAL configuration. This parameter is used to construct an MSAL instance in order\r\n * to make authenticated function calls. If omitted, a default configuration is created.\r\n */\r\n readonly configuration?: msal.Configuration;\r\n /**\r\n * Options to control scope consent pre-warming. If explicitly set to false, no pre-warming is performed.\r\n * If no value is provided, the default scope (i.e. \".default\") is pre-warmed. If a set of scopes is\r\n * provided, the specified scopes are pre-warmed. The scopes should be for a single resource, and they\r\n * should not mix the .default scope with named scopes.\r\n */\r\n readonly prewarmScopes?: false | string[];\r\n};\r\n\r\nexport type AppOptions = {\r\n /**\r\n * The app base url.\r\n */\r\n readonly baseUrl?: string;\r\n\r\n /** App tenant ID */\r\n readonly tenantId?: string;\r\n\r\n /**\r\n * Logger instance to use.\r\n */\r\n readonly logger?: ILogger;\r\n\r\n /**\r\n * Options to control how MSAL is initialized and used.\r\n */\r\n readonly msalOptions?: MsalOptions;\r\n};\r\n\r\ntype AppState =\r\n | {\r\n phase: 'stopped' | 'starting';\r\n startedAt?: never;\r\n msalInstance?: never;\r\n context?: never;\r\n }\r\n | {\r\n phase: 'started';\r\n startedAt: Date;\r\n msalInstance: msal.IPublicClientApplication;\r\n context: teamsJs.app.Context;\r\n };\r\n\r\nexport type ExecOptions = (\r\n | { msalTokenRequest: msal.SilentRequest; permission?: never }\r\n | { msalTokenRequest?: never; permission: string }\r\n | { msalTokenRequest?: never; permission?: never }\r\n) & {\r\n requestHeaders?: Record<string, string>;\r\n};\r\n\r\nexport class App {\r\n readonly options: AppOptions;\r\n readonly http: http.Client;\r\n readonly graph: graph.Client;\r\n readonly clientId: string;\r\n protected _state: AppState = { phase: 'stopped' };\r\n\r\n /**\r\n * the apps logger\r\n */\r\n get log() {\r\n return this._log;\r\n }\r\n protected _log: ILogger;\r\n\r\n /**\r\n * the date/time when the app was successfully started.\r\n */\r\n get startedAt() {\r\n return this._state?.startedAt;\r\n }\r\n\r\n /** the msal instance used in this app. undefined until the app is started. */\r\n get msalInstance() {\r\n return this._state.msalInstance;\r\n }\r\n\r\n constructor(clientId: string, options: AppOptions = {}) {\r\n if (!clientId) {\r\n throw new Error('Invalid client ID.');\r\n }\r\n\r\n this.clientId = clientId;\r\n this.options = options;\r\n this._log = options?.logger || new ConsoleLogger('@teams/client');\r\n this.http = new http.Client({ baseUrl: options?.baseUrl });\r\n this.graph = buildGraphClient(() => this.appStateGuard(), this._log);\r\n }\r\n\r\n /**\r\n * Starts the library and initializes the dependent teams-js and MSAL libraries.\r\n * @returns A promise that will be fulfilled when the app has started, or\r\n * rejected if the initialization fails or times out.\r\n */\r\n async start(): Promise<void> {\r\n if (this._state.phase !== 'stopped') {\r\n this._log.debug(`app already ${this._state.phase}`);\r\n return;\r\n }\r\n\r\n this._log.debug('app starting');\r\n this._state = { phase: 'starting' };\r\n\r\n await teamsJs.app.initialize();\r\n const context = await teamsJs.app.getContext();\r\n\r\n const msalConfig =\r\n this.options.msalOptions?.configuration ?? buildMsalConfig(this.clientId, this._log);\r\n const msalInstance = await msal.createNestablePublicClientApplication(msalConfig);\r\n await msalInstance.initialize();\r\n\r\n this._state = { phase: 'started', msalInstance, context, startedAt: new Date() };\r\n\r\n // pre-warm consent for the specified scopes\r\n if (this.options.msalOptions?.prewarmScopes !== false) {\r\n const scopes = this.options.msalOptions?.prewarmScopes ?? ['.default'];\r\n this._log.debug(`prewarming consent for scopes: ${scopes.join(', ')}`);\r\n await this.ensureConsentForScopes(scopes);\r\n }\r\n\r\n this._log.debug('app started');\r\n }\r\n\r\n /**\r\n * Execute a server-side function\r\n * @param name The unique function name\r\n * @param data The data to send\r\n * @param options Options\r\n * @param options.msalTokenRequest Optional MSAL token request.\r\n * If omitted, a default token request is used.\r\n * @param options.requestHeaders Optional additional request headers.\r\n * @returns The function response\r\n */\r\n async exec<T = unknown>(name: string, data?: unknown, options?: ExecOptions): Promise<T> {\r\n const { msalInstance, context } = this.appStateGuard();\r\n\r\n const accessToken = await acquireMsalAccessToken(\r\n msalInstance,\r\n options?.msalTokenRequest ?? getStandardExecSilentRequest(this.clientId, options?.permission),\r\n this._log\r\n );\r\n\r\n const res = await this.http.post<T>(`/api/functions/${name}`, data, {\r\n headers: {\r\n authorization: `Bearer ${accessToken}`,\r\n 'x-teams-app-session-id': context.app.sessionId,\r\n 'x-teams-channel-id': context.channel?.id,\r\n 'x-teams-chat-id': context.chat?.id,\r\n 'x-teams-meeting-id': context.meeting?.id,\r\n 'x-teams-message-id': context.app.parentMessageId,\r\n 'x-teams-page-id': context.page.id,\r\n 'x-teams-sub-page-id': context.page.subPageId,\r\n 'x-teams-team-id': context.team?.internalId,\r\n ...(options?.requestHeaders ?? {}),\r\n },\r\n });\r\n\r\n return res.data;\r\n }\r\n\r\n /**\r\n * Tests whether the user has consented to the specified scopes without prompting the user for consent.\r\n * @param scopes The scopes to check consent for.The scopes should be for a single resource, and they\r\n * should not mix the .default scope with named scopes.\r\n * @returns A promise that resolves to a boolean indicating whether the user has consented to the scopes.\r\n */\r\n async hasConsentForScopes(scopes: string[]): Promise<boolean> {\r\n const { msalInstance } = this.appStateGuard();\r\n\r\n return await hasConsentForScopes(msalInstance, scopes, this.log);\r\n }\r\n\r\n /**\r\n * Tests whether the user has consented to the specified scopes, and prompts them if not. This is useful for ensuring\r\n * that the user has consented to the required scopes before calling a graph API or other resource.\r\n * @param scopes - The scopes to prewarm consent for. The scopes should be for a single resource, and they\r\n * should not mix the .default scope with named scopes.\r\n * @returns A value indicating whether consent has been acquired for the specified scopes.\r\n */\r\n async ensureConsentForScopes(scopes: string[]): Promise<boolean> {\r\n const { msalInstance } = this.appStateGuard();\r\n\r\n try {\r\n const token = await acquireMsalAccessToken(msalInstance, { scopes }, this.log);\r\n return !!token;\r\n } catch (ex) {\r\n return false;\r\n }\r\n }\r\n\r\n private appStateGuard(): AppState & { phase: 'started' } {\r\n if (this._state.phase !== 'started') {\r\n throw new Error('App not started');\r\n }\r\n return this._state;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/app.ts"],"names":["ConsoleLogger","http","buildGraphClient","teamsJs","buildMsalConfig","msal","acquireMsalAccessToken","getStandardExecSilentRequest","hasConsentForScopes"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2GO,MAAM,GAAI,CAAA;AAAA,EACN,OAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,QAAA;AAAA,EACC,MAAA,GA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jE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,uBAAuB,MAAoC,EAAA;AAC/D,IAAA,MAAM,EAAE,YAAA,EAAiB,GAAA,IAAA,CAAK,aAAc,EAAA;AAE5C,IAAI,IAAA;AACF,MAAM,MAAA,KAAA,GAAQ,MAAMF,gCAAuB,CAAA,YAAA,EAAc,EAAE,MAAO,EAAA,EAAG,KAAK,GAAG,CAAA;AAC7E,MAAA,OAAO,CAAC,CAAC,KAAA;AAAA,aACF,EAAI,EAAA;AACX,MAAO,OAAA,KAAA;AAAA;AACT;AACF,EAEQ,aAAiD,GAAA;AACvD,IAAI,IAAA,IAAA,CAAK,MAAO,CAAA,KAAA,KAAU,SAAW,EAAA;AACnC,MAAM,MAAA,IAAI,MAAM,iBAAiB,CAAA;AAAA;AAEnC,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AAEhB","file":"app.js","sourcesContent":["import * as msal from '@azure/msal-browser';\n\nimport * as teamsJs from '@microsoft/teams-js';\nimport * as http from '@microsoft/teams.common/http';\nimport { ILogger, ConsoleLogger } from '@microsoft/teams.common/logging';\nimport * as graph from '@microsoft/teams.graph';\n\nimport { buildGraphClient } from './graph-utils';\nimport {\n acquireMsalAccessToken,\n buildMsalConfig,\n getStandardExecSilentRequest,\n hasConsentForScopes,\n} from './msal-utils';\n\n/**\n * Options to control how MSAL is initialized and used.\n */\nexport type MsalOptions = (\n | {\n /**\n * MSAL instance to use when making authenticated function calls to remote endpoints.\n * This is useful if you want to use a custom MSAL instance or if you want to share the\n * same MSAL instance across multiple apps.\n */\n readonly msalInstance?: msal.IPublicClientApplication;\n readonly configuration?: never;\n }\n | {\n readonly msalInstance?: never;\n /**\n * MSAL configuration to use when constructing an MSAL instance used\n * to make authenticated function calls to remote endpoints. */\n readonly configuration?: msal.Configuration;\n }\n) & {\n /**\n * Options to control scope consent pre-warming. If explicitly set to false, no pre-warming is performed.\n * If no value is provided, the default scope (i.e. \".default\") is pre-warmed. If a set of scopes is\n * provided, the specified scopes are pre-warmed. The scopes should be for a single resource, and they\n * should not mix the .default scope with named scopes.\n */\n readonly prewarmScopes?: false | string[];\n};\n\nexport type RemoteApiOptions = {\n /**\n * The remote API base URL. If omitted, it's assumed that the remote API is hosted in the same domain as the app.\n */\n readonly baseUrl?: string;\n\n /**\n * The default resource name when building a token request for the Entra token to include when invoking\n * a remote function. This is useful when the API is hosted in an different AAD app from\n * the caller and is typically in the format 'api://<remoteAppClientId>'.\n * When invoking @see exec with default options or a permission name,\n * this value is used to build a token request in the format of '<remoteAppResource>/<permission>'.\n * If omitted, it's assumed that the API is hosted in the same AAD app as the caller, and the\n * token request is made for 'api://<clientId>/permission'.\n */\n readonly remoteAppResource?: string;\n};\n\nexport type AppOptions = {\n /**\n * Logger instance to use.\n */\n readonly logger?: ILogger;\n\n /**\n * Options to control how MSAL is initialized and used.\n */\n readonly msalOptions?: MsalOptions;\n\n /** Options to control how remote APIs are invoked. */\n readonly remoteApiOptions?: RemoteApiOptions;\n};\n\ntype AppState =\n | {\n phase: 'stopped' | 'starting';\n startedAt?: never;\n msalInstance?: never;\n context?: never;\n }\n | {\n phase: 'started';\n startedAt: Date;\n msalInstance: msal.IPublicClientApplication;\n context: teamsJs.app.Context;\n };\n\n/**\n * ExecOptions is used to specify options for the exec method.\n */\nexport type ExecOptions = (\n | { readonly msalTokenRequest?: msal.SilentRequest; readonly permission?: never }\n | { readonly msalTokenRequest?: never; readonly permission?: string }\n) & {\n readonly requestHeaders?: Record<string, string>;\n};\n\n/**\n * The main entry point for this library. This class streamlines Microsoft Teams app development\n * by simplifying the process of managing authentication, interacting with Microsoft Graph APIs,\n * and executing server-side functions.\n */\nexport class App {\n readonly options: AppOptions;\n readonly http: http.Client;\n readonly graph: graph.Client;\n readonly clientId: string;\n protected _state: AppState = { phase: 'stopped' };\n\n /**\n * The apps logger\n */\n get log() {\n return this._log;\n }\n protected _log: ILogger;\n\n /**\n * The date/time when the app was successfully started.\n */\n get startedAt() {\n return this._state?.startedAt;\n }\n\n /** The msal instance used in this app. undefined until the app is started. */\n get msalInstance() {\n return this._state.msalInstance;\n }\n\n constructor(clientId: string, options: AppOptions = {}) {\n if (!clientId) {\n throw new Error('Invalid client ID.');\n }\n\n this.clientId = clientId;\n this.options = options;\n this._log = options?.logger || new ConsoleLogger('@teams/client');\n this.http = new http.Client({ baseUrl: options?.remoteApiOptions?.baseUrl });\n this.graph = buildGraphClient(() => this.appStateGuard(), this._log);\n }\n\n /**\n * Starts the library and initializes the dependent teams-js and MSAL libraries.\n * @returns A promise that will be fulfilled when the app has started, or\n * rejected if the initialization fails or times out.\n */\n async start(): Promise<void> {\n if (this._state.phase !== 'stopped') {\n this._log.debug(`app already ${this._state.phase}`);\n return;\n }\n\n this._log.debug('app starting');\n this._state = { phase: 'starting' };\n\n await teamsJs.app.initialize();\n const context = await teamsJs.app.getContext();\n\n let msalInstance = this.options.msalOptions?.msalInstance;\n if (!msalInstance) {\n const msalConfig =\n this.options.msalOptions?.configuration ?? buildMsalConfig(this.clientId, this._log);\n msalInstance = await msal.createNestablePublicClientApplication(msalConfig);\n await msalInstance.initialize();\n }\n\n this._state = { phase: 'started', msalInstance, context, startedAt: new Date() };\n\n // pre-warm consent for the specified scopes\n if (this.options.msalOptions?.prewarmScopes !== false) {\n const scopes = this.options.msalOptions?.prewarmScopes ?? ['.default'];\n this._log.debug(`prewarming consent for scopes: ${scopes.join(', ')}`);\n await this.ensureConsentForScopes(scopes);\n }\n\n this._log.debug('app started');\n }\n\n /**\n * Execute a server-side function\n * @param name The unique function name\n * @param data The data to send\n * @param options Options\n * @param options.msalTokenRequest Optional MSAL token request.\n * If omitted, a default token request is used.\n * @param options.requestHeaders Optional additional request headers.\n * @returns The function response\n */\n async exec<T = unknown>(name: string, data?: unknown, options?: ExecOptions): Promise<T> {\n const { msalInstance, context } = this.appStateGuard();\n\n const remoteAppResource =\n this.options.remoteApiOptions?.remoteAppResource ?? `api://${this.clientId}`;\n const accessToken = await acquireMsalAccessToken(\n msalInstance,\n options?.msalTokenRequest ??\n getStandardExecSilentRequest(remoteAppResource, options?.permission),\n this._log\n );\n\n const res = await this.http.post<T>(`/api/functions/${name}`, data, {\n headers: {\n authorization: `Bearer ${accessToken}`,\n 'x-teams-app-session-id': context.app.sessionId,\n 'x-teams-channel-id': context.channel?.id,\n 'x-teams-chat-id': context.chat?.id,\n 'x-teams-meeting-id': context.meeting?.id,\n 'x-teams-message-id': context.app.parentMessageId,\n 'x-teams-page-id': context.page.id,\n 'x-teams-sub-page-id': context.page.subPageId,\n 'x-teams-team-id': context.team?.internalId,\n ...(options?.requestHeaders ?? {}),\n },\n });\n\n return res.data;\n }\n\n /**\n * Tests whether the user has consented to the specified scopes without prompting the user for consent.\n * @param scopes The scopes to check consent for.The scopes should be for a single resource, and they\n * should not mix the .default scope with named scopes.\n * @returns A promise that resolves to a boolean indicating whether the user has consented to the scopes.\n */\n async hasConsentForScopes(scopes: string[]): Promise<boolean> {\n const { msalInstance } = this.appStateGuard();\n\n return await hasConsentForScopes(msalInstance, scopes, this.log);\n }\n\n /**\n * Tests whether the user has consented to the specified scopes, and prompts them if not. This is useful for ensuring\n * that the user has consented to the required scopes before calling a graph API or other resource.\n * @param scopes - The scopes to prewarm consent for. The scopes should be for a single resource, and they\n * should not mix the .default scope with named scopes.\n * @returns A value indicating whether consent has been acquired for the specified scopes.\n */\n async ensureConsentForScopes(scopes: string[]): Promise<boolean> {\n const { msalInstance } = this.appStateGuard();\n\n try {\n const token = await acquireMsalAccessToken(msalInstance, { scopes }, this.log);\n return !!token;\n } catch (ex) {\n return false;\n }\n }\n\n private appStateGuard(): AppState & { phase: 'started' } {\n if (this._state.phase !== 'started') {\n throw new Error('App not started');\n }\n return this._state;\n }\n}\n"]}
|
package/dist/app.mjs
CHANGED
|
@@ -12,19 +12,19 @@ class App {
|
|
|
12
12
|
clientId;
|
|
13
13
|
_state = { phase: "stopped" };
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* The apps logger
|
|
16
16
|
*/
|
|
17
17
|
get log() {
|
|
18
18
|
return this._log;
|
|
19
19
|
}
|
|
20
20
|
_log;
|
|
21
21
|
/**
|
|
22
|
-
*
|
|
22
|
+
* The date/time when the app was successfully started.
|
|
23
23
|
*/
|
|
24
24
|
get startedAt() {
|
|
25
25
|
return this._state?.startedAt;
|
|
26
26
|
}
|
|
27
|
-
/**
|
|
27
|
+
/** The msal instance used in this app. undefined until the app is started. */
|
|
28
28
|
get msalInstance() {
|
|
29
29
|
return this._state.msalInstance;
|
|
30
30
|
}
|
|
@@ -35,7 +35,7 @@ class App {
|
|
|
35
35
|
this.clientId = clientId;
|
|
36
36
|
this.options = options;
|
|
37
37
|
this._log = options?.logger || new ConsoleLogger("@teams/client");
|
|
38
|
-
this.http = new http.Client({ baseUrl: options?.baseUrl });
|
|
38
|
+
this.http = new http.Client({ baseUrl: options?.remoteApiOptions?.baseUrl });
|
|
39
39
|
this.graph = buildGraphClient(() => this.appStateGuard(), this._log);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
@@ -52,9 +52,12 @@ class App {
|
|
|
52
52
|
this._state = { phase: "starting" };
|
|
53
53
|
await teamsJs.app.initialize();
|
|
54
54
|
const context = await teamsJs.app.getContext();
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
let msalInstance = this.options.msalOptions?.msalInstance;
|
|
56
|
+
if (!msalInstance) {
|
|
57
|
+
const msalConfig = this.options.msalOptions?.configuration ?? buildMsalConfig(this.clientId, this._log);
|
|
58
|
+
msalInstance = await msal.createNestablePublicClientApplication(msalConfig);
|
|
59
|
+
await msalInstance.initialize();
|
|
60
|
+
}
|
|
58
61
|
this._state = { phase: "started", msalInstance, context, startedAt: /* @__PURE__ */ new Date() };
|
|
59
62
|
if (this.options.msalOptions?.prewarmScopes !== false) {
|
|
60
63
|
const scopes = this.options.msalOptions?.prewarmScopes ?? [".default"];
|
|
@@ -75,9 +78,10 @@ class App {
|
|
|
75
78
|
*/
|
|
76
79
|
async exec(name, data, options) {
|
|
77
80
|
const { msalInstance, context } = this.appStateGuard();
|
|
81
|
+
const remoteAppResource = this.options.remoteApiOptions?.remoteAppResource ?? `api://${this.clientId}`;
|
|
78
82
|
const accessToken = await acquireMsalAccessToken(
|
|
79
83
|
msalInstance,
|
|
80
|
-
options?.msalTokenRequest ?? getStandardExecSilentRequest(
|
|
84
|
+
options?.msalTokenRequest ?? getStandardExecSilentRequest(remoteAppResource, options?.permission),
|
|
81
85
|
this._log
|
|
82
86
|
);
|
|
83
87
|
const res = await this.http.post(`/api/functions/${name}`, data, {
|
package/dist/app.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/app.ts"],"names":[],"mappings":";;;;;;;AAwEO,MAAM,GAAI,CAAA;AAAA,EACN,OAAA;AAAA,EACA,IAAA;AAAA,EACA,KAAA;AAAA,EACA,QAAA;AAAA,EACC,MAAA,GAAmB,EAAE,KAAA,EAAO,SAAU,EAAA;AAAA;AAAA;AAAA;AAAA,EAKhD,IAAI,GAAM,GAAA;AACR,IAAA,OAAO,IAAK,CAAA,IAAA;AAAA;AACd,EACU,IAAA;AAAA;AAAA;AAAA;AAAA,EAKV,IAAI,SAAY,GAAA;AACd,IAAA,OAAO,KAAK,MAAQ,EAAA,SAAA;AAAA;AACtB;AAAA,EAGA,IAAI,YAAe,GAAA;AACjB,IAAA,OAAO,KAAK,MAAO,CAAA,YAAA;AAAA;AACrB,EAEA,WAAY,CAAA,QAAA,EAAkB,OAAsB,GAAA,EAAI,EAAA;AACtD,IAAA,IAAI,CAAC,QAAU,EAAA;AACb,MAAM,MAAA,IAAI,MAAM,oBAAoB,CAAA;AAAA;AAGtC,IAAA,IAAA,CAAK,QAAW,GAAA,QAAA;AAChB,IAAA,IAAA,CAAK,OAAU,GAAA,OAAA;AACf,IAAA,IAAA,CAAK,IAAO,GAAA,OAAA,EAAS,MAAU,IAAA,IAAI,cAAc,eAAe,CAAA;AAChE,IAAK,IAAA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EAAA,cAAA,IAAkB;AAAC;AAClC,KACD,CAAA;AAED,IAAA,OAAO,GAAI,CAAA,IAAA;AAAA;AACb;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,oBAAoB,MAAoC,EAAA;AAC5D,IAAA,MAAM,EAAE,YAAA,EAAiB,GAAA,IAAA,CAAK,aAAc,EAAA;AAE5C,IAAA,OAAO,MAAM,mBAAA,CAAoB,YAAc,EAAA,MAAA,EAAQ,KAAK,GAAG,CAAA;AAAA;AACjE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,uBAAuB,MAAoC,EAAA;AAC/D,IAAA,MAAM,EAAE,YAAA,EAAiB,GAAA,IAAA,CAAK,aAAc,EAAA;AAE5C,IAAI,IAAA;AACF,MAAM,MAAA,KAAA,GAAQ,MAAM,sBAAuB,CAAA,YAAA,EAAc,EAAE,MAAO,EAAA,EAAG,KAAK,GAAG,CAAA;AAC7E,MAAA,OAAO,CAAC,CAAC,KAAA;AAAA,aACF,EAAI,EAAA;AACX,MAAO,OAAA,KAAA;AAAA;AACT;AACF,EAEQ,aAAiD,GAAA;AACvD,IAAI,IAAA,IAAA,CAAK,MAAO,CAAA,KAAA,KAAU,SAAW,EAAA;AACnC,MAAM,MAAA,IAAI,MAAM,iBAAiB,CAAA;AAAA;AAEnC,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AAEhB","file":"app.mjs","sourcesContent":["import * as msal from '@azure/msal-browser';\r\n\r\nimport * as teamsJs from '@microsoft/teams-js';\r\nimport * as http from '@microsoft/teams.common/http';\r\nimport { ILogger, ConsoleLogger } from '@microsoft/teams.common/logging';\r\nimport * as graph from '@microsoft/teams.graph';\r\n\r\nimport { buildGraphClient } from './graph-utils';\r\nimport {\r\n acquireMsalAccessToken,\r\n buildMsalConfig,\r\n getStandardExecSilentRequest,\r\n hasConsentForScopes,\r\n} from './msal-utils';\r\n\r\nexport type MsalOptions = {\r\n /**\r\n * Optional MSAL configuration. This parameter is used to construct an MSAL instance in order\r\n * to make authenticated function calls. If omitted, a default configuration is created.\r\n */\r\n readonly configuration?: msal.Configuration;\r\n /**\r\n * Options to control scope consent pre-warming. If explicitly set to false, no pre-warming is performed.\r\n * If no value is provided, the default scope (i.e. \".default\") is pre-warmed. If a set of scopes is\r\n * provided, the specified scopes are pre-warmed. The scopes should be for a single resource, and they\r\n * should not mix the .default scope with named scopes.\r\n */\r\n readonly prewarmScopes?: false | string[];\r\n};\r\n\r\nexport type AppOptions = {\r\n /**\r\n * The app base url.\r\n */\r\n readonly baseUrl?: string;\r\n\r\n /** App tenant ID */\r\n readonly tenantId?: string;\r\n\r\n /**\r\n * Logger instance to use.\r\n */\r\n readonly logger?: ILogger;\r\n\r\n /**\r\n * Options to control how MSAL is initialized and used.\r\n */\r\n readonly msalOptions?: MsalOptions;\r\n};\r\n\r\ntype AppState =\r\n | {\r\n phase: 'stopped' | 'starting';\r\n startedAt?: never;\r\n msalInstance?: never;\r\n context?: never;\r\n }\r\n | {\r\n phase: 'started';\r\n startedAt: Date;\r\n msalInstance: msal.IPublicClientApplication;\r\n context: teamsJs.app.Context;\r\n };\r\n\r\nexport type ExecOptions = (\r\n | { msalTokenRequest: msal.SilentRequest; permission?: never }\r\n | { msalTokenRequest?: never; permission: string }\r\n | { msalTokenRequest?: never; permission?: never }\r\n) & {\r\n requestHeaders?: Record<string, string>;\r\n};\r\n\r\nexport class App {\r\n readonly options: AppOptions;\r\n readonly http: http.Client;\r\n readonly graph: graph.Client;\r\n readonly clientId: string;\r\n protected _state: AppState = { phase: 'stopped' };\r\n\r\n /**\r\n * the apps logger\r\n */\r\n get log() {\r\n return this._log;\r\n }\r\n protected _log: ILogger;\r\n\r\n /**\r\n * the date/time when the app was successfully started.\r\n */\r\n get startedAt() {\r\n return this._state?.startedAt;\r\n }\r\n\r\n /** the msal instance used in this app. undefined until the app is started. */\r\n get msalInstance() {\r\n return this._state.msalInstance;\r\n }\r\n\r\n constructor(clientId: string, options: AppOptions = {}) {\r\n if (!clientId) {\r\n throw new Error('Invalid client ID.');\r\n }\r\n\r\n this.clientId = clientId;\r\n this.options = options;\r\n this._log = options?.logger || new ConsoleLogger('@teams/client');\r\n this.http = new http.Client({ baseUrl: options?.baseUrl });\r\n this.graph = buildGraphClient(() => this.appStateGuard(), this._log);\r\n }\r\n\r\n /**\r\n * Starts the library and initializes the dependent teams-js and MSAL libraries.\r\n * @returns A promise that will be fulfilled when the app has started, or\r\n * rejected if the initialization fails or times out.\r\n */\r\n async start(): Promise<void> {\r\n if (this._state.phase !== 'stopped') {\r\n this._log.debug(`app already ${this._state.phase}`);\r\n return;\r\n }\r\n\r\n this._log.debug('app starting');\r\n this._state = { phase: 'starting' };\r\n\r\n await teamsJs.app.initialize();\r\n const context = await teamsJs.app.getContext();\r\n\r\n const msalConfig =\r\n this.options.msalOptions?.configuration ?? buildMsalConfig(this.clientId, this._log);\r\n const msalInstance = await msal.createNestablePublicClientApplication(msalConfig);\r\n await msalInstance.initialize();\r\n\r\n this._state = { phase: 'started', msalInstance, context, startedAt: new Date() };\r\n\r\n // pre-warm consent for the specified scopes\r\n if (this.options.msalOptions?.prewarmScopes !== false) {\r\n const scopes = this.options.msalOptions?.prewarmScopes ?? ['.default'];\r\n this._log.debug(`prewarming consent for scopes: ${scopes.join(', ')}`);\r\n await this.ensureConsentForScopes(scopes);\r\n }\r\n\r\n this._log.debug('app started');\r\n }\r\n\r\n /**\r\n * Execute a server-side function\r\n * @param name The unique function name\r\n * @param data The data to send\r\n * @param options Options\r\n * @param options.msalTokenRequest Optional MSAL token request.\r\n * If omitted, a default token request is used.\r\n * @param options.requestHeaders Optional additional request headers.\r\n * @returns The function response\r\n */\r\n async exec<T = unknown>(name: string, data?: unknown, options?: ExecOptions): Promise<T> {\r\n const { msalInstance, context } = this.appStateGuard();\r\n\r\n const accessToken = await acquireMsalAccessToken(\r\n msalInstance,\r\n options?.msalTokenRequest ?? getStandardExecSilentRequest(this.clientId, options?.permission),\r\n this._log\r\n );\r\n\r\n const res = await this.http.post<T>(`/api/functions/${name}`, data, {\r\n headers: {\r\n authorization: `Bearer ${accessToken}`,\r\n 'x-teams-app-session-id': context.app.sessionId,\r\n 'x-teams-channel-id': context.channel?.id,\r\n 'x-teams-chat-id': context.chat?.id,\r\n 'x-teams-meeting-id': context.meeting?.id,\r\n 'x-teams-message-id': context.app.parentMessageId,\r\n 'x-teams-page-id': context.page.id,\r\n 'x-teams-sub-page-id': context.page.subPageId,\r\n 'x-teams-team-id': context.team?.internalId,\r\n ...(options?.requestHeaders ?? {}),\r\n },\r\n });\r\n\r\n return res.data;\r\n }\r\n\r\n /**\r\n * Tests whether the user has consented to the specified scopes without prompting the user for consent.\r\n * @param scopes The scopes to check consent for.The scopes should be for a single resource, and they\r\n * should not mix the .default scope with named scopes.\r\n * @returns A promise that resolves to a boolean indicating whether the user has consented to the scopes.\r\n */\r\n async hasConsentForScopes(scopes: string[]): Promise<boolean> {\r\n const { msalInstance } = this.appStateGuard();\r\n\r\n return await hasConsentForScopes(msalInstance, scopes, this.log);\r\n }\r\n\r\n /**\r\n * Tests whether the user has consented to the specified scopes, and prompts them if not. This is useful for ensuring\r\n * that the user has consented to the required scopes before calling a graph API or other resource.\r\n * @param scopes - The scopes to prewarm consent for. The scopes should be for a single resource, and they\r\n * should not mix the .default scope with named scopes.\r\n * @returns A value indicating whether consent has been acquired for the specified scopes.\r\n */\r\n async ensureConsentForScopes(scopes: string[]): Promise<boolean> {\r\n const { msalInstance } = this.appStateGuard();\r\n\r\n try {\r\n const token = await acquireMsalAccessToken(msalInstance, { scopes }, this.log);\r\n return !!token;\r\n } catch (ex) {\r\n return false;\r\n }\r\n }\r\n\r\n private appStateGuard(): AppState & { phase: 'started' } {\r\n if (this._state.phase !== 'started') {\r\n throw new Error('App not started');\r\n }\r\n return this._state;\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/app.ts"],"names":[],"mappings":";;;;;;;AA2GO,MAAM,GAAI,CAAA;AA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M,mBAAA,CAAoB,YAAc,EAAA,MAAA,EAAQ,KAAK,GAAG,CAAA;AAAA;AACjE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EASA,MAAM,uBAAuB,MAAoC,EAAA;AAC/D,IAAA,MAAM,EAAE,YAAA,EAAiB,GAAA,IAAA,CAAK,aAAc,EAAA;AAE5C,IAAI,IAAA;AACF,MAAM,MAAA,KAAA,GAAQ,MAAM,sBAAuB,CAAA,YAAA,EAAc,EAAE,MAAO,EAAA,EAAG,KAAK,GAAG,CAAA;AAC7E,MAAA,OAAO,CAAC,CAAC,KAAA;AAAA,aACF,EAAI,EAAA;AACX,MAAO,OAAA,KAAA;AAAA;AACT;AACF,EAEQ,aAAiD,GAAA;AACvD,IAAI,IAAA,IAAA,CAAK,MAAO,CAAA,KAAA,KAAU,SAAW,EAAA;AACnC,MAAM,MAAA,IAAI,MAAM,iBAAiB,CAAA;AAAA;AAEnC,IAAA,OAAO,IAAK,CAAA,MAAA;AAAA;AAEhB","file":"app.mjs","sourcesContent":["import * as msal from '@azure/msal-browser';\n\nimport * as teamsJs from '@microsoft/teams-js';\nimport * as http from '@microsoft/teams.common/http';\nimport { ILogger, ConsoleLogger } from '@microsoft/teams.common/logging';\nimport * as graph from '@microsoft/teams.graph';\n\nimport { buildGraphClient } from './graph-utils';\nimport {\n acquireMsalAccessToken,\n buildMsalConfig,\n getStandardExecSilentRequest,\n hasConsentForScopes,\n} from './msal-utils';\n\n/**\n * Options to control how MSAL is initialized and used.\n */\nexport type MsalOptions = (\n | {\n /**\n * MSAL instance to use when making authenticated function calls to remote endpoints.\n * This is useful if you want to use a custom MSAL instance or if you want to share the\n * same MSAL instance across multiple apps.\n */\n readonly msalInstance?: msal.IPublicClientApplication;\n readonly configuration?: never;\n }\n | {\n readonly msalInstance?: never;\n /**\n * MSAL configuration to use when constructing an MSAL instance used\n * to make authenticated function calls to remote endpoints. */\n readonly configuration?: msal.Configuration;\n }\n) & {\n /**\n * Options to control scope consent pre-warming. If explicitly set to false, no pre-warming is performed.\n * If no value is provided, the default scope (i.e. \".default\") is pre-warmed. If a set of scopes is\n * provided, the specified scopes are pre-warmed. The scopes should be for a single resource, and they\n * should not mix the .default scope with named scopes.\n */\n readonly prewarmScopes?: false | string[];\n};\n\nexport type RemoteApiOptions = {\n /**\n * The remote API base URL. If omitted, it's assumed that the remote API is hosted in the same domain as the app.\n */\n readonly baseUrl?: string;\n\n /**\n * The default resource name when building a token request for the Entra token to include when invoking\n * a remote function. This is useful when the API is hosted in an different AAD app from\n * the caller and is typically in the format 'api://<remoteAppClientId>'.\n * When invoking @see exec with default options or a permission name,\n * this value is used to build a token request in the format of '<remoteAppResource>/<permission>'.\n * If omitted, it's assumed that the API is hosted in the same AAD app as the caller, and the\n * token request is made for 'api://<clientId>/permission'.\n */\n readonly remoteAppResource?: string;\n};\n\nexport type AppOptions = {\n /**\n * Logger instance to use.\n */\n readonly logger?: ILogger;\n\n /**\n * Options to control how MSAL is initialized and used.\n */\n readonly msalOptions?: MsalOptions;\n\n /** Options to control how remote APIs are invoked. */\n readonly remoteApiOptions?: RemoteApiOptions;\n};\n\ntype AppState =\n | {\n phase: 'stopped' | 'starting';\n startedAt?: never;\n msalInstance?: never;\n context?: never;\n }\n | {\n phase: 'started';\n startedAt: Date;\n msalInstance: msal.IPublicClientApplication;\n context: teamsJs.app.Context;\n };\n\n/**\n * ExecOptions is used to specify options for the exec method.\n */\nexport type ExecOptions = (\n | { readonly msalTokenRequest?: msal.SilentRequest; readonly permission?: never }\n | { readonly msalTokenRequest?: never; readonly permission?: string }\n) & {\n readonly requestHeaders?: Record<string, string>;\n};\n\n/**\n * The main entry point for this library. This class streamlines Microsoft Teams app development\n * by simplifying the process of managing authentication, interacting with Microsoft Graph APIs,\n * and executing server-side functions.\n */\nexport class App {\n readonly options: AppOptions;\n readonly http: http.Client;\n readonly graph: graph.Client;\n readonly clientId: string;\n protected _state: AppState = { phase: 'stopped' };\n\n /**\n * The apps logger\n */\n get log() {\n return this._log;\n }\n protected _log: ILogger;\n\n /**\n * The date/time when the app was successfully started.\n */\n get startedAt() {\n return this._state?.startedAt;\n }\n\n /** The msal instance used in this app. undefined until the app is started. */\n get msalInstance() {\n return this._state.msalInstance;\n }\n\n constructor(clientId: string, options: AppOptions = {}) {\n if (!clientId) {\n throw new Error('Invalid client ID.');\n }\n\n this.clientId = clientId;\n this.options = options;\n this._log = options?.logger || new ConsoleLogger('@teams/client');\n this.http = new http.Client({ baseUrl: options?.remoteApiOptions?.baseUrl });\n this.graph = buildGraphClient(() => this.appStateGuard(), this._log);\n }\n\n /**\n * Starts the library and initializes the dependent teams-js and MSAL libraries.\n * @returns A promise that will be fulfilled when the app has started, or\n * rejected if the initialization fails or times out.\n */\n async start(): Promise<void> {\n if (this._state.phase !== 'stopped') {\n this._log.debug(`app already ${this._state.phase}`);\n return;\n }\n\n this._log.debug('app starting');\n this._state = { phase: 'starting' };\n\n await teamsJs.app.initialize();\n const context = await teamsJs.app.getContext();\n\n let msalInstance = this.options.msalOptions?.msalInstance;\n if (!msalInstance) {\n const msalConfig =\n this.options.msalOptions?.configuration ?? buildMsalConfig(this.clientId, this._log);\n msalInstance = await msal.createNestablePublicClientApplication(msalConfig);\n await msalInstance.initialize();\n }\n\n this._state = { phase: 'started', msalInstance, context, startedAt: new Date() };\n\n // pre-warm consent for the specified scopes\n if (this.options.msalOptions?.prewarmScopes !== false) {\n const scopes = this.options.msalOptions?.prewarmScopes ?? ['.default'];\n this._log.debug(`prewarming consent for scopes: ${scopes.join(', ')}`);\n await this.ensureConsentForScopes(scopes);\n }\n\n this._log.debug('app started');\n }\n\n /**\n * Execute a server-side function\n * @param name The unique function name\n * @param data The data to send\n * @param options Options\n * @param options.msalTokenRequest Optional MSAL token request.\n * If omitted, a default token request is used.\n * @param options.requestHeaders Optional additional request headers.\n * @returns The function response\n */\n async exec<T = unknown>(name: string, data?: unknown, options?: ExecOptions): Promise<T> {\n const { msalInstance, context } = this.appStateGuard();\n\n const remoteAppResource =\n this.options.remoteApiOptions?.remoteAppResource ?? `api://${this.clientId}`;\n const accessToken = await acquireMsalAccessToken(\n msalInstance,\n options?.msalTokenRequest ??\n getStandardExecSilentRequest(remoteAppResource, options?.permission),\n this._log\n );\n\n const res = await this.http.post<T>(`/api/functions/${name}`, data, {\n headers: {\n authorization: `Bearer ${accessToken}`,\n 'x-teams-app-session-id': context.app.sessionId,\n 'x-teams-channel-id': context.channel?.id,\n 'x-teams-chat-id': context.chat?.id,\n 'x-teams-meeting-id': context.meeting?.id,\n 'x-teams-message-id': context.app.parentMessageId,\n 'x-teams-page-id': context.page.id,\n 'x-teams-sub-page-id': context.page.subPageId,\n 'x-teams-team-id': context.team?.internalId,\n ...(options?.requestHeaders ?? {}),\n },\n });\n\n return res.data;\n }\n\n /**\n * Tests whether the user has consented to the specified scopes without prompting the user for consent.\n * @param scopes The scopes to check consent for.The scopes should be for a single resource, and they\n * should not mix the .default scope with named scopes.\n * @returns A promise that resolves to a boolean indicating whether the user has consented to the scopes.\n */\n async hasConsentForScopes(scopes: string[]): Promise<boolean> {\n const { msalInstance } = this.appStateGuard();\n\n return await hasConsentForScopes(msalInstance, scopes, this.log);\n }\n\n /**\n * Tests whether the user has consented to the specified scopes, and prompts them if not. This is useful for ensuring\n * that the user has consented to the required scopes before calling a graph API or other resource.\n * @param scopes - The scopes to prewarm consent for. The scopes should be for a single resource, and they\n * should not mix the .default scope with named scopes.\n * @returns A value indicating whether consent has been acquired for the specified scopes.\n */\n async ensureConsentForScopes(scopes: string[]): Promise<boolean> {\n const { msalInstance } = this.appStateGuard();\n\n try {\n const token = await acquireMsalAccessToken(msalInstance, { scopes }, this.log);\n return !!token;\n } catch (ex) {\n return false;\n }\n }\n\n private appStateGuard(): AppState & { phase: 'started' } {\n if (this._state.phase !== 'started') {\n throw new Error('App not started');\n }\n return this._state;\n }\n}\n"]}
|
package/dist/msal-utils.d.mts
CHANGED
|
@@ -3,11 +3,11 @@ import { ILogger } from '@microsoft/teams.common/logging';
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Gets a silent request used to acquire an Entra access token for invoking remote functions on behalf of a user.
|
|
6
|
-
* @param
|
|
6
|
+
* @param resource The resource to use, e.g 'api://<clientId>'.
|
|
7
7
|
* @param permission The permission to request. Defaults to 'access_as_user'.
|
|
8
8
|
* @returns
|
|
9
9
|
*/
|
|
10
|
-
declare const getStandardExecSilentRequest: (
|
|
10
|
+
declare const getStandardExecSilentRequest: (resource: string, permission?: string) => msal.SilentRequest;
|
|
11
11
|
/**
|
|
12
12
|
* Builds a default MSAL configuration for the specified client ID.
|
|
13
13
|
* @param clientId The application client ID.
|
package/dist/msal-utils.d.ts
CHANGED
|
@@ -3,11 +3,11 @@ import { ILogger } from '@microsoft/teams.common/logging';
|
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Gets a silent request used to acquire an Entra access token for invoking remote functions on behalf of a user.
|
|
6
|
-
* @param
|
|
6
|
+
* @param resource The resource to use, e.g 'api://<clientId>'.
|
|
7
7
|
* @param permission The permission to request. Defaults to 'access_as_user'.
|
|
8
8
|
* @returns
|
|
9
9
|
*/
|
|
10
|
-
declare const getStandardExecSilentRequest: (
|
|
10
|
+
declare const getStandardExecSilentRequest: (resource: string, permission?: string) => msal.SilentRequest;
|
|
11
11
|
/**
|
|
12
12
|
* Builds a default MSAL configuration for the specified client ID.
|
|
13
13
|
* @param clientId The application client ID.
|
package/dist/msal-utils.js
CHANGED
|
@@ -22,8 +22,8 @@ function _interopNamespace(e) {
|
|
|
22
22
|
|
|
23
23
|
var msal__namespace = /*#__PURE__*/_interopNamespace(msal);
|
|
24
24
|
|
|
25
|
-
const getStandardExecSilentRequest = (
|
|
26
|
-
scopes: [
|
|
25
|
+
const getStandardExecSilentRequest = (resource, permission = "access_as_user") => ({
|
|
26
|
+
scopes: [`${resource}/${permission}`]
|
|
27
27
|
});
|
|
28
28
|
const buildMsalConfig = (clientId, logger) => {
|
|
29
29
|
return {
|
package/dist/msal-utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/msal-utils.ts"],"names":["msal"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAUO,MAAM,4BAA+B,GAAA,CAC1C,
|
|
1
|
+
{"version":3,"sources":["../src/msal-utils.ts"],"names":["msal"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAUO,MAAM,4BAA+B,GAAA,CAC1C,QACA,EAAA,UAAA,GAAa,gBACW,MAAA;AAAA,EACxB,QAAQ,CAAC,CAAA,EAAG,QAAQ,CAAA,CAAA,EAAI,UAAU,CAAE,CAAA;AACtC,CAAA;AASa,MAAA,eAAA,GAAkB,CAAC,QAAA,EAAkB,MAAwC,KAAA;AACxF,EAAO,OAAA;AAAA,IACL,IAAM,EAAA;AAAA,MACJ,QAAA;AAAA,MACA,SAAW,EAAA,EAAA;AAAA,MACX,WAAa,EAAA,GAAA;AAAA,MACb,qBAAuB,EAAA;AAAA,KACzB;AAAA,IACA,MAAQ,EAAA;AAAA,MACN,aAAe,EAAA;AAAA,QACb,iBAAmB,EAAA,KAAA;AAAA,QACnB,cAAA,EAAgB,CAAC,KAAA,EAAO,OAAY,KAAA;AAClC,UAAA,QAAQ,KAAO;AAAA,YACb,KAAKA,gBAAK,QAAS,CAAA,KAAA;AACjB,cAAA,MAAA,CAAO,MAAM,OAAO,CAAA;AACpB,cAAA;AAAA,YACF,KAAKA,gBAAK,QAAS,CAAA,IAAA;AACjB,cAAA,MAAA,CAAO,KAAK,OAAO,CAAA;AACnB,cAAA;AAAA,YACF,KAAKA,gBAAK,QAAS,CAAA,OAAA;AACjB,cAAA,MAAA,CAAO,MAAM,OAAO,CAAA;AACpB,cAAA;AAAA,YACF,KAAKA,gBAAK,QAAS,CAAA,OAAA;AACjB,cAAA,MAAA,CAAO,KAAK,OAAO,CAAA;AACnB,cAAA;AAAA,YACF;AACE,cAAA;AAAA;AACJ;AACF;AACF;AACF,GACF;AACF;AAWO,MAAM,sBAAyB,GAAA,OACpC,YACA,EAAA,OAAA,EACA,MACoB,KAAA;AACpB,EAAI,IAAA;AACF,IAAA,MAAM,QAAW,GAAA,MAAM,YAAa,CAAA,kBAAA,CAAmB,OAAO,CAAA;AAC9D,IAAA,OAAO,QAAS,CAAA,WAAA;AAAA,WACT,EAAI,EAAA;AAGX,IAAM,MAAA,oBAAA,GAAuB,cAAcA,eAAK,CAAA,4BAAA;AAChD,IAAA,IAAI,CAAC,oBAAsB,EAAA;AACzB,MAAO,MAAA,CAAA,KAAA,CAAM,6BAA6B,EAAE,CAAA;AAC5C,MAAM,MAAA,EAAA;AAAA;AACR;AAGF,EAAI,IAAA;AACF,IAAA,MAAA,CAAO,MAAM,qDAAqD,CAAA;AAClE,IAAA,MAAM,QAAW,GAAA,MAAM,YAAa,CAAA,iBAAA,CAAkB,OAAO,CAAA;AAC7D,IAAA,OAAO,QAAS,CAAA,WAAA;AAAA,WACT,EAAI,EAAA;AACX,IAAO,MAAA,CAAA,KAAA,CAAM,4BAA4B,EAAE,CAAA;AAC3C,IAAM,MAAA,EAAA;AAAA;AAEV;AAWO,MAAM,mBAAsB,GAAA,OACjC,YACA,EAAA,MAAA,EACA,MACqB,KAAA;AACrB,EAAI,IAAA;AACF,IAAA,MAAM,aAAa,kBAAmB,CAAA;AAAA,MACpC;AAAA,KACD,CAAA;AAED,IAAO,OAAA,IAAA;AAAA,WACA,EAAI,EAAA;AAGX,IAAM,MAAA,uBAAA,GAA0B,cAAcA,eAAK,CAAA,4BAAA;AACnD,IAAM,MAAA,QAAA,GAAW,0BAA0B,OAAU,GAAA,OAAA;AACrD,IAAO,MAAA,CAAA,GAAA,CAAI,QAAU,EAAA,4BAAA,EAA8B,EAAE,CAAA;AACrD,IAAO,OAAA,KAAA;AAAA;AAEX","file":"msal-utils.js","sourcesContent":["import * as msal from '@azure/msal-browser';\n\nimport { ILogger } from '@microsoft/teams.common/logging';\n\n/**\n * Gets a silent request used to acquire an Entra access token for invoking remote functions on behalf of a user.\n * @param resource The resource to use, e.g 'api://<clientId>'.\n * @param permission The permission to request. Defaults to 'access_as_user'.\n * @returns\n */\nexport const getStandardExecSilentRequest = (\n resource: string,\n permission = 'access_as_user'\n): msal.SilentRequest => ({\n scopes: [`${resource}/${permission}`],\n});\n\n/**\n * Builds a default MSAL configuration for the specified client ID.\n * @param clientId The application client ID.\n * @param logger The logger instance to use for logging MSAL events.\n * @returns A default MSAL configuration object suitable for creating a\n * @see{IPublicClientApplication} instance for a multi-tenant application.\n */\nexport const buildMsalConfig = (clientId: string, logger: ILogger): msal.Configuration => {\n return {\n auth: {\n clientId,\n authority: '',\n redirectUri: '/',\n postLogoutRedirectUri: '/',\n },\n system: {\n loggerOptions: {\n piiLoggingEnabled: false,\n loggerCallback: (level, message) => {\n switch (level) {\n case msal.LogLevel.Error:\n logger.error(message);\n return;\n case msal.LogLevel.Info:\n logger.info(message);\n return;\n case msal.LogLevel.Verbose:\n logger.debug(message);\n return;\n case msal.LogLevel.Warning:\n logger.warn(message);\n return;\n default:\n return;\n }\n },\n },\n },\n };\n};\n\n/**\n * Acquires an access token using MSAL. It first attempts to acquire the token silently,\n * and if that fails with an InteractionRequiredAuthError, it falls back to acquiring the\n * token via a popup.\n * @param msalInstance The MSAL instance to use for acquiring the token.\n * @param request The token request object.\n * @param logger The logger instance to use for logging errors.\n * @returns A promise that resolves to the acquired access token.\n */\nexport const acquireMsalAccessToken = async (\n msalInstance: Pick<msal.IPublicClientApplication, 'acquireTokenSilent' | 'acquireTokenPopup'>,\n request: msal.SilentRequest,\n logger: ILogger\n): Promise<string> => {\n try {\n const response = await msalInstance.acquireTokenSilent(request);\n return response.accessToken;\n } catch (ex) {\n // InteractionRequiredAuthError indicates that the user may not have consented to the requested\n // scope yet -- for this, we can fall back on acquireTokenPopup instead.\n const tryAcquireTokenPopup = ex instanceof msal.InteractionRequiredAuthError;\n if (!tryAcquireTokenPopup) {\n logger.error('acquireTokenSilent failed', ex);\n throw ex;\n }\n }\n\n try {\n logger.debug('acquireTokenSilent failed; trying acquireTokenPopup');\n const response = await msalInstance.acquireTokenPopup(request);\n return response.accessToken;\n } catch (ex) {\n logger.error('acquireTokenPopup failed', ex);\n throw ex;\n }\n};\n\n/**\n * Tests whether the user has consented to the specified scopes by attempting to acquire a token silently.\n * If the token acquisition is successful, it indicates that the user has consented to the scopes.\n * If it fails, it indicates that the user has not consented to the scopes.\n * @param msalInstance The MSAL instance to use.\n * @param scopes The scopes to check consent for. The scopes should not mix resources, or mix default scope with non-default scopes.\n * @param logger The logger instance to use.\n * @returns A promise that resolves to a boolean indicating whether the user has consented to the scopes.\n */\nexport const hasConsentForScopes = async (\n msalInstance: Pick<msal.IPublicClientApplication, 'acquireTokenSilent'>,\n scopes: string[],\n logger: ILogger\n): Promise<boolean> => {\n try {\n await msalInstance.acquireTokenSilent({\n scopes,\n });\n\n return true;\n } catch (ex) {\n // InteractionRequiredAuthError indicates that the user has not consented to the requested scope yet.\n // This is not an error, but may be interesting when trouble shooting.\n const acquireTokenPopupNeeded = ex instanceof msal.InteractionRequiredAuthError;\n const logLevel = acquireTokenPopupNeeded ? 'debug' : 'error';\n logger.log(logLevel, 'hasConsentForScopes failed', ex);\n return false;\n }\n};\n"]}
|
package/dist/msal-utils.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as msal from '@azure/msal-browser';
|
|
2
2
|
|
|
3
|
-
const getStandardExecSilentRequest = (
|
|
4
|
-
scopes: [
|
|
3
|
+
const getStandardExecSilentRequest = (resource, permission = "access_as_user") => ({
|
|
4
|
+
scopes: [`${resource}/${permission}`]
|
|
5
5
|
});
|
|
6
6
|
const buildMsalConfig = (clientId, logger) => {
|
|
7
7
|
return {
|
package/dist/msal-utils.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/msal-utils.ts"],"names":[],"mappings":";;AAUO,MAAM,4BAA+B,GAAA,CAC1C,
|
|
1
|
+
{"version":3,"sources":["../src/msal-utils.ts"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B,cAAc,IAAK,CAAA,4BAAA;AAChD,IAAA,IAAI,CAAC,oBAAsB,EAAA;AACzB,MAAO,MAAA,CAAA,KAAA,CAAM,6BAA6B,EAAE,CAAA;AAC5C,MAAM,MAAA,EAAA;AAAA;AACR;AAGF,EAAI,IAAA;AACF,IAAA,MAAA,CAAO,MAAM,qDAAqD,CAAA;AAClE,IAAA,MAAM,QAAW,GAAA,MAAM,YAAa,CAAA,iBAAA,CAAkB,OAAO,CAAA;AAC7D,IAAA,OAAO,QAAS,CAAA,WAAA;AAAA,WACT,EAAI,EAAA;AACX,IAAO,MAAA,CAAA,KAAA,CAAM,4BAA4B,EAAE,CAAA;AAC3C,IAAM,MAAA,EAAA;AAAA;AAEV;AAWO,MAAM,mBAAsB,GAAA,OACjC,YACA,EAAA,MAAA,EACA,MACqB,KAAA;AACrB,EAAI,IAAA;AACF,IAAA,MAAM,aAAa,kBAAmB,CAAA;AAAA,MACpC;AAAA,KACD,CAAA;AAED,IAAO,OAAA,IAAA;AAAA,WACA,EAAI,EAAA;AAGX,IAAM,MAAA,uBAAA,GAA0B,cAAc,IAAK,CAAA,4BAAA;AACnD,IAAM,MAAA,QAAA,GAAW,0BAA0B,OAAU,GAAA,OAAA;AACrD,IAAO,MAAA,CAAA,GAAA,CAAI,QAAU,EAAA,4BAAA,EAA8B,EAAE,CAAA;AACrD,IAAO,OAAA,KAAA;AAAA;AAEX","file":"msal-utils.mjs","sourcesContent":["import * as msal from '@azure/msal-browser';\n\nimport { ILogger } from '@microsoft/teams.common/logging';\n\n/**\n * Gets a silent request used to acquire an Entra access token for invoking remote functions on behalf of a user.\n * @param resource The resource to use, e.g 'api://<clientId>'.\n * @param permission The permission to request. Defaults to 'access_as_user'.\n * @returns\n */\nexport const getStandardExecSilentRequest = (\n resource: string,\n permission = 'access_as_user'\n): msal.SilentRequest => ({\n scopes: [`${resource}/${permission}`],\n});\n\n/**\n * Builds a default MSAL configuration for the specified client ID.\n * @param clientId The application client ID.\n * @param logger The logger instance to use for logging MSAL events.\n * @returns A default MSAL configuration object suitable for creating a\n * @see{IPublicClientApplication} instance for a multi-tenant application.\n */\nexport const buildMsalConfig = (clientId: string, logger: ILogger): msal.Configuration => {\n return {\n auth: {\n clientId,\n authority: '',\n redirectUri: '/',\n postLogoutRedirectUri: '/',\n },\n system: {\n loggerOptions: {\n piiLoggingEnabled: false,\n loggerCallback: (level, message) => {\n switch (level) {\n case msal.LogLevel.Error:\n logger.error(message);\n return;\n case msal.LogLevel.Info:\n logger.info(message);\n return;\n case msal.LogLevel.Verbose:\n logger.debug(message);\n return;\n case msal.LogLevel.Warning:\n logger.warn(message);\n return;\n default:\n return;\n }\n },\n },\n },\n };\n};\n\n/**\n * Acquires an access token using MSAL. It first attempts to acquire the token silently,\n * and if that fails with an InteractionRequiredAuthError, it falls back to acquiring the\n * token via a popup.\n * @param msalInstance The MSAL instance to use for acquiring the token.\n * @param request The token request object.\n * @param logger The logger instance to use for logging errors.\n * @returns A promise that resolves to the acquired access token.\n */\nexport const acquireMsalAccessToken = async (\n msalInstance: Pick<msal.IPublicClientApplication, 'acquireTokenSilent' | 'acquireTokenPopup'>,\n request: msal.SilentRequest,\n logger: ILogger\n): Promise<string> => {\n try {\n const response = await msalInstance.acquireTokenSilent(request);\n return response.accessToken;\n } catch (ex) {\n // InteractionRequiredAuthError indicates that the user may not have consented to the requested\n // scope yet -- for this, we can fall back on acquireTokenPopup instead.\n const tryAcquireTokenPopup = ex instanceof msal.InteractionRequiredAuthError;\n if (!tryAcquireTokenPopup) {\n logger.error('acquireTokenSilent failed', ex);\n throw ex;\n }\n }\n\n try {\n logger.debug('acquireTokenSilent failed; trying acquireTokenPopup');\n const response = await msalInstance.acquireTokenPopup(request);\n return response.accessToken;\n } catch (ex) {\n logger.error('acquireTokenPopup failed', ex);\n throw ex;\n }\n};\n\n/**\n * Tests whether the user has consented to the specified scopes by attempting to acquire a token silently.\n * If the token acquisition is successful, it indicates that the user has consented to the scopes.\n * If it fails, it indicates that the user has not consented to the scopes.\n * @param msalInstance The MSAL instance to use.\n * @param scopes The scopes to check consent for. The scopes should not mix resources, or mix default scope with non-default scopes.\n * @param logger The logger instance to use.\n * @returns A promise that resolves to a boolean indicating whether the user has consented to the scopes.\n */\nexport const hasConsentForScopes = async (\n msalInstance: Pick<msal.IPublicClientApplication, 'acquireTokenSilent'>,\n scopes: string[],\n logger: ILogger\n): Promise<boolean> => {\n try {\n await msalInstance.acquireTokenSilent({\n scopes,\n });\n\n return true;\n } catch (ex) {\n // InteractionRequiredAuthError indicates that the user has not consented to the requested scope yet.\n // This is not an error, but may be interesting when trouble shooting.\n const acquireTokenPopupNeeded = ex instanceof msal.InteractionRequiredAuthError;\n const logLevel = acquireTokenPopupNeeded ? 'debug' : 'error';\n logger.log(logLevel, 'hasConsentForScopes failed', ex);\n return false;\n }\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microsoft/teams.client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "2.0.0-preview.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -34,21 +34,20 @@
|
|
|
34
34
|
"lint": "npx eslint",
|
|
35
35
|
"lint:fix": "npx eslint --fix",
|
|
36
36
|
"build": "npx tsup",
|
|
37
|
-
"test": "npx jest"
|
|
38
|
-
"fmt": "npx prettier --write \"**/*.{js,ts,tsx,md,json}\""
|
|
37
|
+
"test": "npx jest"
|
|
39
38
|
},
|
|
40
39
|
"dependencies": {
|
|
41
40
|
"@azure/msal-browser": "^4.9.1",
|
|
42
41
|
"uuid": "^11.0.5"
|
|
43
42
|
},
|
|
44
43
|
"peerDependencies": {
|
|
45
|
-
"@microsoft/teams.api": "
|
|
46
|
-
"@microsoft/teams.common": "
|
|
47
|
-
"@microsoft/teams.graph": "
|
|
44
|
+
"@microsoft/teams.api": "2.0.0-preview.0",
|
|
45
|
+
"@microsoft/teams.common": "2.0.0-preview.0",
|
|
46
|
+
"@microsoft/teams.graph": "2.0.0-preview.0",
|
|
48
47
|
"@microsoft/teams-js": "^2.35.0"
|
|
49
48
|
},
|
|
50
49
|
"devDependencies": {
|
|
51
|
-
"@microsoft/teams.config": "
|
|
50
|
+
"@microsoft/teams.config": "2.0.0-preview.0",
|
|
52
51
|
"@types/jest": "^29.5.12",
|
|
53
52
|
"jest": "^29.7.0",
|
|
54
53
|
"rimraf": "^6.0.1",
|