mrpc-js 0.1.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/mrpc.js +20 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mrpc-js",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Zero-dependency mRPC request encoder and HMAC-SHA256 response validator",
5
5
  "keywords": [
6
6
  "mrpc",
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,13 +31,15 @@ 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) {
38
+ export async function validate_mRPC_response(response, tag, token, window= 300) {
37
39
  const key_types = {
38
40
  success: 'boolean',
39
41
  action: 'string',
42
+ timestamp: 'number',
40
43
  request_tag: 'string',
41
44
  seed: 'string',
42
45
  data: 'object',
@@ -63,6 +66,11 @@ export async function validate_mRPC_response(response, tag, token) {
63
66
  if(response.request_tag !== tag) {
64
67
  return false;
65
68
  }
69
+
70
+ if(!time_check(response.timestamp, window)) {
71
+ return false;
72
+ }
73
+
66
74
  const hash = await auth_hash(response.seed, token, response.data);
67
75
 
68
76
  return hash === response.auth;
@@ -74,6 +82,17 @@ function random_hex(length=32) {
74
82
  return Array.from(buf).map(byte => byte.toString(16).padStart(2, '0')).join('');
75
83
  }
76
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
+
77
96
  async function auth_hash(seed, token, payload) {
78
97
  const crypto_api = globalThis.crypto;
79
98
  const data_string = JSON.stringify(payload) + seed;