@kwiz/common 1.0.78 → 1.0.80

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.
Files changed (118) hide show
  1. package/.github/workflows/npm-publish.yml +24 -0
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/sharepoint.js +5 -1
  6. package/lib/cjs/helpers/sharepoint.js.map +1 -1
  7. package/lib/cjs/helpers/typecheckers.js +5 -1
  8. package/lib/cjs/helpers/typecheckers.js.map +1 -1
  9. package/lib/cjs/types/libs/msal.types.js +26 -26
  10. package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
  11. package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
  12. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  13. package/lib/esm/helpers/sharepoint.js +3 -0
  14. package/lib/esm/helpers/sharepoint.js.map +1 -1
  15. package/lib/esm/helpers/typecheckers.js +3 -0
  16. package/lib/esm/helpers/typecheckers.js.map +1 -1
  17. package/lib/esm/types/libs/msal.types.js +26 -26
  18. package/lib/esm/utils/sharepoint.rest/list.js +2 -2
  19. package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
  20. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  21. package/lib/types/helpers/sharepoint.d.ts +1 -0
  22. package/lib/types/helpers/typecheckers.d.ts +1 -0
  23. package/package.json +77 -77
  24. package/readme.md +17 -17
  25. package/src/_dependencies.ts +12 -12
  26. package/src/config.ts +17 -17
  27. package/src/helpers/Guid.ts +181 -181
  28. package/src/helpers/base64.ts +173 -173
  29. package/src/helpers/browser.test.js +13 -13
  30. package/src/helpers/browser.ts +1348 -1348
  31. package/src/helpers/browserinfo.ts +292 -292
  32. package/src/helpers/collections.base.test.js +25 -25
  33. package/src/helpers/collections.base.ts +437 -437
  34. package/src/helpers/collections.ts +107 -107
  35. package/src/helpers/color.ts +54 -54
  36. package/src/helpers/cookies.ts +59 -59
  37. package/src/helpers/date.test.js +119 -119
  38. package/src/helpers/date.ts +188 -188
  39. package/src/helpers/debug.ts +186 -186
  40. package/src/helpers/emails.ts +6 -6
  41. package/src/helpers/eval.ts +5 -5
  42. package/src/helpers/file.test.js +50 -50
  43. package/src/helpers/file.ts +58 -58
  44. package/src/helpers/flatted.ts +149 -149
  45. package/src/helpers/functions.ts +16 -16
  46. package/src/helpers/graph/calendar.types.ts +10 -10
  47. package/src/helpers/http.ts +69 -69
  48. package/src/helpers/images.ts +22 -22
  49. package/src/helpers/json.ts +38 -38
  50. package/src/helpers/md5.ts +189 -189
  51. package/src/helpers/objects.test.js +33 -33
  52. package/src/helpers/objects.ts +270 -270
  53. package/src/helpers/promises.test.js +37 -37
  54. package/src/helpers/promises.ts +165 -165
  55. package/src/helpers/random.ts +27 -27
  56. package/src/helpers/scheduler/scheduler.test.js +103 -103
  57. package/src/helpers/scheduler/scheduler.ts +131 -131
  58. package/src/helpers/sharepoint.ts +776 -772
  59. package/src/helpers/strings.test.js +101 -101
  60. package/src/helpers/strings.ts +317 -317
  61. package/src/helpers/typecheckers.test.js +34 -34
  62. package/src/helpers/typecheckers.ts +266 -262
  63. package/src/helpers/url.test.js +43 -43
  64. package/src/helpers/url.ts +207 -207
  65. package/src/helpers/urlhelper.ts +111 -111
  66. package/src/index.ts +6 -6
  67. package/src/types/auth.ts +54 -54
  68. package/src/types/common.types.ts +15 -15
  69. package/src/types/flatted.types.ts +59 -59
  70. package/src/types/globals.types.ts +6 -6
  71. package/src/types/graph/calendar.types.ts +80 -80
  72. package/src/types/knownscript.types.ts +18 -18
  73. package/src/types/libs/datajs.types.ts +28 -28
  74. package/src/types/libs/ics.types.ts +30 -30
  75. package/src/types/libs/msal.types.ts +49 -49
  76. package/src/types/locales.ts +124 -124
  77. package/src/types/localstoragecache.types.ts +8 -8
  78. package/src/types/location.types.ts +27 -27
  79. package/src/types/moment.ts +11 -11
  80. package/src/types/regex.types.ts +16 -16
  81. package/src/types/rest.types.ts +95 -95
  82. package/src/types/sharepoint.types.ts +1465 -1465
  83. package/src/types/sharepoint.utils.types.ts +287 -287
  84. package/src/utils/auth/common.ts +74 -74
  85. package/src/utils/auth/discovery.test.js +12 -12
  86. package/src/utils/auth/discovery.ts +132 -132
  87. package/src/utils/base64.ts +27 -27
  88. package/src/utils/consolelogger.ts +320 -320
  89. package/src/utils/date.ts +35 -35
  90. package/src/utils/emails.ts +24 -24
  91. package/src/utils/knownscript.ts +286 -286
  92. package/src/utils/localstoragecache.ts +441 -441
  93. package/src/utils/rest.ts +501 -501
  94. package/src/utils/script.ts +170 -170
  95. package/src/utils/sharepoint.rest/common.ts +154 -154
  96. package/src/utils/sharepoint.rest/date.ts +62 -62
  97. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  98. package/src/utils/sharepoint.rest/item.ts +547 -547
  99. package/src/utils/sharepoint.rest/list.ts +1388 -1388
  100. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  101. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  102. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  103. package/src/utils/sharepoint.rest/location.ts +141 -141
  104. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  105. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  106. package/src/utils/sharepoint.rest/user.ts +491 -491
  107. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  108. package/src/utils/sod.ts +194 -194
  109. package/lib/cjs/helpers/_dependencies.js +0 -21
  110. package/lib/cjs/helpers/_dependencies.js.map +0 -1
  111. package/lib/cjs/utils/_dependencies.js +0 -24
  112. package/lib/cjs/utils/_dependencies.js.map +0 -1
  113. package/lib/esm/helpers/_dependencies.js +0 -3
  114. package/lib/esm/helpers/_dependencies.js.map +0 -1
  115. package/lib/esm/utils/_dependencies.js +0 -4
  116. package/lib/esm/utils/_dependencies.js.map +0 -1
  117. package/lib/types/helpers/_dependencies.d.ts +0 -2
  118. package/lib/types/utils/_dependencies.d.ts +0 -3
@@ -1,74 +1,74 @@
1
- import { isNullOrEmptyString, isNullOrUndefined } from "../../helpers/typecheckers";
2
- import { SPFxAuthTokenType } from "../../types/auth";
3
- import { GetJson, shortLocalCache } from "../rest";
4
- import { GetRestBaseUrl } from "../sharepoint.rest/common";
5
-
6
- export function GetTokenAudiencePrefix(appId: string) {
7
- return `api://${appId}`;
8
- }
9
- export function GetDefaultScope(appId: string) {
10
- return `${GetTokenAudiencePrefix(appId)}/access_as_user`;
11
- }
12
- export function GetMSALSiteScope(hostName: string) {
13
- return `https://${hostName}`;
14
- }
15
-
16
- /** Acquire an authorization token for a Outlook, Graph, or SharePoint the same way SPFx clients do */
17
- export async function GetSPFxClientAuthToken(siteUrl: string, spfxTokenType: SPFxAuthTokenType = SPFxAuthTokenType.Graph) {
18
- try {
19
- let acquireURL = `${GetRestBaseUrl(siteUrl)}/SP.OAuth.Token/Acquire`;
20
-
21
- //todo: add all the resource end points (ie. OneNote, Yammer, Stream)
22
- let resource = "";
23
- let isSPOToken = false;
24
- switch (spfxTokenType) {
25
- case SPFxAuthTokenType.Outlook:
26
- resource = "https://outlook.office365.com";
27
- break;
28
- case SPFxAuthTokenType.SharePoint:
29
- case SPFxAuthTokenType.MySite:
30
- isSPOToken = true;
31
- resource = new URL(acquireURL).origin;
32
- if (spfxTokenType === SPFxAuthTokenType.MySite) {
33
- let split = resource.split(".");
34
- split[0] += "-my";
35
- resource = split.join(".");
36
- }
37
- break;
38
- case SPFxAuthTokenType.MySite:
39
- resource = new URL(acquireURL).origin
40
- break;
41
- default:
42
- resource = "https://graph.microsoft.com";
43
- }
44
-
45
- let data = {
46
- resource: resource,
47
- tokenType: isSPOToken ? "SPO" : undefined
48
- };
49
-
50
- let result = await GetJson<{
51
- access_token: string;
52
- expires_on: string;
53
- resource: string;
54
- scope: string;
55
- token_type: string;
56
- }>(
57
- acquireURL,
58
- JSON.stringify(data),
59
- {
60
- ...shortLocalCache,
61
- postCacheKey: `${spfxTokenType}_${_spPageContextInfo.webId}`,
62
- includeDigestInPost: true,
63
- headers: {
64
- "Accept": "application/json;odata.metadata=minimal",
65
- "content-type": "application/json; charset=UTF-8",
66
- "odata-version": "4.0",
67
- }
68
- });
69
-
70
- return !isNullOrUndefined(result) && !isNullOrEmptyString(result.access_token) ? result.access_token : null;
71
- } catch {
72
- }
73
- return null;
74
- }
1
+ import { isNullOrEmptyString, isNullOrUndefined } from "../../helpers/typecheckers";
2
+ import { SPFxAuthTokenType } from "../../types/auth";
3
+ import { GetJson, shortLocalCache } from "../rest";
4
+ import { GetRestBaseUrl } from "../sharepoint.rest/common";
5
+
6
+ export function GetTokenAudiencePrefix(appId: string) {
7
+ return `api://${appId}`;
8
+ }
9
+ export function GetDefaultScope(appId: string) {
10
+ return `${GetTokenAudiencePrefix(appId)}/access_as_user`;
11
+ }
12
+ export function GetMSALSiteScope(hostName: string) {
13
+ return `https://${hostName}`;
14
+ }
15
+
16
+ /** Acquire an authorization token for a Outlook, Graph, or SharePoint the same way SPFx clients do */
17
+ export async function GetSPFxClientAuthToken(siteUrl: string, spfxTokenType: SPFxAuthTokenType = SPFxAuthTokenType.Graph) {
18
+ try {
19
+ let acquireURL = `${GetRestBaseUrl(siteUrl)}/SP.OAuth.Token/Acquire`;
20
+
21
+ //todo: add all the resource end points (ie. OneNote, Yammer, Stream)
22
+ let resource = "";
23
+ let isSPOToken = false;
24
+ switch (spfxTokenType) {
25
+ case SPFxAuthTokenType.Outlook:
26
+ resource = "https://outlook.office365.com";
27
+ break;
28
+ case SPFxAuthTokenType.SharePoint:
29
+ case SPFxAuthTokenType.MySite:
30
+ isSPOToken = true;
31
+ resource = new URL(acquireURL).origin;
32
+ if (spfxTokenType === SPFxAuthTokenType.MySite) {
33
+ let split = resource.split(".");
34
+ split[0] += "-my";
35
+ resource = split.join(".");
36
+ }
37
+ break;
38
+ case SPFxAuthTokenType.MySite:
39
+ resource = new URL(acquireURL).origin
40
+ break;
41
+ default:
42
+ resource = "https://graph.microsoft.com";
43
+ }
44
+
45
+ let data = {
46
+ resource: resource,
47
+ tokenType: isSPOToken ? "SPO" : undefined
48
+ };
49
+
50
+ let result = await GetJson<{
51
+ access_token: string;
52
+ expires_on: string;
53
+ resource: string;
54
+ scope: string;
55
+ token_type: string;
56
+ }>(
57
+ acquireURL,
58
+ JSON.stringify(data),
59
+ {
60
+ ...shortLocalCache,
61
+ postCacheKey: `${spfxTokenType}_${_spPageContextInfo.webId}`,
62
+ includeDigestInPost: true,
63
+ headers: {
64
+ "Accept": "application/json;odata.metadata=minimal",
65
+ "content-type": "application/json; charset=UTF-8",
66
+ "odata-version": "4.0",
67
+ }
68
+ });
69
+
70
+ return !isNullOrUndefined(result) && !isNullOrEmptyString(result.access_token) ? result.access_token : null;
71
+ } catch {
72
+ }
73
+ return null;
74
+ }
@@ -1,13 +1,13 @@
1
- import assert from 'assert/strict';
2
- import test from 'node:test';
3
- import { normalizeGuid } from '../../helpers/strings';
4
- import { isValidGuid } from '../../helpers/typecheckers';
5
- import { DiscoverTenantInfo } from './discovery';
6
-
7
- test('DiscoverTenantInfo', async t => {
8
- global.XMLHttpRequest = require('xhr2');
9
- let info = await DiscoverTenantInfo("kwizcomdev.sharepoint.com");
10
- await t.test("response not null/undefined", t => assert.notDeepEqual(info, null) && assert.notDeepEqual(info, undefined));
11
- await t.test("has valid guid", t => assert.deepEqual(isValidGuid(info && info.idOrName), true));
12
- await t.test("has correct guid", t => assert.deepEqual(normalizeGuid(info && info.idOrName), normalizeGuid("3bf37eb8-6c20-45a9-aff6-ac72d276f375")));
1
+ import assert from 'assert/strict';
2
+ import test from 'node:test';
3
+ import { normalizeGuid } from '../../helpers/strings';
4
+ import { isValidGuid } from '../../helpers/typecheckers';
5
+ import { DiscoverTenantInfo } from './discovery';
6
+
7
+ test('DiscoverTenantInfo', async t => {
8
+ global.XMLHttpRequest = require('xhr2');
9
+ let info = await DiscoverTenantInfo("kwizcomdev.sharepoint.com");
10
+ await t.test("response not null/undefined", t => assert.notDeepEqual(info, null) && assert.notDeepEqual(info, undefined));
11
+ await t.test("has valid guid", t => assert.deepEqual(isValidGuid(info && info.idOrName), true));
12
+ await t.test("has correct guid", t => assert.deepEqual(normalizeGuid(info && info.idOrName), normalizeGuid("3bf37eb8-6c20-45a9-aff6-ac72d276f375")));
13
13
  });
@@ -1,132 +1,132 @@
1
- import { promiseOnce } from "../../helpers/promises";
2
- import { isNullOrEmptyString, isValidGuid } from "../../helpers/typecheckers";
3
- import { AzureEnvironment, ITenantInfo } from "../../types/auth";
4
- import { GetJson, GetJsonSync } from "../rest";
5
-
6
- interface IOpenidConfiguration {
7
- token_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/token
8
- cloud_instance_name: string;//microsoftonline.com
9
- token_endpoint_auth_methods_supported: string[];// ["client_secret_post", "private_key_jwt", "client_secret_basic"]
10
- response_modes_supported: string[];// ["query", "fragment", "form_post"]
11
- response_types_supported: string[];// ["code", "id_token", "code id_token", "token id_token", "token"]
12
- scopes_supported: string[];// ["openid"]
13
- issuer: string;//https://sts.windows.net/7d034656-be03-457d-8d82-60e90cf5f400/
14
- authorization_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/authorize
15
- device_authorization_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/devicecode
16
- end_session_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/logout
17
- userinfo_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/openid/userinfo
18
- tenant_region_scope: string;//NA
19
- cloud_graph_host_name: string;//graph.windows.net
20
- msgraph_host: string;//graph.microsoft.com
21
- }
22
-
23
- function _getFriendlyName(hostName: string) {
24
- if (hostName.indexOf(".sharepoint.") !== -1) {
25
- let hostParts = hostName.split('.');//should be xxx.sharepoint.com or xxx.sharepoint.us
26
- let firstHostPart = hostParts[0];
27
- let lastHostPart = hostParts[hostParts.length - 1] === "us" || hostParts[hostParts.length - 1] === "de" ? hostParts[hostParts.length - 1] : "com";
28
- if (firstHostPart.endsWith("-admin")) firstHostPart = firstHostPart.substring(0, firstHostPart.length - 6);
29
- return `${firstHostPart}.onmicrosoft.${lastHostPart}`;
30
- }
31
- else {
32
- return hostName;//could be an exchange email domain, or bpos customer
33
- }
34
- }
35
-
36
- function _getOpenIdConfigurationUrl(friendlyName: string) {
37
- return `https://login.microsoftonline.com/${friendlyName}/v2.0/.well-known/openid-configuration`;
38
- }
39
-
40
- function _processOpenidConfiguration(config: IOpenidConfiguration, friendlyName: string) {
41
- let data: ITenantInfo = {
42
- environment: AzureEnvironment.Production,
43
- idOrName: null,
44
- authorityUrl: null,
45
- valid: false
46
- };
47
-
48
- let endpoint = config.token_endpoint;//https://xxxx/{tenant}/....
49
- let tenantId = endpoint.replace("//", "/").split('/')[2];//replace :// with :/ split by / and take the second part.
50
- let instance = config.cloud_instance_name;//microsoftonline.us
51
-
52
- data.environment = GetEnvironmentFromACSEndPoint(instance);
53
- if (!isNullOrEmptyString(tenantId) || isValidGuid(tenantId)) {
54
- data.idOrName = tenantId;
55
- } else {
56
- data.idOrName = friendlyName;
57
- }
58
-
59
- data.authorityUrl = `${GetAzureADLoginEndPoint(data.environment)}/${data.idOrName}`;
60
- data.valid = true;
61
-
62
- return data;
63
- }
64
-
65
- export function DiscoverTenantInfo(hostName: string, sync?: false): Promise<ITenantInfo>
66
- export function DiscoverTenantInfo(hostName: string, sync: true): ITenantInfo
67
- export function DiscoverTenantInfo(hostName: string, sync?: boolean): ITenantInfo | Promise<ITenantInfo> {
68
- hostName = hostName.toLowerCase();
69
-
70
- let friendlyName = _getFriendlyName(hostName);
71
- let url = _getOpenIdConfigurationUrl(friendlyName);
72
-
73
- if (sync === true) {
74
- try {
75
- let response = GetJsonSync<IOpenidConfiguration>(url);
76
- let config = response.result;
77
- let data = _processOpenidConfiguration(config, friendlyName);
78
- return data;
79
- } catch (ex) {
80
- console.log(ex);
81
- }
82
- return null;
83
- } else {
84
- return promiseOnce(`DiscoverTenantInfo|${hostName}`, async () => {
85
- try {
86
- let config = await GetJson<IOpenidConfiguration>(url);
87
- let data = _processOpenidConfiguration(config, friendlyName);
88
- return data;
89
- }
90
- catch (ex) {
91
- console.log(ex);
92
- }
93
- return null;
94
- });
95
- }
96
- }
97
-
98
- export function AutoDiscoverTenantInfo(sync?: false): Promise<ITenantInfo>
99
- export function AutoDiscoverTenantInfo(sync: true): ITenantInfo
100
- export function AutoDiscoverTenantInfo(sync?: boolean): ITenantInfo | Promise<ITenantInfo> {
101
- if (sync === true) {
102
- return DiscoverTenantInfo(window.location.hostname.toLowerCase(), true);
103
- }
104
- return DiscoverTenantInfo(window.location.hostname.toLowerCase(), false);
105
- }
106
-
107
- export function GetEnvironmentFromACSEndPoint(ACSEndPoint: string): AzureEnvironment {
108
- switch (ACSEndPoint) {
109
- case "microsoftonline.us":
110
- return AzureEnvironment.USGovernment;
111
- case "microsoftonline.de":
112
- return AzureEnvironment.Germany;
113
- case "accesscontrol.chinacloudapi.cn":
114
- return AzureEnvironment.China;
115
- case "windows-ppe.net":
116
- return AzureEnvironment.PPE;
117
- case "accesscontrol.windows.net":
118
- default:
119
- return AzureEnvironment.Production;
120
- }
121
- }
122
- export function GetAzureADLoginEndPoint(environment: AzureEnvironment): string {
123
- switch (environment) {
124
- case AzureEnvironment.Germany: return "https://login.microsoftonline.de";
125
- case AzureEnvironment.China: return "https://login.chinacloudapi.cn";
126
- case AzureEnvironment.USGovernment: return "https://login.microsoftonline.us";
127
- case AzureEnvironment.PPE: return "https://login.windows-ppe.net";
128
- case AzureEnvironment.Production:
129
- default:
130
- return "https://login.microsoftonline.com";
131
- }
132
- }
1
+ import { promiseOnce } from "../../helpers/promises";
2
+ import { isNullOrEmptyString, isValidGuid } from "../../helpers/typecheckers";
3
+ import { AzureEnvironment, ITenantInfo } from "../../types/auth";
4
+ import { GetJson, GetJsonSync } from "../rest";
5
+
6
+ interface IOpenidConfiguration {
7
+ token_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/token
8
+ cloud_instance_name: string;//microsoftonline.com
9
+ token_endpoint_auth_methods_supported: string[];// ["client_secret_post", "private_key_jwt", "client_secret_basic"]
10
+ response_modes_supported: string[];// ["query", "fragment", "form_post"]
11
+ response_types_supported: string[];// ["code", "id_token", "code id_token", "token id_token", "token"]
12
+ scopes_supported: string[];// ["openid"]
13
+ issuer: string;//https://sts.windows.net/7d034656-be03-457d-8d82-60e90cf5f400/
14
+ authorization_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/authorize
15
+ device_authorization_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/devicecode
16
+ end_session_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/logout
17
+ userinfo_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/openid/userinfo
18
+ tenant_region_scope: string;//NA
19
+ cloud_graph_host_name: string;//graph.windows.net
20
+ msgraph_host: string;//graph.microsoft.com
21
+ }
22
+
23
+ function _getFriendlyName(hostName: string) {
24
+ if (hostName.indexOf(".sharepoint.") !== -1) {
25
+ let hostParts = hostName.split('.');//should be xxx.sharepoint.com or xxx.sharepoint.us
26
+ let firstHostPart = hostParts[0];
27
+ let lastHostPart = hostParts[hostParts.length - 1] === "us" || hostParts[hostParts.length - 1] === "de" ? hostParts[hostParts.length - 1] : "com";
28
+ if (firstHostPart.endsWith("-admin")) firstHostPart = firstHostPart.substring(0, firstHostPart.length - 6);
29
+ return `${firstHostPart}.onmicrosoft.${lastHostPart}`;
30
+ }
31
+ else {
32
+ return hostName;//could be an exchange email domain, or bpos customer
33
+ }
34
+ }
35
+
36
+ function _getOpenIdConfigurationUrl(friendlyName: string) {
37
+ return `https://login.microsoftonline.com/${friendlyName}/v2.0/.well-known/openid-configuration`;
38
+ }
39
+
40
+ function _processOpenidConfiguration(config: IOpenidConfiguration, friendlyName: string) {
41
+ let data: ITenantInfo = {
42
+ environment: AzureEnvironment.Production,
43
+ idOrName: null,
44
+ authorityUrl: null,
45
+ valid: false
46
+ };
47
+
48
+ let endpoint = config.token_endpoint;//https://xxxx/{tenant}/....
49
+ let tenantId = endpoint.replace("//", "/").split('/')[2];//replace :// with :/ split by / and take the second part.
50
+ let instance = config.cloud_instance_name;//microsoftonline.us
51
+
52
+ data.environment = GetEnvironmentFromACSEndPoint(instance);
53
+ if (!isNullOrEmptyString(tenantId) || isValidGuid(tenantId)) {
54
+ data.idOrName = tenantId;
55
+ } else {
56
+ data.idOrName = friendlyName;
57
+ }
58
+
59
+ data.authorityUrl = `${GetAzureADLoginEndPoint(data.environment)}/${data.idOrName}`;
60
+ data.valid = true;
61
+
62
+ return data;
63
+ }
64
+
65
+ export function DiscoverTenantInfo(hostName: string, sync?: false): Promise<ITenantInfo>
66
+ export function DiscoverTenantInfo(hostName: string, sync: true): ITenantInfo
67
+ export function DiscoverTenantInfo(hostName: string, sync?: boolean): ITenantInfo | Promise<ITenantInfo> {
68
+ hostName = hostName.toLowerCase();
69
+
70
+ let friendlyName = _getFriendlyName(hostName);
71
+ let url = _getOpenIdConfigurationUrl(friendlyName);
72
+
73
+ if (sync === true) {
74
+ try {
75
+ let response = GetJsonSync<IOpenidConfiguration>(url);
76
+ let config = response.result;
77
+ let data = _processOpenidConfiguration(config, friendlyName);
78
+ return data;
79
+ } catch (ex) {
80
+ console.log(ex);
81
+ }
82
+ return null;
83
+ } else {
84
+ return promiseOnce(`DiscoverTenantInfo|${hostName}`, async () => {
85
+ try {
86
+ let config = await GetJson<IOpenidConfiguration>(url);
87
+ let data = _processOpenidConfiguration(config, friendlyName);
88
+ return data;
89
+ }
90
+ catch (ex) {
91
+ console.log(ex);
92
+ }
93
+ return null;
94
+ });
95
+ }
96
+ }
97
+
98
+ export function AutoDiscoverTenantInfo(sync?: false): Promise<ITenantInfo>
99
+ export function AutoDiscoverTenantInfo(sync: true): ITenantInfo
100
+ export function AutoDiscoverTenantInfo(sync?: boolean): ITenantInfo | Promise<ITenantInfo> {
101
+ if (sync === true) {
102
+ return DiscoverTenantInfo(window.location.hostname.toLowerCase(), true);
103
+ }
104
+ return DiscoverTenantInfo(window.location.hostname.toLowerCase(), false);
105
+ }
106
+
107
+ export function GetEnvironmentFromACSEndPoint(ACSEndPoint: string): AzureEnvironment {
108
+ switch (ACSEndPoint) {
109
+ case "microsoftonline.us":
110
+ return AzureEnvironment.USGovernment;
111
+ case "microsoftonline.de":
112
+ return AzureEnvironment.Germany;
113
+ case "accesscontrol.chinacloudapi.cn":
114
+ return AzureEnvironment.China;
115
+ case "windows-ppe.net":
116
+ return AzureEnvironment.PPE;
117
+ case "accesscontrol.windows.net":
118
+ default:
119
+ return AzureEnvironment.Production;
120
+ }
121
+ }
122
+ export function GetAzureADLoginEndPoint(environment: AzureEnvironment): string {
123
+ switch (environment) {
124
+ case AzureEnvironment.Germany: return "https://login.microsoftonline.de";
125
+ case AzureEnvironment.China: return "https://login.chinacloudapi.cn";
126
+ case AzureEnvironment.USGovernment: return "https://login.microsoftonline.us";
127
+ case AzureEnvironment.PPE: return "https://login.windows-ppe.net";
128
+ case AzureEnvironment.Production:
129
+ default:
130
+ return "https://login.microsoftonline.com";
131
+ }
132
+ }
@@ -1,28 +1,28 @@
1
- import { blobToBase64 } from "../helpers/base64";
2
- import { isNullOrEmptyString } from "../helpers/typecheckers";
3
- import { GetJson } from "./rest";
4
- import { GetFile } from "./sharepoint.rest/file.folder";
5
-
6
- /** if the file is in SharePoint, provide a siteRelativeUrl so that we can get the file value via REST api to avoid CORS error when accessing a file on the site from within the app web */
7
- export async function imageToBase64(imageSrc: string, siteRelativeUrl?: string): Promise<string> {
8
- try {
9
- let fileBlob: Blob = null;
10
- if (!isNullOrEmptyString(siteRelativeUrl) && imageSrc.toLowerCase().indexOf(siteRelativeUrl.toLowerCase()) >= 0) {
11
- let spFile = await GetFile<Blob>(siteRelativeUrl, imageSrc, true, "blob");
12
- if (spFile && spFile.Exists)
13
- fileBlob = spFile.Content;
14
- }
15
-
16
- //try simple rest if the first option failed
17
- if (fileBlob === null) {
18
- fileBlob = await GetJson<Blob>(imageSrc, null, { responseType: "blob", allowCache: true });
19
- }
20
-
21
- if (fileBlob) {
22
- let base64 = await blobToBase64(fileBlob);
23
- return base64;
24
- }
25
- } catch (e) {
26
- }
27
- return null;
1
+ import { blobToBase64 } from "../helpers/base64";
2
+ import { isNullOrEmptyString } from "../helpers/typecheckers";
3
+ import { GetJson } from "./rest";
4
+ import { GetFile } from "./sharepoint.rest/file.folder";
5
+
6
+ /** if the file is in SharePoint, provide a siteRelativeUrl so that we can get the file value via REST api to avoid CORS error when accessing a file on the site from within the app web */
7
+ export async function imageToBase64(imageSrc: string, siteRelativeUrl?: string): Promise<string> {
8
+ try {
9
+ let fileBlob: Blob = null;
10
+ if (!isNullOrEmptyString(siteRelativeUrl) && imageSrc.toLowerCase().indexOf(siteRelativeUrl.toLowerCase()) >= 0) {
11
+ let spFile = await GetFile<Blob>(siteRelativeUrl, imageSrc, true, "blob");
12
+ if (spFile && spFile.Exists)
13
+ fileBlob = spFile.Content;
14
+ }
15
+
16
+ //try simple rest if the first option failed
17
+ if (fileBlob === null) {
18
+ fileBlob = await GetJson<Blob>(imageSrc, null, { responseType: "blob", allowCache: true });
19
+ }
20
+
21
+ if (fileBlob) {
22
+ let base64 = await blobToBase64(fileBlob);
23
+ return base64;
24
+ }
25
+ } catch (e) {
26
+ }
27
+ return null;
28
28
  }