1 /*
  2  * This is security cordova_plugin (TV specific API).
  3  * Apache License (2004). See http://www.apache.org/licenses/LICENSE-2.0
  4  *
  5  * Copyright (c) 2014, LG Electronics, Inc.
  6  */
  7 
  8 /**
  9  * This represents the security API itself, and provides a global namespace for operating security service.
 10  * @class
 11  */
 12 cordova.define('cordova/plugin/security', function (require, exports, module) {
 13     
 14     function log(msg) {
 15     //    //console.log
 16     }
 17     
 18     var service;
 19     if (window.PalmSystem) {
 20         log("Window.PalmSystem Available");
 21         service = require('cordova/plugin/webos/service');
 22     } else {
 23         service = {
 24             Request : function(uri, params) {
 25                 log(uri + " invoked. But I am a dummy because PalmSystem is not available");
 26                         
 27                 if (typeof params.onFailure === 'function') {
 28                     params.onFailure({
 29                         returnValue:false,
 30                         errorText:"PalmSystem Not Available. Cordova is not installed?"
 31                     });
 32                }
 33         }};
 34     }
 35 
 36     function checkErrorCodeNText(result, errorCode, errorText) {
 37         
 38         if (result.errorCode === undefined || result.errorCode === null ) {
 39             result.errorCode = errorCode;
 40         }
 41         if (result.errorText ===undefined || result.errorText === null) {
 42             result.errorText = errorText;
 43         }
 44     }
 45 
 46     /**
 47      * security interface
 48      */
 49     var Security = function () {
 50     };
 51     
 52     
 53 	/**
 54      * Registers a server certificate to validate the key from the web server as part of a PKI (Public Key Infrastructure).<br>
 55      * Only 1 server certificate is permitted in signage display. Therefore to register a server certificate when another server certificate was registered, revoke(unregister) the registered server certificate first, and register the new server certificate.<br>
 56      * After registration, reboot must be needed.
 57      * @class Security
 58      * @param {Function} successCallback success callback function.
 59      * @param {Function} errorCallback failure callback function.
 60      * @param {Object} options
 61      * <div align=left>
 62      * <table class="hcap_spec" width=400>
 63      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
 64      *   <tbody>
 65      *       <tr><th>userName</th><th>String</th><th>username for this server certificate as 4 to 10-character string chosen from the set [a-zA-Z0-9].</th><th>required</th></tr>
 66      *       <tr><th>password</th><th>String</th><th>password as 4 to 10-character string chosen from the set [a-zA-Z0-9] to get whether this certificate is registered or not, or revoke this certificate in the future.</th><th>required</th></tr>
 67      *       <tr><th>certificate</th><th>String</th><th>certificate string of the full contents in the server certificate file (public root CA of the server certificate file) in the form of PEM.</th><th>required</th></tr>
 68      *   </tbody>
 69      * </table>
 70      * </div>
 71      * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br>
 72      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
 73      * @example
 74      * // Javascript code
 75      * function registerServerCertificate () {
 76      *   var options = {
 77      *      userName : "testserver",
 78      *      password : "passCode1",
 79      *      certificate : "\
 80      *-----BEGIN CERTIFICATE-----\n\
 81      *MIIDhDCCAmwCCQDY8/8psTWE+DANBgkqhkiG9w0BAQsFADCBgzELMAkGA1UEBhMC\n\
 82      *S1IxDjAMBgNVBAgMBVNlb3VsMQ4wDAYDVQQHDAVTZW91bDEMMAoGA1UECgwDTEdF\n\
 83      *MQwwCgYDVQQLDANXTVQxFjAUBgNVBAMMDTEwLjE3Ny4yMjUuNTgxIDAeBgkqhkiG\n\
 84      *9w0BCQEWEWRvb21zZGF5QGtsZHAub3JnMB4XDTE2MDkyMzEwMzY0MloXDTI2MDky\n\
 85      *MTEwMzY0MlowgYMxCzAJBgNVBAYTAktSMQ4wDAYDVQQIDAVTZW91bDEOMAwGA1UE\n\
 86      *BwwFU2VvdWwxDDAKBgNVBAoMA0xHRTEMMAoGA1UECwwDV01UMRYwFAYDVQQDDA0x\n\
 87      *MC4xNzcuMjI1LjU4MSAwHgYJKoZIhvcNAQkBFhFkb29tc2RheUBrbGRwLm9yZzCC\n\
 88      *ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANGU2B1m93M1Wtm1Bvz7BF7S\n\
 89      *ATx7IWB5bE9fteMJmhvb5yYIcwKbBtPBdIyywPe9ROSHiYHubW3GCbw/h/CjC7F6\n\
 90      *gvKGK+Hg6lF2fXVDbR4+qDPoiVY+dv/ZWbaQ2KDjMHSqfHqlZWIqm4Znp4mx3dL1\n\
 91      *DAt2I/S5jcKkO9Xf4g5RCW5dEMTpv0aNgm1nd6YMOGmO0F0r/HwlLNr4cdJwRBWv\n\
 92      *Ce99EZ3H5SY+Iat96sXDQPxfmBtN1H71Hi1+kZ0ugDhLVTEQxq68cXH0QSnWttXx\n\
 93      *p4P1DBWYJswQrjiWP9SXLCTPBawNRnSkSGpqoconhy8xDnp1jvEEd/zpWAh036MC\n\
 94      *AwEAATANBgkqhkiG9w0BAQsFAAOCAQEAKzlLlueFKWfo9IGUdQ/RLjBXD+gBtwF9\n\
 95      *T8qkIA0h8eawK3l4pBjXgyvIybhhnF3q+aBDD6nh0anhnzmlrzyWqWefsVQqDKEG\n\
 96      *iqb01qo8qOAn84pAliOnsNQEx+D1Rb2+ceRUhEYLxZBDbL9iL8MuNPAW7coFw3nm\n\
 97      *eTGV1Nx3OUfK9/EcGbEzNkFP8ZMkckbiDrF5rGHClqL+9FeQ03XMIPGqil2Te6Xq\n\
 98      *vKG3nsUYZymir2Dgl7Z6Vkeo+F8Y6CPD+iIxkgfX+QKRYS/dYoN00o7fsHNJN7WM\n\
 99      *CXqAtsTiHRfF17xHVjXH3HLqR5sIpQqay2RZE2PDQpc7Gaq+L9U81A==\n\
100      *-----END CERTIFICATE-----\n"
101      *   };   
102      *     
103      *   function successCb() {
104      *      // Do something
105      *   }
106      *
107      *   function failureCb(cbObject) {
108      *      var errorCode = cbObject.errorCode;
109      *      var errorText = cbObject.errorText;
110      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
111      *   }
112      *
113      *   var security = new Security();
114      *   security.registerServerCertificate(successCb, failureCb, options);
115      * }
116      * @since 1.4.1
117      */
118     Security.prototype.registerServerCertificate = function (successCallback, errorCallback, options) {
119             log("registerServerCertificate: " + JSON.stringify(options));
120 
121             if (options.userName === undefined || typeof options.userName !== 'string' || options.userName.length < 4 || options.userName.length > 10 ||
122                 options.password === undefined || typeof options.password !== 'string' || options.password.length < 4 || options.password.length > 10 ||
123                 options.certificate === undefined || typeof options.certificate !== 'string') {
124 
125                 if (typeof errorCallback === 'function') {
126                     var result = {};
127                     checkErrorCodeNText(result, "SRSC", "Security.registerServerCertificate returns failure. invalid parameters or out of range.");
128                     errorCallback(result);
129                 }
130 
131                 return;
132             }
133 
134             service.Request("luna://com.webos.service.commercial.signage.storageservice/security/", {
135                 method: "registerServerCertificate",
136                 parameters: {
137                     userName : options.userName,
138                     password : options.password,
139                     certificate : options.certificate
140                 },
141                 onSuccess: function(result) {
142                     log("registerServerCertificate: On Success");
143 
144                     if (result.returnValue === true) {
145                         if (typeof successCallback === 'function') {
146                             successCallback();
147                         }
148                     }
149                 },
150                 onFailure: function(result) {
151                     log("registerServerCertificate: On Failure");
152                     delete result.returnValue;
153                     if (typeof errorCallback === 'function') {
154                         checkErrorCodeNText(result, "SRSC", "Security.registerServerCertificate returns failure.");
155                         errorCallback(result);
156                     }
157                 }
158             });
159 
160             log("Security.registerServerCertificate Done");                    
161     };
162 
163     /**
164      * Unregisters a server certificate and deactivate the TLS authentication and the host verification.
165      * After unregistration, reboot must be needed.
166      * @class Security
167      * @param {Function} successCallback success callback function.
168      * @param {Function} errorCallback failure callback function.     
169      * @param {Object} options
170      * <div align=left>
171      * <table class="hcap_spec" width=400>
172      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
173      *   <tbody>
174      *       <tr><th>userName</th><th>String</th><th>username for the server certificate to unregister.</th><th>required</th></tr>
175      *       <tr><th>password</th><th>String</th><th>password to be used for the registration of the server certificate before</th><th>required</th></tr>
176      *   </tbody>
177      * </table>
178      * </div>
179      * @return <p>If the method is successfully executed, call the success callback function without a parameter.</br>
180      * If an error occurs, failure callback function is called with failure callback object as a parameter.</p>
181      * @example
182      * // Javascript code
183      * function unregisterServerCertificate () {
184      *   var options = {
185      *      userName : "testserver",
186      *      password : "passCode1"
187      *   };   
188      *     
189      *   function successCb() {
190      *      // Do something
191      *   }
192      *
193      *   function failureCb(cbObject) {
194      *      var errorCode = cbObject.errorCode;
195      *      var errorText = cbObject.errorText;
196      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
197      *   }
198      *
199      *   var security = new Security();
200      *   security.unregisterServerCertificate(successCb, failureCb, options);
201      * }
202      * @since 1.4.1
203      */
204     Security.prototype.unregisterServerCertificate = function (successCallback, errorCallback, options) {
205             log("unregisterServerCertificate: " + JSON.stringify(options));
206 
207             if (options.userName === undefined || typeof options.userName !== 'string' || options.userName.length < 4 || options.userName.length > 10 ||
208                 options.password === undefined || typeof options.password !== 'string' || options.password.length < 4 || options.password.length > 10) {
209 
210                 if (typeof errorCallback === 'function') {
211                     var result = {};
212                     checkErrorCodeNText(result, "SUSC", "Security.unregisterServerCertificate returns failure. invalid parameters or out of range.");
213                     errorCallback(result);
214                 }
215 
216                 return;
217             }
218 
219             service.Request("luna://com.webos.service.commercial.signage.storageservice/security/", {
220                 method: "unregisterServerCertificate",
221                 parameters: {
222                     userName : options.userName,
223                     password : options.password
224                 },
225                 onSuccess: function(result) {
226                     log("unregisterServerCertificate: On Success");
227 
228                     if (result.returnValue === true) {
229                         if (typeof successCallback === 'function') {
230                             successCallback();
231                         }
232                     }
233                 },
234                 onFailure: function(result) {
235                     log("unregisterServerCertificate: On Failure");
236                     delete result.returnValue;
237                     if (typeof errorCallback === 'function') {
238                         checkErrorCodeNText(result, "SUSC", "Security.unregisterServerCertificate returns failure.");
239                         errorCallback(result);
240                     }
241                 }
242             });
243 
244             log("Security.unregisterServerCertificate Done");                    
245     };
246 
247 
248     /**
249      * Returns whether a server certificate was registered or not.     
250      * @class Security
251      * @param {Function} successCallback success callback function.
252      * @param {Function} errorCallback failure callback function.     
253      * @param {Object} options
254      * <div align=left>
255      * <table class="hcap_spec" width=400>
256      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th><th>Required</th></tr></thead>
257      *   <tbody>
258      *       <tr><th>userName</th><th>String</th><th>username for the server certificate.</th><th>required</th></tr>
259      *       <tr><th>password</th><th>String</th><th>password to be used for the registration of the server certificate before</th><th>required</th></tr>
260      *   </tbody>
261      * </table>
262      * </div>
263      * @return {Object} 
264      * <div align=left>
265      * <table class="hcap_spec" width=400>
266      *   <thead><tr><th>Property</th><th>Type</th><th>Description</th></tr></thead>
267      *   <tbody>
268      *       <tr><th>exist</th><th>Boolean</th><th>whether a server certificate was registered or not</th></tr>
269      *   </tbody>
270      * </table>
271      * </div>
272      * @example
273      * // Javascript code
274      * function existServerCertificate () {
275      *   var options = {
276      *      userName : "testserver",
277      *      password : "passCode1"
278      *   };   
279      *     
280      *   function successCb(cbObject) {
281      *      console.log("cbObject : " + JSON.stringify(cbObject));
282      *   }
283      *
284      *   function failureCb(cbObject) {
285      *      var errorCode = cbObject.errorCode;
286      *      var errorText = cbObject.errorText;
287      *      console.log ("Error Code [" + errorCode + "]: " + errorText);
288      *   }
289      *
290      *   var security = new Security();
291      *   security.existServerCertificate(successCb, failureCb, options);
292      * }
293      * @since 1.4.1
294      */
295     Security.prototype.existServerCertificate = function (successCallback, errorCallback, options) {
296             log("existServerCertificate: " + JSON.stringify(options));
297 
298             if (options.userName === undefined || typeof options.userName !== 'string' || options.userName.length < 4 || options.userName.length > 10 ||
299                 options.password === undefined || typeof options.password !== 'string' || options.password.length < 4 || options.password.length > 10) {
300 
301                 if (typeof errorCallback === 'function') {
302                     var result = {};
303                     checkErrorCodeNText(result, "SESC", "Security.existServerCertificate returns failure. invalid parameters or out of range.");
304                     errorCallback(result);
305                 }
306 
307                 return;
308             }
309 
310             service.Request("luna://com.webos.service.commercial.signage.storageservice/security/", {
311                 method: "existServerCertificate",
312                 parameters: {
313                     userName : options.userName,
314                     password : options.password
315                 },
316                 onSuccess: function(result) {
317                     log("existServerCertificate: On Success");
318 
319                     if (result.returnValue === true) {
320                         if (typeof successCallback === 'function') {
321                             var cbObj      = {};
322                             cbObj.exist    = result.exist;
323                             successCallback(cbObj);
324                         }
325                     }
326                 },
327                 onFailure: function(result) {
328                     log("existServerCertificate: On Failure");
329                     delete result.returnValue;
330                     if (typeof errorCallback === 'function') {
331                         checkErrorCodeNText(result, "SESC", "Security.existServerCertificate returns failure.");
332                         errorCallback(result);
333                     }
334                 }
335             });
336 
337             log("Security.existServerCertificate Done");
338     };
339 
340     module.exports = Security;
341 });
342 
343 Security = cordova.require('cordova/plugin/security');
344 
345