const { createHash } = require('node:crypto'); function fail(status, code, message) { return Object.assign(new Error(message), { status, code }); } function cookieValue(header, name) { const entries = String(header || '').split(';').map(item => item.trim()); const values = entries.filter(item => item.startsWith(name + '=')); if (values.length > 1) throw fail(400, 'AMBIGUOUS_COOKIE', 'একাধিক login cookie পাওয়া গেছে।'); if (!values.length) return ''; try { return decodeURIComponent(values[0].slice(name.length + 1)); } catch { throw fail(400, 'INVALID_COOKIE', 'Login cookie সঠিক নয়।'); } } function configuredUrl(value) { const url = new URL(value); if (!['http:', 'https:'].includes(url.protocol) || url.username || url.password || url.hash) { throw new Error('A trusted HTTP(S) service URL is required'); } return url.href; } function createWorkspaceResolver({ authMeUrl, clientMeUrl, fetchImpl = fetch, timeoutMs = 5000 }) { const authUrl = configuredUrl(authMeUrl); const clientUrl = configuredUrl(clientMeUrl); async function read(url, headers) { let response; try { response = await fetchImpl(url, { method: 'GET', headers, redirect: 'error', signal: AbortSignal.timeout(timeoutMs) }); } catch { throw fail(503, 'IDENTITY_SERVICE_UNAVAILABLE', 'Workspace যাচাইয়ের service এখন পাওয়া যাচ্ছে না।'); } if ([401, 403, 404].includes(response.status)) { throw fail(401, 'LOGIN_REQUIRED', 'আপনার Aura account-এ আবার login করুন।'); } if (!response.ok) throw fail(503, 'IDENTITY_SERVICE_UNAVAILABLE', 'Workspace যাচাই করা যায়নি।'); try { return await response.json(); } catch { throw fail(503, 'INVALID_IDENTITY_RESPONSE', 'Workspace service সঠিক উত্তর দেয়নি।'); } } return async function resolveWorkspace(request) { const token = cookieValue(request.headers.cookie, 'aura_access'); if (!token || token.length > 8192 || /[\r\n]/.test(token)) { throw fail(401, 'LOGIN_REQUIRED', 'আপনার Aura account-এ login করুন।'); } const identity = await read(authUrl, { Authorization: 'Bearer ' + token, Accept: 'application/json' }); const user = identity?.user; if (!user || typeof user.id !== 'string' || !user.id || user.isActive !== true) { throw fail(401, 'INACTIVE_ACCOUNT', 'সক্রিয় Aura account প্রয়োজন।'); } const url = new URL(request.url, 'http://studio.invalid'); const requested = String(request.headers['x-aura-tenant'] || url.searchParams.get('tenant') || cookieValue(request.headers.cookie, 'aura_active_tenant') || '').trim(); if (requested.length > 256 || /[\r\n]/.test(requested)) { throw fail(400, 'INVALID_WORKSPACE', 'Workspace নির্বাচন সঠিক নয়।'); } const privileged = user.role === 'owner' || user.role === 'admin'; if (privileged && !requested) { throw fail(409, 'WORKSPACE_REQUIRED', 'এই কাজের জন্য আগে একটি client workspace নির্বাচন করুন।'); } // Client's existing gate owns membership/owner-switch decisions. Never trust a supplied client ID. const headers = { Cookie: 'aura_access=' + encodeURIComponent(token), Accept: 'application/json' }; if (requested) headers['x-aura-tenant'] = requested; const context = await read(clientUrl, headers); if (context?.success !== true || typeof context.tenant !== 'string' || !context.tenant.trim() || context.tenant.length > 256) { throw fail(403, 'WORKSPACE_UNAVAILABLE', 'আপনার workspace প্রস্তুত বা অনুমোদিত নয়।'); } const tenant = context.tenant.trim().toLowerCase(); return { tenant, namespace: createHash('sha256').update(tenant).digest('hex'), userId: user.id, role: context.role, canWriteProducts: context.perms?.products === true, displayName: typeof context.clientName === 'string' ? context.clientName : '' }; }; } function requireProductWrite(workspace) { if (workspace.canWriteProducts !== true) { throw fail(403, 'CATALOG_PERMISSION_REQUIRED', 'এই workspace-এর product পরিবর্তনের অনুমতি প্রয়োজন।'); } } module.exports = { createWorkspaceResolver, requireProductWrite, fail };