@it-enterprise/digital-signature 1.1.4 → 1.1.5

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@it-enterprise/digital-signature",
3
- "version": "1.1.4",
3
+ "version": "1.1.5",
4
4
  "description": "digital signature",
5
5
  "private": false,
6
6
  "main": "src/index.js",
package/readme.md CHANGED
@@ -102,6 +102,7 @@ import auth from '@it-enterprise/jwtauthentication';
102
102
  const ds = new DigitalSignature(
103
103
  new Models.GraphQlSettingProvider(
104
104
  "uk", // Язык ошибок
105
+ userId, // id пользователя (для сохранения ключей и предпочитаемого типа ключа)
105
106
  "https://m.it.ua/GraphQlServer/", // Адрес сервера GraphQl
106
107
  "https://m.it.ua/ws/", // Адрес веб-сервисов
107
108
  auth) // Библиотека @it-enterprise/jwtauthentication
@@ -115,6 +116,7 @@ await ds.initialise();
115
116
  const ds = new DigitalSignature(
116
117
  new Models.DefaultSettingProvider(
117
118
  "uk", // Язык ошибок
119
+ userId, // id пользователя (для сохранения ключей и предпочитаемого типа ключа)
118
120
  location.pathname + "api/ds/", // Путь к API ЕЦП
119
121
  glSign) // Значение глобального параметра GlSign в виде строки или обьекта Models.GlSign
120
122
  );
@@ -228,8 +230,6 @@ const keyInfo = await ds.readPrivateKeyKSP(
228
230
 
229
231
  ```javascript
230
232
  await ds.storePrivateKeyInfo(
231
- // Идентификатор пользователя
232
- userId,
233
233
  // Информация о ключе
234
234
  privateKeyInfo,
235
235
  // Будет ли ключ сохранён после закрытия вкладки
@@ -240,8 +240,6 @@ await ds.storePrivateKeyInfo(
240
240
 
241
241
  ```javascript
242
242
  const keyInfo = await ds.getStoredPrivateKeyInfo(
243
- // Идентификатор пользователя
244
- userId,
245
243
  // Тип ключа (необязательно)
246
244
  keyType
247
245
  ```
@@ -250,8 +248,6 @@ const keyInfo = await ds.getStoredPrivateKeyInfo(
250
248
 
251
249
  ```javascript
252
250
  await ds.removeStoredPrivateKeyInfo(
253
- // Идентификатор пользователя
254
- userId,
255
251
  // Идентификатор ключа (необязательно)
256
252
  keyId
257
253
  ```
@@ -49,6 +49,14 @@ export default class DigitalSignature {
49
49
  this._readedKey = null;
50
50
  }
51
51
 
52
+ get PRIVATE_KEY_TYPE() {
53
+ return "_PrivateKeyType";
54
+ }
55
+
56
+ get PRIVATE_KEY_INFO() {
57
+ return "_PrivateKeyInfo";
58
+ }
59
+
52
60
  /**
53
61
  * Считанный приватный ключ
54
62
  * @type {PrivateKeyInfo}
@@ -84,6 +92,8 @@ export default class DigitalSignature {
84
92
  break;
85
93
  }
86
94
 
95
+ this._preferredKeyType = type;
96
+
87
97
  await this.initialise();
88
98
  this.keyType = type;
89
99
  }
@@ -93,6 +103,7 @@ export default class DigitalSignature {
93
103
  * @returns {Promise<number>} Текущий тип библиотеки
94
104
  */
95
105
  async initialise() {
106
+
96
107
  if (!this._glSign) {
97
108
  this._glSign = await Promise.resolve(this._settingsProvider.getGlSign());
98
109
  }
@@ -111,7 +122,7 @@ export default class DigitalSignature {
111
122
 
112
123
  if (!this._euSign) {
113
124
  this._euSign = this._glSign.PreferHarware ? this._euSignKeyMedia : this._euSignFile;
114
- this.keyType = this._glSign.PreferHarware ? DigitalSignatureKeyType.Token : DigitalSignatureKeyType.File;
125
+ this.keyType = this._glSign.PreferHarware ? DigitalSignatureKeyType.Token : this._preferredKeyType;
115
126
  }
116
127
 
117
128
  const euSign = this._euSign;
@@ -582,7 +593,7 @@ export default class DigitalSignature {
582
593
  if (typeof internal !== "boolean") {
583
594
  internal = false;
584
595
  }
585
- const isNamedData = typeof data === "object";
596
+ const isNamedData = typeof data === "object" && !(data instanceof Uint8Array);
586
597
  if (!internal) {
587
598
  const hashedData = await this._euSign.HashData(this._readedKey.getHashAlgo(), isNamedData ? data.val : data, false);
588
599
  if(isNamedData) {
@@ -707,47 +718,46 @@ export default class DigitalSignature {
707
718
 
708
719
  /**
709
720
  * Сохранить ключ
710
- * @param {string} userId - id пользователя
711
721
  * @param {PrivateKeyInfo} privateKeyInfo - Ключ
712
722
  * @param {boolean} toLocalStorage - Будет ли ключ сохранён после закрытия вкладки
713
723
  */
714
- async storePrivateKeyInfo(userId, privateKeyInfo, toLocalStorage) {
724
+ async storePrivateKeyInfo(privateKeyInfo, toLocalStorage) {
715
725
  if(privateKeyInfo.ksp && privateKeyInfo.ksp == EndUserConstants.EndUserKSP.DIIA) {
716
726
  return;
717
727
  }
718
728
 
719
729
  const storage = toLocalStorage ? localStorage : sessionStorage;
720
- const keys = await this.getStoredPrivateKeyInfo(userId);
730
+ const keys = await this.getStoredPrivateKeyInfo();
721
731
 
722
732
  if(keys.length == 0 || keys.filter(key => key.id == privateKeyInfo.id).length < 1) {
723
- const keys = await this.getPrivateKeyInfoFromStorage(userId, toLocalStorage);
733
+ const keys = await this.getPrivateKeyInfoFromStorage(toLocalStorage);
724
734
  keys.push(privateKeyInfo);
725
735
  const data = await this._euSignFile.ProtectDataByPassword(JSON.stringify(keys), "", true);
726
- storage[`${userId}_PrivateKeyInfo`] = data;
736
+ storage[this._userId + this.PRIVATE_KEY_INFO] = data;
727
737
  }
728
738
  }
729
739
 
730
740
  /**
731
741
  * Получить сохранённые ключи
732
- * @param {string} userId - Id пользователя
733
742
  * @param {number?} keyType - Вид ключа
734
743
  * @returns {Array<PrivateKeyInfo>} Сохранённые ключи
735
744
  */
736
- async getStoredPrivateKeyInfo(userId, keyType) {
737
- const local = await this.getPrivateKeyInfoFromStorage(userId, true, keyType);
738
- const session = await this.getPrivateKeyInfoFromStorage(userId, false, keyType);
745
+ async getStoredPrivateKeyInfo(keyType) {
746
+ const local = await this.getPrivateKeyInfoFromStorage(true, keyType);
747
+ const session = await this.getPrivateKeyInfoFromStorage(false, keyType);
739
748
  return local.concat(session);
740
749
  }
741
750
 
742
- async getPrivateKeyInfoFromStorage(userId, fromLocalStorage, keyType) {
751
+ async getPrivateKeyInfoFromStorage(fromLocalStorage, keyType) {
743
752
  const storage = fromLocalStorage ? localStorage : sessionStorage;
753
+ const storedKeys = storage[this._userId + this.PRIVATE_KEY_INFO];
744
754
 
745
755
  let ls;
746
756
  const result = [];
747
- if(!storage[`${userId}_PrivateKeyInfo`]) {
757
+ if(!storedKeys) {
748
758
  return result;
749
759
  }
750
- const data = await this._euSignFile.UnprotectDataByPassword(storage[`${userId}_PrivateKeyInfo`], "", true);
760
+ const data = await this._euSignFile.UnprotectDataByPassword(storedKeys, "", true);
751
761
  try{
752
762
  ls = JSON.parse(data);
753
763
  if(keyType >= 0) {
@@ -785,17 +795,16 @@ export default class DigitalSignature {
785
795
 
786
796
  /**
787
797
  * Удалить сохранённые ключи
788
- * @param {string} userId - id пользователя
789
798
  * @param {string} keyId - Идентификатор ключа
790
799
  */
791
- async removeStoredPrivateKeyInfo(userId, keyId) {
800
+ async removeStoredPrivateKeyInfo(keyId) {
792
801
  if (keyId === undefined) {
793
- localStorage.removeItem(`${userId}_PrivateKeyInfo`);
794
- sessionStorage.removeItem(`${userId}_PrivateKeyInfo`);
802
+ localStorage.removeItem(this._userId + this.PRIVATE_KEY_INFO);
803
+ sessionStorage.removeItem(this._userId + this.PRIVATE_KEY_INFO);
795
804
  }
796
805
  else{
797
- const localStoredKeys = await this.getPrivateKeyInfoFromStorage(userId, true);
798
- const sessionStoredKeys = await this.getPrivateKeyInfoFromStorage(userId, false);
806
+ const localStoredKeys = await this.getPrivateKeyInfoFromStorage(true);
807
+ const sessionStoredKeys = await this.getPrivateKeyInfoFromStorage(false);
799
808
  const storage = localStoredKeys.filter(item => item.id == keyId).length > 0 ? localStorage : sessionStorage;
800
809
  const keys = storage == localStorage ? localStoredKeys : sessionStoredKeys;
801
810
  const keyIndex = keys.findIndex((element) => element.id == keyId);
@@ -803,12 +812,39 @@ export default class DigitalSignature {
803
812
  if(keyIndex > -1) {
804
813
  keys.splice(keyIndex, 1);
805
814
  const data = await this._euSignFile.ProtectDataByPassword(JSON.stringify(keys), "", true);
806
- storage[`${userId}_PrivateKeyInfo`] = data;
815
+ storage[this._userId + this.PRIVATE_KEY_INFO] = data;
807
816
  }
808
817
  }
809
818
  }
810
819
 
820
+ /**
821
+ * Получить предпочитаемый тип ключа
822
+ */
823
+ get _preferredKeyType () {
824
+ const keyType = parseInt(localStorage[this._userId + this.PRIVATE_KEY_TYPE]);
825
+ if (typeof keyType === "number" && keyType > -1) {
826
+ return keyType;
827
+ } else {
828
+ localStorage[this._userId + this.PRIVATE_KEY_TYPE] = DigitalSignatureKeyType.File;
829
+ return DigitalSignatureKeyType.File;
830
+ }
831
+ }
832
+
833
+ /**
834
+ * Задать предпочитаемый тип ключа
835
+ * @param {DigitalSignatureKeyType | number} keyType - Тип ключа
836
+ */
837
+ set _preferredKeyType (keyType) {
838
+ if (typeof keyType === "number" && keyType > -1) {
839
+ localStorage[this._userId + this.PRIVATE_KEY_TYPE] = keyType;
840
+ }
841
+ }
842
+
811
843
  get _resourses() {
812
844
  return Resourses[this._settings.language];
813
845
  }
846
+
847
+ get _userId () {
848
+ return typeof this._settings.userId === "function" ? this._settings.userId() : this._settings.userId;
849
+ }
814
850
  }
package/src/Models.js CHANGED
@@ -10,11 +10,13 @@ const LIBRARY_VERSION = "1.3.49";
10
10
  export class DigitalSignatureSettings {
11
11
  /**
12
12
  * @param {string} language - Язык. Поддержываемые значения: en, ru, uk
13
+ * @param {string} userId - id пользователя (для сохранения ключей и предпочитаемого типа ключа)
13
14
  * @param {string} httpProxyServiceURL - Ссылка на ProxyHandler
14
15
  * @param {UriCertificatesProvider | WebCalcCertificatesProvider} certificatesProvider - Список центров сертификации, или ссылка на их скачивание
15
16
  */
16
- constructor(language, httpProxyServiceURL, certificatesProvider, mssServiceURL, libraryUrl) {
17
+ constructor(language, userId, httpProxyServiceURL, certificatesProvider, mssServiceURL, libraryUrl) {
17
18
  this.language = language || "ru";
19
+ this.userId = userId;
18
20
  this.httpProxyServiceURL = httpProxyServiceURL;
19
21
  this.certificatesProvider = certificatesProvider;
20
22
  this.mssServiceURL = mssServiceURL;
@@ -28,12 +30,13 @@ export class DigitalSignatureSettings {
28
30
  export class DefaultSettingProvider {
29
31
  /**
30
32
  * @param {string} language - Язык ошибок
33
+ * @param {string | function} userId - id пользователя (для сохранения ключей и предпочитаемого типа ключа)
31
34
  * @param {string} glSign - ПГУ GlSign
32
35
  * @param {string} basePath - путь к ProxyHandler
33
36
  * @param {string} certificatesPath - путь к папке с сертификатами
34
37
  *
35
38
  */
36
- constructor(language, basePath) {
39
+ constructor(language, userId, basePath) {
37
40
  if (typeof basePath !== "string") {
38
41
  throw {
39
42
  code: EndUserError.EU_ERROR_BAD_PARAMETER,
@@ -44,6 +47,7 @@ export class DefaultSettingProvider {
44
47
  }
45
48
 
46
49
  this.language = language;
50
+ this.userId = userId;
47
51
  this.basePath = basePath;
48
52
  }
49
53
 
@@ -56,6 +60,7 @@ export class DefaultSettingProvider {
56
60
  if (!this._settings) {
57
61
  this._settings = new DigitalSignatureSettings(
58
62
  this.language,
63
+ this.userId,
59
64
  this.basePath + "/ProxyHandler",
60
65
  new UriCertificatesProvider(
61
66
  this.basePath + "/files?name=CAs.json",
@@ -76,11 +81,12 @@ export class DefaultSettingProvider {
76
81
  export class GraphQlSettingProvider {
77
82
  /**
78
83
  * @param {string} language - Язык ошибок
84
+ * @param {string | function} userId - id пользователя (для сохранения ключей и предпочитаемого типа ключа)
79
85
  * @param {string} graphQlUri - Ссылка на GraphQl сервер
80
86
  * @param {string} wsUri - Ссылка на веб-сервисы
81
87
  * @param {Object} auth - Функция для получения токена авторизации
82
88
  */
83
- constructor(language, graphQlUri, wsUri, auth) {
89
+ constructor(language, userId, graphQlUri, wsUri, auth) {
84
90
  if (typeof graphQlUri !== "string") {
85
91
  throw {
86
92
  code: EndUserError.EU_ERROR_BAD_PARAMETER,
@@ -92,6 +98,7 @@ export class GraphQlSettingProvider {
92
98
  }
93
99
 
94
100
  this.language = language;
101
+ this.userId = userId;
95
102
  this.graphQlUri = graphQlUri;
96
103
  this.wsUri = wsUri;
97
104
  this.auth = auth;
@@ -104,6 +111,7 @@ export class GraphQlSettingProvider {
104
111
  getSettings(testMode) {
105
112
  return new DigitalSignatureSettings(
106
113
  this.language,
114
+ this.userId,
107
115
  this.graphQlUri + "/api/digitalSignature/ProxyHandler",
108
116
  new GraphQlCertificatesProvider(testMode, this.graphQlUri, this.wsUri)
109
117
  );