@cocreate/api 1.26.0 → 1.29.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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. package/src/server.js +5 -50
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/api",
3
- "version": "1.26.0",
3
+ "version": "1.29.0",
4
4
  "description": "A secure, multi-tenant API execution gateway and proxy layer that dynamically resolves endpoint actions, processes AST micro-operators, handles webhooks, and manages third-party integrations with strict tenant environment isolation.",
5
5
  "keywords": [
6
6
  "api-orchestration",
@@ -54,9 +54,9 @@
54
54
  "@cocreate/actions": "^1.23.0",
55
55
  "@cocreate/crud-client": "^1.36.0",
56
56
  "@cocreate/element-prototype": "^1.34.0",
57
- "@cocreate/render": "^1.49.0",
57
+ "@cocreate/render": "^1.49.2",
58
58
  "@cocreate/socket-client": "^1.42.0",
59
- "@cocreate/utils": "^1.46.0"
59
+ "@cocreate/utils": "^1.50.3"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@cocreate/webpack": "^1.7.0"
package/src/server.js CHANGED
@@ -21,9 +21,8 @@
21
21
  // you must obtain a commercial license from CoCreate LLC.
22
22
  // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
23
23
 
24
- import crypto from 'node:crypto';
25
24
  import { URL } from 'node:url';
26
- import { getValueFromObject, objectToSearchParams, astAsync } from '@cocreate/utils';
25
+ import { getValueFromObject, objectToSearchParams, astAsync, generateJWT, verifySignature, buildAuthHeaders } from '@cocreate/utils';
27
26
 
28
27
  // Core module references bound dynamically upon initialization
29
28
  let server = null;
@@ -162,9 +161,9 @@ function createSandbox(context) {
162
161
  }
163
162
  },
164
163
  jwt: (args) => {
165
- const privateKey = args?.privateKey;
164
+ const privateKey = args?.privateKey || args?.secret;
166
165
  if (!privateKey) throw new Error("Missing cryptographic signing key inside $jwt context parameters");
167
- return generateJWT(args, privateKey);
166
+ return generateJWT(args, privateKey, args.alg || 'HS256');
168
167
  }
169
168
  };
170
169
 
@@ -449,52 +448,8 @@ export async function request(req, res) {
449
448
  }
450
449
  }
451
450
 
452
- export function generateJWT(config, secret) {
453
- try {
454
- const header = config.header || { alg: 'HS256', typ: 'JWT' };
455
- const payload = config.payload || {};
456
-
457
- const encodedHeader = Buffer.from(JSON.stringify(header)).toString('base64url');
458
- const encodedPayload = Buffer.from(JSON.stringify(payload)).toString('base64url');
459
-
460
- const signatureInput = `${encodedHeader}.${encodedPayload}`;
461
-
462
- const hmac = crypto.createHmac('sha256', secret);
463
- hmac.update(signatureInput);
464
- const signature = hmac.digest('base64url');
465
-
466
- return `${signatureInput}.${signature}`;
467
- } catch (err) {
468
- console.error('[@cocreate/api] Failed to generate JWT:', err);
469
- throw err;
470
- }
471
- }
472
-
473
- export function verifySignature(payload, signature, secret, algorithm = 'sha256') {
474
- try {
475
- if (!payload || !signature || !secret) return false;
476
-
477
- const hmac = crypto.createHmac(algorithm, secret);
478
- hmac.update(payload);
479
-
480
- const expectedSignature = hmac.digest();
481
- let receivedSignature = Buffer.isBuffer(signature) ? signature : Buffer.from(signature, 'hex');
482
-
483
- if (receivedSignature.length !== expectedSignature.length) {
484
- receivedSignature = Buffer.from(signature, 'base64');
485
- }
486
-
487
- if (receivedSignature.length !== expectedSignature.length) {
488
- console.warn('[@cocreate/api] Signature verification rejected immediately due to byte-length mismatch.');
489
- return false;
490
- }
491
-
492
- return crypto.timingSafeEqual(expectedSignature, receivedSignature);
493
- } catch (err) {
494
- console.error('[@cocreate/api] Failed signature verification:', err.message);
495
- return false;
496
- }
497
- }
451
+ // Unified re-exports from security helper module to prevent duplicate function definitions
452
+ export { generateJWT, verifySignature, buildAuthHeaders };
498
453
 
499
454
  export async function makeHttpRequest(url, options) {
500
455
  let controller, timeoutId;