mrpc-js 0.1.0 → 0.1.2
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/src/mrpc.js +23 -3
package/package.json
CHANGED
package/src/mrpc.js
CHANGED
|
@@ -18,6 +18,7 @@ export async function create_mRPC(user_id, token, version, payload) {
|
|
|
18
18
|
request_tag: tag,
|
|
19
19
|
seed: seed,
|
|
20
20
|
data: payload,
|
|
21
|
+
timestamp: Date.now(),
|
|
21
22
|
auth: await auth_hash(seed, token, payload)
|
|
22
23
|
};
|
|
23
24
|
|
|
@@ -30,22 +31,25 @@ export async function create_mRPC(user_id, token, version, payload) {
|
|
|
30
31
|
* @param {Record<string, any>} response
|
|
31
32
|
* @param {string} tag
|
|
32
33
|
* @param {string} token
|
|
34
|
+
* @param {number} [window=300] - Time window for request in seconds
|
|
33
35
|
* @returns {Promise<boolean>}
|
|
34
36
|
*/
|
|
35
37
|
|
|
36
|
-
export async function validate_mRPC_response(response, tag, token) {
|
|
37
|
-
const valid_keys = ['success', 'request_tag', 'seed', 'data', 'auth'];
|
|
38
|
+
export async function validate_mRPC_response(response, tag, token, window= 300) {
|
|
38
39
|
const key_types = {
|
|
39
40
|
success: 'boolean',
|
|
41
|
+
action: 'string',
|
|
42
|
+
timestamp: 'number',
|
|
40
43
|
request_tag: 'string',
|
|
41
44
|
seed: 'string',
|
|
42
45
|
data: 'object',
|
|
43
46
|
auth: 'string'
|
|
44
47
|
}
|
|
48
|
+
const valid_keys = Object.keys(key_types);
|
|
45
49
|
|
|
46
50
|
const keys = Object.keys(response);
|
|
47
51
|
|
|
48
|
-
if (keys.length !==
|
|
52
|
+
if (keys.length !== valid_keys.length) {
|
|
49
53
|
return false;
|
|
50
54
|
}
|
|
51
55
|
|
|
@@ -62,6 +66,11 @@ export async function validate_mRPC_response(response, tag, token) {
|
|
|
62
66
|
if(response.request_tag !== tag) {
|
|
63
67
|
return false;
|
|
64
68
|
}
|
|
69
|
+
|
|
70
|
+
if(!time_check(response.timestamp, window)) {
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
65
74
|
const hash = await auth_hash(response.seed, token, response.data);
|
|
66
75
|
|
|
67
76
|
return hash === response.auth;
|
|
@@ -73,6 +82,17 @@ function random_hex(length=32) {
|
|
|
73
82
|
return Array.from(buf).map(byte => byte.toString(16).padStart(2, '0')).join('');
|
|
74
83
|
}
|
|
75
84
|
|
|
85
|
+
function time_check(timestamp, window ) {
|
|
86
|
+
const ms_window = window * 1000;
|
|
87
|
+
const now = Date.now();
|
|
88
|
+
|
|
89
|
+
if(now < (timestamp - ms_window)) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
return now <= (timestamp + ms_window);
|
|
93
|
+
|
|
94
|
+
}
|
|
95
|
+
|
|
76
96
|
async function auth_hash(seed, token, payload) {
|
|
77
97
|
const crypto_api = globalThis.crypto;
|
|
78
98
|
const data_string = JSON.stringify(payload) + seed;
|