class Rutoken { constructor() { this.errors = [ `Расширение Rutoken не обнаружено`, `Плагин Rutoken не найден`, `Устройство Рутокен ЭЦП не обнаружено`, `Неверный PIN-код`, ]; this.error = null; this.deviceId = -1; this.retriesLeft = 0; this.certificate = {}; this.plugin = null; this.rutoken = rutoken.ready .then(() => { let isChrome = !!window.chrome; let isFirefox = typeof InstallTrigger !== "undefined"; if (isChrome || isFirefox) { console.log("Chrome and other"); return rutoken.isExtensionInstalled(); } else { return Promise.resolve(true); } }) .then((result) => { if (result) { return rutoken.isPluginInstalled(); } else { this.error = this.errors[0]; throw this.errors[0]; } }) .then((result) => { if (result) { return rutoken.loadPlugin(); } else { this.error = this.errors[1]; throw this.errors[1]; } }); let promise = Promise.resolve(); this.rutoken = this.rutoken.then((plugin) => { this.plugin = plugin; this.algorithmKey = plugin.PUBLIC_KEY_ALGORITHM_RSA; this.algorithmHash = plugin.HASH_TYPE_SHA256; this.algorithmSignatureSize = 512; promise = promise.then(() => plugin.enumerateDevices()); return promise.then((deviceIds) => { if (deviceIds.length > 0) { this.deviceId = deviceIds[0]; return plugin .getDeviceInfo( this.deviceId, plugin.TOKEN_INFO_PIN_RETRIES_LEFT ) .then((retriesLeft) => { this.retriesLeft = retriesLeft; }); } else { this.error = this.errors[2]; throw this.errors[2]; } }); }); } _login() { return (this.rutoken = this.rutoken .then(() => { let pin = prompt(`PIN-Code:`); if (pin) { return this.plugin.login(this.deviceId, pin); } else { this.error = null; } }) .catch((err) => { if (err.message == this.plugin.errorCodes.PIN_INCORRECT) { this.error = this.errors[3]; alert(this.errors[3]); } })); } registration(data) { let sequense = this._login(); sequense = sequense .then(() => { const dateOption = { year: "numeric", month: "numeric", day: "numeric", hour: "numeric", minute: "numeric", second: "numeric", }; const id = `plugin${new Date() .toLocaleString("ru", dateOption) .replace(/[\W]+/g, "")}`; const keyId = id .split("") .map((x) => x.charCodeAt(0).toString(16)) .join(":"); const options = { publicKeyAlgorithm: this.algorithmKey, signatureSize: this.algorithmSignatureSize, keyType: 0, id: keyId, }; return this.plugin.generateKeyPair( this.deviceId, null, "", options ); }) .then((keyId) => { const options = { hashAlgorithm: this.algorithmHash }; const subject = [ { rdn: "commonName", value: "RU", }, { rdn: "O", value: "АНО ВО «Гуманитарный университет»", }, { rdn: "INN", value: "6670433160", }, { rdn: "OGRN", value: "1169600000668", }, ]; const extensions = { keyUsage: ["digitalSignature"] }; return this.plugin.createPkcs10( this.deviceId, keyId, subject, extensions, options ); }) .then((pkcs10Request) => { let request = { method: "cert-gen", cms: pkcs10Request, //commonName: data.commonName, userName: data.userName, }; return fetch("/api-pki.php", { method: "POST", headers: { "Content-Type": "application/json;charset=utf-8", }, body: JSON.stringify(request), }).then((response) => response.json()); }) .then((data) => { return this.plugin.importCertificate( this.deviceId, data.cert, this.plugin.CERT_CATEGORY_USER ); }) .then((certId) => { const options = { detached: false, addUserCertificate: true, useHardwareHash: true, }; this.certId = certId; console.log("certId", certId); return this.plugin.sign( this.certId, this.certId, this.certId, false, options ); }) .then((signature) => { console.log(signature); const request = { method: "cert-complete", cms: signature }; return fetch("/api-pki.php", { method: "POST", headers: { "Content-Type": "application/json;charset=utf-8", }, body: JSON.stringify(request), }).then((response) => response.json()); }); } startAuthentification() { this.plugin.logout(this.deviceId); let sequense = this._login(); sequense.then(() => { let certs = []; this.plugin .enumerateCertificates( this.deviceId, this.plugin.CERT_CATEGORY_USER ) .then((certificates) => { return this.plugin .getCertificate(0, certificates[0]) .then(function (res) { return res; }); }) .then((certificate) => { let options = {}; return this.plugin.sign( this.deviceId, certificate, "textToSign", this.plugin.DATA_FORMAT_PLAIN, options ); }) .then((recmss) => { console.log("cert"); console.log(cms); }); }); } }