import React from "react"; import Link from "next/link"; import { SpinnerCircular } from "spinners-react"; import moment from "moment"; import dynamic from 'next/dynamic'; import { concatSeries } from 'async'; import fileDownload from 'js-file-download'; import * as Sentry from "@sentry/nextjs"; import FormMessage from "./FormMessage"; import { downloadQuestionnaire, uploadSignedFile } from "../../../actions"; //import { generateSignature } from "../../../utils/digital_signature"; export default class DigitalCertificates extends React.Component { constructor(props) { super(props); this.state = { loading: true, certificates: [], certificates_error: null, certificate_selected: undefined, signing: false, show_all: false, }; this.cryptopro = null; } async componentDidMount() { const { company, main, head_person } = this.props; //console.log({ company, main }); //console.log("DC", "CDM"); const { CryptoPro } = await import('ruscryptojs'); this.cryptopro = new CryptoPro(); this.cryptopro.init() .then((info) => { //console.log('Initialized', info); // console.log('-'.repeat(20)); // for(let i in this.cryptopro) // { // console.log(i); // } // console.log('-'.repeat(20)); this.cryptopro.listCertificates() .then((certificates_list) => { console.log({ certificates_list }); console.log("all certificates", JSON.stringify(certificates_list)); Sentry.captureMessage("Client digital sign step, list of all digital certificates"); concatSeries(certificates_list, (certificate, callback) => { //console.log(certificate.id, certificate.name); this.cryptopro.certificateInfo(certificate.id) .then((cert) => { const errors = []; let apply = true; console.log("certificate", JSON.stringify(cert)); console.log("company", JSON.stringify(company)); //console.log({ cert }); if(cert.Subject['OGRNIP'] !== undefined && cert.Subject['OGRNIP'] !== null && cert.Subject['OGRNIP'] !== "") { if(cert.Subject['INN'] !== undefined && cert.Subject['INN'] !== null && cert.Subject['INN'] !== "") { if(main.inn !== cert.Subject['INN']) { errors.push('inn'); } } else { apply = false; } } else { if(cert.Subject['INNLE'] !== undefined && cert.Subject['INNLE'] !== null && cert.Subject['INNLE'] !== "") { if(main.inn !== cert.Subject['INNLE']) { errors.push('inn'); } } else { apply = false; } } let owner_valid = true; const fields = [ "lastname", "firstname", "middlename", ]; const cert_owner = `${ cert.Subject['SN']} ${ cert.Subject['G']}`.toUpperCase(); for(let i in fields) { if(head_person[fields[i]] !== null && head_person[fields[i]] !== "") { if(cert_owner.indexOf(head_person[fields[i]].toUpperCase().trim()) < 0) { owner_valid = false; } } } if(!owner_valid) { errors.push('name'); } if(cert.ValidToDate !== null && cert.ValidToDate !== undefined && cert.ValidToDate !== "") { if(moment(cert.ValidToDate) < moment()) { errors.push('date'); } } else { errors.push('date'); } if(!cert.IsValid) { errors.push('invalid'); } if(apply) { callback(null, [ { id: certificate.id, fields: cert.Subject, valid_to_date: moment(cert.ValidToDate).format("DD.MM.YYYY"), errors } ]); } else { callback(null, []); } }) .catch((error_certificates_info) => { Sentry.captureMessage("Client digital sign step error"); console.error({ error_certificates_info }); this.setState({ loading: false, certificates_error: "CERTIFICATES" }); }); }, (error, certificates) => { console.log("applicable certificates", JSON.stringify(certificates)); Sentry.captureMessage("Client digital sign step, list of applicable digital certificates"); if(certificates.length === 0) { this.setState({ loading: false, certificates_error: "INVALID" }); } else { const list = []; let valid_certificate_exists = false; for(let i in certificates) { if(certificates[i].errors.length === 0) { list.push(certificates[i]); valid_certificate_exists = true; } } for(let i in certificates) { if(certificates[i].errors.length !== 0) { list.push(certificates[i]); } } const update = { loading: false, certificates: list, certificates_error: null }; if(list[0] !== undefined) { if(list[0].errors.length === 0) { update.certificate_selected = list[0]; } } if(!valid_certificate_exists) { update.certificates_error = "INVALID"; } this.setState(update); } }); }) .catch((error_certificates_list) => { console.error({ error_certificates_list }); this.setState({ loading: false, certificates_error: "CERTIFICATES" }); }); }) .catch((error_init) => { console.error({ error_init }); this.setState({ loading: false, certificates_error: "NOT_INSTALLED" }); }); } _sign = () => { this.setState({ signing: true }, () => { const { company, main, onSignDigital } = this.props; const filename = `${ main.inn }_questionnaire_${ moment().format("DDMMYYYY_HHmmss") }.sig`; downloadQuestionnaire({ filename, download: false, base64: true }) .then(({ file }) => { let file_to_sign = file; const now = moment().format("DDMMYYYY_HHmmss"); this.cryptopro.signData(file_to_sign, this.state.certificate_selected.id) .then(signature => { //console.log({ signature }); let signature_file = new File([ signature ], filename); uploadSignedFile(signature_file, company.questionnaire_id, true) .then(() => { onSignDigital(); }) .catch(() => { this.setState({ signing: false }); }); }) .catch((error_sign) => { console.error({ error_sign }); this.setState({ signing: false }); }) }) .catch((e) => { console.error("exception on sign"); console.error(e); }); }); } render() { const { company, main, head_person } = this.props; const { loading, certificates, certificates_error, certificate_selected, signing, show_all } = this.state; //console.log("render()", { certificates }); console.log({ certificates_error }); if(loading) { return (
Список сертификатов, доступных на Вашем компьютере: