godprotocol 2.3.70 → 2.3.71
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/Services.js +102 -90
- package/server/utils.js +6 -1
package/package.json
CHANGED
|
@@ -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();
|