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 +1 -1
- package/server/Routable/Callbacks.js +26 -0
- package/server/Routable/Services.js +102 -90
- package/server/utils.js +6 -1
- package/server/version_control.js +20 -0
package/package.json
CHANGED
|
@@ -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
|
-
|
|
76
|
-
|
|
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
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
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 (
|
|
94
|
-
|
|
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 (
|
|
100
|
-
|
|
101
|
-
|
|
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
|
-
|
|
108
|
-
|
|
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
|
-
|
|
112
|
-
|
|
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
|
-
|
|
139
|
+
let reply = await response.json();
|
|
116
140
|
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
!headers["Authorization"] &&
|
|
120
|
-
!servic.opened
|
|
121
|
-
) {
|
|
122
|
-
return {
|
|
141
|
+
if (typeof reply?.ok !== "boolean") {
|
|
142
|
+
reply = {
|
|
123
143
|
ok: false,
|
|
124
|
-
|
|
125
|
-
|
|
144
|
+
message: `Response calling ${service} service at path ${path}`,
|
|
145
|
+
data: reply,
|
|
126
146
|
};
|
|
127
147
|
}
|
|
128
148
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
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.
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
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, {
|
|
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
|