1 | |
---|
2 | function True_False(data){ |
---|
3 | |
---|
4 | if (data == "true" ){ |
---|
5 | return "Verdadero"; |
---|
6 | } |
---|
7 | if (data == "false"){ |
---|
8 | return "Falso"; |
---|
9 | } |
---|
10 | |
---|
11 | } |
---|
12 | |
---|
13 | function SerealizeMyJson(data){ |
---|
14 | for (var i = 0; i < data.signatures.length; i++) { |
---|
15 | data.signatures[i].integrityCheck = True_False(data.signatures[i].integrityCheck); |
---|
16 | data.signatures[i].signerCertificateStillValid = True_False(data.signatures[i].signerCertificateStillValid); |
---|
17 | data.signatures[i].signerCertificateValidAtTimeOfSigning = True_False(data.signatures[i].signerCertificateValidAtTimeOfSigning); |
---|
18 | data.signatures[i].signatureCoversWholeDocument = True_False(data.signatures[i].signatureCoversWholeDocument); |
---|
19 | data.signatures[i].certificatesVerifiedAgainstTheKeyStore = True_False(data.signatures[i].certificatesVerifiedAgainstTheKeyStore); |
---|
20 | } |
---|
21 | return data; |
---|
22 | } |
---|
23 | |
---|
24 | |
---|
25 | //Obtenemos la informacion de documento |
---|
26 | function InfoDocumentDataTablePDF(signedFileId){ |
---|
27 | |
---|
28 | $.ajax({ |
---|
29 | url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/"+signedFileId, |
---|
30 | type: "get", |
---|
31 | dataType: "json", |
---|
32 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
33 | success: function(response) { |
---|
34 | |
---|
35 | INFO_DATATABLE = SerealizeMyJson(response); |
---|
36 | DataTablePDF(response); |
---|
37 | }, |
---|
38 | error: function(jqXHR, textStatus, errorThrown){ |
---|
39 | alert(textStatus+", "+ errorThrown+" el documento PDF para mostrar la info de la sign"); |
---|
40 | } |
---|
41 | }); |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | // Cuarto paso (Se envia la información del token para terminar la firma) |
---|
46 | function FinalizarFirma(signature){ |
---|
47 | |
---|
48 | $.ajax({ |
---|
49 | type: 'POST', |
---|
50 | contentType: 'application/json', |
---|
51 | //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs/resenas", |
---|
52 | url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas", |
---|
53 | dataType: 'json', |
---|
54 | data: JSON.stringify({"signature":signature.hex}), |
---|
55 | xhrFields: {withCredentials: true}, |
---|
56 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
57 | success: function(data, textStatus, jqXHR){ |
---|
58 | |
---|
59 | alert('Archivo firmado correctamente: ' + data['signedFileId']); |
---|
60 | var linkToDownload = "<a href=\"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\">descargar archivo firmado</a>"; |
---|
61 | //alert(linkToDownload); |
---|
62 | document.getElementById("respuesta").innerHTML = linkToDownload; |
---|
63 | |
---|
64 | InfoDocumentDataTablePDF(data['signedFileId']); |
---|
65 | |
---|
66 | }, |
---|
67 | error: function(jqXHR, textStatus, errorThrown){ |
---|
68 | alert('error en pdfs/resenas: ' + textStatus); |
---|
69 | $("#respuesta").html("error en pdfs/resenas: " + textStatus); |
---|
70 | } |
---|
71 | }); |
---|
72 | |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | //Tercer paso (Obtenemos el hash de pdf enviado por el servidor y luego procesa la información en el token) |
---|
77 | function ObtenerHashPDFServer(parameters){ |
---|
78 | |
---|
79 | $.ajax({ |
---|
80 | type: 'POST', |
---|
81 | contentType: 'application/json', |
---|
82 | //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs", |
---|
83 | url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs", |
---|
84 | dataType: "json", |
---|
85 | data: parameters, |
---|
86 | xhrFields: {withCredentials: true}, |
---|
87 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
88 | success: function(data, textStatus, jqXHR){ |
---|
89 | var json_x = data; |
---|
90 | var hash = json_x['hash']; |
---|
91 | //alert("hash recibido del servidor "+hash); |
---|
92 | var hashtype = "SHA-256"; |
---|
93 | var lang = "eng"; |
---|
94 | |
---|
95 | //Procesa la información |
---|
96 | window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then( |
---|
97 | function(signature) { |
---|
98 | FinalizarFirma(signature.hex); |
---|
99 | }, |
---|
100 | function(err) { |
---|
101 | log_text("sign() failed: " + err); |
---|
102 | var error; |
---|
103 | log_text("sign() failed: " + err); |
---|
104 | if(err == "Error: user_cancel") { |
---|
105 | alert("sign() failed: El usuario cancelo la operación"); |
---|
106 | error = "El usuario cancelo la operación"; |
---|
107 | } |
---|
108 | else if(err == "Error: no_certificates") { |
---|
109 | alert("sign() failed: No hay certificado disponible"); |
---|
110 | error = "No hay certificado disponible"; |
---|
111 | } |
---|
112 | else if(err == "Error: no_implementation") { |
---|
113 | alert("sign() failed: No hay soporte para el manejo del certificado"); |
---|
114 | error = "No hay soporte para el manejo del certificado"; |
---|
115 | } |
---|
116 | |
---|
117 | //alert("sign() failed: " + err); |
---|
118 | $("#respuesta").html("sign() failed: " + error); |
---|
119 | }); |
---|
120 | |
---|
121 | }, |
---|
122 | error: function(jqXHR, textStatus, errorThrown){ |
---|
123 | //alert('error: ' + textStatus); |
---|
124 | //var responseText = jQuery.parseJSON(jqXHR.responseText); |
---|
125 | alert('ajax error function: ' + jqXHR.responseText); |
---|
126 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
127 | } |
---|
128 | |
---|
129 | }); |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | //Segundo paso (Seleccionamos el Certificado Firmante) |
---|
135 | function ObtenerCertificadoFirmante(response){ |
---|
136 | |
---|
137 | // identificador del archivo en el servidor |
---|
138 | var fileId = response.fileId.toString(); |
---|
139 | var cert; |
---|
140 | |
---|
141 | window.hwcrypto.getCertificate({lang: "en"}).then( |
---|
142 | function(response) { |
---|
143 | var cert = response; |
---|
144 | var parameters = JSON.stringify({ |
---|
145 | "fileId":fileId, |
---|
146 | "certificate":cert.hex, |
---|
147 | "reason":"Certificado", |
---|
148 | "location":"CENDITEL", |
---|
149 | "contact":"582746574336", |
---|
150 | "signatureVisible":"true" |
---|
151 | //"page": VALOR_PAGE, |
---|
152 | //"coord_x": VALOR_X, |
---|
153 | //"coord_y": VALOR_Y |
---|
154 | }); |
---|
155 | |
---|
156 | // ahora llamar al ajax de obtener la resena del pdf |
---|
157 | ObtenerHashPDFServer(parameters); |
---|
158 | |
---|
159 | }, |
---|
160 | function(err) { |
---|
161 | log_text("getCertificate() failed: " + err); |
---|
162 | var error; |
---|
163 | if(err == "Error: user_cancel") { |
---|
164 | alert("getCertificate() failed: El usuario cancelo la operación" ); |
---|
165 | error = "El usuario cancelo la operación"; |
---|
166 | } |
---|
167 | else if(err == "Error: no_certificates") { |
---|
168 | alert("getCertificate() failed: No hay certificado disponible") ; |
---|
169 | error = "No hay certificado disponible"; |
---|
170 | } |
---|
171 | else if(err == "Error: no_implementation") { |
---|
172 | alert("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
173 | error = "No hay soporte para el manejo del certificado"; |
---|
174 | } |
---|
175 | //alert("getCertificate() failed: " + err); |
---|
176 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
177 | } |
---|
178 | |
---|
179 | ); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | // Primer paso (Subir el documento al servidor) |
---|
186 | function SubirDocumentServer(formData){ |
---|
187 | |
---|
188 | $.ajax({ |
---|
189 | //url: "https://192.168.12.125:8443/Murachi/0.1/archivos", |
---|
190 | url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos", |
---|
191 | type: "post", |
---|
192 | dataType: "json", |
---|
193 | data: formData, |
---|
194 | cache: false, |
---|
195 | contentType: false, |
---|
196 | processData: false, |
---|
197 | xhrFields: {withCredentials: true}, |
---|
198 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
199 | success: function(response) { |
---|
200 | |
---|
201 | document.getElementById("respuesta").innerHTML = manejoJsonPDF(JSON.stringify(response)); |
---|
202 | ObtenerCertificadoFirmante(response); |
---|
203 | }, |
---|
204 | error: function(response) { |
---|
205 | //Que se ejecuta cuando finalice la petición de con error |
---|
206 | $("#respuesta").html('Error, al subir el archivo al servidor...!!!'); |
---|
207 | alert("ocurrio un error, al subir el archivo al servidor") |
---|
208 | } |
---|
209 | }); |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | $("#Form-Format-Visible").on('submit', function(event) { |
---|
215 | event.preventDefault(); |
---|
216 | |
---|
217 | var formData = new FormData(); |
---|
218 | formData.append("upload", $("#file-sign-ft_Vble")[0].files[0]); |
---|
219 | SubirDocumentServer(formData); |
---|
220 | }); |
---|