Read existing Client catalogs through verified workspace context
This commit is contained in:
+26
-5
@@ -21,15 +21,16 @@ function configuredUrl(value) {
|
||||
return url.href;
|
||||
}
|
||||
|
||||
function createWorkspaceResolver({ authMeUrl, clientMeUrl, fetchImpl = fetch, timeoutMs = 5000 }) {
|
||||
function createWorkspaceResolver({ authMeUrl, clientMeUrl, clientCatalogUrl, fetchImpl = fetch, timeoutMs = 5000 }) {
|
||||
const authUrl = configuredUrl(authMeUrl);
|
||||
const clientUrl = configuredUrl(clientMeUrl);
|
||||
const catalogUrl = clientCatalogUrl ? configuredUrl(clientCatalogUrl) : null;
|
||||
|
||||
async function read(url, headers) {
|
||||
async function read(url, headers, maxBytes = 256 * 1024, deadlineMs = timeoutMs) {
|
||||
let response;
|
||||
try {
|
||||
response = await fetchImpl(url, {
|
||||
method: 'GET', headers, redirect: 'error', signal: AbortSignal.timeout(timeoutMs)
|
||||
method: 'GET', headers, redirect: 'error', signal: AbortSignal.timeout(deadlineMs)
|
||||
});
|
||||
} catch {
|
||||
throw fail(503, 'IDENTITY_SERVICE_UNAVAILABLE', 'Workspace যাচাইয়ের service এখন পাওয়া যাচ্ছে না।');
|
||||
@@ -38,7 +39,17 @@ function createWorkspaceResolver({ authMeUrl, clientMeUrl, fetchImpl = fetch, ti
|
||||
throw fail(401, 'LOGIN_REQUIRED', 'আপনার Aura account-এ আবার login করুন।');
|
||||
}
|
||||
if (!response.ok) throw fail(503, 'IDENTITY_SERVICE_UNAVAILABLE', 'Workspace যাচাই করা যায়নি।');
|
||||
try { return await response.json(); }
|
||||
try {
|
||||
if (Number(response.headers?.get('content-length')) > maxBytes) throw new Error('Response limit');
|
||||
if (response.body) {
|
||||
const chunks = []; let size = 0;
|
||||
for await (const chunk of response.body) { size += chunk.length; if (size > maxBytes) throw new Error('Response limit'); chunks.push(Buffer.from(chunk)); }
|
||||
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
||||
}
|
||||
const value = await response.json();
|
||||
if (Buffer.byteLength(JSON.stringify(value)) > maxBytes) throw new Error('Response limit');
|
||||
return value;
|
||||
}
|
||||
catch { throw fail(503, 'INVALID_IDENTITY_RESPONSE', 'Workspace service সঠিক উত্তর দেয়নি।'); }
|
||||
}
|
||||
|
||||
@@ -78,7 +89,7 @@ function createWorkspaceResolver({ authMeUrl, clientMeUrl, fetchImpl = fetch, ti
|
||||
}
|
||||
const tenant = context.tenant.trim().toLowerCase();
|
||||
const clientId = context.clientId.toLowerCase();
|
||||
return {
|
||||
const workspace = {
|
||||
tenant, clientId,
|
||||
namespace: createHash('sha256').update('client:' + clientId).digest('hex'),
|
||||
userId: user.id,
|
||||
@@ -86,6 +97,16 @@ function createWorkspaceResolver({ authMeUrl, clientMeUrl, fetchImpl = fetch, ti
|
||||
canWriteProducts: context.perms?.products === true,
|
||||
displayName: typeof context.clientName === 'string' ? context.clientName : ''
|
||||
};
|
||||
if (catalogUrl) workspace.readConnectedCatalog = async () => {
|
||||
let catalog;
|
||||
try { catalog = await read(catalogUrl, { ...headers, 'x-aura-tenant': tenant }, 16 * 1024 * 1024, 35000); }
|
||||
catch { throw fail(503, 'CONNECTED_CATALOG_UNAVAILABLE', 'Connected catalog এখন পড়া যায়নি; আগের data মুছে দেওয়া হয়নি।'); }
|
||||
if (catalog?.success !== true || typeof catalog.clientId !== 'string' || catalog.clientId.toLowerCase() !== clientId || typeof catalog.tenant !== 'string' || catalog.tenant.toLowerCase() !== tenant) {
|
||||
throw fail(403, 'CATALOG_WORKSPACE_MISMATCH', 'Catalog-এর workspace মেলেনি।');
|
||||
}
|
||||
return catalog;
|
||||
};
|
||||
return workspace;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user