@sitecore-content-sdk/core 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/constants.js +3 -2
- package/dist/cjs/tools/auth/flow.js +70 -0
- package/dist/cjs/tools/auth/index.js +20 -0
- package/dist/cjs/tools/auth/models.js +2 -0
- package/dist/cjs/tools/auth/renewal.js +82 -0
- package/dist/cjs/tools/auth/tenant-state.js +110 -0
- package/dist/cjs/tools/auth/tenant-store.js +243 -0
- package/dist/cjs/tools/index.js +26 -3
- package/dist/esm/constants.js +2 -1
- package/dist/esm/tools/auth/flow.js +67 -0
- package/dist/esm/tools/auth/index.js +4 -0
- package/dist/esm/tools/auth/models.js +1 -0
- package/dist/esm/tools/auth/renewal.js +77 -0
- package/dist/esm/tools/auth/tenant-state.js +73 -0
- package/dist/esm/tools/auth/tenant-store.js +206 -0
- package/dist/esm/tools/index.js +3 -1
- package/package.json +2 -2
- package/types/constants.d.ts +2 -1
- package/types/tools/auth/flow.d.ts +27 -0
- package/types/tools/auth/index.d.ts +4 -0
- package/types/tools/auth/models.d.ts +94 -0
- package/types/tools/auth/renewal.d.ts +22 -0
- package/types/tools/auth/tenant-state.d.ts +21 -0
- package/types/tools/auth/tenant-store.d.ts +63 -0
- package/types/tools/index.d.ts +3 -1
- package/dist/cjs/tools/auth/fetch-bearer-token.js +0 -43
- package/dist/esm/tools/auth/fetch-bearer-token.js +0 -36
- package/types/tools/auth/fetch-bearer-token.d.ts +0 -13
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { clientCredentialsFlow } from './flow';
|
|
2
|
+
export { renewClientToken, renewAuthIfExpired, validateAuthInfo } from './renewal';
|
|
3
|
+
export { getActiveTenant, setActiveTenant, clearActiveTenant } from './tenant-state';
|
|
4
|
+
export { writeTenantAuthInfo, readTenantAuthInfo, deleteTenantAuthInfo, readTenantInfo, getAllTenantsInfo, writeTenantInfo, } from './tenant-store';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { getActiveTenant, clearActiveTenant } from './tenant-state';
|
|
2
|
+
import { clientCredentialsFlow } from './flow';
|
|
3
|
+
import { writeTenantAuthInfo, readTenantAuthInfo, deleteTenantAuthInfo, readTenantInfo, } from './tenant-store';
|
|
4
|
+
/**
|
|
5
|
+
* Validates whether a given auth config is still valid (i.e., not expired).
|
|
6
|
+
* @param {TenantAuth} authInfo - The tenant auth configuration.
|
|
7
|
+
* @returns True if the token is still valid, false if expired.
|
|
8
|
+
*/
|
|
9
|
+
export function validateAuthInfo(authInfo) {
|
|
10
|
+
const now = new Date();
|
|
11
|
+
const expiry = new Date(authInfo.expires_at);
|
|
12
|
+
return now < expiry;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Renews the token for a given tenant using stored credentials.
|
|
16
|
+
* @param {TenantAuth} authInfo - Current authentication info for the tenant.
|
|
17
|
+
* @param {TenantInfo} tenantInfo - Public metadata about the tenant (e.g., clientId).
|
|
18
|
+
* @returns Promise<void>
|
|
19
|
+
* @throws If credentials are missing or renewal fails.
|
|
20
|
+
*/
|
|
21
|
+
export async function renewClientToken(authInfo, tenantInfo) {
|
|
22
|
+
const result = await clientCredentialsFlow({
|
|
23
|
+
clientId: tenantInfo.clientId,
|
|
24
|
+
clientSecret: authInfo.clientSecret,
|
|
25
|
+
organizationId: tenantInfo.organizationId,
|
|
26
|
+
tenantId: tenantInfo.tenantId,
|
|
27
|
+
audience: tenantInfo.audience,
|
|
28
|
+
authority: tenantInfo.authority,
|
|
29
|
+
baseUrl: tenantInfo.baseUrl,
|
|
30
|
+
});
|
|
31
|
+
const tenantId = tenantInfo.tenantId;
|
|
32
|
+
await writeTenantAuthInfo(tenantId, {
|
|
33
|
+
clientSecret: authInfo.clientSecret,
|
|
34
|
+
access_token: result.data.access_token,
|
|
35
|
+
expires_in: result.data.expires_in,
|
|
36
|
+
expires_at: new Date(Date.now() + result.data.expires_in * 1000).toISOString(),
|
|
37
|
+
});
|
|
38
|
+
console.info(`\n Token for tenant ${tenantId} renewed.`);
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Ensures a valid token exists, renews it if expired.
|
|
42
|
+
* Returns tenant context if successful, otherwise null.
|
|
43
|
+
*/
|
|
44
|
+
export async function renewAuthIfExpired() {
|
|
45
|
+
const tenantId = getActiveTenant();
|
|
46
|
+
if (!tenantId)
|
|
47
|
+
return null;
|
|
48
|
+
const authInfo = await readTenantAuthInfo(tenantId);
|
|
49
|
+
if (!authInfo)
|
|
50
|
+
return null;
|
|
51
|
+
const isValid = validateAuthInfo(authInfo);
|
|
52
|
+
if (isValid) {
|
|
53
|
+
return { tenantId };
|
|
54
|
+
}
|
|
55
|
+
const tenantInfo = await readTenantInfo(tenantId);
|
|
56
|
+
if (!tenantInfo)
|
|
57
|
+
return null;
|
|
58
|
+
console.info(`\n Token for tenant ${tenantId} is expired. Renewing...`);
|
|
59
|
+
try {
|
|
60
|
+
if (authInfo.clientSecret) {
|
|
61
|
+
await renewClientToken(authInfo, tenantInfo);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
// <TODO>: Implement Device auth token renewal.
|
|
65
|
+
throw new Error('\n Please use clientSecret for authentication.');
|
|
66
|
+
}
|
|
67
|
+
return { tenantId };
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
console.error(`\n Failed to renew token for tenant '${tenantId}'`);
|
|
71
|
+
console.warn(`\n Cleaning up stale authentication data for tenant '${tenantId}'...`);
|
|
72
|
+
await deleteTenantAuthInfo(tenantId);
|
|
73
|
+
clearActiveTenant();
|
|
74
|
+
console.info('\n You will need to login again to re-authenticate.');
|
|
75
|
+
process.exit(1);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/require-jsdoc */
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
const configDir = path.join(os.homedir(), '.sitecore', 'sitecore-tools');
|
|
6
|
+
const settingsFile = path.join(configDir, 'settings.json');
|
|
7
|
+
/**
|
|
8
|
+
* Gets the ID of the currently active tenant from settings.json.
|
|
9
|
+
* @returns The active tenant ID if present, otherwise null.
|
|
10
|
+
*/
|
|
11
|
+
export let getActiveTenant = _getActiveTenant;
|
|
12
|
+
/**
|
|
13
|
+
* Clears the currently active tenant from settings.json by deleting the file.
|
|
14
|
+
*/
|
|
15
|
+
export let clearActiveTenant = _clearActiveTenant;
|
|
16
|
+
// mock setup for unit tests to make sinon happy and mock-able with esbuild/tsx
|
|
17
|
+
// https://sinonjs.org/how-to/typescript-swc/
|
|
18
|
+
// This, plus the `_` names make the exports writable for sinon
|
|
19
|
+
export const unitMocks = {
|
|
20
|
+
set clearActiveTenant(mockImplementation) {
|
|
21
|
+
clearActiveTenant = mockImplementation;
|
|
22
|
+
},
|
|
23
|
+
get clearActiveTenant() {
|
|
24
|
+
return _clearActiveTenant;
|
|
25
|
+
},
|
|
26
|
+
set getActiveTenant(mockImplementation) {
|
|
27
|
+
getActiveTenant = mockImplementation;
|
|
28
|
+
},
|
|
29
|
+
get getActiveTenant() {
|
|
30
|
+
return _getActiveTenant;
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
function _getActiveTenant() {
|
|
34
|
+
var _a;
|
|
35
|
+
if (!fs.existsSync(settingsFile)) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const content = fs.readFileSync(settingsFile, 'utf-8');
|
|
40
|
+
const data = JSON.parse(content);
|
|
41
|
+
return (_a = data.activeTenant) !== null && _a !== void 0 ? _a : null;
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.error(`\n Failed to read active tenant: ${error.message}`);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Sets the currently active tenant by writing to settings.json.
|
|
50
|
+
* @param {string} tenantId - The tenant ID to set as active.
|
|
51
|
+
*/
|
|
52
|
+
export function setActiveTenant(tenantId) {
|
|
53
|
+
try {
|
|
54
|
+
if (!fs.existsSync(configDir)) {
|
|
55
|
+
fs.mkdirSync(configDir, { recursive: true });
|
|
56
|
+
}
|
|
57
|
+
const data = { activeTenant: tenantId };
|
|
58
|
+
fs.writeFileSync(settingsFile, JSON.stringify(data, null, 2));
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
console.error(`\n Failed to set active tenant '${tenantId}': ${error.message}`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function _clearActiveTenant() {
|
|
65
|
+
try {
|
|
66
|
+
if (fs.existsSync(settingsFile)) {
|
|
67
|
+
fs.unlinkSync(settingsFile);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error(`\n Failed to clear active tenant: ${error.message}`);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/* eslint-disable jsdoc/require-jsdoc */
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
const CLAIMS = 'https://auth.sitecorecloud.io/claims';
|
|
6
|
+
const rootDir = path.join(os.homedir(), '.sitecore', 'sitecore-tools');
|
|
7
|
+
/**
|
|
8
|
+
* Decodes a JWT without verifying its signature.
|
|
9
|
+
* @param {string} token - The access token string.
|
|
10
|
+
* @returns Decoded payload object or null if invalid.
|
|
11
|
+
*/
|
|
12
|
+
export let decodeJwtPayload = _decodeJwtPayload;
|
|
13
|
+
/**
|
|
14
|
+
* Write the authentication configuration for a tenant.
|
|
15
|
+
* @param {string} tenantId - The tenant ID.
|
|
16
|
+
* @param {TenantAuth} authInfo - The tenant's auth data.
|
|
17
|
+
*/
|
|
18
|
+
export let writeTenantAuthInfo = _writeTenantAuthInfo;
|
|
19
|
+
/**
|
|
20
|
+
* Read the authentication configuration for a tenant.
|
|
21
|
+
* @param {string} tenantId - The tenant ID.
|
|
22
|
+
* @returns Parsed auth config or null if not found or failed to read.
|
|
23
|
+
*/
|
|
24
|
+
export let readTenantAuthInfo = _readTenantAuthInfo;
|
|
25
|
+
/**
|
|
26
|
+
* Write the public metadata information for a tenant.
|
|
27
|
+
* @param {TenantInfo} info - The tenant info object.
|
|
28
|
+
*/
|
|
29
|
+
export let writeTenantInfo = _writeTenantInfo;
|
|
30
|
+
/**
|
|
31
|
+
* Read the public metadata information for a tenant.
|
|
32
|
+
* @param {string} tenantId - The tenant ID.
|
|
33
|
+
* @returns Parsed tenant info or null if not found or failed to read.
|
|
34
|
+
*/
|
|
35
|
+
export let readTenantInfo = _readTenantInfo;
|
|
36
|
+
/**
|
|
37
|
+
* Deletes the stored auth.json file for the given tenant.
|
|
38
|
+
* @param {string} tenantId - The tenant ID.
|
|
39
|
+
*/
|
|
40
|
+
export let deleteTenantAuthInfo = _deleteTenantAuthInfo;
|
|
41
|
+
/**
|
|
42
|
+
* Scans the CLI root directory and returns all valid tenant infos.
|
|
43
|
+
* @returns A list of TenantInfo objects found in {tenant-id}/info.json files.
|
|
44
|
+
*/
|
|
45
|
+
export let getAllTenantsInfo = _getAllTenantsInfo;
|
|
46
|
+
// mock setup for unit tests to make sinon happy and mock-able with esbuild/tsx
|
|
47
|
+
// https://sinonjs.org/how-to/typescript-swc/
|
|
48
|
+
// This, plus the `_` names make the exports writable for sinon
|
|
49
|
+
export const unitMocks = {
|
|
50
|
+
set decodeJwtPayload(mockImplementation) {
|
|
51
|
+
decodeJwtPayload = mockImplementation;
|
|
52
|
+
},
|
|
53
|
+
get decodeJwtPayload() {
|
|
54
|
+
return _decodeJwtPayload;
|
|
55
|
+
},
|
|
56
|
+
set writeTenantAuthInfo(mockImplementation) {
|
|
57
|
+
writeTenantAuthInfo = mockImplementation;
|
|
58
|
+
},
|
|
59
|
+
get writeTenantAuthInfo() {
|
|
60
|
+
return _writeTenantAuthInfo;
|
|
61
|
+
},
|
|
62
|
+
set readTenantAuthInfo(mockImplementation) {
|
|
63
|
+
readTenantAuthInfo = mockImplementation;
|
|
64
|
+
},
|
|
65
|
+
get readTenantAuthInfo() {
|
|
66
|
+
return _readTenantAuthInfo;
|
|
67
|
+
},
|
|
68
|
+
set writeTenantInfo(mockImplementation) {
|
|
69
|
+
writeTenantInfo = mockImplementation;
|
|
70
|
+
},
|
|
71
|
+
get writeTenantInfo() {
|
|
72
|
+
return _writeTenantInfo;
|
|
73
|
+
},
|
|
74
|
+
set readTenantInfo(mockImplementation) {
|
|
75
|
+
readTenantInfo = mockImplementation;
|
|
76
|
+
},
|
|
77
|
+
get readTenantInfo() {
|
|
78
|
+
return _readTenantInfo;
|
|
79
|
+
},
|
|
80
|
+
set deleteTenantAuthInfo(mockImplementation) {
|
|
81
|
+
deleteTenantAuthInfo = mockImplementation;
|
|
82
|
+
},
|
|
83
|
+
get deleteTenantAuthInfo() {
|
|
84
|
+
return _deleteTenantAuthInfo;
|
|
85
|
+
},
|
|
86
|
+
set getAllTenantsInfo(mockImplementation) {
|
|
87
|
+
getAllTenantsInfo = mockImplementation;
|
|
88
|
+
},
|
|
89
|
+
get getAllTenantsInfo() {
|
|
90
|
+
return _getAllTenantsInfo;
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
/**
|
|
94
|
+
* Get the full path to the tenant-specific folder.
|
|
95
|
+
* @param {string} tenantId - The tenant ID.
|
|
96
|
+
* @returns The absolute path to the tenant directory.
|
|
97
|
+
*/
|
|
98
|
+
export function getTenantPath(tenantId) {
|
|
99
|
+
return path.join(rootDir, tenantId);
|
|
100
|
+
}
|
|
101
|
+
async function _writeTenantAuthInfo(tenantId, authInfo) {
|
|
102
|
+
try {
|
|
103
|
+
const dir = getTenantPath(tenantId);
|
|
104
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
105
|
+
fs.writeFileSync(path.join(dir, 'auth.json'), JSON.stringify(authInfo, null, 2));
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
console.error(`\n Failed to write auth.json for tenant '${tenantId}': ${error.message}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function _readTenantAuthInfo(tenantId) {
|
|
112
|
+
const filePath = path.join(getTenantPath(tenantId), 'auth.json');
|
|
113
|
+
if (!fs.existsSync(filePath))
|
|
114
|
+
return null;
|
|
115
|
+
try {
|
|
116
|
+
const raw = fs.readFileSync(filePath, 'utf-8');
|
|
117
|
+
return JSON.parse(raw);
|
|
118
|
+
}
|
|
119
|
+
catch (error) {
|
|
120
|
+
console.error(`\n Failed to read auth.json for tenant '${tenantId}': ${error.message}`);
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function _writeTenantInfo(info) {
|
|
125
|
+
try {
|
|
126
|
+
const dir = getTenantPath(info.tenantId);
|
|
127
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
128
|
+
fs.writeFileSync(path.join(dir, 'info.json'), JSON.stringify(info, null, 2));
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
console.error(`\n Failed to write info.json for tenant '${info.tenantId}': ${error.message}`);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
async function _readTenantInfo(tenantId) {
|
|
135
|
+
const infoFilePath = path.join(getTenantPath(tenantId), 'info.json');
|
|
136
|
+
if (!fs.existsSync(infoFilePath)) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
const content = fs.readFileSync(infoFilePath, 'utf-8');
|
|
141
|
+
return JSON.parse(content);
|
|
142
|
+
}
|
|
143
|
+
catch (error) {
|
|
144
|
+
console.error(`\n Failed to read info.json for tenant '${tenantId}': ${error.message}`);
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
async function _deleteTenantAuthInfo(tenantId) {
|
|
149
|
+
const filePath = path.join(getTenantPath(tenantId), 'auth.json');
|
|
150
|
+
try {
|
|
151
|
+
if (fs.existsSync(filePath)) {
|
|
152
|
+
fs.unlinkSync(filePath);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
catch (error) {
|
|
156
|
+
console.error(`\n Failed to delete auth.json for tenant '${tenantId}': ${error.message}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function _getAllTenantsInfo() {
|
|
160
|
+
if (!fs.existsSync(rootDir))
|
|
161
|
+
return [];
|
|
162
|
+
const subDirs = fs
|
|
163
|
+
.readdirSync(rootDir)
|
|
164
|
+
.filter((entry) => fs.statSync(path.join(rootDir, entry)).isDirectory());
|
|
165
|
+
const tenants = [];
|
|
166
|
+
for (const dir of subDirs) {
|
|
167
|
+
const infoPath = path.join(rootDir, dir, 'info.json');
|
|
168
|
+
if (fs.existsSync(infoPath)) {
|
|
169
|
+
try {
|
|
170
|
+
const content = fs.readFileSync(infoPath, 'utf-8');
|
|
171
|
+
const data = JSON.parse(content);
|
|
172
|
+
if (data.tenantId && data.tenantName && data.organizationId && data.clientId) {
|
|
173
|
+
tenants.push({
|
|
174
|
+
tenantId: data.tenantId,
|
|
175
|
+
tenantName: data.tenantName,
|
|
176
|
+
organizationId: data.organizationId,
|
|
177
|
+
clientId: data.clientId,
|
|
178
|
+
authority: data.authority,
|
|
179
|
+
audience: data.audience,
|
|
180
|
+
baseUrl: data.baseUrl,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
console.error('\n Failed to read tenant info file', error.message);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return tenants;
|
|
190
|
+
}
|
|
191
|
+
function _decodeJwtPayload(token) {
|
|
192
|
+
try {
|
|
193
|
+
const base64Payload = token.split('.')[1];
|
|
194
|
+
const payload = Buffer.from(base64Payload, 'base64').toString('utf-8');
|
|
195
|
+
const decoded = JSON.parse(payload);
|
|
196
|
+
return {
|
|
197
|
+
tokenTenantId: decoded === null || decoded === void 0 ? void 0 : decoded[`${CLAIMS}/tenant_id`],
|
|
198
|
+
tokenOrgId: decoded === null || decoded === void 0 ? void 0 : decoded[`${CLAIMS}/org_id`],
|
|
199
|
+
tokenTenantName: decoded === null || decoded === void 0 ? void 0 : decoded[`${CLAIMS}/tenant_name`],
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
catch (error) {
|
|
203
|
+
console.error('\n Failed to decode access token:', error.message);
|
|
204
|
+
return null;
|
|
205
|
+
}
|
|
206
|
+
}
|
package/dist/esm/tools/index.js
CHANGED
|
@@ -2,4 +2,6 @@ export { generateSites } from './generateSites';
|
|
|
2
2
|
export { generateMetadata } from './generateMetadata';
|
|
3
3
|
export { scaffoldComponent } from './scaffold';
|
|
4
4
|
export * from './templating';
|
|
5
|
-
export
|
|
5
|
+
export * from './auth/models';
|
|
6
|
+
import * as auth from './auth';
|
|
7
|
+
export { auth };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sitecore-content-sdk/core",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.14",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
},
|
|
78
78
|
"description": "",
|
|
79
79
|
"types": "types/index.d.ts",
|
|
80
|
-
"gitHead": "
|
|
80
|
+
"gitHead": "7a86b92b3c3fd27c57067509613d3a99d3edc74f",
|
|
81
81
|
"files": [
|
|
82
82
|
"dist",
|
|
83
83
|
"types",
|
package/types/constants.d.ts
CHANGED
|
@@ -5,5 +5,6 @@ export declare enum SitecoreTemplateId {
|
|
|
5
5
|
export declare const siteNameError = "The siteName cannot be empty";
|
|
6
6
|
export declare const SITECORE_EDGE_URL_DEFAULT = "https://edge-platform.sitecorecloud.io";
|
|
7
7
|
export declare const HIDDEN_RENDERING_NAME = "Hidden Rendering";
|
|
8
|
-
export declare const
|
|
8
|
+
export declare const DEFAULT_SITECORE_AUTH_DOMAIN = "https://auth.sitecorecloud.io";
|
|
9
9
|
export declare const DEFAULT_SITECORE_AUTH_AUDIENCE = "https://api.sitecorecloud.io";
|
|
10
|
+
export declare const DEFAULT_SITECORE_AUTH_BASE_URL = "https://edge-platform.sitecorecloud.io/cs/api";
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { TenantArgs } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Performs the OAuth 2.0 client credentials flow to obtain a JWT access token
|
|
4
|
+
* from the Sitecore Identity Provider using the provided client credentials.
|
|
5
|
+
* @param {object} [args] - The arguments for client credentials flow
|
|
6
|
+
* @param {string} [args.clientId] - The client ID registered with Sitecore Identity
|
|
7
|
+
* @param {string} [args.clientSecret] - The client secret associated with the client ID
|
|
8
|
+
* @param {string} [args.organizationId] - The ID of the organization the client belongs to
|
|
9
|
+
* @param {string} [args.tenantId] - The tenant ID representing the specific Sitecore environment
|
|
10
|
+
* @param {string} [args.audience] - The API audience the token is intended for. Defaults to `constants.DEFAULT_SITECORE_AUTH_AUDIENCE`
|
|
11
|
+
* @param {string} [args.authority] - The auth server base URL. Defaults to `constants.DEFAULT_SITECORE_AUTH_DOMAIN`
|
|
12
|
+
* @param {string} [args.baseUrl] - The base URL for the API, used to construct the audience. Defaults to `constants.DEFAULT_SITECORE_AUTH_BASE_URL`
|
|
13
|
+
* @returns A Promise that resolves to the access token response (including access token, token type, expiry, etc.)
|
|
14
|
+
* @throws Will log and exit the process if the request fails or returns a non-OK status
|
|
15
|
+
*/
|
|
16
|
+
export declare let clientCredentialsFlow: typeof _clientCredentialsFlow;
|
|
17
|
+
export declare const unitMocks: {
|
|
18
|
+
clientCredentialsFlow: typeof _clientCredentialsFlow;
|
|
19
|
+
};
|
|
20
|
+
declare function _clientCredentialsFlow({ clientId, clientSecret, organizationId, tenantId, audience, authority, baseUrl, }: TenantArgs): Promise<{
|
|
21
|
+
data: any;
|
|
22
|
+
tokenOrgId: any;
|
|
23
|
+
tokenTenantId: any;
|
|
24
|
+
tokenTenantName: any;
|
|
25
|
+
accessToken: any;
|
|
26
|
+
}>;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { clientCredentialsFlow } from './flow';
|
|
2
|
+
export { renewClientToken, renewAuthIfExpired, validateAuthInfo } from './renewal';
|
|
3
|
+
export { getActiveTenant, setActiveTenant, clearActiveTenant } from './tenant-state';
|
|
4
|
+
export { writeTenantAuthInfo, readTenantAuthInfo, deleteTenantAuthInfo, readTenantInfo, getAllTenantsInfo, writeTenantInfo, } from './tenant-store';
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CLI arguments used for authentication and tenant identification.
|
|
3
|
+
*/
|
|
4
|
+
export interface TenantArgs {
|
|
5
|
+
/**
|
|
6
|
+
* OAuth2 client ID used to identify the application
|
|
7
|
+
*/
|
|
8
|
+
clientId: string;
|
|
9
|
+
/**
|
|
10
|
+
* Client secret used for client credentials flow
|
|
11
|
+
*/
|
|
12
|
+
clientSecret?: string;
|
|
13
|
+
/**
|
|
14
|
+
* Organization ID associated with the tenant
|
|
15
|
+
*/
|
|
16
|
+
organizationId?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Tenant ID used for scoping the login
|
|
19
|
+
*/
|
|
20
|
+
tenantId?: string;
|
|
21
|
+
/**
|
|
22
|
+
* OAuth2 audience (e.g., API base URL the token is intended for)
|
|
23
|
+
*/
|
|
24
|
+
audience?: string;
|
|
25
|
+
/**
|
|
26
|
+
* Auth authority/issuer URL (e.g., Sitecore identity endpoint)
|
|
27
|
+
*/
|
|
28
|
+
authority?: string;
|
|
29
|
+
/**
|
|
30
|
+
* Base URL for the target Sitecore Content Management API
|
|
31
|
+
*/
|
|
32
|
+
baseUrl?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface TenantSettings {
|
|
35
|
+
/**
|
|
36
|
+
* Currently active tenant ID tracked by the CLI
|
|
37
|
+
*/
|
|
38
|
+
activeTenant?: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Auth configuration stored per tenant for accessing Sitecore APIs.
|
|
42
|
+
*/
|
|
43
|
+
export interface TenantAuth {
|
|
44
|
+
/**
|
|
45
|
+
* Access token issued by the identity provider
|
|
46
|
+
*/
|
|
47
|
+
access_token: string;
|
|
48
|
+
/**
|
|
49
|
+
* Token expiration duration in seconds
|
|
50
|
+
*/
|
|
51
|
+
expires_in: number;
|
|
52
|
+
/**
|
|
53
|
+
* Exact ISO timestamp when the token expires
|
|
54
|
+
*/
|
|
55
|
+
expires_at: string;
|
|
56
|
+
/**
|
|
57
|
+
* Secret used for client credentials flow and re-authenticate
|
|
58
|
+
*/
|
|
59
|
+
clientSecret?: string;
|
|
60
|
+
refresh_token?: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Public metadata for a known tenant.
|
|
64
|
+
*/
|
|
65
|
+
export interface TenantInfo {
|
|
66
|
+
/**
|
|
67
|
+
* Unique ID of the tenant
|
|
68
|
+
*/
|
|
69
|
+
tenantId: string;
|
|
70
|
+
/**
|
|
71
|
+
* Human-readable name of the tenant
|
|
72
|
+
*/
|
|
73
|
+
tenantName: string;
|
|
74
|
+
/**
|
|
75
|
+
* Organization ID the tenant belongs to
|
|
76
|
+
*/
|
|
77
|
+
organizationId: string;
|
|
78
|
+
/**
|
|
79
|
+
* Client ID associated with this tenant's authentication
|
|
80
|
+
*/
|
|
81
|
+
clientId: string;
|
|
82
|
+
/**
|
|
83
|
+
* OAuth2 audience (e.g., API base URL the token is intended for)
|
|
84
|
+
*/
|
|
85
|
+
audience: string;
|
|
86
|
+
/**
|
|
87
|
+
* Auth authority/issuer URL (e.g., Sitecore identity endpoint)
|
|
88
|
+
*/
|
|
89
|
+
authority: string;
|
|
90
|
+
/**
|
|
91
|
+
* Base URL for the target Sitecore Content Management API
|
|
92
|
+
*/
|
|
93
|
+
baseUrl: string;
|
|
94
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TenantAuth, TenantInfo } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Validates whether a given auth config is still valid (i.e., not expired).
|
|
4
|
+
* @param {TenantAuth} authInfo - The tenant auth configuration.
|
|
5
|
+
* @returns True if the token is still valid, false if expired.
|
|
6
|
+
*/
|
|
7
|
+
export declare function validateAuthInfo(authInfo: TenantAuth): boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Renews the token for a given tenant using stored credentials.
|
|
10
|
+
* @param {TenantAuth} authInfo - Current authentication info for the tenant.
|
|
11
|
+
* @param {TenantInfo} tenantInfo - Public metadata about the tenant (e.g., clientId).
|
|
12
|
+
* @returns Promise<void>
|
|
13
|
+
* @throws If credentials are missing or renewal fails.
|
|
14
|
+
*/
|
|
15
|
+
export declare function renewClientToken(authInfo: TenantAuth, tenantInfo: TenantInfo): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* Ensures a valid token exists, renews it if expired.
|
|
18
|
+
* Returns tenant context if successful, otherwise null.
|
|
19
|
+
*/
|
|
20
|
+
export declare function renewAuthIfExpired(): Promise<{
|
|
21
|
+
tenantId: string;
|
|
22
|
+
} | null>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gets the ID of the currently active tenant from settings.json.
|
|
3
|
+
* @returns The active tenant ID if present, otherwise null.
|
|
4
|
+
*/
|
|
5
|
+
export declare let getActiveTenant: typeof _getActiveTenant;
|
|
6
|
+
/**
|
|
7
|
+
* Clears the currently active tenant from settings.json by deleting the file.
|
|
8
|
+
*/
|
|
9
|
+
export declare let clearActiveTenant: typeof _clearActiveTenant;
|
|
10
|
+
export declare const unitMocks: {
|
|
11
|
+
clearActiveTenant: typeof _clearActiveTenant;
|
|
12
|
+
getActiveTenant: typeof _getActiveTenant;
|
|
13
|
+
};
|
|
14
|
+
declare function _getActiveTenant(): string | null;
|
|
15
|
+
/**
|
|
16
|
+
* Sets the currently active tenant by writing to settings.json.
|
|
17
|
+
* @param {string} tenantId - The tenant ID to set as active.
|
|
18
|
+
*/
|
|
19
|
+
export declare function setActiveTenant(tenantId: string): void;
|
|
20
|
+
declare function _clearActiveTenant(): void;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { TenantAuth, TenantInfo } from './models';
|
|
2
|
+
/**
|
|
3
|
+
* Decodes a JWT without verifying its signature.
|
|
4
|
+
* @param {string} token - The access token string.
|
|
5
|
+
* @returns Decoded payload object or null if invalid.
|
|
6
|
+
*/
|
|
7
|
+
export declare let decodeJwtPayload: typeof _decodeJwtPayload;
|
|
8
|
+
/**
|
|
9
|
+
* Write the authentication configuration for a tenant.
|
|
10
|
+
* @param {string} tenantId - The tenant ID.
|
|
11
|
+
* @param {TenantAuth} authInfo - The tenant's auth data.
|
|
12
|
+
*/
|
|
13
|
+
export declare let writeTenantAuthInfo: typeof _writeTenantAuthInfo;
|
|
14
|
+
/**
|
|
15
|
+
* Read the authentication configuration for a tenant.
|
|
16
|
+
* @param {string} tenantId - The tenant ID.
|
|
17
|
+
* @returns Parsed auth config or null if not found or failed to read.
|
|
18
|
+
*/
|
|
19
|
+
export declare let readTenantAuthInfo: typeof _readTenantAuthInfo;
|
|
20
|
+
/**
|
|
21
|
+
* Write the public metadata information for a tenant.
|
|
22
|
+
* @param {TenantInfo} info - The tenant info object.
|
|
23
|
+
*/
|
|
24
|
+
export declare let writeTenantInfo: typeof _writeTenantInfo;
|
|
25
|
+
/**
|
|
26
|
+
* Read the public metadata information for a tenant.
|
|
27
|
+
* @param {string} tenantId - The tenant ID.
|
|
28
|
+
* @returns Parsed tenant info or null if not found or failed to read.
|
|
29
|
+
*/
|
|
30
|
+
export declare let readTenantInfo: typeof _readTenantInfo;
|
|
31
|
+
/**
|
|
32
|
+
* Deletes the stored auth.json file for the given tenant.
|
|
33
|
+
* @param {string} tenantId - The tenant ID.
|
|
34
|
+
*/
|
|
35
|
+
export declare let deleteTenantAuthInfo: typeof _deleteTenantAuthInfo;
|
|
36
|
+
/**
|
|
37
|
+
* Scans the CLI root directory and returns all valid tenant infos.
|
|
38
|
+
* @returns A list of TenantInfo objects found in {tenant-id}/info.json files.
|
|
39
|
+
*/
|
|
40
|
+
export declare let getAllTenantsInfo: typeof _getAllTenantsInfo;
|
|
41
|
+
export declare const unitMocks: {
|
|
42
|
+
decodeJwtPayload: typeof _decodeJwtPayload;
|
|
43
|
+
writeTenantAuthInfo: typeof _writeTenantAuthInfo;
|
|
44
|
+
readTenantAuthInfo: typeof _readTenantAuthInfo;
|
|
45
|
+
writeTenantInfo: typeof _writeTenantInfo;
|
|
46
|
+
readTenantInfo: typeof _readTenantInfo;
|
|
47
|
+
deleteTenantAuthInfo: typeof _deleteTenantAuthInfo;
|
|
48
|
+
getAllTenantsInfo: typeof _getAllTenantsInfo;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Get the full path to the tenant-specific folder.
|
|
52
|
+
* @param {string} tenantId - The tenant ID.
|
|
53
|
+
* @returns The absolute path to the tenant directory.
|
|
54
|
+
*/
|
|
55
|
+
export declare function getTenantPath(tenantId: string): string;
|
|
56
|
+
declare function _writeTenantAuthInfo(tenantId: string, authInfo: TenantAuth): Promise<void>;
|
|
57
|
+
declare function _readTenantAuthInfo(tenantId: string): Promise<TenantAuth | null>;
|
|
58
|
+
declare function _writeTenantInfo(info: TenantInfo): Promise<void>;
|
|
59
|
+
declare function _readTenantInfo(tenantId: string): Promise<TenantInfo | null>;
|
|
60
|
+
declare function _deleteTenantAuthInfo(tenantId: string): Promise<void>;
|
|
61
|
+
declare function _getAllTenantsInfo(): TenantInfo[];
|
|
62
|
+
declare function _decodeJwtPayload(token: string): Record<string, any> | null;
|
|
63
|
+
export {};
|
package/types/tools/index.d.ts
CHANGED
|
@@ -2,4 +2,6 @@ export { generateSites, GenerateSitesConfig } from './generateSites';
|
|
|
2
2
|
export { generateMetadata } from './generateMetadata';
|
|
3
3
|
export { scaffoldComponent } from './scaffold';
|
|
4
4
|
export * from './templating';
|
|
5
|
-
export
|
|
5
|
+
export * from './auth/models';
|
|
6
|
+
import * as auth from './auth';
|
|
7
|
+
export { auth };
|