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 | url: "https://192.168.12.154:8443/Murachi/0.1/archivos/"+signedFileId, |
---|
31 | type: "get", |
---|
32 | dataType: "json", |
---|
33 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
34 | success: function(response) { |
---|
35 | |
---|
36 | if (FIRMA_VISIBLE == true){ |
---|
37 | |
---|
38 | INFO_DATATABLE = SerealizeMyJson(response); |
---|
39 | |
---|
40 | CONT += 1; |
---|
41 | if (CONT == 1){ |
---|
42 | DATATABLE_SIGN_VISIBLE = DataTablePDF(response); |
---|
43 | |
---|
44 | } |
---|
45 | if (CONT > 1){ |
---|
46 | DATATABLE_SIGN_VISIBLE.destroy(); |
---|
47 | DATATABLE_SIGN_VISIBLE = DataTablePDF(response); |
---|
48 | } |
---|
49 | |
---|
50 | CONTAINER = false; |
---|
51 | $("#pdf-main-container").hide(); |
---|
52 | $("#myJson_wrapper").show(); |
---|
53 | } |
---|
54 | else{ |
---|
55 | |
---|
56 | INFO_DATATABLE_NO_VISIBLE = SerealizeMyJson(response); |
---|
57 | CONT_NO_VISIBLE += 1; |
---|
58 | if (CONT_NO_VISIBLE == 1){ |
---|
59 | DATATABLE_SIGN_NO_VISIBLE = DataTablePDFNoVisible(response); |
---|
60 | |
---|
61 | } |
---|
62 | if (CONT_NO_VISIBLE > 1){ |
---|
63 | DATATABLE_SIGN_NO_VISIBLE.destroy(); |
---|
64 | DATATABLE_SIGN_NO_VISIBLE = DataTablePDFNoVisible(response); |
---|
65 | } |
---|
66 | |
---|
67 | CONTAINER2 = false; |
---|
68 | $("#pdf-main-container2").hide(); |
---|
69 | $("#myJsonPDF_NO_VISIBLE_wrapper").show(); |
---|
70 | |
---|
71 | |
---|
72 | } |
---|
73 | |
---|
74 | }, |
---|
75 | error: function(jqXHR, textStatus, errorThrown){ |
---|
76 | alert(textStatus+", "+ errorThrown+" el documento PDF para mostrar la info de la sign"); |
---|
77 | } |
---|
78 | }); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | // Cuarto paso (Se envia la información del token para terminar la firma) |
---|
83 | function FinalizarFirma(signature){ |
---|
84 | |
---|
85 | $.ajax({ |
---|
86 | type: 'POST', |
---|
87 | contentType: 'application/json', |
---|
88 | url:"https://192.168.12.154:8443/Murachi/0.1/archivos/pdfs/resenas", |
---|
89 | //url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas", |
---|
90 | dataType: 'json', |
---|
91 | data: JSON.stringify({"signature":signature}), |
---|
92 | xhrFields: {withCredentials: true}, |
---|
93 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
94 | success: function(data, textStatus, jqXHR){ |
---|
95 | |
---|
96 | //var linkToDownload = "<a href=\"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\"><h4>Descargar archivo firmado</h4></a>"; |
---|
97 | var linkToDownload = "<a href=\"https://192.168.12.154:8443/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\"><h4>Descargar archivo firmado</h4></a>"; |
---|
98 | document.getElementById("log").innerHTML = ''; |
---|
99 | document.getElementById("respuesta").innerHTML = '<center><h2>Archivo firmado correctamente:</h2> <br>'+linkToDownload+'</center>'; |
---|
100 | |
---|
101 | InfoDocumentDataTablePDF(data['signedFileId']); |
---|
102 | |
---|
103 | }, |
---|
104 | error: function(jqXHR, textStatus, errorThrown){ |
---|
105 | alert('error en pdfs/resenas: ' + textStatus); |
---|
106 | $("#respuesta").html("error en pdfs/resenas: " + textStatus); |
---|
107 | } |
---|
108 | }); |
---|
109 | |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | //Tercer paso (Obtenemos el hash de pdf enviado por el servidor y luego procesa la información en el token) |
---|
114 | function ObtenerHashPDFServer(parameters,cert){ |
---|
115 | |
---|
116 | $.ajax({ |
---|
117 | type: 'POST', |
---|
118 | contentType: 'application/json', |
---|
119 | url:"https://192.168.12.154:8443/Murachi/0.1/archivos/pdfs2", |
---|
120 | //url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs", |
---|
121 | dataType: "json", |
---|
122 | data: parameters, |
---|
123 | xhrFields: {withCredentials: true}, |
---|
124 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
125 | success: function(data, textStatus, jqXHR){ |
---|
126 | var json_x = data; |
---|
127 | var hash = json_x['hash']; |
---|
128 | //alert("hash recibido del servidor "+hash); |
---|
129 | var hashtype = "SHA-256"; |
---|
130 | var lang = "eng"; |
---|
131 | |
---|
132 | //Procesa la información |
---|
133 | window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then( |
---|
134 | function(signature) { |
---|
135 | FinalizarFirma(signature.hex); |
---|
136 | }, |
---|
137 | function(err) { |
---|
138 | log_text("sign() failed: " + err); |
---|
139 | var error; |
---|
140 | log_text("sign() failed: " + err); |
---|
141 | if(err == "Error: user_cancel") { |
---|
142 | alert("sign() failed: El usuario cancelo la operación"); |
---|
143 | error = "El usuario cancelo la operación"; |
---|
144 | } |
---|
145 | else if(err == "Error: no_certificates") { |
---|
146 | alert("sign() failed: No hay certificado disponible"); |
---|
147 | error = "No hay certificado disponible"; |
---|
148 | } |
---|
149 | else if(err == "Error: no_implementation") { |
---|
150 | alert("sign() failed: No hay soporte para el manejo del certificado"); |
---|
151 | error = "No hay soporte para el manejo del certificado"; |
---|
152 | } |
---|
153 | |
---|
154 | //alert("sign() failed: " + err); |
---|
155 | $("#respuesta").html("sign() failed: " + error); |
---|
156 | }); |
---|
157 | |
---|
158 | }, |
---|
159 | error: function(jqXHR, textStatus, errorThrown){ |
---|
160 | //alert('error: ' + textStatus); |
---|
161 | //var responseText = jQuery.parseJSON(jqXHR.responseText); |
---|
162 | alert('ajax error function: ' + jqXHR.responseText); |
---|
163 | $("#respuesta").html("error function: " + jqXHR.responseText); |
---|
164 | } |
---|
165 | |
---|
166 | }); |
---|
167 | |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | //Segundo paso (Seleccionamos el Certificado Firmante) |
---|
172 | function ObtenerCertificadoFirmante(response){ |
---|
173 | |
---|
174 | // identificador del archivo en el servidor |
---|
175 | var fileId = response.fileId.toString(); |
---|
176 | var cert; |
---|
177 | |
---|
178 | window.hwcrypto.getCertificate({lang: "en"}).then( |
---|
179 | function(response) { |
---|
180 | var cert = response; |
---|
181 | var parameters = ""; |
---|
182 | |
---|
183 | if (FIRMA_VISIBLE == true){ |
---|
184 | parameters = JSON.stringify({ |
---|
185 | "fileId":fileId, |
---|
186 | "certificate":cert.hex, |
---|
187 | "reason":"Certificado", |
---|
188 | "location":"CENDITEL", |
---|
189 | "contact":"582746574336", |
---|
190 | "signatureVisible":"true", |
---|
191 | "signaturePage": VALOR_PAGE, |
---|
192 | "xPos": VALOR_X, |
---|
193 | "yPos": VALOR_Y |
---|
194 | }); |
---|
195 | } |
---|
196 | else{ |
---|
197 | parameters = JSON.stringify({ |
---|
198 | "fileId":fileId, |
---|
199 | "certificate":cert.hex, |
---|
200 | "reason":"Certificado", |
---|
201 | "location":"CENDITEL", |
---|
202 | "contact":"582746574336", |
---|
203 | "signatureVisible":"false", |
---|
204 | "signaturePage": "", |
---|
205 | "xPos": "", |
---|
206 | "yPos": "" |
---|
207 | }); |
---|
208 | } |
---|
209 | // ahora llamar al ajax de obtener la resena del pdf |
---|
210 | ObtenerHashPDFServer(parameters, cert); |
---|
211 | |
---|
212 | }, |
---|
213 | function(err) { |
---|
214 | log_text("getCertificate() failed: " + err); |
---|
215 | var error; |
---|
216 | if(err == "Error: user_cancel") { |
---|
217 | alert("getCertificate() failed: El usuario cancelo la operación" ); |
---|
218 | error = "El usuario cancelo la operación"; |
---|
219 | } |
---|
220 | else if(err == "Error: no_certificates") { |
---|
221 | alert("getCertificate() failed: No hay certificado disponible") ; |
---|
222 | error = "No hay certificado disponible"; |
---|
223 | } |
---|
224 | else if(err == "Error: no_implementation") { |
---|
225 | alert("getCertificate() failed: No hay soporte para el manejo del certificado"); |
---|
226 | error = "No hay soporte para el manejo del certificado"; |
---|
227 | } |
---|
228 | //alert("getCertificate() failed: " + err); |
---|
229 | $("#respuesta").html("getCertificate() failed: " + error); |
---|
230 | } |
---|
231 | |
---|
232 | ); |
---|
233 | } |
---|
234 | |
---|
235 | |
---|
236 | |
---|
237 | |
---|
238 | // Primer paso (Subir el documento al servidor) |
---|
239 | function SubirDocumentServer(formData){ |
---|
240 | |
---|
241 | $.ajax({ |
---|
242 | url: "https://192.168.12.154:8443/Murachi/0.1/archivos", |
---|
243 | //url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos", |
---|
244 | type: "post", |
---|
245 | dataType: "json", |
---|
246 | data: formData, |
---|
247 | cache: false, |
---|
248 | contentType: false, |
---|
249 | processData: false, |
---|
250 | xhrFields: {withCredentials: true}, |
---|
251 | headers: {"Authorization":"Basic YWRtaW46YWRtaW4="}, |
---|
252 | success: function(response) { |
---|
253 | |
---|
254 | document.getElementById("respuesta").innerHTML = manejoJsonPDF(JSON.stringify(response)); |
---|
255 | ObtenerCertificadoFirmante(response); |
---|
256 | }, |
---|
257 | error: function(response) { |
---|
258 | //Que se ejecuta cuando finalice la petición de con error |
---|
259 | $("#respuesta").html('Error, al subir el archivo al servidor...!!!'); |
---|
260 | alert("ocurrio un error, al subir el archivo al servidor") |
---|
261 | } |
---|
262 | }); |
---|
263 | |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | $("#Form-Format-Visible").on('submit', function(event) { |
---|
268 | event.preventDefault(); |
---|
269 | |
---|
270 | var formData = new FormData(); |
---|
271 | formData.append("upload", $("#file-sign-ft_Vble")[0].files[0]); |
---|
272 | |
---|
273 | $('#myJson_wrapper').hide(); |
---|
274 | FIRMA_VISIBLE = true; |
---|
275 | SubirDocumentServer(formData); |
---|
276 | |
---|
277 | }); |
---|