@microsoft/teamsfx 0.3.3-alpha.3dc53ce2.0 → 0.3.3-alpha.7e7c7c23.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 +2 -2
- package/dist/index.esm2017.js +1413 -0
- package/dist/index.esm2017.js.map +1 -0
- package/dist/{index.js → index.esm2017.mjs} +1467 -1506
- package/dist/index.esm2017.mjs.map +1 -0
- package/dist/index.esm5.js +1575 -0
- package/dist/index.esm5.js.map +1 -0
- package/dist/index.node.cjs.js +1653 -0
- package/dist/index.node.cjs.js.map +1 -0
- package/package.json +58 -78
- package/types/teamsfx.d.ts +0 -2
- package/dist/index.js.map +0 -1
- package/dist/teamsfx.js +0 -30
- package/dist/teamsfx.js.map +0 -1
- package/dist-esm/src/bot/teamsBotSsoPrompt.browser.js +0 -118
- package/dist-esm/src/bot/teamsBotSsoPrompt.browser.js.map +0 -1
- package/dist-esm/src/bot/teamsBotSsoPrompt.js +0 -349
- package/dist-esm/src/bot/teamsBotSsoPrompt.js.map +0 -1
- package/dist-esm/src/bot/teamsBotSsoPromptTokenResponse.js +0 -2
- package/dist-esm/src/bot/teamsBotSsoPromptTokenResponse.js.map +0 -1
- package/dist-esm/src/core/cache.browser.js +0 -22
- package/dist-esm/src/core/cache.browser.js.map +0 -1
- package/dist-esm/src/core/cache.js +0 -28
- package/dist-esm/src/core/cache.js.map +0 -1
- package/dist-esm/src/core/configurationProvider.js +0 -124
- package/dist-esm/src/core/configurationProvider.js.map +0 -1
- package/dist-esm/src/core/defaultTediousConnectionConfiguration.browser.js +0 -28
- package/dist-esm/src/core/defaultTediousConnectionConfiguration.browser.js.map +0 -1
- package/dist-esm/src/core/defaultTediousConnectionConfiguration.js +0 -182
- package/dist-esm/src/core/defaultTediousConnectionConfiguration.js.map +0 -1
- package/dist-esm/src/core/errors.js +0 -97
- package/dist-esm/src/core/errors.js.map +0 -1
- package/dist-esm/src/core/msGraphAuthProvider.js +0 -68
- package/dist-esm/src/core/msGraphAuthProvider.js.map +0 -1
- package/dist-esm/src/core/msGraphClientProvider.js +0 -65
- package/dist-esm/src/core/msGraphClientProvider.js.map +0 -1
- package/dist-esm/src/credential/m365TenantCredential.browser.js +0 -38
- package/dist-esm/src/credential/m365TenantCredential.browser.js.map +0 -1
- package/dist-esm/src/credential/m365TenantCredential.js +0 -126
- package/dist-esm/src/credential/m365TenantCredential.js.map +0 -1
- package/dist-esm/src/credential/onBehalfOfUserCredential.browser.js +0 -46
- package/dist-esm/src/credential/onBehalfOfUserCredential.browser.js.map +0 -1
- package/dist-esm/src/credential/onBehalfOfUserCredential.js +0 -178
- package/dist-esm/src/credential/onBehalfOfUserCredential.js.map +0 -1
- package/dist-esm/src/credential/teamsUserCredential.browser.js +0 -462
- package/dist-esm/src/credential/teamsUserCredential.browser.js.map +0 -1
- package/dist-esm/src/credential/teamsUserCredential.js +0 -56
- package/dist-esm/src/credential/teamsUserCredential.js.map +0 -1
- package/dist-esm/src/index.js +0 -14
- package/dist-esm/src/index.js.map +0 -1
- package/dist-esm/src/models/accessTokenResult.js +0 -4
- package/dist-esm/src/models/accessTokenResult.js.map +0 -1
- package/dist-esm/src/models/authCodeResult.js +0 -4
- package/dist-esm/src/models/authCodeResult.js.map +0 -1
- package/dist-esm/src/models/configuration.js +0 -20
- package/dist-esm/src/models/configuration.js.map +0 -1
- package/dist-esm/src/models/grantType.js +0 -11
- package/dist-esm/src/models/grantType.js.map +0 -1
- package/dist-esm/src/models/ssoTokenInfo.js +0 -4
- package/dist-esm/src/models/ssoTokenInfo.js.map +0 -1
- package/dist-esm/src/models/userinfo.js +0 -4
- package/dist-esm/src/models/userinfo.js.map +0 -1
- package/dist-esm/src/util/logger.js +0 -134
- package/dist-esm/src/util/logger.js.map +0 -1
- package/dist-esm/src/util/utils.js +0 -130
- package/dist-esm/src/util/utils.js.map +0 -1
- package/dist-esm/src/util/utils.node.js +0 -23
- package/dist-esm/src/util/utils.node.js.map +0 -1
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
|
2
|
-
// Licensed under the MIT license.
|
|
3
|
-
import { __awaiter } from "tslib";
|
|
4
|
-
import { formatString } from "../util/utils";
|
|
5
|
-
import { ErrorCode, ErrorMessage, ErrorWithCode } from "../core/errors";
|
|
6
|
-
/**
|
|
7
|
-
* Represent Microsoft 365 tenant identity, and it is usually used when user is not involved.
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* Only works in in server side.
|
|
11
|
-
*
|
|
12
|
-
* @beta
|
|
13
|
-
*/
|
|
14
|
-
export class M365TenantCredential {
|
|
15
|
-
/**
|
|
16
|
-
* Constructor of M365TenantCredential.
|
|
17
|
-
*
|
|
18
|
-
* @remarks
|
|
19
|
-
* Only works in in server side.
|
|
20
|
-
* @beta
|
|
21
|
-
*/
|
|
22
|
-
constructor() {
|
|
23
|
-
throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "M365TenantCredential"), ErrorCode.RuntimeNotSupported);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Get access token for credential.
|
|
27
|
-
*
|
|
28
|
-
* @remarks
|
|
29
|
-
* Only works in in server side.
|
|
30
|
-
* @beta
|
|
31
|
-
*/
|
|
32
|
-
getToken(scopes, options) {
|
|
33
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
-
throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "M365TenantCredential"), ErrorCode.RuntimeNotSupported);
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
//# sourceMappingURL=m365TenantCredential.browser.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"m365TenantCredential.browser.js","sourceRoot":"","sources":["../../../src/credential/m365TenantCredential.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAGlC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAExE;;;;;;;GAOG;AACH,MAAM,OAAO,oBAAoB;IAC/B;;;;;;OAMG;IACH;QACE,MAAM,IAAI,aAAa,CACrB,YAAY,CAAC,YAAY,CAAC,0BAA0B,EAAE,sBAAsB,CAAC,EAC7E,SAAS,CAAC,mBAAmB,CAC9B,CAAC;IACJ,CAAC;IAED;;;;;;OAMG;IACG,QAAQ,CACZ,MAAyB,EACzB,OAAyB;;YAEzB,MAAM,IAAI,aAAa,CACrB,YAAY,CAAC,YAAY,CAAC,0BAA0B,EAAE,sBAAsB,CAAC,EAC7E,SAAS,CAAC,mBAAmB,CAC9B,CAAC;QACJ,CAAC;KAAA;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AccessToken, TokenCredential, GetTokenOptions } from \"@azure/identity\";\nimport { formatString } from \"../util/utils\";\nimport { ErrorCode, ErrorMessage, ErrorWithCode } from \"../core/errors\";\n\n/**\n * Represent Microsoft 365 tenant identity, and it is usually used when user is not involved.\n *\n * @remarks\n * Only works in in server side.\n *\n * @beta\n */\nexport class M365TenantCredential implements TokenCredential {\n /**\n * Constructor of M365TenantCredential.\n *\n * @remarks\n * Only works in in server side.\n * @beta\n */\n constructor() {\n throw new ErrorWithCode(\n formatString(ErrorMessage.BrowserRuntimeNotSupported, \"M365TenantCredential\"),\n ErrorCode.RuntimeNotSupported\n );\n }\n\n /**\n * Get access token for credential.\n *\n * @remarks\n * Only works in in server side.\n * @beta\n */\n async getToken(\n scopes: string | string[],\n options?: GetTokenOptions\n ): Promise<AccessToken | null> {\n throw new ErrorWithCode(\n formatString(ErrorMessage.BrowserRuntimeNotSupported, \"M365TenantCredential\"),\n ErrorCode.RuntimeNotSupported\n );\n }\n}\n"]}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
|
2
|
-
// Licensed under the MIT license.
|
|
3
|
-
import { __awaiter } from "tslib";
|
|
4
|
-
import { internalLogger } from "../util/logger";
|
|
5
|
-
import { validateScopesType, formatString, getScopesArray } from "../util/utils";
|
|
6
|
-
import { getAuthenticationConfiguration } from "../core/configurationProvider";
|
|
7
|
-
import { ErrorCode, ErrorMessage, ErrorWithCode } from "../core/errors";
|
|
8
|
-
import { createConfidentialClientApplication } from "../util/utils.node";
|
|
9
|
-
/**
|
|
10
|
-
* Represent Microsoft 365 tenant identity, and it is usually used when user is not involved like time-triggered automation job.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* loadConfiguration(); // load configuration from environment variables
|
|
15
|
-
* const credential = new M365TenantCredential();
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @remarks
|
|
19
|
-
* Only works in in server side.
|
|
20
|
-
*
|
|
21
|
-
* @beta
|
|
22
|
-
*/
|
|
23
|
-
export class M365TenantCredential {
|
|
24
|
-
/**
|
|
25
|
-
* Constructor of M365TenantCredential.
|
|
26
|
-
*
|
|
27
|
-
* @remarks
|
|
28
|
-
* Only works in in server side.
|
|
29
|
-
*
|
|
30
|
-
* @throws {@link ErrorCode|InvalidConfiguration} when client id, client secret or tenant id is not found in config.
|
|
31
|
-
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
32
|
-
*
|
|
33
|
-
* @beta
|
|
34
|
-
*/
|
|
35
|
-
constructor() {
|
|
36
|
-
internalLogger.info("Create M365 tenant credential");
|
|
37
|
-
const config = this.loadAndValidateConfig();
|
|
38
|
-
this.msalClient = createConfidentialClientApplication(config);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Get access token for credential.
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* await credential.getToken(["User.Read.All"]) // Get Graph access token for single scope using string array
|
|
46
|
-
* await credential.getToken("User.Read.All") // Get Graph access token for single scope using string
|
|
47
|
-
* await credential.getToken(["User.Read.All", "Calendars.Read"]) // Get Graph access token for multiple scopes using string array
|
|
48
|
-
* await credential.getToken("User.Read.All Calendars.Read") // Get Graph access token for multiple scopes using space-separated string
|
|
49
|
-
* await credential.getToken("https://graph.microsoft.com/User.Read.All") // Get Graph access token with full resource URI
|
|
50
|
-
* await credential.getToken(["https://outlook.office.com/Mail.Read"]) // Get Outlook access token
|
|
51
|
-
* ```
|
|
52
|
-
*
|
|
53
|
-
* @param {string | string[]} scopes - The list of scopes for which the token will have access.
|
|
54
|
-
* @param {GetTokenOptions} options - The options used to configure any requests this TokenCredential implementation might make.
|
|
55
|
-
*
|
|
56
|
-
* @throws {@link ErrorCode|ServiceError} when get access token with authentication error.
|
|
57
|
-
* @throws {@link ErrorCode|InternalError} when get access token with unknown error.
|
|
58
|
-
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
59
|
-
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.
|
|
60
|
-
*
|
|
61
|
-
* @returns Access token with expected scopes.
|
|
62
|
-
* Throw error if get access token failed.
|
|
63
|
-
*
|
|
64
|
-
* @beta
|
|
65
|
-
*/
|
|
66
|
-
getToken(scopes, options) {
|
|
67
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
68
|
-
let accessToken;
|
|
69
|
-
validateScopesType(scopes);
|
|
70
|
-
const scopesStr = typeof scopes === "string" ? scopes : scopes.join(" ");
|
|
71
|
-
internalLogger.info("Get access token with scopes: " + scopesStr);
|
|
72
|
-
try {
|
|
73
|
-
const scopesArray = getScopesArray(scopes);
|
|
74
|
-
const authenticationResult = yield this.msalClient.acquireTokenByClientCredential({
|
|
75
|
-
scopes: scopesArray,
|
|
76
|
-
});
|
|
77
|
-
if (authenticationResult) {
|
|
78
|
-
accessToken = {
|
|
79
|
-
token: authenticationResult.accessToken,
|
|
80
|
-
expiresOnTimestamp: authenticationResult.expiresOn.getTime(),
|
|
81
|
-
};
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
catch (err) {
|
|
85
|
-
const errorMsg = "Get M365 tenant credential failed with error: " + err.message;
|
|
86
|
-
internalLogger.error(errorMsg);
|
|
87
|
-
throw new ErrorWithCode(errorMsg, ErrorCode.ServiceError);
|
|
88
|
-
}
|
|
89
|
-
if (!accessToken) {
|
|
90
|
-
const errorMsg = "Get M365 tenant credential access token failed with empty access token";
|
|
91
|
-
internalLogger.error(errorMsg);
|
|
92
|
-
throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);
|
|
93
|
-
}
|
|
94
|
-
return accessToken;
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
/**
|
|
98
|
-
* Load and validate authentication configuration
|
|
99
|
-
* @returns Authentication configuration
|
|
100
|
-
*/
|
|
101
|
-
loadAndValidateConfig() {
|
|
102
|
-
internalLogger.verbose("Validate authentication configuration");
|
|
103
|
-
const config = getAuthenticationConfiguration();
|
|
104
|
-
if (!config) {
|
|
105
|
-
internalLogger.error(ErrorMessage.AuthenticationConfigurationNotExists);
|
|
106
|
-
throw new ErrorWithCode(ErrorMessage.AuthenticationConfigurationNotExists, ErrorCode.InvalidConfiguration);
|
|
107
|
-
}
|
|
108
|
-
if (config.clientId && (config.clientSecret || config.certificateContent) && config.tenantId) {
|
|
109
|
-
return config;
|
|
110
|
-
}
|
|
111
|
-
const missingValues = [];
|
|
112
|
-
if (!config.clientId) {
|
|
113
|
-
missingValues.push("clientId");
|
|
114
|
-
}
|
|
115
|
-
if (!config.clientSecret && !config.certificateContent) {
|
|
116
|
-
missingValues.push("clientSecret or certificateContent");
|
|
117
|
-
}
|
|
118
|
-
if (!config.tenantId) {
|
|
119
|
-
missingValues.push("tenantId");
|
|
120
|
-
}
|
|
121
|
-
const errorMsg = formatString(ErrorMessage.InvalidConfiguration, missingValues.join(", "), "undefined");
|
|
122
|
-
internalLogger.error(errorMsg);
|
|
123
|
-
throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
//# sourceMappingURL=m365TenantCredential.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"m365TenantCredential.js","sourceRoot":"","sources":["../../../src/credential/m365TenantCredential.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAIlC,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AACjF,OAAO,EAAE,8BAA8B,EAAE,MAAM,+BAA+B,CAAC;AAC/E,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAExE,OAAO,EAAE,mCAAmC,EAAE,MAAM,oBAAoB,CAAC;AAEzE;;;;;;;;;;;;;GAaG;AACH,MAAM,OAAO,oBAAoB;IAG/B;;;;;;;;;;OAUG;IACH;QACE,cAAc,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;QAErD,MAAM,MAAM,GAAG,IAAI,CAAC,qBAAqB,EAAE,CAAC;QAE5C,IAAI,CAAC,UAAU,GAAG,mCAAmC,CAAC,MAAM,CAAC,CAAC;IAChE,CAAC;IAED;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACG,QAAQ,CACZ,MAAyB,EACzB,OAAyB;;YAEzB,IAAI,WAAW,CAAC;YAChB,kBAAkB,CAAC,MAAM,CAAC,CAAC;YAC3B,MAAM,SAAS,GAAG,OAAO,MAAM,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YACzE,cAAc,CAAC,IAAI,CAAC,gCAAgC,GAAG,SAAS,CAAC,CAAC;YAElE,IAAI;gBACF,MAAM,WAAW,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;gBAC3C,MAAM,oBAAoB,GAAG,MA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B,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;YACpB,aAAa,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAChC;QAED,IAAI,CAAC,MAAM,CAAC,YAAY,IAAI,CAAC,MAAM,CAAC,kBAAkB,EAAE;YACtD,aAAa,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAC;SAC1D;QAED,IAAI,CAAC,MAAM,CAAC,QAAQ,EAAE;YACpB,aAAa,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAChC;QAED,MAAM,QAAQ,GAAG,YAAY,CAC3B,YAAY,CAAC,oBAAoB,EACjC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,EACxB,WAAW,CACZ,CAAC;QACF,cAAc,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC/B,MAAM,IAAI,aAAa,CAAC,QAAQ,EAAE,SAAS,CAAC,oBAAoB,CAAC,CAAC;IACpE,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AccessToken, TokenCredential, GetTokenOptions } from \"@azure/identity\";\nimport { AuthenticationConfiguration } from \"../models/configuration\";\nimport { internalLogger } from \"../util/logger\";\nimport { validateScopesType, formatString, getScopesArray } from \"../util/utils\";\nimport { getAuthenticationConfiguration } from \"../core/configurationProvider\";\nimport { ErrorCode, ErrorMessage, ErrorWithCode } from \"../core/errors\";\nimport { ConfidentialClientApplication } from \"@azure/msal-node\";\nimport { createConfidentialClientApplication } from \"../util/utils.node\";\n\n/**\n * Represent Microsoft 365 tenant identity, and it is usually used when user is not involved like time-triggered automation job.\n *\n * @example\n * ```typescript\n * loadConfiguration(); // load configuration from environment variables\n * const credential = new M365TenantCredential();\n * ```\n *\n * @remarks\n * Only works in in server side.\n *\n * @beta\n */\nexport class M365TenantCredential implements TokenCredential {\n private readonly msalClient: ConfidentialClientApplication;\n\n /**\n * Constructor of M365TenantCredential.\n *\n * @remarks\n * Only works in in server side.\n *\n * @throws {@link ErrorCode|InvalidConfiguration} when client id, client secret or tenant id is not found in config.\n * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.\n *\n * @beta\n */\n constructor() {\n internalLogger.info(\"Create M365 tenant credential\");\n\n const config = this.loadAndValidateConfig();\n\n this.msalClient = createConfidentialClientApplication(config);\n }\n\n /**\n * Get access token for credential.\n *\n * @example\n * ```typescript\n * await credential.getToken([\"User.Read.All\"]) // Get Graph access token for single scope using string array\n * await credential.getToken(\"User.Read.All\") // Get Graph access token for single scope using string\n * await credential.getToken([\"User.Read.All\", \"Calendars.Read\"]) // Get Graph access token for multiple scopes using string array\n * await credential.getToken(\"User.Read.All Calendars.Read\") // Get Graph access token for multiple scopes using space-separated string\n * await credential.getToken(\"https://graph.microsoft.com/User.Read.All\") // Get Graph access token with full resource URI\n * await credential.getToken([\"https://outlook.office.com/Mail.Read\"]) // Get Outlook access token\n * ```\n *\n * @param {string | string[]} scopes - The list of scopes for which the token will have access.\n * @param {GetTokenOptions} options - The options used to configure any requests this TokenCredential implementation might make.\n *\n * @throws {@link ErrorCode|ServiceError} when get access token with authentication error.\n * @throws {@link ErrorCode|InternalError} when get access token with unknown error.\n * @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.\n * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is nodeJS.\n *\n * @returns Access token with expected scopes.\n * Throw error if get access token failed.\n *\n * @beta\n */\n async getToken(\n scopes: string | string[],\n options?: GetTokenOptions\n ): Promise<AccessToken | null> {\n let accessToken;\n validateScopesType(scopes);\n const scopesStr = typeof scopes === \"string\" ? scopes : scopes.join(\" \");\n internalLogger.info(\"Get access token with scopes: \" + scopesStr);\n\n try {\n const scopesArray = getScopesArray(scopes);\n const authenticationResult = await this.msalClient.acquireTokenByClientCredential({\n scopes: scopesArray,\n });\n if (authenticationResult) {\n accessToken = {\n token: authenticationResult.accessToken,\n expiresOnTimestamp: authenticationResult.expiresOn!.getTime(),\n };\n }\n } catch (err: any) {\n const errorMsg = \"Get M365 tenant credential failed with error: \" + err.message;\n internalLogger.error(errorMsg);\n throw new ErrorWithCode(errorMsg, ErrorCode.ServiceError);\n }\n\n if (!accessToken) {\n const errorMsg = \"Get M365 tenant credential access token failed with empty access token\";\n internalLogger.error(errorMsg);\n throw new ErrorWithCode(errorMsg, ErrorCode.InternalError);\n }\n\n return accessToken;\n }\n\n /**\n * Load and validate authentication configuration\n * @returns Authentication configuration\n */\n private loadAndValidateConfig(): AuthenticationConfiguration {\n internalLogger.verbose(\"Validate authentication configuration\");\n\n const config = getAuthenticationConfiguration();\n\n if (!config) {\n internalLogger.error(ErrorMessage.AuthenticationConfigurationNotExists);\n throw new ErrorWithCode(\n ErrorMessage.AuthenticationConfigurationNotExists,\n ErrorCode.InvalidConfiguration\n );\n }\n\n if (config.clientId && (config.clientSecret || config.certificateContent) && config.tenantId) {\n return config;\n }\n\n const missingValues = [];\n\n if (!config.clientId) {\n missingValues.push(\"clientId\");\n }\n\n if (!config.clientSecret && !config.certificateContent) {\n missingValues.push(\"clientSecret or certificateContent\");\n }\n\n if (!config.tenantId) {\n missingValues.push(\"tenantId\");\n }\n\n const errorMsg = formatString(\n ErrorMessage.InvalidConfiguration,\n missingValues.join(\", \"),\n \"undefined\"\n );\n internalLogger.error(errorMsg);\n throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);\n }\n}\n"]}
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
|
2
|
-
// Licensed under the MIT license.
|
|
3
|
-
import { __awaiter } from "tslib";
|
|
4
|
-
import { formatString } from "../util/utils";
|
|
5
|
-
import { ErrorWithCode, ErrorCode, ErrorMessage } from "../core/errors";
|
|
6
|
-
/**
|
|
7
|
-
* Represent on-behalf-of flow to get user identity, and it is designed to be used in Azure Function or Bot scenarios.
|
|
8
|
-
*
|
|
9
|
-
* @remarks
|
|
10
|
-
* Can only be used in server side.
|
|
11
|
-
*
|
|
12
|
-
* @beta
|
|
13
|
-
*/
|
|
14
|
-
export class OnBehalfOfUserCredential {
|
|
15
|
-
/**
|
|
16
|
-
* Constructor of OnBehalfOfUserCredential
|
|
17
|
-
*
|
|
18
|
-
* @remarks
|
|
19
|
-
* Can Only works in in server side.
|
|
20
|
-
* @beta
|
|
21
|
-
*/
|
|
22
|
-
constructor(ssoToken) {
|
|
23
|
-
throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "OnBehalfOfUserCredential"), ErrorCode.RuntimeNotSupported);
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* Get access token from credential.
|
|
27
|
-
* @remarks
|
|
28
|
-
* Can only be used in server side.
|
|
29
|
-
* @beta
|
|
30
|
-
*/
|
|
31
|
-
getToken(scopes, options) {
|
|
32
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
-
throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "OnBehalfOfUserCredential"), ErrorCode.RuntimeNotSupported);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Get basic user info from SSO token.
|
|
38
|
-
* @remarks
|
|
39
|
-
* Can only be used in server side.
|
|
40
|
-
* @beta
|
|
41
|
-
*/
|
|
42
|
-
getUserInfo() {
|
|
43
|
-
throw new ErrorWithCode(formatString(ErrorMessage.BrowserRuntimeNotSupported, "OnBehalfOfUserCredential"), ErrorCode.RuntimeNotSupported);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
//# sourceMappingURL=onBehalfOfUserCredential.browser.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"onBehalfOfUserCredential.browser.js","sourceRoot":"","sources":["../../../src/credential/onBehalfOfUserCredential.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;;AAIlC,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,aAAa,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAExE;;;;;;;GAOG;AACH,MAAM,OAAO,wBAAwB;IACnC;;;;;;OAMG;IACH,YAAY,QAAgB;QAC1B,MAAM,IAAI,aAAa,CACrB,YAAY,CAAC,YAAY,CAAC,0BAA0B,EAAE,0BAA0B,CAAC,EACjF,SAAS,CAAC,mBAAmB,CAC9B,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACG,QAAQ,CACZ,MAAyB,EACzB,OAAyB;;YAEzB,MAAM,IAAI,aAAa,CACrB,YAAY,CAAC,YAAY,CAAC,0BAA0B,EAAE,0BAA0B,CAAC,EACjF,SAAS,CAAC,mBAAmB,CAC9B,CAAC;QACJ,CAAC;KAAA;IAED;;;;;OAKG;IACI,WAAW;QAChB,MAAM,IAAI,aAAa,CACrB,YAAY,CAAC,YAAY,CAAC,0BAA0B,EAAE,0BAA0B,CAAC,EACjF,SAAS,CAAC,mBAAmB,CAC9B,CAAC;IACJ,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AccessToken, GetTokenOptions, TokenCredential } from \"@azure/identity\";\nimport { UserInfo } from \"../models/userinfo\";\nimport { formatString } from \"../util/utils\";\nimport { ErrorWithCode, ErrorCode, ErrorMessage } from \"../core/errors\";\n\n/**\n * Represent on-behalf-of flow to get user identity, and it is designed to be used in Azure Function or Bot scenarios.\n *\n * @remarks\n * Can only be used in server side.\n *\n * @beta\n */\nexport class OnBehalfOfUserCredential implements TokenCredential {\n /**\n * Constructor of OnBehalfOfUserCredential\n *\n * @remarks\n * Can Only works in in server side.\n * @beta\n */\n constructor(ssoToken: string) {\n throw new ErrorWithCode(\n formatString(ErrorMessage.BrowserRuntimeNotSupported, \"OnBehalfOfUserCredential\"),\n ErrorCode.RuntimeNotSupported\n );\n }\n\n /**\n * Get access token from credential.\n * @remarks\n * Can only be used in server side.\n * @beta\n */\n async getToken(\n scopes: string | string[],\n options?: GetTokenOptions\n ): Promise<AccessToken | null> {\n throw new ErrorWithCode(\n formatString(ErrorMessage.BrowserRuntimeNotSupported, \"OnBehalfOfUserCredential\"),\n ErrorCode.RuntimeNotSupported\n );\n }\n\n /**\n * Get basic user info from SSO token.\n * @remarks\n * Can only be used in server side.\n * @beta\n */\n public getUserInfo(): Promise<UserInfo> {\n throw new ErrorWithCode(\n formatString(ErrorMessage.BrowserRuntimeNotSupported, \"OnBehalfOfUserCredential\"),\n ErrorCode.RuntimeNotSupported\n );\n }\n}\n"]}
|
|
@@ -1,178 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Microsoft Corporation.
|
|
2
|
-
// Licensed under the MIT license.
|
|
3
|
-
import { __awaiter } from "tslib";
|
|
4
|
-
import { config } from "../core/configurationProvider";
|
|
5
|
-
import { internalLogger } from "../util/logger";
|
|
6
|
-
import { formatString, getScopesArray, getUserInfoFromSsoToken, parseJwt, validateScopesType, } from "../util/utils";
|
|
7
|
-
import { ErrorWithCode, ErrorCode, ErrorMessage } from "../core/errors";
|
|
8
|
-
import { createConfidentialClientApplication } from "../util/utils.node";
|
|
9
|
-
/**
|
|
10
|
-
* Represent on-behalf-of flow to get user identity, and it is designed to be used in server side.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```typescript
|
|
14
|
-
* loadConfiguration(); // load configuration from environment variables
|
|
15
|
-
* const credential = new OnBehalfOfUserCredential(ssoToken);
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @remarks
|
|
19
|
-
* Can only be used in server side.
|
|
20
|
-
*
|
|
21
|
-
* @beta
|
|
22
|
-
*/
|
|
23
|
-
export class OnBehalfOfUserCredential {
|
|
24
|
-
/**
|
|
25
|
-
* Constructor of OnBehalfOfUserCredential
|
|
26
|
-
*
|
|
27
|
-
* @remarks
|
|
28
|
-
* Only works in in server side.
|
|
29
|
-
*
|
|
30
|
-
* @param {string} ssoToken - User token provided by Teams SSO feature.
|
|
31
|
-
*
|
|
32
|
-
* @throws {@link ErrorCode|InvalidConfiguration} when client id, client secret, certificate content, authority host or tenant id is not found in config.
|
|
33
|
-
* @throws {@link ErrorCode|InternalError} when SSO token is not valid.
|
|
34
|
-
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.
|
|
35
|
-
*
|
|
36
|
-
* @beta
|
|
37
|
-
*/
|
|
38
|
-
constructor(ssoToken) {
|
|
39
|
-
var _a, _b, _c, _d, _e;
|
|
40
|
-
internalLogger.info("Get on behalf of user credential");
|
|
41
|
-
const missingConfigurations = [];
|
|
42
|
-
if (!((_a = config === null || config === void 0 ? void 0 : config.authentication) === null || _a === void 0 ? void 0 : _a.clientId)) {
|
|
43
|
-
missingConfigurations.push("clientId");
|
|
44
|
-
}
|
|
45
|
-
if (!((_b = config === null || config === void 0 ? void 0 : config.authentication) === null || _b === void 0 ? void 0 : _b.authorityHost)) {
|
|
46
|
-
missingConfigurations.push("authorityHost");
|
|
47
|
-
}
|
|
48
|
-
if (!((_c = config === null || config === void 0 ? void 0 : config.authentication) === null || _c === void 0 ? void 0 : _c.clientSecret) && !((_d = config === null || config === void 0 ? void 0 : config.authentication) === null || _d === void 0 ? void 0 : _d.certificateContent)) {
|
|
49
|
-
missingConfigurations.push("clientSecret or certificateContent");
|
|
50
|
-
}
|
|
51
|
-
if (!((_e = config === null || config === void 0 ? void 0 : config.authentication) === null || _e === void 0 ? void 0 : _e.tenantId)) {
|
|
52
|
-
missingConfigurations.push("tenantId");
|
|
53
|
-
}
|
|
54
|
-
if (missingConfigurations.length != 0) {
|
|
55
|
-
const errorMsg = formatString(ErrorMessage.InvalidConfiguration, missingConfigurations.join(", "), "undefined");
|
|
56
|
-
internalLogger.error(errorMsg);
|
|
57
|
-
throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);
|
|
58
|
-
}
|
|
59
|
-
this.msalClient = createConfidentialClientApplication(config.authentication);
|
|
60
|
-
const decodedSsoToken = parseJwt(ssoToken);
|
|
61
|
-
this.ssoToken = {
|
|
62
|
-
token: ssoToken,
|
|
63
|
-
expiresOnTimestamp: decodedSsoToken.exp,
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
/**
|
|
67
|
-
* Get access token from credential.
|
|
68
|
-
*
|
|
69
|
-
* @example
|
|
70
|
-
* ```typescript
|
|
71
|
-
* await credential.getToken([]) // Get SSO token using empty string array
|
|
72
|
-
* await credential.getToken("") // Get SSO token using empty string
|
|
73
|
-
* await credential.getToken([".default"]) // Get Graph access token with default scope using string array
|
|
74
|
-
* await credential.getToken(".default") // Get Graph access token with default scope using string
|
|
75
|
-
* await credential.getToken(["User.Read"]) // Get Graph access token for single scope using string array
|
|
76
|
-
* await credential.getToken("User.Read") // Get Graph access token for single scope using string
|
|
77
|
-
* await credential.getToken(["User.Read", "Application.Read.All"]) // Get Graph access token for multiple scopes using string array
|
|
78
|
-
* await credential.getToken("User.Read Application.Read.All") // Get Graph access token for multiple scopes using space-separated string
|
|
79
|
-
* await credential.getToken("https://graph.microsoft.com/User.Read") // Get Graph access token with full resource URI
|
|
80
|
-
* await credential.getToken(["https://outlook.office.com/Mail.Read"]) // Get Outlook access token
|
|
81
|
-
* ```
|
|
82
|
-
*
|
|
83
|
-
* @param {string | string[]} scopes - The list of scopes for which the token will have access.
|
|
84
|
-
* @param {GetTokenOptions} options - The options used to configure any requests this TokenCredential implementation might make.
|
|
85
|
-
*
|
|
86
|
-
* @throws {@link ErrorCode|InternalError} when failed to acquire access token on behalf of user with unknown error.
|
|
87
|
-
* @throws {@link ErrorCode|TokenExpiredError} when SSO token has already expired.
|
|
88
|
-
* @throws {@link ErrorCode|UiRequiredError} when need user consent to get access token.
|
|
89
|
-
* @throws {@link ErrorCode|ServiceError} when failed to get access token from simple auth server.
|
|
90
|
-
* @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.
|
|
91
|
-
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.
|
|
92
|
-
*
|
|
93
|
-
* @returns Access token with expected scopes.
|
|
94
|
-
*
|
|
95
|
-
* @remarks
|
|
96
|
-
* If scopes is empty string or array, it returns SSO token.
|
|
97
|
-
* If scopes is non-empty, it returns access token for target scope.
|
|
98
|
-
*
|
|
99
|
-
* @beta
|
|
100
|
-
*/
|
|
101
|
-
getToken(scopes, options) {
|
|
102
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
-
validateScopesType(scopes);
|
|
104
|
-
const scopesArray = getScopesArray(scopes);
|
|
105
|
-
let result;
|
|
106
|
-
if (!scopesArray.length) {
|
|
107
|
-
internalLogger.info("Get SSO token.");
|
|
108
|
-
if (Math.floor(Date.now() / 1000) > this.ssoToken.expiresOnTimestamp) {
|
|
109
|
-
const errorMsg = "Sso token has already expired.";
|
|
110
|
-
internalLogger.error(errorMsg);
|
|
111
|
-
throw new ErrorWithCode(errorMsg, ErrorCode.TokenExpiredError);
|
|
112
|
-
}
|
|
113
|
-
result = this.ssoToken;
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
internalLogger.info("Get access token with scopes: " + scopesArray.join(" "));
|
|
117
|
-
let authenticationResult;
|
|
118
|
-
try {
|
|
119
|
-
authenticationResult = yield this.msalClient.acquireTokenOnBehalfOf({
|
|
120
|
-
oboAssertion: this.ssoToken.token,
|
|
121
|
-
scopes: scopesArray,
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
catch (error) {
|
|
125
|
-
throw this.generateAuthServerError(error);
|
|
126
|
-
}
|
|
127
|
-
if (!authenticationResult) {
|
|
128
|
-
const errorMsg = "Access token is null";
|
|
129
|
-
internalLogger.error(errorMsg);
|
|
130
|
-
throw new ErrorWithCode(formatString(ErrorMessage.FailToAcquireTokenOnBehalfOfUser, errorMsg), ErrorCode.InternalError);
|
|
131
|
-
}
|
|
132
|
-
result = {
|
|
133
|
-
token: authenticationResult.accessToken,
|
|
134
|
-
expiresOnTimestamp: authenticationResult.expiresOn.getTime(),
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
return result;
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Get basic user info from SSO token.
|
|
142
|
-
*
|
|
143
|
-
* @example
|
|
144
|
-
* ```typescript
|
|
145
|
-
* const currentUser = getUserInfo();
|
|
146
|
-
* ```
|
|
147
|
-
*
|
|
148
|
-
* @throws {@link ErrorCode|InternalError} when SSO token is not valid.
|
|
149
|
-
* @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.
|
|
150
|
-
*
|
|
151
|
-
* @returns Basic user info with user displayName, objectId and preferredUserName.
|
|
152
|
-
*
|
|
153
|
-
* @beta
|
|
154
|
-
*/
|
|
155
|
-
getUserInfo() {
|
|
156
|
-
internalLogger.info("Get basic user info from SSO token");
|
|
157
|
-
return getUserInfoFromSsoToken(this.ssoToken.token);
|
|
158
|
-
}
|
|
159
|
-
generateAuthServerError(err) {
|
|
160
|
-
const errorMessage = err.errorMessage;
|
|
161
|
-
if (err.name === "InteractionRequiredAuthError") {
|
|
162
|
-
const fullErrorMsg = "Failed to get access token from AAD server, interaction required: " + errorMessage;
|
|
163
|
-
internalLogger.warn(fullErrorMsg);
|
|
164
|
-
return new ErrorWithCode(fullErrorMsg, ErrorCode.UiRequiredError);
|
|
165
|
-
}
|
|
166
|
-
else if (errorMessage && errorMessage.indexOf("AADSTS500133") >= 0) {
|
|
167
|
-
const fullErrorMsg = "Failed to get access token from AAD server, sso token expired: " + errorMessage;
|
|
168
|
-
internalLogger.error(fullErrorMsg);
|
|
169
|
-
return new ErrorWithCode(fullErrorMsg, ErrorCode.TokenExpiredError);
|
|
170
|
-
}
|
|
171
|
-
else {
|
|
172
|
-
const fullErrorMsg = formatString(ErrorMessage.FailToAcquireTokenOnBehalfOfUser, errorMessage);
|
|
173
|
-
internalLogger.error(fullErrorMsg);
|
|
174
|
-
return new ErrorWithCode(fullErrorMsg, ErrorCode.ServiceError);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
//# sourceMappingURL=onBehalfOfUserCredential.js.map
|
|
@@ -1 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KAAK,8BAA8B,EAAE;YAC/C,MAAM,YAAY,GAChB,oEAAoE,GAAG,YAAY,CAAC;YACtF,cAAc,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;YAClC,OAAO,IAAI,aAAa,CAAC,YAAY,EAAE,SAAS,CAAC,eAAe,CAAC,CAAC;SACnE;aAAM,IAAI,YAAY,IAAI,YAAY,CAAC,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,EAAE;YACpE,MAAM,YAAY,GAChB,iEAAiE,GAAG,YAAY,CAAC;YACnF,cAAc,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACnC,OAAO,IAAI,aAAa,CAAC,YAAY,EAAE,SAAS,CAAC,iBAAiB,CAAC,CAAC;SACrE;aAAM;YACL,MAAM,YAAY,GAAG,YAAY,CAC/B,YAAY,CAAC,gCAAgC,EAC7C,YAAY,CACb,CAAC;YACF,cAAc,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;YACnC,OAAO,IAAI,aAAa,CAAC,YAAY,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;SAChE;IACH,CAAC;CACF","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { AccessToken, GetTokenOptions, TokenCredential } from \"@azure/identity\";\nimport { AuthenticationResult, ConfidentialClientApplication } from \"@azure/msal-node\";\nimport { config } from \"../core/configurationProvider\";\nimport { UserInfo } from \"../models/userinfo\";\nimport { internalLogger } from \"../util/logger\";\nimport {\n formatString,\n getScopesArray,\n getUserInfoFromSsoToken,\n parseJwt,\n validateScopesType,\n} from \"../util/utils\";\nimport { ErrorWithCode, ErrorCode, ErrorMessage } from \"../core/errors\";\nimport { createConfidentialClientApplication } from \"../util/utils.node\";\n\n/**\n * Represent on-behalf-of flow to get user identity, and it is designed to be used in server side.\n *\n * @example\n * ```typescript\n * loadConfiguration(); // load configuration from environment variables\n * const credential = new OnBehalfOfUserCredential(ssoToken);\n * ```\n *\n * @remarks\n * Can only be used in server side.\n *\n * @beta\n */\nexport class OnBehalfOfUserCredential implements TokenCredential {\n private msalClient: ConfidentialClientApplication;\n private ssoToken: AccessToken;\n\n /**\n * Constructor of OnBehalfOfUserCredential\n *\n * @remarks\n * Only works in in server side.\n *\n * @param {string} ssoToken - User token provided by Teams SSO feature.\n *\n * @throws {@link ErrorCode|InvalidConfiguration} when client id, client secret, certificate content, authority host or tenant id is not found in config.\n * @throws {@link ErrorCode|InternalError} when SSO token is not valid.\n * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.\n *\n * @beta\n */\n constructor(ssoToken: string) {\n internalLogger.info(\"Get on behalf of user credential\");\n\n const missingConfigurations: string[] = [];\n if (!config?.authentication?.clientId) {\n missingConfigurations.push(\"clientId\");\n }\n\n if (!config?.authentication?.authorityHost) {\n missingConfigurations.push(\"authorityHost\");\n }\n\n if (!config?.authentication?.clientSecret && !config?.authentication?.certificateContent) {\n missingConfigurations.push(\"clientSecret or certificateContent\");\n }\n\n if (!config?.authentication?.tenantId) {\n missingConfigurations.push(\"tenantId\");\n }\n\n if (missingConfigurations.length != 0) {\n const errorMsg = formatString(\n ErrorMessage.InvalidConfiguration,\n missingConfigurations.join(\", \"),\n \"undefined\"\n );\n internalLogger.error(errorMsg);\n throw new ErrorWithCode(errorMsg, ErrorCode.InvalidConfiguration);\n }\n\n this.msalClient = createConfidentialClientApplication(config.authentication!);\n\n const decodedSsoToken = parseJwt(ssoToken);\n this.ssoToken = {\n token: ssoToken,\n expiresOnTimestamp: decodedSsoToken.exp,\n };\n }\n\n /**\n * Get access token from credential.\n *\n * @example\n * ```typescript\n * await credential.getToken([]) // Get SSO token using empty string array\n * await credential.getToken(\"\") // Get SSO token using empty string\n * await credential.getToken([\".default\"]) // Get Graph access token with default scope using string array\n * await credential.getToken(\".default\") // Get Graph access token with default scope using string\n * await credential.getToken([\"User.Read\"]) // Get Graph access token for single scope using string array\n * await credential.getToken(\"User.Read\") // Get Graph access token for single scope using string\n * await credential.getToken([\"User.Read\", \"Application.Read.All\"]) // Get Graph access token for multiple scopes using string array\n * await credential.getToken(\"User.Read Application.Read.All\") // Get Graph access token for multiple scopes using space-separated string\n * await credential.getToken(\"https://graph.microsoft.com/User.Read\") // Get Graph access token with full resource URI\n * await credential.getToken([\"https://outlook.office.com/Mail.Read\"]) // Get Outlook access token\n * ```\n *\n * @param {string | string[]} scopes - The list of scopes for which the token will have access.\n * @param {GetTokenOptions} options - The options used to configure any requests this TokenCredential implementation might make.\n *\n * @throws {@link ErrorCode|InternalError} when failed to acquire access token on behalf of user with unknown error.\n * @throws {@link ErrorCode|TokenExpiredError} when SSO token has already expired.\n * @throws {@link ErrorCode|UiRequiredError} when need user consent to get access token.\n * @throws {@link ErrorCode|ServiceError} when failed to get access token from simple auth server.\n * @throws {@link ErrorCode|InvalidParameter} when scopes is not a valid string or string array.\n * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.\n *\n * @returns Access token with expected scopes.\n *\n * @remarks\n * If scopes is empty string or array, it returns SSO token.\n * If scopes is non-empty, it returns access token for target scope.\n *\n * @beta\n */\n async getToken(\n scopes: string | string[],\n options?: GetTokenOptions\n ): Promise<AccessToken | null> {\n validateScopesType(scopes);\n\n const scopesArray = getScopesArray(scopes);\n\n let result: AccessToken | null;\n if (!scopesArray.length) {\n internalLogger.info(\"Get SSO token.\");\n if (Math.floor(Date.now() / 1000) > this.ssoToken.expiresOnTimestamp) {\n const errorMsg = \"Sso token has already expired.\";\n internalLogger.error(errorMsg);\n throw new ErrorWithCode(errorMsg, ErrorCode.TokenExpiredError);\n }\n result = this.ssoToken;\n } else {\n internalLogger.info(\"Get access token with scopes: \" + scopesArray.join(\" \"));\n\n let authenticationResult: AuthenticationResult | null;\n try {\n authenticationResult = await this.msalClient.acquireTokenOnBehalfOf({\n oboAssertion: this.ssoToken.token,\n scopes: scopesArray,\n });\n } catch (error) {\n throw this.generateAuthServerError(error);\n }\n\n if (!authenticationResult) {\n const errorMsg = \"Access token is null\";\n internalLogger.error(errorMsg);\n throw new ErrorWithCode(\n formatString(ErrorMessage.FailToAcquireTokenOnBehalfOfUser, errorMsg),\n ErrorCode.InternalError\n );\n }\n\n result = {\n token: authenticationResult.accessToken,\n expiresOnTimestamp: authenticationResult.expiresOn!.getTime(),\n };\n }\n\n return result;\n }\n\n /**\n * Get basic user info from SSO token.\n *\n * @example\n * ```typescript\n * const currentUser = getUserInfo();\n * ```\n *\n * @throws {@link ErrorCode|InternalError} when SSO token is not valid.\n * @throws {@link ErrorCode|RuntimeNotSupported} when runtime is browser.\n *\n * @returns Basic user info with user displayName, objectId and preferredUserName.\n *\n * @beta\n */\n public getUserInfo(): UserInfo {\n internalLogger.info(\"Get basic user info from SSO token\");\n return getUserInfoFromSsoToken(this.ssoToken.token);\n }\n\n private generateAuthServerError(err: any): Error {\n const errorMessage = err.errorMessage;\n if (err.name === \"InteractionRequiredAuthError\") {\n const fullErrorMsg =\n \"Failed to get access token from AAD server, interaction required: \" + errorMessage;\n internalLogger.warn(fullErrorMsg);\n return new ErrorWithCode(fullErrorMsg, ErrorCode.UiRequiredError);\n } else if (errorMessage && errorMessage.indexOf(\"AADSTS500133\") >= 0) {\n const fullErrorMsg =\n \"Failed to get access token from AAD server, sso token expired: \" + errorMessage;\n internalLogger.error(fullErrorMsg);\n return new ErrorWithCode(fullErrorMsg, ErrorCode.TokenExpiredError);\n } else {\n const fullErrorMsg = formatString(\n ErrorMessage.FailToAcquireTokenOnBehalfOfUser,\n errorMessage\n );\n internalLogger.error(fullErrorMsg);\n return new ErrorWithCode(fullErrorMsg, ErrorCode.ServiceError);\n }\n }\n}\n"]}
|