@kwiz/node 1.0.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/.madgerc +3 -0
- package/LICENSE +21 -0
- package/README.md +2 -0
- package/dist/SPO/common.d.ts +3 -0
- package/dist/SPO/common.js +23 -0
- package/dist/SPO/common.js.map +1 -0
- package/dist/auth/discovery.d.ts +2 -0
- package/dist/auth/discovery.js +45 -0
- package/dist/auth/discovery.js.map +1 -0
- package/dist/auth/msal.d.ts +3 -0
- package/dist/auth/msal.js +40 -0
- package/dist/auth/msal.js.map +1 -0
- package/dist/axios.d.ts +7 -0
- package/dist/axios.js +30 -0
- package/dist/axios.js.map +1 -0
- package/dist/index.d.ts +0 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/package.json +72 -0
- package/src/SPO/common.ts +17 -0
- package/src/auth/discovery.test.js +9 -0
- package/src/auth/discovery.ts +61 -0
- package/src/auth/msal.ts +41 -0
- package/src/axios.ts +32 -0
- package/src/index.ts +0 -0
package/.madgerc
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 KWIZ Corp
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { AuthContextType, ITenantInfo } from "@kwiz/common";
|
|
2
|
+
export declare function ConfigureSPOAuth(config?: AuthContextType): void;
|
|
3
|
+
export declare function getAxiosConfigSharePoint(tenantInfo: ITenantInfo, siteUrl: string): Promise<import("axios").AxiosRequestConfig<any>>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAxiosConfigSharePoint = exports.ConfigureSPOAuth = void 0;
|
|
4
|
+
const common_1 = require("@kwiz/common");
|
|
5
|
+
const msal_1 = require("../auth/msal");
|
|
6
|
+
const axios_1 = require("../axios");
|
|
7
|
+
var auth = null;
|
|
8
|
+
function ConfigureSPOAuth(config) {
|
|
9
|
+
auth = config;
|
|
10
|
+
}
|
|
11
|
+
exports.ConfigureSPOAuth = ConfigureSPOAuth;
|
|
12
|
+
async function getAxiosConfigSharePoint(tenantInfo, siteUrl) {
|
|
13
|
+
if ((0, common_1.isNullOrUndefined)(auth))
|
|
14
|
+
throw Error("Call ConfigureSPOAuth first");
|
|
15
|
+
// only certificate supported
|
|
16
|
+
// DisableCustomAppAuthentication property, that disable this kind of auth., however it can be overriden using this command:
|
|
17
|
+
// Set-SPOTenant -DisableCustomAppAuthentication $false
|
|
18
|
+
let token = await (0, msal_1.GetMSALToken)(tenantInfo, (0, common_1.GetMSALSiteScope)(siteUrl), auth);
|
|
19
|
+
let config = (0, axios_1.getAxiosConfigBearer)(token, { contantType: "application/json; odata=nometadata" });
|
|
20
|
+
return config;
|
|
21
|
+
}
|
|
22
|
+
exports.getAxiosConfigSharePoint = getAxiosConfigSharePoint;
|
|
23
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../src/SPO/common.ts"],"names":[],"mappings":";;;AAAA,yCAAiG;AACjG,uCAA4C;AAC5C,oCAAgD;AAEhD,IAAI,IAAI,GAAoB,IAAI,CAAC;AACjC,SAAgB,gBAAgB,CAAC,MAAwB;IACrD,IAAI,GAAG,MAAM,CAAC;AAClB,CAAC;AAFD,4CAEC;AACM,KAAK,UAAU,wBAAwB,CAAC,UAAuB,EAAE,OAAe;IACnF,IAAI,IAAA,0BAAiB,EAAC,IAAI,CAAC;QAAE,MAAM,KAAK,CAAC,6BAA6B,CAAC,CAAC;IACxE,6BAA6B;IAC7B,4HAA4H;IAC5H,uDAAuD;IACvD,IAAI,KAAK,GAAG,MAAM,IAAA,mBAAY,EAAC,UAAU,EAAE,IAAA,yBAAgB,EAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;IAC5E,IAAI,MAAM,GAAG,IAAA,4BAAoB,EAAC,KAAK,EAAE,EAAE,WAAW,EAAE,oCAAoC,EAAE,CAAC,CAAC;IAChG,OAAO,MAAM,CAAC;AAClB,CAAC;AARD,4DAQC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DiscoverTenantInfo = void 0;
|
|
4
|
+
const common_1 = require("@kwiz/common");
|
|
5
|
+
const axios_1 = require("axios");
|
|
6
|
+
function DiscoverTenantInfo(hostName) {
|
|
7
|
+
hostName = hostName.toLowerCase();
|
|
8
|
+
return (0, common_1.promiseOnce)(`DiscoverTenantInfo|${hostName}`, async () => {
|
|
9
|
+
let data = {
|
|
10
|
+
environment: common_1.$AzureEnvironment.Production,
|
|
11
|
+
idOrName: null,
|
|
12
|
+
authorityUrl: null,
|
|
13
|
+
valid: false
|
|
14
|
+
};
|
|
15
|
+
let tenantId = null;
|
|
16
|
+
let friendlyName = null;
|
|
17
|
+
try {
|
|
18
|
+
if (hostName.indexOf(".sharepoint.") !== -1) {
|
|
19
|
+
let hostParts = hostName.split('.'); //should be xxx.sharepoint.com or xxx.sharepoint.us
|
|
20
|
+
let firstHostPart = hostParts[0];
|
|
21
|
+
let lastHostPart = hostParts[hostParts.length - 1] === "us" || hostParts[hostParts.length - 1] === "de" ? hostParts[hostParts.length - 1] : "com";
|
|
22
|
+
if (firstHostPart.endsWith("-admin"))
|
|
23
|
+
firstHostPart = firstHostPart.substring(0, firstHostPart.length - 6);
|
|
24
|
+
friendlyName = `${firstHostPart}.onmicrosoft.${lastHostPart}`;
|
|
25
|
+
}
|
|
26
|
+
else
|
|
27
|
+
friendlyName = hostName; //could be an exchange email domain, or bpos customer
|
|
28
|
+
let config = await axios_1.default.get(`https://login.microsoftonline.com/${friendlyName}/v2.0/.well-known/openid-configuration`);
|
|
29
|
+
let endpoint = config.data.token_endpoint; //https://xxxx/{tenant}/....
|
|
30
|
+
tenantId = endpoint.replace("//", "/").split('/')[2]; //replace :// with :/ split by / and take the second part.
|
|
31
|
+
let instance = config.data.cloud_instance_name; //microsoftonline.us
|
|
32
|
+
data.environment = (0, common_1.GetEnvironmentFromACSEndPoint)(instance);
|
|
33
|
+
if (!(0, common_1.isNullOrEmptyString)(tenantId) || (0, common_1.isValidGuid)(tenantId))
|
|
34
|
+
data.idOrName = tenantId;
|
|
35
|
+
else
|
|
36
|
+
data.idOrName = friendlyName;
|
|
37
|
+
data.authorityUrl = `${(0, common_1.GetAzureADLoginEndPoint)(data.environment)}/${data.idOrName}`;
|
|
38
|
+
data.valid = true;
|
|
39
|
+
}
|
|
40
|
+
catch (e) { }
|
|
41
|
+
return data;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
exports.DiscoverTenantInfo = DiscoverTenantInfo;
|
|
45
|
+
//# sourceMappingURL=discovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"discovery.js","sourceRoot":"","sources":["../../src/auth/discovery.ts"],"names":[],"mappings":";;;AAAA,yCAAqK;AACrK,iCAA0B;AAE1B,SAAgB,kBAAkB,CAAC,QAAgB;IAC/C,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;IAClC,OAAO,IAAA,oBAAW,EAAC,sBAAsB,QAAQ,EAAE,EAAE,KAAK,IAAI,EAAE;QAC5D,IAAI,IAAI,GAAgB;YACpB,WAAW,EAAE,0BAAiB,CAAC,UAAU;YACzC,QAAQ,EAAE,IAAI;YACd,YAAY,EAAE,IAAI;YAClB,KAAK,EAAE,KAAK;SACf,CAAC;QAEF,IAAI,QAAQ,GAAW,IAAI,CAAC;QAC5B,IAAI,YAAY,GAAW,IAAI,CAAC;QAEhC,IAAI,CAAC;YACD,IAAI,QAAQ,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;gBAC1C,IAAI,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAA,mDAAmD;gBACvF,IAAI,aAAa,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;gBACjC,IAAI,YAAY,GAAG,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,IAAI,IAAI,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;gBAClJ,IAAI,aAAa,CAAC,QAAQ,CAAC,QAAQ,CAAC;oBAAE,aAAa,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC,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1 = require("@azure/msal-node");
|
|
5
|
+
//find tenant id? https://login.microsoftonline.com/kwizcom.onmicrosoft.com/.well-known/openid-configuration
|
|
6
|
+
//https://stackoverflow.com/questions/54771270/msal-ad-token-not-valid-with-sharepoint-online-csom
|
|
7
|
+
var apps = {};
|
|
8
|
+
function GetApp(tenantInfo, auth) {
|
|
9
|
+
let key = `${tenantInfo.idOrName}|${auth.authenticationMode}`;
|
|
10
|
+
if (!apps[key]) {
|
|
11
|
+
auth.authenticationMode === "secret" /* AuthenticationModes.clientSecret */
|
|
12
|
+
? apps[key] = new msal_node_1.ConfidentialClientApplication({
|
|
13
|
+
auth: {
|
|
14
|
+
clientId: auth.clientId,
|
|
15
|
+
authority: tenantInfo.authorityUrl,
|
|
16
|
+
clientSecret: auth.clientSecret
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
: apps[key] = new msal_node_1.ConfidentialClientApplication({
|
|
20
|
+
auth: {
|
|
21
|
+
clientId: auth.clientId,
|
|
22
|
+
authority: tenantInfo.authorityUrl,
|
|
23
|
+
clientCertificate: {
|
|
24
|
+
thumbprint: auth.thumbprint,
|
|
25
|
+
privateKey: auth.privateKey
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
return apps[key];
|
|
31
|
+
}
|
|
32
|
+
/** client secret not supported by SharePoint, must use certificate */
|
|
33
|
+
async function GetMSALToken(tenantInfo, scope, auth) {
|
|
34
|
+
let token = await GetApp(tenantInfo, auth).acquireTokenByClientCredential({
|
|
35
|
+
scopes: [`${scope}/.default`]
|
|
36
|
+
});
|
|
37
|
+
return token.accessToken;
|
|
38
|
+
}
|
|
39
|
+
exports.GetMSALToken = GetMSALToken;
|
|
40
|
+
//# sourceMappingURL=msal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"msal.js","sourceRoot":"","sources":["../../src/auth/msal.ts"],"names":[],"mappings":";;;AAAA,gDAAiE;AAEjE,4GAA4G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ios";
|
|
2
|
+
type axiosConfigOptions = {
|
|
3
|
+
contantType?: "application/json" | "application/json; odata=nometadata" | "application/xml";
|
|
4
|
+
};
|
|
5
|
+
export declare function getAxiosConfigBearer(token: string, options?: axiosConfigOptions): AxiosRequestConfig<any>;
|
|
6
|
+
export declare function getAxiosConfig(token?: string, options?: axiosConfigOptions): AxiosRequestConfig<any>;
|
|
7
|
+
export {};
|
package/dist/axios.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAxiosConfig = exports.getAxiosConfigBearer = void 0;
|
|
4
|
+
const common_1 = require("@kwiz/common");
|
|
5
|
+
const https_1 = require("https");
|
|
6
|
+
function getAxiosConfigBearer(token, options) {
|
|
7
|
+
return getAxiosConfig(`Bearer ${token}`, options);
|
|
8
|
+
}
|
|
9
|
+
exports.getAxiosConfigBearer = getAxiosConfigBearer;
|
|
10
|
+
function getAxiosConfig(token, options) {
|
|
11
|
+
//allow self sign ssl certificates
|
|
12
|
+
https_1.globalAgent.options.rejectUnauthorized = false;
|
|
13
|
+
const config = {
|
|
14
|
+
httpAgent: new https_1.Agent({
|
|
15
|
+
rejectUnauthorized: false
|
|
16
|
+
}),
|
|
17
|
+
headers: {}
|
|
18
|
+
};
|
|
19
|
+
if (!(0, common_1.isNullOrEmptyString)(token))
|
|
20
|
+
config.headers.Authorization = token;
|
|
21
|
+
if (options) {
|
|
22
|
+
if (!(0, common_1.isNullOrEmptyString)(options.contantType)) {
|
|
23
|
+
config.headers["Content-Type"] = options.contantType;
|
|
24
|
+
config.headers["Accept"] = options.contantType;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return config;
|
|
28
|
+
}
|
|
29
|
+
exports.getAxiosConfig = getAxiosConfig;
|
|
30
|
+
//# sourceMappingURL=axios.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"axios.js","sourceRoot":"","sources":["../src/axios.ts"],"names":[],"mappings":";;;AAAA,yCAAmD;AAEnD,iCAA2C;AAK3C,SAAgB,oBAAoB,CAAC,KAAa,EAAE,OAA4B;IAC5E,OAAO,cAAc,CAAC,UAAU,KAAK,EAAE,EAAE,OAAO,CAAC,CAAC;AACtD,CAAC;AAFD,oDAEC;AAED,SAAgB,cAAc,CAAC,KAAc,EAAE,OAA4B;IACvE,kCAAkC;IAClC,mBAAW,CAAC,OAAO,CAAC,kBAAkB,GAAG,KAAK,CAAC;IAC/C,MAAM,MAAM,GAA4B;QACpC,SAAS,EAAE,IAAI,aAAK,CAAC;YACjB,kBAAkB,EAAE,KAAK;SAC5B,CAAC;QACF,OAAO,EAAE,EAAE;KACd,CAAC;IAEF,IAAI,CAAC,IAAA,4BAAmB,EAAC,KAAK,CAAC;QAC3B,MAAM,CAAC,OAAQ,CAAC,aAAa,GAAG,KAAK,CAAC;IAE1C,IAAI,OAAO,EAAE,CAAC;QACV,IAAI,CAAC,IAAA,4BAAmB,EAAC,OAAO,CAAC,WAAW,CAAC,EAAE,CAAC;YAC5C,MAAM,CAAC,OAAQ,CAAC,cAAc,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC;YACtD,MAAM,CAAC,OAAQ,CAAC,QAAQ,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC;QACpD,CAAC;IACL,CAAC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC;AApBD,wCAoBC"}
|
package/dist/index.d.ts
ADDED
|
File without changes
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kwiz/node",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "KWIZ utilities and helpers for node applications",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
},
|
|
14
|
+
"./package.json": "./package.json"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"watch": "tsc -watch",
|
|
18
|
+
"build": "npm run test && tsc",
|
|
19
|
+
"build-explain": "tsc --explainFiles",
|
|
20
|
+
"check-dependencies": "madge --circular ./src",
|
|
21
|
+
"create-link": "npm link",
|
|
22
|
+
"test": "node --import tsx --test src",
|
|
23
|
+
"npm-v-patch": "npm version patch",
|
|
24
|
+
"npm-v-major": "npm version major",
|
|
25
|
+
"npm-publish": "npm publish --access public",
|
|
26
|
+
"reset-repo": "git fetch origin && git reset --hard origin/main",
|
|
27
|
+
"link-local-common": "npm link @kwiz/common"
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/KWizCom/node.git"
|
|
32
|
+
},
|
|
33
|
+
"keywords": [
|
|
34
|
+
"KWIZ",
|
|
35
|
+
"SharePoint",
|
|
36
|
+
"SPO",
|
|
37
|
+
"Teams",
|
|
38
|
+
"Utilities",
|
|
39
|
+
"Helpers",
|
|
40
|
+
"Node"
|
|
41
|
+
],
|
|
42
|
+
"author": "Shai Petel",
|
|
43
|
+
"contributors": [
|
|
44
|
+
"Shai Petel",
|
|
45
|
+
"Kevin Vieira"
|
|
46
|
+
],
|
|
47
|
+
"license": "MIT",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/KWizCom/node/issues",
|
|
50
|
+
"email": "support@kwizcom.com"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/KWizCom/node#readme",
|
|
53
|
+
"private": false,
|
|
54
|
+
"engines": {
|
|
55
|
+
"node": ">=16"
|
|
56
|
+
},
|
|
57
|
+
"packageManager": "npm@9.5.1",
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@types/node": "^20.11.24",
|
|
60
|
+
"madge": "^6.1.0",
|
|
61
|
+
"tsx": "^4.7.1",
|
|
62
|
+
"typescript": "^5.3.3"
|
|
63
|
+
},
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@azure/msal-node": "^2.6.4",
|
|
66
|
+
"@kwiz/common": "^1.0.7",
|
|
67
|
+
"axios": "^1.6.7",
|
|
68
|
+
"esbuild": "^0.19.12",
|
|
69
|
+
"get-tsconfig": "^4.7.2",
|
|
70
|
+
"resolve-pkg-maps": "^1.0.0"
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { AuthContextType, GetMSALSiteScope, ITenantInfo, isNullOrUndefined } from "@kwiz/common";
|
|
2
|
+
import { GetMSALToken } from "../auth/msal";
|
|
3
|
+
import { getAxiosConfigBearer } from "../axios";
|
|
4
|
+
|
|
5
|
+
var auth: AuthContextType = null;
|
|
6
|
+
export function ConfigureSPOAuth(config?: AuthContextType) {
|
|
7
|
+
auth = config;
|
|
8
|
+
}
|
|
9
|
+
export async function getAxiosConfigSharePoint(tenantInfo: ITenantInfo, siteUrl: string) {
|
|
10
|
+
if (isNullOrUndefined(auth)) throw Error("Call ConfigureSPOAuth first");
|
|
11
|
+
// only certificate supported
|
|
12
|
+
// DisableCustomAppAuthentication property, that disable this kind of auth., however it can be overriden using this command:
|
|
13
|
+
// Set-SPOTenant -DisableCustomAppAuthentication $false
|
|
14
|
+
let token = await GetMSALToken(tenantInfo, GetMSALSiteScope(siteUrl), auth);
|
|
15
|
+
let config = getAxiosConfigBearer(token, { contantType: "application/json; odata=nometadata" });
|
|
16
|
+
return config;
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import assert from 'assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { DiscoverTenantInfo } from "./discovery";
|
|
4
|
+
|
|
5
|
+
test('DiscoverTenantInfo', async t => {
|
|
6
|
+
const tenantName = "kwizcom.com";
|
|
7
|
+
const tenantInfo = await DiscoverTenantInfo(tenantName);
|
|
8
|
+
assert.strictEqual(tenantInfo.idOrName, "7d034656-be03-457d-8d82-60e90cf5f400");
|
|
9
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { $AzureEnvironment, GetAzureADLoginEndPoint, GetEnvironmentFromACSEndPoint, ITenantInfo, isNullOrEmptyString, isValidGuid, promiseOnce } from "@kwiz/common";
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
|
|
4
|
+
export function DiscoverTenantInfo(hostName: string) {
|
|
5
|
+
hostName = hostName.toLowerCase();
|
|
6
|
+
return promiseOnce(`DiscoverTenantInfo|${hostName}`, async () => {
|
|
7
|
+
let data: ITenantInfo = {
|
|
8
|
+
environment: $AzureEnvironment.Production,
|
|
9
|
+
idOrName: null,
|
|
10
|
+
authorityUrl: null,
|
|
11
|
+
valid: false
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
let tenantId: string = null;
|
|
15
|
+
let friendlyName: string = null;
|
|
16
|
+
|
|
17
|
+
try {
|
|
18
|
+
if (hostName.indexOf(".sharepoint.") !== -1) {
|
|
19
|
+
let hostParts = hostName.split('.');//should be xxx.sharepoint.com or xxx.sharepoint.us
|
|
20
|
+
let firstHostPart = hostParts[0];
|
|
21
|
+
let lastHostPart = hostParts[hostParts.length - 1] === "us" || hostParts[hostParts.length - 1] === "de" ? hostParts[hostParts.length - 1] : "com";
|
|
22
|
+
if (firstHostPart.endsWith("-admin")) firstHostPart = firstHostPart.substring(0, firstHostPart.length - 6);
|
|
23
|
+
friendlyName = `${firstHostPart}.onmicrosoft.${lastHostPart}`;
|
|
24
|
+
}
|
|
25
|
+
else friendlyName = hostName;//could be an exchange email domain, or bpos customer
|
|
26
|
+
|
|
27
|
+
let config = await axios.get<{
|
|
28
|
+
token_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/token
|
|
29
|
+
cloud_instance_name: string;//microsoftonline.com
|
|
30
|
+
token_endpoint_auth_methods_supported: string[];// ["client_secret_post", "private_key_jwt", "client_secret_basic"]
|
|
31
|
+
response_modes_supported: string[];// ["query", "fragment", "form_post"]
|
|
32
|
+
response_types_supported: string[];// ["code", "id_token", "code id_token", "token id_token", "token"]
|
|
33
|
+
scopes_supported: string[];// ["openid"]
|
|
34
|
+
issuer: string;//https://sts.windows.net/7d034656-be03-457d-8d82-60e90cf5f400/
|
|
35
|
+
authorization_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/authorize
|
|
36
|
+
device_authorization_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/devicecode
|
|
37
|
+
end_session_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/oauth2/logout
|
|
38
|
+
userinfo_endpoint: string;//https://login.microsoftonline.com/7d034656-be03-457d-8d82-60e90cf5f400/openid/userinfo
|
|
39
|
+
tenant_region_scope: string;//NA
|
|
40
|
+
cloud_graph_host_name: string;//graph.windows.net
|
|
41
|
+
msgraph_host: string;//graph.microsoft.com
|
|
42
|
+
}>(`https://login.microsoftonline.com/${friendlyName}/v2.0/.well-known/openid-configuration`);
|
|
43
|
+
|
|
44
|
+
let endpoint = config.data.token_endpoint;//https://xxxx/{tenant}/....
|
|
45
|
+
tenantId = endpoint.replace("//", "/").split('/')[2];//replace :// with :/ split by / and take the second part.
|
|
46
|
+
let instance = config.data.cloud_instance_name;//microsoftonline.us
|
|
47
|
+
|
|
48
|
+
data.environment = GetEnvironmentFromACSEndPoint(instance);
|
|
49
|
+
if (!isNullOrEmptyString(tenantId) || isValidGuid(tenantId))
|
|
50
|
+
data.idOrName = tenantId;
|
|
51
|
+
else
|
|
52
|
+
data.idOrName = friendlyName;
|
|
53
|
+
|
|
54
|
+
data.authorityUrl = `${GetAzureADLoginEndPoint(data.environment)}/${data.idOrName}`;
|
|
55
|
+
data.valid = true;
|
|
56
|
+
}
|
|
57
|
+
catch (e) { }
|
|
58
|
+
|
|
59
|
+
return data;
|
|
60
|
+
});
|
|
61
|
+
}
|
package/src/auth/msal.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { ConfidentialClientApplication } from "@azure/msal-node";
|
|
2
|
+
import { AuthContextType, AuthenticationModes, ITenantInfo } from "@kwiz/common";
|
|
3
|
+
//find tenant id? https://login.microsoftonline.com/kwizcom.onmicrosoft.com/.well-known/openid-configuration
|
|
4
|
+
//https://stackoverflow.com/questions/54771270/msal-ad-token-not-valid-with-sharepoint-online-csom
|
|
5
|
+
|
|
6
|
+
var apps: { [tenant: string]: ConfidentialClientApplication } = {};
|
|
7
|
+
|
|
8
|
+
function GetApp(tenantInfo: ITenantInfo, auth: AuthContextType) {
|
|
9
|
+
let key = `${tenantInfo.idOrName}|${auth.authenticationMode}`
|
|
10
|
+
if (!apps[key]) {
|
|
11
|
+
auth.authenticationMode === AuthenticationModes.clientSecret
|
|
12
|
+
? apps[key] = new ConfidentialClientApplication({
|
|
13
|
+
auth: {
|
|
14
|
+
clientId: auth.clientId,
|
|
15
|
+
authority: tenantInfo.authorityUrl,
|
|
16
|
+
clientSecret: auth.clientSecret
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
})
|
|
20
|
+
: apps[key] = new ConfidentialClientApplication({
|
|
21
|
+
auth: {
|
|
22
|
+
clientId: auth.clientId,
|
|
23
|
+
authority: tenantInfo.authorityUrl,
|
|
24
|
+
clientCertificate: {
|
|
25
|
+
thumbprint: auth.thumbprint,
|
|
26
|
+
privateKey: auth.privateKey
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return apps[key];
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** client secret not supported by SharePoint, must use certificate */
|
|
36
|
+
export async function GetMSALToken(tenantInfo: ITenantInfo, scope: string, auth: AuthContextType) {
|
|
37
|
+
let token = await GetApp(tenantInfo, auth).acquireTokenByClientCredential({
|
|
38
|
+
scopes: [`${scope}/.default`]
|
|
39
|
+
});
|
|
40
|
+
return token.accessToken;
|
|
41
|
+
}
|
package/src/axios.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { isNullOrEmptyString } from "@kwiz/common";
|
|
2
|
+
import { AxiosRequestConfig } from "axios";
|
|
3
|
+
import { Agent, globalAgent } from "https";
|
|
4
|
+
|
|
5
|
+
type axiosConfigOptions = {
|
|
6
|
+
contantType?: "application/json" | "application/json; odata=nometadata" | "application/xml";
|
|
7
|
+
}
|
|
8
|
+
export function getAxiosConfigBearer(token: string, options?: axiosConfigOptions) {
|
|
9
|
+
return getAxiosConfig(`Bearer ${token}`, options);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function getAxiosConfig(token?: string, options?: axiosConfigOptions) {
|
|
13
|
+
//allow self sign ssl certificates
|
|
14
|
+
globalAgent.options.rejectUnauthorized = false;
|
|
15
|
+
const config: AxiosRequestConfig<any> = {
|
|
16
|
+
httpAgent: new Agent({
|
|
17
|
+
rejectUnauthorized: false
|
|
18
|
+
}),
|
|
19
|
+
headers: {}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
if (!isNullOrEmptyString(token))
|
|
23
|
+
config.headers!.Authorization = token;
|
|
24
|
+
|
|
25
|
+
if (options) {
|
|
26
|
+
if (!isNullOrEmptyString(options.contantType)) {
|
|
27
|
+
config.headers!["Content-Type"] = options.contantType;
|
|
28
|
+
config.headers!["Accept"] = options.contantType;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return config;
|
|
32
|
+
}
|
package/src/index.ts
ADDED
|
File without changes
|