@microsoft/teamsfx 0.5.2-alpha.313131ea.0 → 0.5.2-alpha.c917ae3e5.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # TeamsFx SDK for TypeScript/JavaScript
2
2
 
3
- TeamsFx aims to reduce the developer tasks of implementing identity and access to cloud resources down to single-line statements with "zero configuration".
3
+ TeamsFx aims to reduce the developer tasks of leveraging Teams SSO and access to cloud resources down to single-line statements with "zero configuration".
4
4
 
5
5
  Use the library to:
6
6
 
@@ -23,7 +23,7 @@ Please check the [README](https://github.com/OfficeDev/TeamsFx/blob/main/package
23
23
 
24
24
  - Node.js version 10.x.x or higher
25
25
  - A project created by the Teams Toolkit VS Code extension or `teamsfx` CLI tool.
26
- - If your project has installed `botbuilder` related [packages](https://github.com/Microsoft/botbuilder-js#packages) as dependencies, ensure they are of the same version and the version `>= 4.9.3`. ([Issue - all of the BOTBUILDER packages should be the same version](https://github.com/BotBuilderCommunity/botbuilder-community-js/issues/57#issuecomment-508538548))
26
+ - If your project has installed `botbuilder` related [packages](https://github.com/Microsoft/botbuilder-js#packages) as dependencies, ensure they are of the same version and the version `>= 4.15.0`. ([Issue - all of the BOTBUILDER packages should be the same version](https://github.com/BotBuilderCommunity/botbuilder-community-js/issues/57#issuecomment-508538548))
27
27
 
28
28
  ### Install the `@microsoft/teamsfx` package
29
29
 
@@ -33,80 +33,114 @@ Install the TeamsFx SDK for TypeScript/JavaScript with `npm`:
33
33
  npm install @microsoft/teamsfx
34
34
  ```
35
35
 
36
- ### Create and authenticate a `MicrosoftGraphClient`
36
+ ### Scenarios
37
37
 
38
- To create a graph client object to access the Microsoft Graph API, you will need the credential to do authentication. The SDK provides several credential classes to choose that meets various requirements.
38
+ TeamsFx SDK is built to be used in browser and NodeJS environment. Common scenarios include:
39
+ - Teams tab application
40
+ - Azure Function
41
+ - Teams bot
39
42
 
40
- Please note that you need to load configuration before using any credentials.
43
+ ### Create and authenticate a service like `MicrosoftGraphClient`
41
44
 
42
- - In browser environment, you need to explicitly pass in the config parameters. The scaffolded React project has provided environment variables to use.
45
+ To create a graph client object to access the Microsoft Graph API, you will need the credential to do authentication. The SDK provides APIs to configure for developers. Please choose the proper identity type and follow below steps:
43
46
 
44
- ```ts
45
- loadConfiguration({
46
- authentication: {
47
- initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
48
- clientId: process.env.REACT_APP_CLIENT_ID,
49
- },
50
- });
51
- ```
47
+ #### Invoke Graph API on behalf of Teams User (User Identity)
52
48
 
53
- - In NodeJS environment like Azure Function, you can just call `loadConfiguration`. It will load from environment variables by default.
49
+ Use the snippet below:
54
50
 
55
51
  ```ts
56
- loadConfiguration();
52
+ // Equivalent to:
53
+ // const teamsfx = new TeamsFx(IdentityType.User, {
54
+ // initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
55
+ // clientId: process.env.REACT_APP_CLIENT_ID,
56
+ // }
57
+ const teamsfx = new TeamsFx();
58
+ const graphClient = createMicrosoftGraphClient(teamsfx, ["User.Read"]); // Initializes MS Graph SDK using our MsGraphAuthProvider
59
+ const profile = await graphClient.api("/me").get(); // Get the profile of current user
57
60
  ```
58
61
 
59
- #### Using Teams App User Credential
62
+ #### Invoke Graph API without user (Application Identity)
60
63
 
64
+ It doesn't require the interaction with Teams user. You can call Microsoft Graph as application identity.
61
65
  Use the snippet below:
62
66
 
63
- **Note:** You can only use this credential class in browser application like Teams Tab App.
64
-
65
67
  ```ts
66
- loadConfiguration({
67
- authentication: {
68
- initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
69
- clientId: process.env.REACT_APP_CLIENT_ID,
70
- },
71
- });
72
- const credential = new TeamsUserCredential();
73
- const graphClient = createMicrosoftGraphClient(credential, ["User.Read"]); // Initializes MS Graph SDK using our MsGraphAuthProvider
74
- const profile = await graphClient.api("/me").get();
68
+ // Equivalent to:
69
+ // const teamsfx = new TeamsFx(IdentityType.App, {
70
+ // initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
71
+ // clientId: process.env.REACT_APP_CLIENT_ID,
72
+ // });
73
+ const teamsfx = new TeamsFx(IdentityType.App);
74
+ const graphClient = createMicrosoftGraphClient(teamsfx);
75
+ const profile = await graphClient.api("/users/{object_id_of_another_people}").get(); // Get the profile of certain user
75
76
  ```
76
77
 
77
- #### Using Microsoft 365 Tenant Credential
78
+ ## Core Concepts & Code Structure
78
79
 
79
- It doesn't require the interaction with Teams App user. You can call Microsoft Graph as application.
80
- Use the snippet below:
80
+ ### TeamsFx class
81
+ `TeamsFx` class instance reads all TeamsFx settings from environment variables by default. You can also set customized configuration values to override the default values. Please check [Override configuration](#override-configuration) for details.
81
82
 
82
- ```ts
83
- loadConfiguration();
84
- const credential = new M365TenantCredential();
85
- const graphClient = createMicrosoftGraphClient(credential);
86
- const profile = await graphClient.api("/users/{object_id_of_another_people}").get();
87
- ```
83
+ When creating a TeamsFx instance, you also need to specify the identity type. There are 2 identity types:
88
84
 
89
- ## Core Concepts & Code Structure
85
+ #### User Identity
86
+ Using `new TeamsFx(IdentityType.User)` means the application will be authenticated as current Teams user. This one is the default choice. You need to call `TeamsFx:setSsoToken()` when you use user identity in NodeJS environment(without browser).
87
+
88
+ You can use `TeamsFx:getUserInfo()` to get user's basic information.
89
+ `TeamsFx:login()` is used to let user perform consent process if you want to use SSO to get access token for certain OAuth scopes.
90
+
91
+ #### Application Identity
92
+ Using `new TeamsFx(IdentityType.App)` means the application will be authenticated as an application. The permission usually need administrator's approval.
93
+
94
+ `TeamsFx:getCredential()` provides credential instances automatically corresponding to identity type:
95
+ - User Identity: It means that you can access resources on behalf of current Teams user.
96
+ - App Identity: It means that you are acting as a managed app identity which usually need admin consent for resources.
90
97
 
91
98
  ### Credential
92
99
 
100
+ //Developers should choose identity type when initializing TeamsFx. SDK provides 2 types: User and App.
101
+ After developer has specified the identity type when initializing TeamsFx, SDK uses different kinds of credential class to represent the identity and get access token by corresponding auth flow.
102
+
93
103
  There are 3 credential classes that are used to help simplifying authentication. They are located under [credential](src/credential) folder.
94
- Credential classes implements `TokenCredential` interface that is broadly used in Azure library APIs. They are designed to provide access token for specific scopes.
95
- The credential classes represents different identity under certain scenarios.
104
+ Credential classes implements `TokenCredential` interface that is broadly used in Azure library APIs. They are designed to provide access token for specific scopes. Other APIs that relies on credential call `TeamsFx:getCredential()` to get an instance of `TokenCredential`.
96
105
 
97
- `TeamsUserCredential` represents Teams current user's identity. Using this credential will request user consent at the first time.
98
- `M365TenantCredential` represents Microsoft 365 tenant identity. It is usually used when user is not involved like time-triggered automation job.
99
- `OnBehalfOfUserCredential` uses on-behalf-of flow. It needs an access token and you can get a new token for different scope. It's designed to be used in Azure Function or Bot scenarios.
106
+ Here's the corresponding scenarios that each credential class targets.
100
107
 
101
- ### Bot
108
+ #### User Identity in browser environment
109
+ `TeamsUserCredential` represents Teams current user's identity. Using this credential will request user consent at the first time. It leverages the Teams SSO and On-Behalf-Of flow to do token exchange. SDK uses this credential when developer choose "User" identity in browser environment.
110
+
111
+ Required configuration: initiateLoginEndpoint, clientId.
112
+
113
+ #### User Identity in NodeJS environment
114
+ `OnBehalfOfUserCredential` uses On-Behalf-Of flow and need Teams ssoToken. It's designed to be used in Azure Function or Bot scenarios. SDK uses this credential when developer choose "User" identity in NodeJS environment.
115
+
116
+ Required configuration: authorityHost, tenantId, clientId, clientSecret / certificateContent.
117
+
118
+ #### Application Identity in NodeJS environment
119
+ `AppCredential` represents the application identity. It is usually used when user is not involved like time-triggered automation job. SDK uses this credential when developer choose "App" identity in NodeJS environment.
120
+
121
+ Required configuration: tenantId, clientId, clientSecret / certificateContent.
122
+
123
+ ### Bot SSO
102
124
 
103
125
  Bot related classes are stored under [bot](src/bot) folder.
104
126
 
105
- `TeamsBotSsoPrompt` has a good integration with Bot framework. It simplifies the authentication process when you develops bot application.
127
+ `TeamsBotSsoPrompt` has a good integration with Bot framework. It simplifies the authentication process when you develops bot application and want to leverage the Bot SSO.
128
+
129
+ Required configuration: initiateLoginEndpoint, tenantId, clientId, applicationIdUri.
106
130
 
107
131
  ### Helper Function
108
132
 
109
- TeamsFx SDK provides helper functions to ease the configuration for third-party libraries. They are located under [core](src/core) folder.
133
+ TeamsFx SDK provides several helper functions to ease the configuration for third-party libraries. They are located under [core](src/core) folder.
134
+
135
+ #### Microsoft Graph Service
136
+ `createMicrosoftGraphClient` and `MsGraphAuthProvider` help to create authenticated Graph instance.
137
+
138
+ #### SQL
139
+ `getTediousConnectionConfig` returns a tedious connection config.
140
+
141
+ Required configuration:
142
+ - sqlServerEndpoint, sqlUsername, sqlPassword if you want to use user identity
143
+ - sqlServerEndpoint, sqlIdentityId if you want to use MSI identity
110
144
 
111
145
  ### Error Handling
112
146
 
@@ -116,8 +150,8 @@ For example, to filter out specific error, you could use the following check:
116
150
 
117
151
  ```ts
118
152
  try {
119
- const credential = new TeamsUserCredential();
120
- await credential.login("User.Read");
153
+ const teamsfx = new TeamsFx();
154
+ await teamsfx.login("User.Read");
121
155
  } catch (err: unknown) {
122
156
  if (err instanceof ErrorWithCode && err.code !== ErrorCode.ConsentFailed) {
123
157
  throw err;
@@ -132,8 +166,8 @@ And if credential instance is used in other library like Microsoft Graph, it's p
132
166
 
133
167
  ```ts
134
168
  try {
135
- const credential = new TeamsUserCredential();
136
- const graphClient = createMicrosoftGraphClient(credential, ["User.Read"]); // Initializes MS Graph SDK using our MsGraphAuthProvider
169
+ const teamsfx = new TeamsFx();
170
+ const graphClient = createMicrosoftGraphClient(teamsfx, ["User.Read"]); // Initializes MS Graph SDK using our MsGraphAuthProvider
137
171
  const profile = await graphClient.api("/me").get();
138
172
  } catch (err: unknown) {
139
173
  // ErrorWithCode is handled by Graph client
@@ -157,17 +191,11 @@ The following sections provide several code snippets covering some of the most c
157
191
 
158
192
  ### Use Graph API in tab app
159
193
 
160
- Use `TeamsUserCredential` and `createMicrosoftGraphClient`.
194
+ Use `TeamsFx` and `createMicrosoftGraphClient`.
161
195
 
162
196
  ```ts
163
- loadConfiguration({
164
- authentication: {
165
- initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
166
- clientId: process.env.REACT_APP_CLIENT_ID,
167
- },
168
- });
169
- const credential: any = new TeamsUserCredential();
170
- const graphClient = createMicrosoftGraphClient(credential, ["User.Read"]);
197
+ const teamsfx = new TeamsFx();
198
+ const graphClient = createMicrosoftGraphClient(teamsfx, ["User.Read"]);
171
199
  const profile = await graphClient.api("/me").get();
172
200
  ```
173
201
 
@@ -176,17 +204,11 @@ const profile = await graphClient.api("/me").get();
176
204
  Use `axios` library to make HTTP request to Azure Function.
177
205
 
178
206
  ```ts
179
- loadConfiguration({
180
- authentication: {
181
- initiateLoginEndpoint: process.env.REACT_APP_START_LOGIN_PAGE_URL,
182
- clientId: process.env.REACT_APP_CLIENT_ID,
183
- },
184
- });
185
- const credential: any = new TeamsUserCredential();
186
- const token = credential.getToken(""); // Get SSO token for the user
207
+ const teamsfx = new TeamsFx();
208
+ const token = teamsfx.getCredential().getToken(""); // Get SSO token for the user
187
209
  // Call API hosted in Azure Functions on behalf of user
188
- const apiConfig = getResourceConfiguration(ResourceType.API);
189
- const response = await axios.default.get(apiConfig.endpoint + "api/httptrigger1", {
210
+ const apiEndpoint = teamsfx.getConfig("apiEndpoint");
211
+ const response = await axios.default.get(apiEndpoint + "api/httptrigger1", {
190
212
  headers: {
191
213
  authorization: "Bearer " + token,
192
214
  },
@@ -199,12 +221,17 @@ Use `tedious` library to access SQL and leverage `DefaultTediousConnectionConfig
199
221
  Apart from `tedious`, you can also compose connection config of other SQL libraries based on the result of `sqlConnectionConfig.getConfig()`.
200
222
 
201
223
  ```ts
202
- loadConfiguration();
203
- const sqlConnectConfig = new DefaultTediousConnectionConfiguration();
224
+ // Equivalent to:
225
+ // const sqlConnectConfig = new DefaultTediousConnectionConfiguration({
226
+ // sqlServerEndpoint: process.env.SQL_ENDPOINT,
227
+ // sqlUsername: process.env.SQL_USER_NAME,
228
+ // sqlPassword: process.env.SQL_PASSWORD,
229
+ // });
230
+ const teamsfx = new TeamsFx();
204
231
  // if there's only one SQL database
205
- const config = await sqlConnectConfig.getConfig();
232
+ const config = await getTediousConnectionConfig(teamsfx);
206
233
  // if there are multiple SQL databases
207
- const config2 = await sqlConnectionConfig.getConfig("your database name");
234
+ const config2 = await getTediousConnectionConfig(teamsfx, "your database name");
208
235
  const connection = new Connection(config);
209
236
  connection.on("connect", (error) => {
210
237
  if (error) {
@@ -216,14 +243,17 @@ connection.on("connect", (error) => {
216
243
  ### Use certificate-based authentication in Azure Function
217
244
 
218
245
  ```ts
219
- loadConfiguration({
220
- authentication: {
221
- clientId: process.env.M365_CLIENT_ID,
222
- certificateContent: "The content of a PEM-encoded public/private key certificate",
223
- authorityHost: process.env.M365_AUTHORITY_HOST,
224
- tenantId: process.env.M365_TENANT_ID,
225
- },
246
+ const authConfig = {
247
+ clientId: process.env.M365_CLIENT_ID,
248
+ certificateContent: "The content of a PEM-encoded public/private key certificate",
249
+ authorityHost: process.env.M365_AUTHORITY_HOST,
250
+ tenantId: process.env.M365_TENANT_ID,
251
+ };
252
+ const teamsfx = new TeamsFx(IdentityType.App);
253
+ teamsfx.setCustomeConfig({
254
+ certificateContent: "The content of a PEM-encoded public/private key certificate"
226
255
  });
256
+ const token = teamsfx.getCredential().getToken();
227
257
  ```
228
258
 
229
259
  ### Use Graph API in Bot application
@@ -239,9 +269,9 @@ const convoState = new ConversationState(new MemoryStorage());
239
269
  const dialogState = convoState.createProperty("dialogState");
240
270
  const dialogs = new DialogSet(dialogState);
241
271
 
242
- loadConfiguration();
272
+ const teamsfx = new TeamsFx();
243
273
  dialogs.add(
244
- new TeamsBotSsoPrompt("TeamsBotSsoPrompt", {
274
+ new TeamsBotSsoPrompt(teamsfx, "TeamsBotSsoPrompt", {
245
275
  scopes: ["User.Read"],
246
276
  })
247
277
  );
@@ -264,7 +294,7 @@ dialogs.add(
264
294
  );
265
295
  ```
266
296
 
267
- ## Troubleshooting
297
+ ## Advanced Customization
268
298
 
269
299
  ### Configure log
270
300
 
@@ -309,6 +339,44 @@ setLogFunction((level: LogLevel, message: string) => {
309
339
  });
310
340
  ```
311
341
 
342
+
343
+ ### Override configuration
344
+ You can pass custom config when creating `TeamsFx` instance to override default configuration or set required fields when environment variables are missing.
345
+
346
+ - If you have created tab project using VS Code toolkit, the following config values will be used from pre-configured environment variables:
347
+ * authorityHost (REACT_APP_AUTHORITY_HOST)
348
+ * tenantId (REACT_APP_TENANT_ID)
349
+ * clientId (REACT_APP_CLIENT_ID)
350
+ * initiateLoginEndpoint (REACT_APP_START_LOGIN_PAGE_URL)
351
+ * applicationIdUri (REACT_APP_START_LOGIN_PAGE_URL)
352
+ * apiEndpoint (REACT_APP_FUNC_ENDPOINT)
353
+ * apiName (REACT_APP_FUNC_NAME)
354
+
355
+ - If you have created Azure Function / Bot project using VS Code toolkit, the following config values will be used from pre-configured environment variables:
356
+ * initiateLoginEndpoint (INITIATE_LOGIN_ENDPOINT)
357
+ * authorityHost (M365_AUTHORITY_HOST)
358
+ * tenantId (M365_TENANT_ID)
359
+ * clientId (M365_CLIENT_ID)
360
+ * clientSecret (M365_CLIENT_SECRET)
361
+ * applicationIdUri (M365_APPLICATION_ID_URI)
362
+ * apiEndpoint (API_ENDPOINT)
363
+ * sqlServerEndpoint (SQL_ENDPOINT)
364
+ * sqlUsername (SQL_USER_NAME)
365
+ * sqlPassword (SQL_PASSWORD)
366
+ * sqlDatabaseName (SQL_DATABASE_NAME)
367
+ * sqlIdentityId (IDENTITY_ID)
368
+
369
+ ## How to fix the breaking change if upgraded from previous SDK version
370
+
371
+ If you are using the version of SDK that has `loadConfiguration()`, you can follow these steps to upgrade to the latest SDK version.
372
+ 1. Remove `loadConfiguration()` and pass customized settings using `new TeamsFx(IdentityType.User, { ...customConfig })`.
373
+ 2. Replace `new TeamsUserCredential()` with `new TeamsFx()`.
374
+ 3. Replace `new M365TenantCredential()` with `new TeamsFx(IdentityType.App)`.
375
+ 4. Replace `new OnBehalfOfUserCredential(ssoToken)` with `new TeamsFx().setSsoToken(ssoToken)`.
376
+ 5. Pass the instance of `TeamsFx` to helper functions to replace credential instance.
377
+
378
+ Also see [TeamsFx class](#teamsfx-class) for furthur description.
379
+
312
380
  ## Next steps
313
381
 
314
382
  Please take a look at the [Samples](https://github.com/OfficeDev/TeamsFx-Samples) project for detailed examples on how to use this library.