function HermesFdaSvc(encryptionRequired, baseUrl, pluginContainerId) { this.servicio = new HermesMsg(baseUrl); this.servicio.encryptionRequired = encryptionRequired; this.plugin = new HermesFDAPlugin(pluginContainerId); this.ListarCertificados = 1; this.ObtenerCertificado = 2; this.FirmarDigitalmente = 3; this.Certificados = null; this.CertificadosAutorizados = null; this.ServicioDetenido = null; this.CertificadosListos = null; this.Aviso = null; this.pin = ""; } HermesFdaSvc.prototype.Inicializar = function (callback, callbackError) { this.plugin.render(); var obj = this; // El método ProcesarComando va a responder los mensajes // recibidos this.servicio.MensajeListo = function(mensaje) { obj.ProcesarComando(mensaje); } this.servicio.ServicioDetenido = function () { if (obj.ServicioDetenido != null) { obj.ServicioDetenido(); } } // Obtener de una vez los certificados window.setTimeout( function() { obj.plugin.getDigitalSignatureCertificates( function (certsStr) { obj.Certificados = certsStr; obj.CertificadosAutorizados = certsStr; if (obj.CertificadosListos != null) { try { obj.CertificadosListos(obj); } catch(e) { } } } ); }, 500 ); } HermesFdaSvc.prototype.Arrancar = function (callback, callbackError) { return this.servicio.RegistrarServicio(callback, callbackError); } HermesFdaSvc.prototype.Detener = function (callback, callbackError) { return this.servicio.DetenerServicio(callback, callbackError); } HermesFdaSvc.prototype.ProcesarComando = function (mensaje) { var comando = JSON.parse(mensaje.Mensaje); // Si el pin cambio borrar el pin anterior y poner el nuevo if (this.pin != comando.Pin) { for(var i = 0; i < this.pin.length; i++) { this.plugin.getPlugin().deletePinChar(); } for(var i = 0; i < comando.Pin.length; i++) { this.plugin.getPlugin().processPinChar(comando.Pin.charAt(i)); } this.pin = comando.Pin; } if (comando.Codigo == this.ListarCertificados) { this.EjecutarComandoListarCertificados(mensaje, comando.Argumento); } else if (comando.Codigo == this.ObtenerCertificado) { this.EjecutarComandoObtenerCertificado(mensaje, comando.Argumento); } else if (comando.Codigo == this.FirmarDigitalmente) { this.EjecutarComandoFirmarDigitalmente(mensaje, comando.Argumento); } } HermesFdaSvc.prototype.ServicioId = function () { return this.servicio.ServicioId(); } HermesFdaSvc.prototype.EjecutarComandoListarCertificados = function (mensaje, argumento) { this.Avisar("Listar certificados"); var respuesta = this.CrearRespuestaBase(); respuesta.Certificados = this.CertificadosAutorizados; this.servicio.ResponderMensaje(mensaje, JSON.stringify(respuesta)); } HermesFdaSvc.prototype.EjecutarComandoObtenerCertificado = function (mensaje, argumento) { this.Avisar("Obtener certificado " + argumento); var obj = this; this.plugin.getCertificate( argumento, function (certificado) { var respuesta = obj.CrearRespuestaBase(); respuesta.Certificado = certificado; obj.servicio.ResponderMensaje(mensaje, JSON.stringify(respuesta)); } ); } HermesFdaSvc.prototype.EjecutarComandoFirmarDigitalmente = function (mensaje, argumento) { var obj = this; this.Avisar("Firmar digitalmente con " + argumento.certId); this.plugin.signPkcs1( argumento.certId, argumento.digest, function (pkcs1) { var respuesta = obj.CrearRespuestaBase(); respuesta.Pkcs1 = pkcs1; obj.servicio.ResponderMensaje(mensaje, JSON.stringify(respuesta)); obj.pin = ""; obj.plugin.setPin(""); } ); } HermesFdaSvc.prototype.CrearRespuestaBase = function () { var respuesta = { isPinMissing: this.plugin.isPinMissing(), isPinInvalid: this.plugin.isPinInvalid(), pinRetryCount: this.plugin.pinRetryCount(), pinModuleName: this.plugin.pinModuleName(), pinNeeded: this.plugin.pinNeeded(), isInstalled: this.plugin.isInstalled(), isUpdated: this.plugin.isUpdated(), pinLength: this.plugin.getPlugin().pinLength, lastError: this.plugin.getPlugin().lastError, lastErrorMessage: this.plugin.getPlugin().lastErrorMessage, lastLowLevelError: this.plugin.getPlugin().lastLowLevelError }; return respuesta; } HermesFdaSvc.prototype.Avisar = function (msg) { if (this.Aviso != null) { this.Aviso(msg); } }