@sitecore-content-sdk/cli 0.2.0-beta.13 → 0.2.0-beta.14
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/dist/cjs/scripts/auth/list.js +8 -3
- package/dist/cjs/scripts/auth/login.js +19 -11
- package/dist/cjs/scripts/auth/logout.js +12 -6
- package/dist/cjs/scripts/auth/status.js +10 -5
- package/dist/esm/scripts/auth/list.js +5 -1
- package/dist/esm/scripts/auth/login.js +13 -6
- package/dist/esm/scripts/auth/logout.js +7 -2
- package/dist/esm/scripts/auth/status.js +6 -2
- package/package.json +3 -3
- package/types/scripts/auth/list.d.ts +1 -0
- package/types/scripts/auth/login.d.ts +2 -1
- package/types/scripts/auth/logout.d.ts +1 -0
- package/types/scripts/auth/status.d.ts +1 -0
- package/dist/cjs/scripts/auth/models.js +0 -2
- package/dist/cjs/utils/auth/flow.js +0 -72
- package/dist/cjs/utils/auth/renewal.js +0 -95
- package/dist/cjs/utils/auth/tenant-state.js +0 -91
- package/dist/cjs/utils/auth/tenant-store.js +0 -212
- package/dist/esm/scripts/auth/models.js +0 -1
- package/dist/esm/utils/auth/flow.js +0 -68
- package/dist/esm/utils/auth/renewal.js +0 -90
- package/dist/esm/utils/auth/tenant-state.js +0 -53
- package/dist/esm/utils/auth/tenant-store.js +0 -170
- package/types/scripts/auth/models.d.ts +0 -94
- package/types/utils/auth/flow.d.ts +0 -24
- package/types/utils/auth/renewal.d.ts +0 -22
- package/types/utils/auth/tenant-state.d.ts +0 -14
- package/types/utils/auth/tenant-store.d.ts +0 -40
|
@@ -9,13 +9,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.list = void 0;
|
|
13
|
-
const
|
|
12
|
+
exports.list = exports.unitMock = void 0;
|
|
13
|
+
const tools_1 = require("@sitecore-content-sdk/core/tools");
|
|
14
|
+
let { getAllTenantsInfo } = tools_1.auth;
|
|
15
|
+
const unitMock = (authModule) => {
|
|
16
|
+
getAllTenantsInfo = authModule.getAllTenantsInfo;
|
|
17
|
+
};
|
|
18
|
+
exports.unitMock = unitMock;
|
|
14
19
|
exports.list = {
|
|
15
20
|
command: 'list',
|
|
16
21
|
describe: 'List all known tenants',
|
|
17
22
|
handler: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
-
const tenants =
|
|
23
|
+
const tenants = getAllTenantsInfo();
|
|
19
24
|
if (tenants.length === 0) {
|
|
20
25
|
console.log('\n No tenant information found.');
|
|
21
26
|
return;
|
|
@@ -9,10 +9,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.login = void 0;
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
|
|
12
|
+
exports.login = exports.unitMock = void 0;
|
|
13
|
+
const tools_1 = require("@sitecore-content-sdk/core/tools");
|
|
14
|
+
const core_1 = require("@sitecore-content-sdk/core");
|
|
15
|
+
let { setActiveTenant, writeTenantAuthInfo, writeTenantInfo, clientCredentialsFlow } = tools_1.auth;
|
|
16
|
+
const unitMock = (formModule) => {
|
|
17
|
+
setActiveTenant = formModule.setActiveTenant;
|
|
18
|
+
writeTenantAuthInfo = formModule.writeTenantAuthInfo;
|
|
19
|
+
writeTenantInfo = formModule.writeTenantInfo;
|
|
20
|
+
clientCredentialsFlow = formModule.clientCredentialsFlow;
|
|
21
|
+
};
|
|
22
|
+
exports.unitMock = unitMock;
|
|
16
23
|
exports.login = {
|
|
17
24
|
command: 'login',
|
|
18
25
|
describe: 'Login into a tenant',
|
|
@@ -65,9 +72,10 @@ exports.login = {
|
|
|
65
72
|
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
66
73
|
const { clientId } = argv;
|
|
67
74
|
let authResult, tenantId, organizationId, tenantName;
|
|
75
|
+
const { DEFAULT_SITECORE_AUTH_DOMAIN, DEFAULT_SITECORE_AUTH_AUDIENCE, DEFAULT_SITECORE_AUTH_BASE_URL, } = core_1.constants;
|
|
68
76
|
if (argv.clientSecret) {
|
|
69
77
|
try {
|
|
70
|
-
const authData = yield
|
|
78
|
+
const authData = yield clientCredentialsFlow({
|
|
71
79
|
clientId,
|
|
72
80
|
clientSecret: argv.clientSecret,
|
|
73
81
|
organizationId: argv.organizationId,
|
|
@@ -91,22 +99,22 @@ exports.login = {
|
|
|
91
99
|
console.log('\n Please provide client secret for authentication.');
|
|
92
100
|
process.exit(1);
|
|
93
101
|
}
|
|
94
|
-
yield
|
|
102
|
+
yield writeTenantAuthInfo(tenantId, {
|
|
95
103
|
clientSecret: argv.clientSecret,
|
|
96
104
|
access_token: authResult.access_token,
|
|
97
105
|
expires_in: authResult.expires_in,
|
|
98
106
|
expires_at: new Date(Date.now() + authResult.expires_in * 1000).toISOString(),
|
|
99
107
|
});
|
|
100
|
-
yield
|
|
108
|
+
yield writeTenantInfo({
|
|
101
109
|
tenantId,
|
|
102
110
|
organizationId,
|
|
103
111
|
clientId,
|
|
104
112
|
tenantName,
|
|
105
|
-
authority: argv.authority ||
|
|
106
|
-
audience: argv.audience ||
|
|
107
|
-
baseUrl: argv.baseUrl ||
|
|
113
|
+
authority: argv.authority || DEFAULT_SITECORE_AUTH_DOMAIN,
|
|
114
|
+
audience: argv.audience || DEFAULT_SITECORE_AUTH_AUDIENCE,
|
|
115
|
+
baseUrl: argv.baseUrl || DEFAULT_SITECORE_AUTH_BASE_URL,
|
|
108
116
|
});
|
|
109
|
-
|
|
117
|
+
setActiveTenant(tenantId);
|
|
110
118
|
console.info(`\n Logged in successfully to tenant ${tenantId}.`);
|
|
111
119
|
}),
|
|
112
120
|
};
|
|
@@ -9,20 +9,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.logout = void 0;
|
|
13
|
-
const
|
|
14
|
-
|
|
12
|
+
exports.logout = exports.unitMock = void 0;
|
|
13
|
+
const tools_1 = require("@sitecore-content-sdk/core/tools");
|
|
14
|
+
let { deleteTenantAuthInfo, getActiveTenant, clearActiveTenant } = tools_1.auth;
|
|
15
|
+
const unitMock = (formModule) => {
|
|
16
|
+
deleteTenantAuthInfo = formModule.deleteTenantAuthInfo || deleteTenantAuthInfo;
|
|
17
|
+
getActiveTenant = formModule.getActiveTenant || getActiveTenant;
|
|
18
|
+
clearActiveTenant = formModule.clearActiveTenant || clearActiveTenant;
|
|
19
|
+
};
|
|
20
|
+
exports.unitMock = unitMock;
|
|
15
21
|
exports.logout = {
|
|
16
22
|
command: 'logout',
|
|
17
23
|
describe: 'Logout from the active tenant',
|
|
18
24
|
handler: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
const tenantId =
|
|
25
|
+
const tenantId = getActiveTenant();
|
|
20
26
|
if (!tenantId) {
|
|
21
27
|
console.error('\n No active tenant found. Please login first.');
|
|
22
28
|
return;
|
|
23
29
|
}
|
|
24
|
-
|
|
25
|
-
|
|
30
|
+
clearActiveTenant();
|
|
31
|
+
deleteTenantAuthInfo(tenantId);
|
|
26
32
|
console.info(`\n Logged out from tenant ${tenantId}`);
|
|
27
33
|
}),
|
|
28
34
|
};
|
|
@@ -9,19 +9,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.status = void 0;
|
|
13
|
-
const
|
|
14
|
-
|
|
12
|
+
exports.status = exports.unitMock = void 0;
|
|
13
|
+
const tools_1 = require("@sitecore-content-sdk/core/tools");
|
|
14
|
+
let { readTenantInfo, renewAuthIfExpired } = tools_1.auth;
|
|
15
|
+
const unitMock = (formModule) => {
|
|
16
|
+
readTenantInfo = formModule.readTenantInfo || readTenantInfo;
|
|
17
|
+
renewAuthIfExpired = formModule.renewAuthIfExpired || renewAuthIfExpired;
|
|
18
|
+
};
|
|
19
|
+
exports.unitMock = unitMock;
|
|
15
20
|
exports.status = {
|
|
16
21
|
command: 'status',
|
|
17
22
|
describe: 'Show current status of active tenant',
|
|
18
23
|
handler: () => __awaiter(void 0, void 0, void 0, function* () {
|
|
19
|
-
const context = yield
|
|
24
|
+
const context = yield renewAuthIfExpired();
|
|
20
25
|
if (!context) {
|
|
21
26
|
console.log('\n No valid authentication found. Please login.');
|
|
22
27
|
return;
|
|
23
28
|
}
|
|
24
|
-
const tenantInfo = yield
|
|
29
|
+
const tenantInfo = yield readTenantInfo(context.tenantId);
|
|
25
30
|
console.log('\n Active tenant:');
|
|
26
31
|
console.log(` Tenant ID : ${context.tenantId}`);
|
|
27
32
|
console.log(` Tenant Name : ${(tenantInfo === null || tenantInfo === void 0 ? void 0 : tenantInfo.tenantName) || 'N/A'}`);
|
|
@@ -7,7 +7,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
10
|
+
import { auth } from '@sitecore-content-sdk/core/tools';
|
|
11
|
+
let { getAllTenantsInfo } = auth;
|
|
12
|
+
export const unitMock = (authModule) => {
|
|
13
|
+
getAllTenantsInfo = authModule.getAllTenantsInfo;
|
|
14
|
+
};
|
|
11
15
|
export const list = {
|
|
12
16
|
command: 'list',
|
|
13
17
|
describe: 'List all known tenants',
|
|
@@ -7,9 +7,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
12
|
-
|
|
10
|
+
import { auth } from '@sitecore-content-sdk/core/tools';
|
|
11
|
+
import { constants } from '@sitecore-content-sdk/core';
|
|
12
|
+
let { setActiveTenant, writeTenantAuthInfo, writeTenantInfo, clientCredentialsFlow } = auth;
|
|
13
|
+
export const unitMock = (formModule) => {
|
|
14
|
+
setActiveTenant = formModule.setActiveTenant;
|
|
15
|
+
writeTenantAuthInfo = formModule.writeTenantAuthInfo;
|
|
16
|
+
writeTenantInfo = formModule.writeTenantInfo;
|
|
17
|
+
clientCredentialsFlow = formModule.clientCredentialsFlow;
|
|
18
|
+
};
|
|
13
19
|
export const login = {
|
|
14
20
|
command: 'login',
|
|
15
21
|
describe: 'Login into a tenant',
|
|
@@ -62,6 +68,7 @@ export const login = {
|
|
|
62
68
|
handler: (argv) => __awaiter(void 0, void 0, void 0, function* () {
|
|
63
69
|
const { clientId } = argv;
|
|
64
70
|
let authResult, tenantId, organizationId, tenantName;
|
|
71
|
+
const { DEFAULT_SITECORE_AUTH_DOMAIN, DEFAULT_SITECORE_AUTH_AUDIENCE, DEFAULT_SITECORE_AUTH_BASE_URL, } = constants;
|
|
65
72
|
if (argv.clientSecret) {
|
|
66
73
|
try {
|
|
67
74
|
const authData = yield clientCredentialsFlow({
|
|
@@ -99,9 +106,9 @@ export const login = {
|
|
|
99
106
|
organizationId,
|
|
100
107
|
clientId,
|
|
101
108
|
tenantName,
|
|
102
|
-
authority: argv.authority ||
|
|
103
|
-
audience: argv.audience ||
|
|
104
|
-
baseUrl: argv.baseUrl ||
|
|
109
|
+
authority: argv.authority || DEFAULT_SITECORE_AUTH_DOMAIN,
|
|
110
|
+
audience: argv.audience || DEFAULT_SITECORE_AUTH_AUDIENCE,
|
|
111
|
+
baseUrl: argv.baseUrl || DEFAULT_SITECORE_AUTH_BASE_URL,
|
|
105
112
|
});
|
|
106
113
|
setActiveTenant(tenantId);
|
|
107
114
|
console.info(`\n Logged in successfully to tenant ${tenantId}.`);
|
|
@@ -7,8 +7,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import { auth } from '@sitecore-content-sdk/core/tools';
|
|
11
|
+
let { deleteTenantAuthInfo, getActiveTenant, clearActiveTenant } = auth;
|
|
12
|
+
export const unitMock = (formModule) => {
|
|
13
|
+
deleteTenantAuthInfo = formModule.deleteTenantAuthInfo || deleteTenantAuthInfo;
|
|
14
|
+
getActiveTenant = formModule.getActiveTenant || getActiveTenant;
|
|
15
|
+
clearActiveTenant = formModule.clearActiveTenant || clearActiveTenant;
|
|
16
|
+
};
|
|
12
17
|
export const logout = {
|
|
13
18
|
command: 'logout',
|
|
14
19
|
describe: 'Logout from the active tenant',
|
|
@@ -7,8 +7,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import {
|
|
11
|
-
|
|
10
|
+
import { auth } from '@sitecore-content-sdk/core/tools';
|
|
11
|
+
let { readTenantInfo, renewAuthIfExpired } = auth;
|
|
12
|
+
export const unitMock = (formModule) => {
|
|
13
|
+
readTenantInfo = formModule.readTenantInfo || readTenantInfo;
|
|
14
|
+
renewAuthIfExpired = formModule.renewAuthIfExpired || renewAuthIfExpired;
|
|
15
|
+
};
|
|
12
16
|
export const status = {
|
|
13
17
|
command: 'status',
|
|
14
18
|
describe: 'Show current status of active tenant',
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sitecore-content-sdk/cli",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.14",
|
|
4
4
|
"description": "Sitecore Content SDK CLI",
|
|
5
5
|
"main": "dist/cjs/cli.js",
|
|
6
6
|
"module": "dist/esm/cli.js",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"url": "https://github.com/sitecore/content-sdk/issues"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@sitecore-content-sdk/core": "0.2.0-beta.
|
|
37
|
+
"@sitecore-content-sdk/core": "0.2.0-beta.14",
|
|
38
38
|
"dotenv": "^16.5.0",
|
|
39
39
|
"dotenv-expand": "^12.0.2",
|
|
40
40
|
"resolve": "^1.22.10",
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"ts-node": "^10.9.1",
|
|
61
61
|
"typescript": "~5.8.3"
|
|
62
62
|
},
|
|
63
|
-
"gitHead": "
|
|
63
|
+
"gitHead": "7a86b92b3c3fd27c57067509613d3a99d3edc74f",
|
|
64
64
|
"files": [
|
|
65
65
|
"dist",
|
|
66
66
|
"types"
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.BASE_URL = exports.AUDIENCE = exports.AUTH0_DOMAIN = void 0;
|
|
13
|
-
exports.clientCredentialsFlow = clientCredentialsFlow;
|
|
14
|
-
const tenant_store_1 = require("./tenant-store");
|
|
15
|
-
exports.AUTH0_DOMAIN = 'https://auth.sitecorecloud.io';
|
|
16
|
-
exports.AUDIENCE = 'https://api.sitecorecloud.io';
|
|
17
|
-
exports.BASE_URL = 'https://edge-platform.sitecorecloud.io/cs/api';
|
|
18
|
-
const GRANT_TYPE = 'client_credentials';
|
|
19
|
-
/**
|
|
20
|
-
* Performs the OAuth 2.0 client credentials flow to obtain a JWT access token
|
|
21
|
-
* from the Sitecore Identity Provider using the provided client credentials.
|
|
22
|
-
* @param {object} [args] - The arguments for client credentials flow
|
|
23
|
-
* @param {string} [args.clientId] - The client ID registered with Sitecore Identity
|
|
24
|
-
* @param {string} [args.clientSecret] - The client secret associated with the client ID
|
|
25
|
-
* @param {string} [args.organizationId] - The ID of the organization the client belongs to
|
|
26
|
-
* @param {string} [args.tenantId] - The tenant ID representing the specific Sitecore environment
|
|
27
|
-
* @param {string} [args.audience] - The API audience the token is intended for. Defaults to `AUDIENCE`
|
|
28
|
-
* @param {string} [args.authority] - The auth server base URL. Defaults to `AUTH0_DOMAIN`
|
|
29
|
-
* @param {string} [args.baseUrl] - The base URL for the API, used to construct the audience
|
|
30
|
-
* @returns A Promise that resolves to the access token response (including access token, token type, expiry, etc.)
|
|
31
|
-
* @throws Will log and exit the process if the request fails or returns a non-OK status
|
|
32
|
-
*/
|
|
33
|
-
function clientCredentialsFlow(_a) {
|
|
34
|
-
return __awaiter(this, arguments, void 0, function* ({ clientId, clientSecret, organizationId, tenantId, audience = exports.AUDIENCE, authority = exports.AUTH0_DOMAIN, baseUrl = exports.BASE_URL, }) {
|
|
35
|
-
const params = new URLSearchParams({
|
|
36
|
-
client_id: clientId,
|
|
37
|
-
client_secret: clientSecret !== null && clientSecret !== void 0 ? clientSecret : '',
|
|
38
|
-
organization_id: organizationId !== null && organizationId !== void 0 ? organizationId : '',
|
|
39
|
-
tenant_id: tenantId !== null && tenantId !== void 0 ? tenantId : '',
|
|
40
|
-
audience,
|
|
41
|
-
grant_type: GRANT_TYPE,
|
|
42
|
-
baseUrl: baseUrl !== null && baseUrl !== void 0 ? baseUrl : '',
|
|
43
|
-
});
|
|
44
|
-
try {
|
|
45
|
-
const response = yield fetch(`${authority}/oauth/token`, {
|
|
46
|
-
method: 'POST',
|
|
47
|
-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
|
48
|
-
body: params.toString(),
|
|
49
|
-
});
|
|
50
|
-
const data = yield response.json();
|
|
51
|
-
if (!response.ok) {
|
|
52
|
-
throw new Error(data.error_description || data.error || 'Error during client credentials flow');
|
|
53
|
-
}
|
|
54
|
-
const decodedPayload = (0, tenant_store_1.decodeJwtPayload)(data.access_token) || {};
|
|
55
|
-
if (!(decodedPayload === null || decodedPayload === void 0 ? void 0 : decodedPayload.tokenTenantId) || !decodedPayload.tokenOrgId) {
|
|
56
|
-
throw new Error('\n Token is missing required claims tenant_id or org_id.');
|
|
57
|
-
}
|
|
58
|
-
const { tokenTenantId, tokenOrgId, tokenTenantName } = decodedPayload;
|
|
59
|
-
if (tenantId && tenantId !== tokenTenantId) {
|
|
60
|
-
throw new Error('\n Mismatch: Provided tenant ID does not match claims tenant ID.');
|
|
61
|
-
}
|
|
62
|
-
if (organizationId && organizationId !== tokenOrgId) {
|
|
63
|
-
throw new Error('\n Mismatch: Provided organization ID does not match claims organization ID.');
|
|
64
|
-
}
|
|
65
|
-
return { data, tokenOrgId, tokenTenantId, tokenTenantName };
|
|
66
|
-
}
|
|
67
|
-
catch (error) {
|
|
68
|
-
console.error('\n Error during client credentials flow:', error instanceof Error ? error.message : error);
|
|
69
|
-
throw error;
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.validateAuthInfo = validateAuthInfo;
|
|
13
|
-
exports.renewClientToken = renewClientToken;
|
|
14
|
-
exports.renewAuthIfExpired = renewAuthIfExpired;
|
|
15
|
-
const tenant_state_1 = require("./tenant-state");
|
|
16
|
-
const flow_1 = require("./flow");
|
|
17
|
-
const tenant_store_1 = require("./tenant-store");
|
|
18
|
-
/**
|
|
19
|
-
* Validates whether a given auth config is still valid (i.e., not expired).
|
|
20
|
-
* @param {TenantAuth} authInfo - The tenant auth configuration.
|
|
21
|
-
* @returns True if the token is still valid, false if expired.
|
|
22
|
-
*/
|
|
23
|
-
function validateAuthInfo(authInfo) {
|
|
24
|
-
const now = new Date();
|
|
25
|
-
const expiry = new Date(authInfo.expires_at);
|
|
26
|
-
return now < expiry;
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Renews the token for a given tenant using stored credentials.
|
|
30
|
-
* @param {TenantAuth} authInfo - Current authentication info for the tenant.
|
|
31
|
-
* @param {TenantInfo} tenantInfo - Public metadata about the tenant (e.g., clientId).
|
|
32
|
-
* @returns Promise<void>
|
|
33
|
-
* @throws If credentials are missing or renewal fails.
|
|
34
|
-
*/
|
|
35
|
-
function renewClientToken(authInfo, tenantInfo) {
|
|
36
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
-
const result = yield (0, flow_1.clientCredentialsFlow)({
|
|
38
|
-
clientId: tenantInfo.clientId,
|
|
39
|
-
clientSecret: authInfo.clientSecret,
|
|
40
|
-
organizationId: tenantInfo.organizationId,
|
|
41
|
-
tenantId: tenantInfo.tenantId,
|
|
42
|
-
audience: tenantInfo.audience,
|
|
43
|
-
authority: tenantInfo.authority,
|
|
44
|
-
baseUrl: tenantInfo.baseUrl,
|
|
45
|
-
});
|
|
46
|
-
const tenantId = tenantInfo.tenantId;
|
|
47
|
-
yield (0, tenant_store_1.writeTenantAuthInfo)(tenantId, {
|
|
48
|
-
clientSecret: authInfo.clientSecret,
|
|
49
|
-
access_token: result.data.access_token,
|
|
50
|
-
expires_in: result.data.expires_in,
|
|
51
|
-
expires_at: new Date(Date.now() + result.data.expires_in * 1000).toISOString(),
|
|
52
|
-
});
|
|
53
|
-
console.info(`\n Token for tenant ${tenantId} renewed.`);
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Ensures a valid token exists, renews it if expired.
|
|
58
|
-
* Returns tenant context if successful, otherwise null.
|
|
59
|
-
*/
|
|
60
|
-
function renewAuthIfExpired() {
|
|
61
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
-
const tenantId = (0, tenant_state_1.getActiveTenant)();
|
|
63
|
-
if (!tenantId)
|
|
64
|
-
return null;
|
|
65
|
-
const authInfo = yield (0, tenant_store_1.readTenantAuthInfo)(tenantId);
|
|
66
|
-
if (!authInfo)
|
|
67
|
-
return null;
|
|
68
|
-
const isValid = validateAuthInfo(authInfo);
|
|
69
|
-
if (isValid) {
|
|
70
|
-
return { tenantId };
|
|
71
|
-
}
|
|
72
|
-
const tenantInfo = yield (0, tenant_store_1.readTenantInfo)(tenantId);
|
|
73
|
-
if (!tenantInfo)
|
|
74
|
-
return null;
|
|
75
|
-
console.info(`\n Token for tenant ${tenantId} is expired. Renewing...`);
|
|
76
|
-
try {
|
|
77
|
-
if (authInfo.clientSecret) {
|
|
78
|
-
yield renewClientToken(authInfo, tenantInfo);
|
|
79
|
-
}
|
|
80
|
-
else {
|
|
81
|
-
// <TODO>: Implement Device auth token renewal.
|
|
82
|
-
throw new Error('\n Please use clientSecret for authentication.');
|
|
83
|
-
}
|
|
84
|
-
return { tenantId };
|
|
85
|
-
}
|
|
86
|
-
catch (err) {
|
|
87
|
-
console.error(`\n Failed to renew token for tenant '${tenantId}'`);
|
|
88
|
-
console.warn(`\n Cleaning up stale authentication data for tenant '${tenantId}'...`);
|
|
89
|
-
yield (0, tenant_store_1.deleteTenantAuthInfo)(tenantId);
|
|
90
|
-
(0, tenant_state_1.clearActiveTenant)();
|
|
91
|
-
console.info('\n You will need to login again to re-authenticate.');
|
|
92
|
-
process.exit(1);
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.getActiveTenant = getActiveTenant;
|
|
37
|
-
exports.setActiveTenant = setActiveTenant;
|
|
38
|
-
exports.clearActiveTenant = clearActiveTenant;
|
|
39
|
-
const fs = __importStar(require("fs"));
|
|
40
|
-
const path = __importStar(require("path"));
|
|
41
|
-
const os = __importStar(require("os"));
|
|
42
|
-
const configDir = path.join(os.homedir(), '.sitecore', 'sitecore-tools');
|
|
43
|
-
const settingsFile = path.join(configDir, 'settings.json');
|
|
44
|
-
/**
|
|
45
|
-
* Gets the ID of the currently active tenant from settings.json.
|
|
46
|
-
* @returns The active tenant ID if present, otherwise null.
|
|
47
|
-
*/
|
|
48
|
-
function getActiveTenant() {
|
|
49
|
-
var _a;
|
|
50
|
-
if (!fs.existsSync(settingsFile)) {
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
try {
|
|
54
|
-
const content = fs.readFileSync(settingsFile, 'utf-8');
|
|
55
|
-
const data = JSON.parse(content);
|
|
56
|
-
return (_a = data.activeTenant) !== null && _a !== void 0 ? _a : null;
|
|
57
|
-
}
|
|
58
|
-
catch (error) {
|
|
59
|
-
console.error(`\n Failed to read active tenant: ${error.message}`);
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Sets the currently active tenant by writing to settings.json.
|
|
65
|
-
* @param {string} tenantId - The tenant ID to set as active.
|
|
66
|
-
*/
|
|
67
|
-
function setActiveTenant(tenantId) {
|
|
68
|
-
try {
|
|
69
|
-
if (!fs.existsSync(configDir)) {
|
|
70
|
-
fs.mkdirSync(configDir, { recursive: true });
|
|
71
|
-
}
|
|
72
|
-
const data = { activeTenant: tenantId };
|
|
73
|
-
fs.writeFileSync(settingsFile, JSON.stringify(data, null, 2));
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
console.error(`\n Failed to set active tenant '${tenantId}': ${error.message}`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
/**
|
|
80
|
-
* Clears the currently active tenant from settings.json by deleting the file.
|
|
81
|
-
*/
|
|
82
|
-
function clearActiveTenant() {
|
|
83
|
-
try {
|
|
84
|
-
if (fs.existsSync(settingsFile)) {
|
|
85
|
-
fs.unlinkSync(settingsFile);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
console.error(`\n Failed to clear active tenant: ${error.message}`);
|
|
90
|
-
}
|
|
91
|
-
}
|