1 | /** |
---|
2 | * Función que se conecta al servicio web de verificación de la firma electrónica captura el evento submit y toma el |
---|
3 | * formulario para firmar. |
---|
4 | */ |
---|
5 | $(function(){ |
---|
6 | $("#firmar").on("submit", function(e) { |
---|
7 | e.preventDefault(); |
---|
8 | var f = $(this); |
---|
9 | |
---|
10 | /** FIRMA DE PDF |
---|
11 | * chequea si el botón radio PDF esta seleccionado y entra a la sección en caso contrario va al otro if |
---|
12 | * (botón radio BDOC) |
---|
13 | */ |
---|
14 | if(document.SignFormat.optradio[0].checked) { |
---|
15 | |
---|
16 | var formData = new FormData(); |
---|
17 | formData.append("upload", $("#file-sign")[0].files[0]); |
---|
18 | console.log($("#file-sign")[0].files[0]); |
---|
19 | |
---|
20 | $.ajax({ |
---|
21 | url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos", |
---|
22 | type: "post", |
---|
23 | dataType: "json", |
---|
24 | data: formData, |
---|
25 | cache: false, |
---|
26 | contentType: false, |
---|
27 | xhrFields: { withCredentials: true }, |
---|
28 | processData: false, |
---|
29 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
30 | success: function(response) { |
---|
31 | var html = manejoJsonPDF(JSON.stringify(response)); |
---|
32 | document.getElementById("respuesta").innerHTML = html; |
---|
33 | |
---|
34 | // identificador del archivo en el servidor |
---|
35 | var fileId = response.fileId.toString(); |
---|
36 | |
---|
37 | // seleccionar certificado del firmante |
---|
38 | var cert; |
---|
39 | window.hwcrypto.getCertificate({lang: "en"}).then( |
---|
40 | function(response) { |
---|
41 | var cert = response; |
---|
42 | console.log(cert); |
---|
43 | console.log(cert.hex); |
---|
44 | var parameters = JSON.stringify({ |
---|
45 | "fileId":fileId, |
---|
46 | "certificate":cert.hex, |
---|
47 | "reason":"Certificado", |
---|
48 | "location":"CENDITEL", |
---|
49 | "contact":"582746574336", |
---|
50 | "signatureVisible":"true" |
---|
51 | }); |
---|
52 | |
---|
53 | // ajax para obtener la resena del pdf |
---|
54 | $.ajax({ |
---|
55 | type: 'POST', |
---|
56 | contentType: 'application/json', |
---|
57 | url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs", |
---|
58 | dataType: "json", |
---|
59 | data: parameters, |
---|
60 | xhrFields: { withCredentials: true }, |
---|
61 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
62 | success: function(data, textStatus, jqXHR) { |
---|
63 | var json_x = data; |
---|
64 | console.log("***** hash data ******"); |
---|
65 | console.log(data); |
---|
66 | console.log("***** hash data ******") |
---|
67 | var hash = json_x['hash']; |
---|
68 | console.log("****** json_x[hash] ******"); |
---|
69 | console.log(hash); |
---|
70 | console.log("****** json_x[hash] *****"); |
---|
71 | var hashtype = "SHA-256"; |
---|
72 | var lang = "eng"; |
---|
73 | window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then( |
---|
74 | function(signature) { |
---|
75 | var firmaEnArray8 = new Uint8Array(signature.value); |
---|
76 | var firma = buf2hex(firmaEnArray8); |
---|
77 | console.log("******* Signature.hex *******"); |
---|
78 | console.log(signature); |
---|
79 | console.log(signature.value); |
---|
80 | console.log(firmaEnArray8); |
---|
81 | console.log(firma); |
---|
82 | console.log(signature.hex); |
---|
83 | console.log("******* Signature.hex *******"); |
---|
84 | $.ajax({ |
---|
85 | type: 'POST', |
---|
86 | contentType: 'application/json', |
---|
87 | url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas", |
---|
88 | dataType: 'json', |
---|
89 | data: JSON.stringify({"signature":signature.hex}), |
---|
90 | xhrFields: { withCredentials: true }, |
---|
91 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
92 | success: function(data, textStatus, jqXHR){ |
---|
93 | alert('Archivo firmado correctamente: ' + data['signedFileId']); |
---|
94 | var linkToDownload = "<a class='btn btn-primary' href=\"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\"> Descargar archivo firmado </a>"; |
---|
95 | document.getElementById("respuesta").innerHTML = linkToDownload; |
---|
96 | }, |
---|
97 | error: function(jqXHR, textStatus, errorThrown) { |
---|
98 | alert('error en pdfs/resenas: ' + textStatus); |
---|
99 | $("#respuesta").html("error en pdfs/resenas: " + textStatus); |
---|
100 | } |
---|
101 | }); |
---|
102 | }, |
---|
103 | function(err) { |
---|
104 | log_text("sign() failed: " + err); |
---|
105 | var error; |
---|
106 | log_text("sign() failed: " + err); |
---|
107 | if(err == "Error: user_cancel") { |
---|
108 | alert("sign() failed: El usuario cancelo la operación"); |
---|
109 | error = "El usuario cancelo la operación"; |
---|
110 | } |
---|
111 | else if(err == "Error: no_certificates") { |
---|
112 | alert("sign() failed: No hay certificado disponible"); |
---|
113 | error = "No hay certificado disponible"; |
---|
114 | } |
---|
115 | else if(err == "Error: no_implementation") { |
---|
116 | alert("sign() failed: No hay soporte para el manejo del certificado"); |
---|
117 | error = "No hay soporte para el manejo del certificado"; |
---|
118 | } |
---|
119 | |
---|
120 | $("#respuesta").html("sign() failed: " + error); |
---|
121 | } |
---|
122 | ); |
---|
123 | }, |
---|
124 | error: function(jqXHR, textStatus, errorThrown) { |
---|
125 | alert('ajax error function: ' + jqXHR.responseText); |
---|
126 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
127 | } |
---|
128 | }); |
---|
129 | }, |
---|
130 | function(err) { |
---|
131 | log_text("getCertificate() failed: " + err); |
---|
132 | var error; |
---|
133 | if(err == "Error: user_cancel") { |
---|
134 | alert("getCertificate() failed: El usuario cancelo la operación" ); |
---|
135 | error = "El usuario cancelo la operación"; |
---|
136 | } |
---|
137 | else if(err == "Error: no_certificates") { |
---|
138 | alert("getCertificate() failed: No hay certificado disponible") ; |
---|
139 | error = "No hay certificado disponible"; |
---|
140 | } |
---|
141 | else if(err == "Error: no_implementation") { |
---|
142 | alert("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
143 | error = "No hay soporte para el manejo del certificado"; |
---|
144 | } |
---|
145 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
146 | } |
---|
147 | ); |
---|
148 | }, |
---|
149 | error: function(response) { |
---|
150 | //Que se ejecuta cuando finalice la petición de con error |
---|
151 | $("#respuesta").html('Error...!!!'); |
---|
152 | alert("ocurrio un error") |
---|
153 | } |
---|
154 | }); |
---|
155 | } |
---|
156 | |
---|
157 | /** FIRMA DE BDOC |
---|
158 | * chequea si el botón radio BDOC esta seleccionado y entra |
---|
159 | */ |
---|
160 | if(document.SignFormat.optradio[1].checked) { |
---|
161 | var formData = new FormData(); |
---|
162 | var fileInput = document.getElementById("file-sign"); |
---|
163 | var list = fileInput.files; |
---|
164 | for (var i=0; i < list.length; i++) { |
---|
165 | formData.append("upload", $("#file-sign")[0].files[i]); |
---|
166 | } |
---|
167 | $.ajax({ |
---|
168 | url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/bdocs/cargas", |
---|
169 | type: "post", |
---|
170 | dataType: "json", |
---|
171 | data: formData, |
---|
172 | cache: false, |
---|
173 | contentType: false, |
---|
174 | xhrFields: { withCredentials: true }, |
---|
175 | processData: false, |
---|
176 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
177 | success: function(response) { |
---|
178 | |
---|
179 | var fileId = response.containerId.toString(); |
---|
180 | var cert; // seleccionar certificado del firmante |
---|
181 | |
---|
182 | window.hwcrypto.getCertificate({lang: "en"}).then( |
---|
183 | function(response) { |
---|
184 | var cert = response; |
---|
185 | var parameters = JSON.stringify({ |
---|
186 | "fileId":fileId, |
---|
187 | "certificate":cert.hex, |
---|
188 | "city":"Merida", |
---|
189 | "state":"Merida", |
---|
190 | "postalCode":"5101", |
---|
191 | "country":"Venezuela", |
---|
192 | "role":"Analista", |
---|
193 | "addSignature":true |
---|
194 | }); |
---|
195 | |
---|
196 | // ajax para obtener la resena del bdoc |
---|
197 | $.ajax({ |
---|
198 | type: 'POST', |
---|
199 | contentType: 'application/json', |
---|
200 | url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/bdocs/firmas/pre", |
---|
201 | dataType: "json", |
---|
202 | data: parameters, |
---|
203 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
204 | xhrFields: { withCredentials: true }, |
---|
205 | success: function(data, textStatus, jqXHR) { |
---|
206 | var json_x = data; |
---|
207 | var hash = json_x['hash']; |
---|
208 | var hashtype = "SHA-256"; |
---|
209 | var lang = "eng"; |
---|
210 | |
---|
211 | window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then( |
---|
212 | function(signature) { |
---|
213 | $.ajax({ |
---|
214 | type: 'POST', |
---|
215 | contentType: 'application/json', |
---|
216 | url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/bdocs/firmas/post", |
---|
217 | dataType: 'json', |
---|
218 | data: JSON.stringify({"signature":signature.hex, "containerId":fileId}), |
---|
219 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
220 | xhrFields: { withCredentials: true }, |
---|
221 | success: function(data, textStatus, jqXHR) { |
---|
222 | alert('Archivo firmado correctamente: ' + data['signedFileId']); |
---|
223 | var linkToDownload = "<a href=\"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\">descargar archivo firmado</a>"; |
---|
224 | document.getElementById("respuesta").innerHTML = linkToDownload; |
---|
225 | }, |
---|
226 | error: function(jqXHR, textStatus, errorThrown){ |
---|
227 | alert('error en /bdocs/resenas: ' + textStatus); |
---|
228 | $("#respuesta").html("error en /bdocs/resenas: " + textStatus); |
---|
229 | } |
---|
230 | }); |
---|
231 | }, |
---|
232 | function(err) { |
---|
233 | log_text("sign() failed: " + err); |
---|
234 | var error; |
---|
235 | if(err == "Error: user_cancel") { |
---|
236 | alert("sign() failed: El usuario cancelo la operación"); |
---|
237 | error = "El usuario cancelo la operación"; |
---|
238 | } |
---|
239 | else if(err == "Error: no_certificates") { |
---|
240 | alert("sign() failed: No hay certificado disponible"); |
---|
241 | error = "No hay certificado disponible"; |
---|
242 | } |
---|
243 | else if(err == "Error: no_implementation") { |
---|
244 | alert("sign() failed: No hay soporte para el manejo del certificado"); |
---|
245 | error = "No hay soporte para el manejo del certificado"; |
---|
246 | } |
---|
247 | $("#respuesta").html("sign() failed: " + error); |
---|
248 | } |
---|
249 | ); |
---|
250 | }, |
---|
251 | error: function(jqXHR, textStatus, errorThrown) { |
---|
252 | alert('ajax error function: ' + jqXHR.responseText); |
---|
253 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
254 | } |
---|
255 | }); |
---|
256 | }, |
---|
257 | function(err) { |
---|
258 | log_text("getCertificate() failed: " + err); |
---|
259 | var error; |
---|
260 | if (err == "Error: user_cancel") { |
---|
261 | alert("getCertificate() failed: El usuario cancelo la operación"); |
---|
262 | error = "El usuario cancelo la operación"; |
---|
263 | } |
---|
264 | else if(err == "Error: no_certificates") { |
---|
265 | alert("getCertificate() failed: No hay certificado disponible"); |
---|
266 | error = "No hay certificado disponible"; |
---|
267 | } |
---|
268 | else if (err == "Error: no_implementation") { |
---|
269 | alert("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
270 | error = "No hay soporte para el manejo del certificado"; |
---|
271 | } |
---|
272 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
273 | } |
---|
274 | ); |
---|
275 | |
---|
276 | }, |
---|
277 | error: function(xhr, status, error) { |
---|
278 | // Se ejecuta cuando finalice la petición de con error |
---|
279 | $("#respuesta").html('Error...!!!'); |
---|
280 | alert(xhr.responseText); |
---|
281 | alert(error); |
---|
282 | alert("ocurrio un error en cargas"); |
---|
283 | } |
---|
284 | }); |
---|
285 | } |
---|
286 | }); |
---|
287 | }); |
---|