godprotocol 2.3.70 → 2.3.72

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "godprotocol",
3
- "version": "2.3.70",
3
+ "version": "2.3.72",
4
4
  "description": "A distributed computing environment for Web 4.0 — integrating AI, decentralisation, and virtual computation.",
5
5
  "main": "Index.js",
6
6
  "type": "module",
@@ -8,12 +8,22 @@ class Callbacks {
8
8
  this.on_service_callbacks = new Array();
9
9
  this.after_service_callbacks = new Array();
10
10
  this.service_error_callbacks = new Array();
11
+ this.headers_rejected_callbacks = new Array();
12
+ this.headers_resolved_callbacks = new Array();
11
13
  }
12
14
 
13
15
  before(fn) {
14
16
  this.before_callbacks.push(fn);
15
17
  }
16
18
 
19
+ headers_resolved(fn) {
20
+ this.headers_resolved_callbacks.push(fn);
21
+ }
22
+
23
+ headers_rejected(fn) {
24
+ this.headers_rejected_callbacks.push(fn);
25
+ }
26
+
17
27
  on_service(fn) {
18
28
  this.on_service_callbacks.push(fn);
19
29
  }
@@ -62,6 +72,22 @@ class Callbacks {
62
72
  return { ok: true };
63
73
  };
64
74
 
75
+ handle_headers_rejected_callback = async (payload) => {
76
+ for (const callback of this.headers_rejected_callbacks) {
77
+ await callback(payload, this.gp);
78
+ }
79
+
80
+ return { ok: true };
81
+ };
82
+
83
+ handler_headers_resolved_callback = async (payload) => {
84
+ for (const callback of this.headers_resolved_callbacks) {
85
+ await callback(payload, this.gp);
86
+ }
87
+
88
+ return { ok: true };
89
+ };
90
+
65
91
  handle_on_service_callback = async (payload) => {
66
92
  for (const callback of this.on_service_callbacks) {
67
93
  const result = await callback(payload, this.gp);
@@ -72,110 +72,122 @@ class Services {
72
72
 
73
73
  if (!servic) return;
74
74
 
75
- if (opts.interface) {
76
- return servic.interface || null;
77
- }
75
+ let call = async (path, body, header) => {
76
+ let headers = {
77
+ "Content-Type": "application/json",
78
+ Accept: "application/json",
79
+ "x-api-version":
80
+ servic.local && !header?.api_version?.startsWith(`${servic.local}:`)
81
+ ? `${servic.local}:${header?.api_version || servic.api_version}`
82
+ : header?.api_version || servic.api_version || "v1",
83
+ };
78
84
 
79
- return {
80
- api_version: servic.api_version,
81
- uri: servic.uri,
82
- interface: servic.interface,
83
- call: async (path, body, header) => {
84
- let headers = {
85
- "Content-Type": "application/json",
86
- Accept: "application/json",
87
- "x-api-version":
88
- servic.local && !header?.api_version?.startsWith(`${servic.local}:`)
89
- ? `${servic.local}:${header?.api_version || servic.api_version}`
90
- : header?.api_version || servic.api_version || "v1",
91
- };
85
+ if (header?.api_key) {
86
+ headers["x-api-key"] = header.api_key;
87
+ } else if (servic.api_key) {
88
+ headers["x-api-key"] = servic.api_key;
89
+ }
90
+
91
+ if (header?.token) {
92
+ headers["Authorization"] = `Bearer ${header.token}`;
93
+ headers["x-platform"] = this.platform_uri;
94
+ } else if (servic.profile_key) {
95
+ headers["Authorization"] = `${servic.profile_key}`;
96
+ } else if (header?.profile) {
97
+ let token = await this.get_token(servic, header);
92
98
 
93
- if (header?.api_key) {
94
- headers["x-api-key"] = header.api_key;
95
- } else if (servic.api_key) {
96
- headers["x-api-key"] = servic.api_key;
99
+ if (typeof token !== "string") {
100
+ if (token?.ok === false) return token;
97
101
  }
98
102
 
99
- if (header?.token) {
100
- headers["Authorization"] = `Bearer ${header.token}`;
101
- headers["x-platform"] = this.platform_uri;
102
- } else if (servic.profile_key) {
103
- headers["Authorization"] = `${servic.profile_key}`;
104
- } else if (header?.profile) {
105
- let token = await this.get_token(servic, header);
103
+ if (token) headers["Authorization"] = `Bearer ${token}`;
104
+ headers["x-platform"] = this.platform_uri;
105
+ }
106
106
 
107
- if (typeof token !== "string") {
108
- if (token?.ok === false) return token;
109
- }
107
+ if (header?.xplatform) headers["x-platform"] = header.xplatform;
108
+
109
+ if (
110
+ !headers["x-api-key"] &&
111
+ !headers["Authorization"] &&
112
+ !servic.opened
113
+ ) {
114
+ return {
115
+ ok: false,
116
+ status_code: "service_unauthorised",
117
+ message: "Service unauthorised",
118
+ };
119
+ }
110
120
 
111
- if (token) headers["Authorization"] = `Bearer ${token}`;
112
- headers["x-platform"] = this.platform_uri;
113
- }
121
+ let url = `${servic.local ? this.gp.server.domain : servic.url}/${path}`;
122
+
123
+ let request_payload = {
124
+ service: servic,
125
+ url,
126
+ path,
127
+ headers,
128
+ body,
129
+ };
130
+ await this.callbacks.handle_on_service_callback(request_payload);
131
+
132
+ try {
133
+ let response = await fetch(url, {
134
+ method: "POST",
135
+ headers,
136
+ body: JSON.stringify(body || {}),
137
+ });
114
138
 
115
- if (header?.xplatform) headers["x-platform"] = header.xplatform;
139
+ let reply = await response.json();
116
140
 
117
- if (
118
- !headers["x-api-key"] &&
119
- !headers["Authorization"] &&
120
- !servic.opened
121
- ) {
122
- return {
141
+ if (typeof reply?.ok !== "boolean") {
142
+ reply = {
123
143
  ok: false,
124
- status_code: "service_unauthorised",
125
- message: "Service unauthorised",
144
+ message: `Response calling ${service} service at path ${path}`,
145
+ data: reply,
126
146
  };
127
147
  }
128
148
 
129
- let url = `${servic.local ? this.gp.server.domain : servic.url}/${path}`;
130
-
131
- let request_payload = {
132
- service: servic,
133
- url,
134
- path,
135
- headers,
136
- body,
149
+ await this.callbacks.handle_after_service_callback({
150
+ request_payload,
151
+ response: reply,
152
+ });
153
+
154
+ return reply;
155
+ } catch (e) {
156
+ let pload = {
157
+ ok: false,
158
+ message:
159
+ e.message || `Error calling ${service} service at path ${path}`,
160
+ status: 500,
137
161
  };
138
- await this.callbacks.handle_on_service_callback(request_payload);
139
-
140
- try {
141
- let response = await fetch(url, {
142
- method: "POST",
143
- headers,
144
- body: JSON.stringify(body || {}),
145
- });
146
-
147
- let reply = await response.json();
148
-
149
- if (typeof reply?.ok !== "boolean") {
150
- reply = {
151
- ok: false,
152
- message: `Response calling ${service} service at path ${path}`,
153
- data: reply,
154
- };
155
- }
156
-
157
- await this.callbacks.handle_after_service_callback({
158
- request_payload,
159
- response: reply,
160
- });
161
-
162
- return reply;
163
- } catch (e) {
164
- let pload = {
165
- ok: false,
166
- message:
167
- e.message || `Error calling ${service} service at path ${path}`,
168
- status: 500,
169
- };
170
- await this.callbacks.handle_error_service_callback({
171
- request_payload,
172
- reply: pload,
173
- error_object: e,
174
- });
162
+ await this.callbacks.handle_error_service_callback({
163
+ request_payload,
164
+ reply: pload,
165
+ error_object: e,
166
+ });
175
167
 
176
- return pload;
177
- }
178
- },
168
+ return pload;
169
+ }
170
+ };
171
+
172
+ let interface_;
173
+ if (servic.interface) {
174
+ interface_ = new servic.interface({
175
+ gp: this.gp,
176
+ call,
177
+ uri: servic.uri,
178
+ api_version: servic.api_version,
179
+ });
180
+ }
181
+
182
+ if (opts.interface) {
183
+ return interface_ || null;
184
+ }
185
+
186
+ return {
187
+ api_version: servic.api_version,
188
+ uri: servic.uri,
189
+ interface: interface_,
190
+ call,
179
191
  };
180
192
  };
181
193
 
package/server/utils.js CHANGED
@@ -1,4 +1,9 @@
1
- import crypto, { createHash, randomBytes, createCipheriv } from "crypto";
1
+ import crypto, {
2
+ createHash,
3
+ randomBytes,
4
+ createCipheriv,
5
+ createDecipheriv,
6
+ } from "crypto";
2
7
 
3
8
  const deriveKey = (secret) =>
4
9
  createHash("sha256").update(String(secret)).digest();
@@ -418,9 +418,29 @@ const version_middleware = async (req, res, routers) => {
418
418
  error_stage = "Header";
419
419
  securityResult = await router.handle_security(route, req, version); // was: name-based lookup inside handle_security
420
420
 
421
+ const headersCallbackPayload = {
422
+ headers: req.headers,
423
+ query,
424
+ route: name,
425
+ route_config: route,
426
+ version,
427
+ method: req.method,
428
+ };
429
+
421
430
  if (securityResult !== true) {
431
+ await route.handle_headers_rejected_callback({
432
+ ...headersCallbackPayload,
433
+ security: securityResult,
434
+ authorized: false,
435
+ });
436
+
422
437
  return respond(securityResult, res);
423
438
  }
439
+ await route.handle_headers_resolved_callback({
440
+ ...headersCallbackPayload,
441
+ security: securityResult,
442
+ authorized: true,
443
+ });
424
444
 
425
445
  // -----------------------------------------------------------------
426
446
  // BODY