flamerest 1.0.103 → 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 +67 -0
  2. package/package.json +1 -1
package/REST.js CHANGED
@@ -111,6 +111,18 @@ class FLAMEREST {
111
111
  if (type !== 'GET')
112
112
  requestBody.body = params;
113
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
+
114
126
  // Делаем запрос
115
127
  let response = await fetch(url, requestBody);
116
128
 
@@ -753,6 +765,61 @@ class FLAMEREST {
753
765
  return result;
754
766
  }
755
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
+
756
823
 
757
824
  }
758
825
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flamerest",
3
- "version": "1.0.103",
3
+ "version": "1.0.104",
4
4
  "description": "",
5
5
  "main": "REST.js",
6
6
  "typings": "./REST.d.ts",