flamerest 1.0.102 → 1.0.104

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/REST.js +70 -0
  2. package/package.json +1 -1
package/REST.js CHANGED
@@ -71,6 +71,9 @@ class FLAMEREST {
71
71
  responseType = 'json';
72
72
  }
73
73
 
74
+ // Подставляем сервер автоматом в запрос начинающийся с /
75
+ if (url[0] === '/' && url[1] !== '/') url = this.SERVER + url;
76
+
74
77
  let that = this;
75
78
 
76
79
  // Фетч поддерживается - получаем через него, это быстрее
@@ -108,6 +111,18 @@ class FLAMEREST {
108
111
  if (type !== 'GET')
109
112
  requestBody.body = params;
110
113
 
114
+ // Создаём подпись каждого запроса
115
+ let SIGN = "empty";
116
+ try {
117
+ SIGN = await this.hmac_sha256(requestBody.body, user_hash);
118
+ }
119
+ catch (ex) {
120
+ window.alert('Can`t create a request');
121
+ throw ex;
122
+ }
123
+
124
+ requestBody.headers['sign'] = SIGN;
125
+
111
126
  // Делаем запрос
112
127
  let response = await fetch(url, requestBody);
113
128
 
@@ -750,6 +765,61 @@ class FLAMEREST {
750
765
  return result;
751
766
  }
752
767
 
768
+ /**
769
+ *
770
+ * @param {string} data
771
+ * @param {string} key
772
+ * @returns
773
+ */
774
+ async hmac_sha256(message, secret_key) {
775
+
776
+ if (!(typeof data === 'string') && !(typeof key === 'string'))
777
+ throw new TypeError('Expected string input data.')
778
+
779
+ const enc = new TextEncoder();
780
+ const encodedKey = enc.encode(secret_key);
781
+ const encodedMessage = enc.encode(message);
782
+
783
+ const data = encodedMessage;
784
+ const key = encodedKey;
785
+
786
+ if (!(data instanceof Uint8Array) && !(key instanceof Uint8Array))
787
+ throw new TypeError('Expected Uint8Array input data.')
788
+
789
+
790
+ if (typeof window === 'undefined' && typeof Deno === 'undefined') {
791
+ const { createHmac } = await import('crypto')
792
+ return Uint8Array.from([...createHmac('SHA256', key).update(data).digest()])
793
+ } else {
794
+
795
+ if (typeof window.crypto.subtle === 'undefined') throw "Can`t create hash";
796
+
797
+ return window.crypto.subtle
798
+ .importKey(
799
+ 'raw',
800
+ key,
801
+ { name: 'HMAC', hash: { name: 'SHA-256' } },
802
+ false,
803
+ ['sign', 'verify']
804
+ )
805
+ .then(key => window.crypto.subtle.sign('HMAC', key, data))
806
+ .then(signature => {
807
+
808
+ // Преобразование ArrayBuffer в Uint8Array
809
+ const signatureArray = new Uint8Array(signature);
810
+
811
+ // Преобразование Uint8Array в строку в формате Hex
812
+ const hexString = Array.from(signatureArray)
813
+ .map(byte => byte.toString(16).padStart(2, '0'))
814
+ .join('');
815
+
816
+ return hexString;
817
+
818
+ })
819
+ }
820
+ }
821
+
822
+
753
823
 
754
824
  }
755
825
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flamerest",
3
- "version": "1.0.102",
3
+ "version": "1.0.104",
4
4
  "description": "",
5
5
  "main": "REST.js",
6
6
  "typings": "./REST.d.ts",