| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var constants = require("./modules/constants"); |
| 2 | 1 | var events = require("./modules/events"); |
| 3 | 1 | var logger = require("./modules/logger"); |
| 4 | 1 | var ajax = require("./modules/ajax"); |
| 5 | 1 | var events = require("./modules/events"); |
| 6 | 1 | var cloud = require("./modules/waitForCloud"); |
| 7 | 1 | var api_act = require("./modules/api_act"); |
| 8 | 1 | var api_auth = require("./modules/api_auth"); |
| 9 | 1 | var api_sec = require("./modules/api_sec"); |
| 10 | 1 | var api_hash = require("./modules/api_hash"); |
| 11 | 1 | var api_mbaas = require("./modules/api_mbaas"); |
| 12 | 1 | var api_cloud = require("./modules/api_cloud"); |
| 13 | 1 | var api_push = require("./modules/api_push"); |
| 14 | 1 | var fhparams = require("./modules/fhparams"); |
| 15 | 1 | var appProps = require("./modules/appProps"); |
| 16 | 1 | var device = require("./modules/device"); |
| 17 | 1 | var syncCloudHandler = require("./modules/sync_cloud_handler"); |
| 18 | ||
| 19 | 1 | var defaultFail = function(msg, error) { |
| 20 | 0 | logger.error(msg + ":" + JSON.stringify(error)); |
| 21 | }; | |
| 22 | ||
| 23 | 1 | var addListener = function(type, listener) { |
| 24 | 2 | events.addListener(type, listener); |
| 25 | if (type === constants.INIT_EVENT) { | |
| 26 | //for fhinit event, need to check the status of cloud and may need to fire the listener immediately. | |
| 27 | if (cloud.isReady()) { | |
| 28 | 0 | listener(null, { |
| 29 | host: cloud.getCloudHostUrl() | |
| 30 | }); | |
| 31 | } else if (cloud.getInitError()) { | |
| 32 | 0 | listener(cloud.getInitError()); |
| 33 | } | |
| 34 | } | |
| 35 | }; | |
| 36 | ||
| 37 | 1 | var once = function(type, listener) { |
| 38 | if (type === constants.INIT_EVENT && cloud.isReady()) { | |
| 39 | 0 | listener(null, { |
| 40 | host: cloud.getCloudHostUrl() | |
| 41 | }); | |
| 42 | } else if (type === constants.INIT_EVENT && cloud.getInitError()) { | |
| 43 | 0 | listener(cloud.getInitError()); |
| 44 | } else { | |
| 45 | 0 | events.once(type, listener); |
| 46 | } | |
| 47 | }; | |
| 48 | ||
| 49 | //Legacy shim. Init hapens based on fhconfig.json or, for v2, global var called fh_app_props which is injected as part of the index.html wrapper | |
| 50 | 1 | var init = function(opts, success, fail) { |
| 51 | 0 | logger.warn("$fh.init will be deprecated soon"); |
| 52 | 0 | cloud.ready(function(err, host) { |
| 53 | if (err) { | |
| 54 | if (typeof fail === "function") { | |
| 55 | 0 | return fail(err); |
| 56 | } | |
| 57 | } else { | |
| 58 | if (typeof success === "function") { | |
| 59 | 0 | success(host.host); |
| 60 | } | |
| 61 | } | |
| 62 | }); | |
| 63 | }; | |
| 64 | ||
| 65 | 1 | var fh = window.$fh || {}; |
| 66 | 1 | fh.init = init; |
| 67 | 1 | fh.act = api_act; |
| 68 | 1 | fh.auth = api_auth; |
| 69 | 1 | fh.cloud = api_cloud; |
| 70 | 1 | fh.sec = api_sec; |
| 71 | 1 | fh.hash = api_hash; |
| 72 | 1 | fh.push = api_push; |
| 73 | 1 | fh.ajax = fh.__ajax = ajax; |
| 74 | 1 | fh.mbaas = api_mbaas; |
| 75 | ||
| 76 | // Mount sync to fh namespace | |
| 77 | 1 | fh.sync = require("fh-sync-js"); |
| 78 | 1 | fh.sync.setCloudHandler(syncCloudHandler); |
| 79 | ||
| 80 | 1 | fh._getDeviceId = device.getDeviceId; |
| 81 | 1 | fh.fh_timeout = 60000; //keep backward compatible |
| 82 | ||
| 83 | 1 | fh.getCloudURL = function() { |
| 84 | 0 | return cloud.getCloudHostUrl(); |
| 85 | }; | |
| 86 | ||
| 87 | 1 | fh.getFHParams = function() { |
| 88 | 0 | return fhparams.buildFHParams(); |
| 89 | }; | |
| 90 | ||
| 91 | 1 | fh.getFHHeaders = function() { |
| 92 | 0 | return fhparams.getFHHeaders(); |
| 93 | }; | |
| 94 | ||
| 95 | //events | |
| 96 | 1 | fh.addListener = addListener; |
| 97 | 1 | fh.on = addListener; |
| 98 | 1 | fh.once = once; |
| 99 | 1 | var methods = ["removeListener", "removeAllListeners", "setMaxListeners", "listeners", "emit"]; |
| 100 | for (var i = 0; i < methods.length; i++) { | |
| 101 | 5 | fh[methods[i]] = events[methods[i]]; |
| 102 | } | |
| 103 | ||
| 104 | //keep backward compatibility | |
| 105 | 1 | fh.on(constants.INIT_EVENT, function(err, host) { |
| 106 | if (err) { | |
| 107 | 0 | fh.cloud_props = {}; |
| 108 | 0 | fh.app_props = {}; |
| 109 | } else { | |
| 110 | 1 | fh.cloud_props = { |
| 111 | hosts: { | |
| 112 | url: host.host | |
| 113 | } | |
| 114 | }; | |
| 115 | 1 | fh.app_props = appProps.getAppProps(); |
| 116 | } | |
| 117 | }); | |
| 118 | ||
| 119 | //keep backward compatibility | |
| 120 | 1 | fh.on(constants.INTERNAL_CONFIG_LOADED_EVENT, function(err, host) { |
| 121 | if (err) { | |
| 122 | 0 | fh.app_props = {}; |
| 123 | } else { | |
| 124 | 1 | fh.app_props = appProps.getAppProps(); |
| 125 | } | |
| 126 | ||
| 127 | // Emit config loaded event - appprops set at this point | |
| 128 | // V2 legacy SDK uses this to know when to fire $fh.ready (i.e. appprops is now set) | |
| 129 | 1 | events.emit(constants.CONFIG_LOADED_EVENT, null); |
| 130 | }); | |
| 131 | ||
| 132 | //for test | |
| 133 | 1 | fh.reset = cloud.reset; |
| 134 | //we should really stop polluting global name space. Ideally we should ask browserify to use "$fh" when umd-fy the module. However, "$" is not allowed as the standard module name. | |
| 135 | //So, we assign $fh to the window name space directly here. (otherwise, we have to fork the grunt browserify plugin, then fork browerify and the dependent umd module, really not worthing the effort). | |
| 136 | 1 | window.$fh = fh; |
| 137 | 1 | module.exports = fh; |
| Line | Hits | Source |
|---|---|---|
| 1 | 2 | var urlparser = require('url'); |
| 2 | ||
| 3 | 2 | var XDomainRequestWrapper = function(xdr){ |
| 4 | 0 | this.xdr = xdr; |
| 5 | 0 | this.isWrapper = true; |
| 6 | 0 | this.readyState = 0; |
| 7 | 0 | this.onreadystatechange = null; |
| 8 | 0 | this.status = 0; |
| 9 | 0 | this.statusText = ""; |
| 10 | 0 | this.responseText = ""; |
| 11 | 0 | this.headers = {}; |
| 12 | 0 | var self = this; |
| 13 | 0 | this.xdr.onload = function(){ |
| 14 | 0 | self.readyState = 4; |
| 15 | 0 | self.status = 200; |
| 16 | 0 | self.statusText = ""; |
| 17 | 0 | self.responseText = self.xdr.responseText; |
| 18 | if(self.onreadystatechange){ | |
| 19 | 0 | self.onreadystatechange(); |
| 20 | } | |
| 21 | }; | |
| 22 | 0 | this.xdr.onerror = function(){ |
| 23 | if(self.onerror){ | |
| 24 | 0 | self.onerror(); |
| 25 | } | |
| 26 | 0 | self.readyState = 4; |
| 27 | 0 | self.status = 0; |
| 28 | 0 | self.statusText = ""; |
| 29 | if(self.onreadystatechange){ | |
| 30 | 0 | self.onreadystatechange(); |
| 31 | } | |
| 32 | }; | |
| 33 | 0 | this.xdr.ontimeout = function(){ |
| 34 | 0 | self.readyState = 4; |
| 35 | 0 | self.status = 408; |
| 36 | 0 | self.statusText = "timeout"; |
| 37 | if(self.onreadystatechange){ | |
| 38 | 0 | self.onreadystatechange(); |
| 39 | } | |
| 40 | }; | |
| 41 | }; | |
| 42 | ||
| 43 | 2 | XDomainRequestWrapper.prototype.open = function(method, url, asyn){ |
| 44 | 0 | var parsedUrl = urlparser.parse(url, true); |
| 45 | 0 | parsedUrl.query = parsedUrl.query || {}; |
| 46 | 0 | parsedUrl.query.fh_headers = this.headers; |
| 47 | 0 | this.xdr.open(method, urlparser.format(parsedUrl)); |
| 48 | }; | |
| 49 | ||
| 50 | 2 | XDomainRequestWrapper.prototype.send = function(data){ |
| 51 | 0 | this.xdr.send(data); |
| 52 | }; | |
| 53 | ||
| 54 | 2 | XDomainRequestWrapper.prototype.abort = function(){ |
| 55 | 0 | this.xdr.abort(); |
| 56 | }; | |
| 57 | ||
| 58 | 2 | XDomainRequestWrapper.prototype.setRequestHeader = function(n, v){ |
| 59 | //not supported by xdr | |
| 60 | //Good doc on limitations of XDomainRequest http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx | |
| 61 | //XDomainRequest doesn't allow setting custom request headers. But it is the only available option to do CORS requests in IE8 & 9. In IE10, they finally start to use standard XMLHttpRequest. | |
| 62 | //To support FH auth tokens in IE8&9, we will append them as query parameters, use the key "fh_headers" | |
| 63 | 0 | this.headers[n] = v; |
| 64 | }; | |
| 65 | ||
| 66 | 2 | XDomainRequestWrapper.prototype.getResponseHeader = function(n){ |
| 67 | //not supported by xdr | |
| 68 | }; | |
| 69 | ||
| 70 | 2 | module.exports = XDomainRequestWrapper; |
| 71 |
| Line | Hits | Source |
|---|---|---|
| 1 | //a shameless copy from https://github.com/ForbesLindesay/ajax/blob/master/index.js. | |
| 2 | //it has the same methods and config options as jQuery/zeptojs but very light weight. see http://api.jquery.com/jQuery.ajax/ | |
| 3 | //a few small changes are made for supporting IE 8 and other features: | |
| 4 | //1. use getXhr function to replace the default XMLHttpRequest implementation for supporting IE8 | |
| 5 | //2. Integrate with events emitter. So to subscribe ajax events, you can do $fh.on("ajaxStart", handler). See http://api.jquery.com/Ajax_Events/ for full list of events | |
| 6 | //3. allow passing xhr factory method through options: e.g. $fh.ajax({xhr: function(){/*own implementation of xhr*/}}); | |
| 7 | //4. Use fh_timeout value as the default timeout | |
| 8 | //5. an extra option called "tryJSONP" to allow try the same call with JSONP if normal CORS failed - should only be used internally | |
| 9 | //6. for jsonp, allow to specify the callback query param name using the "jsonp" option | |
| 10 | ||
| 11 | 2 | var eventsHandler = require("./events"); |
| 12 | 2 | var XDomainRequestWrapper = require("./XDomainRequestWrapper"); |
| 13 | 2 | var logger = require("./logger"); |
| 14 | ||
| 15 | 2 | var type |
| 16 | try { | |
| 17 | 2 | type = require('type-of') |
| 18 | } catch (ex) { | |
| 19 | //hide from browserify | |
| 20 | 0 | var r = require |
| 21 | 0 | type = r('type') |
| 22 | } | |
| 23 | ||
| 24 | 2 | var jsonpID = 0, |
| 25 | document = window.document, | |
| 26 | key, | |
| 27 | name, | |
| 28 | rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, | |
| 29 | scriptTypeRE = /^(?:text|application)\/javascript/i, | |
| 30 | xmlTypeRE = /^(?:text|application)\/xml/i, | |
| 31 | jsonType = 'application/json', | |
| 32 | htmlType = 'text/html', | |
| 33 | blankRE = /^\s*$/; | |
| 34 | ||
| 35 | 2 | var ajax = module.exports = function (options) { |
| 36 | 3 | var settings = extend({}, options || {}) |
| 37 | //keep backward compatibility | |
| 38 | if(window && window.$fh && typeof window.$fh.fh_timeout === "number"){ | |
| 39 | 2 | ajax.settings.timeout = window.$fh.fh_timeout; |
| 40 | } | |
| 41 | ||
| 42 | for (key in ajax.settings) | |
| 43 | if (settings[key] === undefined) settings[key] = ajax.settings[key] | |
| 44 | ||
| 45 | 3 | ajaxStart(settings) |
| 46 | ||
| 47 | if (!settings.crossDomain) { | |
| 48 | 3 | settings.crossDomain = /^([\w-]+:)?\/\/([^\/]+)/.test(settings.url) && (RegExp.$1 != window.location.protocol || RegExp.$2 != window.location.host) |
| 49 | } | |
| 50 | ||
| 51 | 3 | var dataType = settings.dataType, |
| 52 | hasPlaceholder = /=\?/.test(settings.url) | |
| 53 | if (dataType == 'jsonp' || hasPlaceholder) { | |
| 54 | if (!hasPlaceholder) { | |
| 55 | 0 | settings.url = appendQuery(settings.url, (settings.jsonp? settings.jsonp: '_callback') + '=?'); |
| 56 | } | |
| 57 | 0 | return ajax.JSONP(settings) |
| 58 | } | |
| 59 | ||
| 60 | if (!settings.url) settings.url = window.location.toString() | |
| 61 | 3 | serializeData(settings) |
| 62 | ||
| 63 | 3 | var mime = settings.accepts[dataType], |
| 64 | baseHeaders = {}, | |
| 65 | protocol = /^([\w-]+:)\/\//.test(settings.url) ? RegExp.$1 : window.location.protocol, | |
| 66 | xhr = settings.xhr(settings.crossDomain), | |
| 67 | abortTimeout = null; | |
| 68 | ||
| 69 | if (!settings.crossDomain) baseHeaders['X-Requested-With'] = 'XMLHttpRequest' | |
| 70 | if (mime) { | |
| 71 | 3 | baseHeaders['Accept'] = mime |
| 72 | if (mime.indexOf(',') > -1) mime = mime.split(',', 2)[0] | |
| 73 | 3 | xhr.overrideMimeType && xhr.overrideMimeType(mime) |
| 74 | } | |
| 75 | if (settings.contentType || (settings.data && !settings.formdata && settings.type.toUpperCase() != 'GET')) | |
| 76 | baseHeaders['Content-Type'] = (settings.contentType || 'application/x-www-form-urlencoded') | |
| 77 | 3 | settings.headers = extend(baseHeaders, settings.headers || {}) |
| 78 | ||
| 79 | if (typeof Titanium !== 'undefined') { | |
| 80 | 0 | xhr.onerror = function(){ |
| 81 | if (!abortTimeout){ | |
| 82 | 0 | return; |
| 83 | } | |
| 84 | 0 | clearTimeout(abortTimeout); |
| 85 | 0 | ajaxError(null, 'error', xhr, settings); |
| 86 | }; | |
| 87 | } | |
| 88 | ||
| 89 | 3 | xhr.onreadystatechange = function () { |
| 90 | ||
| 91 | if (xhr.readyState == 4) { | |
| 92 | 3 | clearTimeout(abortTimeout) |
| 93 | 3 | abortTimeout = undefined; |
| 94 | 3 | var result, error = false |
| 95 | if(settings.tryJSONP){ | |
| 96 | //check if the request has fail. In some cases, we may want to try jsonp as well. Again, FH only... | |
| 97 | if(xhr.status === 0 && settings.crossDomain && !xhr.isTimeout && protocol != 'file:'){ | |
| 98 | 0 | logger.debug("retry ajax call with jsonp") |
| 99 | 0 | settings.type = "GET"; |
| 100 | 0 | settings.dataType = "jsonp"; |
| 101 | ||
| 102 | if (settings.data) { | |
| 103 | 0 | settings.data = "_jsonpdata=" + JSON.stringify( |
| 104 | require("./fhparams").addFHParams(JSON.parse(settings.data)) | |
| 105 | ); | |
| 106 | } else { | |
| 107 | 0 | settings.data = "_jsonpdata=" + settings.data; |
| 108 | } | |
| 109 | ||
| 110 | 0 | return ajax(settings); |
| 111 | } | |
| 112 | } | |
| 113 | if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304 || (xhr.status == 0 && protocol == 'file:')) { | |
| 114 | 2 | dataType = dataType || mimeToDataType(xhr.getResponseHeader('content-type')) |
| 115 | 2 | result = xhr.responseText |
| 116 | 2 | logger.debug("ajax response :: status = " + xhr.status + " :: body = " + result) |
| 117 | ||
| 118 | try { | |
| 119 | if (dataType == 'script')(1, eval)(result) | |
| 120 | else if (dataType == 'xml') result = xhr.responseXML | |
| 121 | else if (dataType == 'json') result = blankRE.test(result) ? null : JSON.parse(result) | |
| 122 | } catch (e) { | |
| 123 | 0 | error = e |
| 124 | } | |
| 125 | ||
| 126 | if (error) { | |
| 127 | 0 | logger.debug("ajax error", error); |
| 128 | 0 | ajaxError(error, 'parsererror', xhr, settings) |
| 129 | } | |
| 130 | else ajaxSuccess(result, xhr, settings) | |
| 131 | } else { | |
| 132 | 1 | ajaxError(null, 'error', xhr, settings) |
| 133 | } | |
| 134 | } | |
| 135 | } | |
| 136 | ||
| 137 | 3 | var async = 'async' in settings ? settings.async : true |
| 138 | 3 | logger.debug("ajax call settings", settings) |
| 139 | 3 | xhr.open(settings.type, settings.url, async) |
| 140 | ||
| 141 | for (name in settings.headers) xhr.setRequestHeader(name, settings.headers[name]) | |
| 142 | ||
| 143 | if (ajaxBeforeSend(xhr, settings) === false) { | |
| 144 | 0 | logger.debug("ajax call is aborted due to ajaxBeforeSend") |
| 145 | 0 | xhr.abort() |
| 146 | 0 | return false |
| 147 | } | |
| 148 | ||
| 149 | if (settings.timeout > 0) abortTimeout = setTimeout(function () { | |
| 150 | 0 | logger.debug("ajax call timed out") |
| 151 | 0 | xhr.onreadystatechange = empty |
| 152 | 0 | xhr.abort() |
| 153 | 0 | xhr.isTimeout = true |
| 154 | 0 | ajaxError(null, 'timeout', xhr, settings) |
| 155 | }, settings.timeout) | |
| 156 | ||
| 157 | // avoid sending empty string (#319) | |
| 158 | 3 | xhr.send(settings.data ? settings.data : null) |
| 159 | 3 | return xhr |
| 160 | } | |
| 161 | ||
| 162 | ||
| 163 | // trigger a custom event and return true | |
| 164 | function triggerAndReturn(context, eventName, data) { | |
| 165 | 15 | eventsHandler.emit(eventName, data); |
| 166 | 15 | return true; |
| 167 | } | |
| 168 | ||
| 169 | // trigger an Ajax "global" event | |
| 170 | function triggerGlobal(settings, context, eventName, data) { | |
| 171 | if (settings.global) return triggerAndReturn(context || document, eventName, data) | |
| 172 | } | |
| 173 | ||
| 174 | // Number of active Ajax requests | |
| 175 | 2 | ajax.active = 0 |
| 176 | ||
| 177 | function ajaxStart(settings) { | |
| 178 | if (settings.global && ajax.active++ === 0) triggerGlobal(settings, null, 'ajaxStart') | |
| 179 | } | |
| 180 | ||
| 181 | function ajaxStop(settings) { | |
| 182 | if (settings.global && !(--ajax.active)) triggerGlobal(settings, null, 'ajaxStop') | |
| 183 | } | |
| 184 | ||
| 185 | // triggers an extra global event "ajaxBeforeSend" that's like "ajaxSend" but cancelable | |
| 186 | function ajaxBeforeSend(xhr, settings) { | |
| 187 | 3 | var context = settings.context |
| 188 | if (settings.beforeSend.call(context, xhr, settings) === false) | |
| 189 | return false | |
| 190 | ||
| 191 | 3 | triggerGlobal(settings, context, 'ajaxSend', [xhr, settings]) |
| 192 | } | |
| 193 | ||
| 194 | function ajaxSuccess(data, xhr, settings) { | |
| 195 | 2 | var context = settings.context, |
| 196 | status = 'success' | |
| 197 | 2 | settings.success.call(context, data, status, xhr) |
| 198 | 2 | triggerGlobal(settings, context, 'ajaxSuccess', [xhr, settings, data]) |
| 199 | 2 | ajaxComplete(status, xhr, settings) |
| 200 | } | |
| 201 | // type: "timeout", "error", "abort", "parsererror" | |
| 202 | function ajaxError(error, type, xhr, settings) { | |
| 203 | 1 | var context = settings.context |
| 204 | 1 | settings.error.call(context, xhr, type, error) |
| 205 | 1 | triggerGlobal(settings, context, 'ajaxError', [xhr, settings, error]) |
| 206 | 1 | ajaxComplete(type, xhr, settings) |
| 207 | } | |
| 208 | // status: "success", "notmodified", "error", "timeout", "abort", "parsererror" | |
| 209 | function ajaxComplete(status, xhr, settings) { | |
| 210 | 3 | var context = settings.context |
| 211 | 3 | settings.complete.call(context, xhr, status) |
| 212 | 3 | triggerGlobal(settings, context, 'ajaxComplete', [xhr, settings]) |
| 213 | 3 | ajaxStop(settings) |
| 214 | } | |
| 215 | ||
| 216 | // Empty function, used as default callback | |
| 217 | function empty() {} | |
| 218 | ||
| 219 | 2 | ajax.JSONP = function (options) { |
| 220 | if (!('type' in options)) return ajax(options) | |
| 221 | ||
| 222 | 0 | var callbackName = 'jsonp' + (++jsonpID), |
| 223 | script = document.createElement('script'), | |
| 224 | abort = function () { | |
| 225 | //todo: remove script | |
| 226 | //$(script).remove() | |
| 227 | if (callbackName in window) window[callbackName] = empty | |
| 228 | 0 | ajaxComplete('abort', xhr, options) |
| 229 | }, | |
| 230 | xhr = { | |
| 231 | abort: abort | |
| 232 | }, abortTimeout, | |
| 233 | head = document.getElementsByTagName("head")[0] || document.documentElement | |
| 234 | ||
| 235 | if (options.error) script.onerror = function () { | |
| 236 | 0 | xhr.abort() |
| 237 | 0 | options.error() |
| 238 | } | |
| 239 | ||
| 240 | 0 | window[callbackName] = function (data) { |
| 241 | 0 | clearTimeout(abortTimeout) |
| 242 | 0 | abortTimeout = undefined; |
| 243 | //todo: remove script | |
| 244 | //$(script).remove() | |
| 245 | 0 | delete window[callbackName] |
| 246 | 0 | ajaxSuccess(data, xhr, options) |
| 247 | } | |
| 248 | ||
| 249 | 0 | serializeData(options) |
| 250 | 0 | script.src = options.url.replace(/=\?/, '=' + callbackName) |
| 251 | ||
| 252 | // Use insertBefore instead of appendChild to circumvent an IE6 bug. | |
| 253 | // This arises when a base node is used (see jQuery bugs #2709 and #4378). | |
| 254 | 0 | head.insertBefore(script, head.firstChild); |
| 255 | ||
| 256 | if (options.timeout > 0) abortTimeout = setTimeout(function () { | |
| 257 | 0 | xhr.abort() |
| 258 | 0 | ajaxComplete('timeout', xhr, options) |
| 259 | }, options.timeout) | |
| 260 | ||
| 261 | 0 | return xhr |
| 262 | } | |
| 263 | ||
| 264 | function isIE(){ | |
| 265 | 3 | var ie = false; |
| 266 | if(navigator.userAgent && navigator.userAgent.indexOf("MSIE") >=0 ){ | |
| 267 | 0 | ie = true; |
| 268 | } | |
| 269 | 3 | return ie; |
| 270 | } | |
| 271 | ||
| 272 | function getXhr(crossDomain){ | |
| 273 | 3 | var xhr = null; |
| 274 | //always use XMLHttpRequest if available | |
| 275 | if(window.XMLHttpRequest){ | |
| 276 | 3 | xhr = new XMLHttpRequest(); |
| 277 | } | |
| 278 | //for IE8 only. Need to make sure it's not used when running inside Cordova. | |
| 279 | if(isIE() && (crossDomain === true) && typeof window.XDomainRequest !== "undefined" && typeof window.cordova === "undefined"){ | |
| 280 | 0 | xhr = new XDomainRequestWrapper(new XDomainRequest()); |
| 281 | } | |
| 282 | // For Titanium SDK | |
| 283 | if (typeof Titanium !== 'undefined'){ | |
| 284 | 0 | var params = {}; |
| 285 | if(ajax.settings && ajax.settings.timeout){ | |
| 286 | 0 | params.timeout = ajax.settings.timeout; |
| 287 | } | |
| 288 | 0 | xhr = Titanium.Network.createHTTPClient(params); |
| 289 | } | |
| 290 | ||
| 291 | 3 | return xhr; |
| 292 | } | |
| 293 | ||
| 294 | 2 | ajax.settings = { |
| 295 | // Default type of request | |
| 296 | type: 'GET', | |
| 297 | // Callback that is executed before request | |
| 298 | beforeSend: empty, | |
| 299 | // Callback that is executed if the request succeeds | |
| 300 | success: empty, | |
| 301 | // Callback that is executed the the server drops error | |
| 302 | error: empty, | |
| 303 | // Callback that is executed on request complete (both: error and success) | |
| 304 | complete: empty, | |
| 305 | // The context for the callbacks | |
| 306 | context: null, | |
| 307 | // Whether to trigger "global" Ajax events | |
| 308 | global: true, | |
| 309 | // Transport | |
| 310 | xhr: getXhr, | |
| 311 | // MIME types mapping | |
| 312 | accepts: { | |
| 313 | script: 'text/javascript, application/javascript', | |
| 314 | json: jsonType, | |
| 315 | xml: 'application/xml, text/xml', | |
| 316 | html: htmlType, | |
| 317 | text: 'text/plain' | |
| 318 | }, | |
| 319 | // Whether the request is to another domain | |
| 320 | crossDomain: false | |
| 321 | } | |
| 322 | ||
| 323 | function mimeToDataType(mime) { | |
| 324 | 0 | return mime && (mime == htmlType ? 'html' : |
| 325 | mime == jsonType ? 'json' : | |
| 326 | scriptTypeRE.test(mime) ? 'script' : | |
| 327 | xmlTypeRE.test(mime) && 'xml') || 'text' | |
| 328 | } | |
| 329 | ||
| 330 | function appendQuery(url, query) { | |
| 331 | 0 | return (url + '&' + query).replace(/[&?]{1,2}/, '?') |
| 332 | } | |
| 333 | ||
| 334 | // serialize payload and append it to the URL for GET requests | |
| 335 | function serializeData(options) { | |
| 336 | if (type(options.data) === 'object') { | |
| 337 | if(typeof options.data.append === "function"){ | |
| 338 | //we are dealing with FormData, do not serialize | |
| 339 | 0 | options.formdata = true; |
| 340 | } else { | |
| 341 | 0 | options.data = param(options.data) |
| 342 | } | |
| 343 | } | |
| 344 | if (options.data && (!options.type || options.type.toUpperCase() == 'GET')) | |
| 345 | options.url = appendQuery(options.url, options.data) | |
| 346 | } | |
| 347 | ||
| 348 | 2 | ajax.get = function (url, success) { |
| 349 | 0 | return ajax({ |
| 350 | url: url, | |
| 351 | success: success | |
| 352 | }) | |
| 353 | } | |
| 354 | ||
| 355 | 2 | ajax.post = function (url, data, success, dataType) { |
| 356 | if (type(data) === 'function') dataType = dataType || success, success = data, data = null | |
| 357 | 0 | return ajax({ |
| 358 | type: 'POST', | |
| 359 | url: url, | |
| 360 | data: data, | |
| 361 | success: success, | |
| 362 | dataType: dataType | |
| 363 | }) | |
| 364 | } | |
| 365 | ||
| 366 | 2 | ajax.getJSON = function (url, success) { |
| 367 | 0 | return ajax({ |
| 368 | url: url, | |
| 369 | success: success, | |
| 370 | dataType: 'json' | |
| 371 | }) | |
| 372 | } | |
| 373 | ||
| 374 | 2 | var escape = encodeURIComponent; |
| 375 | ||
| 376 | function serialize(params, obj, traditional, scope) { | |
| 377 | 0 | var array = type(obj) === 'array'; |
| 378 | for (var key in obj) { | |
| 379 | 0 | var value = obj[key]; |
| 380 | ||
| 381 | if (scope) key = traditional ? scope : scope + '[' + (array ? '' : key) + ']' | |
| 382 | // handle data in serializeArray() format | |
| 383 | if (!scope && array) params.add(value.name, value.value) | |
| 384 | // recurse into nested objects | |
| 385 | else if (traditional ? (type(value) === 'array') : (type(value) === 'object')) | |
| 386 | serialize(params, value, traditional, key) | |
| 387 | else params.add(key, value) | |
| 388 | } | |
| 389 | } | |
| 390 | ||
| 391 | function param(obj, traditional) { | |
| 392 | 0 | var params = [] |
| 393 | 0 | params.add = function (k, v) { |
| 394 | 0 | this.push(escape(k) + '=' + escape(v)) |
| 395 | } | |
| 396 | 0 | serialize(params, obj, traditional) |
| 397 | 0 | return params.join('&').replace('%20', '+') |
| 398 | } | |
| 399 | ||
| 400 | function extend(target) { | |
| 401 | 6 | var slice = Array.prototype.slice; |
| 402 | 6 | slice.call(arguments, 1).forEach(function (source) { |
| 403 | for (key in source) | |
| 404 | if (source[key] !== undefined) | |
| 405 | target[key] = source[key] | |
| 406 | }) | |
| 407 | 6 | return target |
| 408 | } | |
| 409 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var logger =require("./logger"); |
| 2 | 1 | var cloud = require("./waitForCloud"); |
| 3 | 1 | var fhparams = require("./fhparams"); |
| 4 | 1 | var ajax = require("./ajax"); |
| 5 | 1 | var handleError = require("./handleError"); |
| 6 | 1 | var appProps = require("./appProps"); |
| 7 | 1 | var _ = require('underscore'); |
| 8 | ||
| 9 | function doActCall(opts, success, fail){ | |
| 10 | 0 | var cloud_host = cloud.getCloudHost(); |
| 11 | 0 | var url = cloud_host.getActUrl(opts.act); |
| 12 | 0 | var params = opts.req || {}; |
| 13 | 0 | var headers = fhparams.getFHHeaders(); |
| 14 | if (opts.headers) { | |
| 15 | 0 | headers = _.extend(headers, opts.headers); |
| 16 | } | |
| 17 | 0 | return ajax({ |
| 18 | "url": url, | |
| 19 | "tryJSONP": true, | |
| 20 | "type": "POST", | |
| 21 | "dataType": "json", | |
| 22 | "data": JSON.stringify(params), | |
| 23 | "headers": headers, | |
| 24 | "contentType": "application/json", | |
| 25 | "timeout": opts.timeout || appProps.timeout, | |
| 26 | "success": success, | |
| 27 | "error": function(req, statusText, error){ | |
| 28 | 0 | return handleError(fail, req, statusText, error); |
| 29 | } | |
| 30 | }); | |
| 31 | } | |
| 32 | ||
| 33 | 1 | module.exports = function(opts, success, fail){ |
| 34 | 0 | logger.debug("act is called"); |
| 35 | if(!fail){ | |
| 36 | 0 | fail = function(msg, error){ |
| 37 | 0 | logger.debug(msg + ":" + JSON.stringify(error)); |
| 38 | }; | |
| 39 | } | |
| 40 | ||
| 41 | if(!opts.act){ | |
| 42 | 0 | return fail('act_no_action', {}); |
| 43 | } | |
| 44 | ||
| 45 | 0 | cloud.ready(function(err, cloudHost){ |
| 46 | 0 | logger.debug("Calling fhact now"); |
| 47 | if(err){ | |
| 48 | 0 | return fail(err.message, err); |
| 49 | } else { | |
| 50 | 0 | doActCall(opts, success, fail); |
| 51 | } | |
| 52 | }); | |
| 53 | }; | |
| 54 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var logger = require("./logger"); |
| 2 | 1 | var cloud = require("./waitForCloud"); |
| 3 | 1 | var fhparams = require("./fhparams"); |
| 4 | 1 | var ajax = require("./ajax"); |
| 5 | 1 | var handleError = require("./handleError"); |
| 6 | 1 | var device = require("./device"); |
| 7 | 1 | var constants = require("./constants"); |
| 8 | 1 | var checkAuth = require("./checkAuth"); |
| 9 | 1 | var appProps = require("./appProps"); |
| 10 | 1 | var data = require('./data'); |
| 11 | ||
| 12 | function callAuthEndpoint(endpoint, data, opts, success, fail){ | |
| 13 | 0 | var app_props = appProps.getAppProps(); |
| 14 | 0 | var path = app_props.host + constants.boxprefix + "admin/authpolicy/" + endpoint; |
| 15 | ||
| 16 | if (app_props.local) { | |
| 17 | 0 | path = cloud.getCloudHostUrl() + constants.boxprefix + "admin/authpolicy/" + endpoint; |
| 18 | } | |
| 19 | ||
| 20 | 0 | ajax({ |
| 21 | "url": path, | |
| 22 | "type": "POST", | |
| 23 | "tryJSONP": true, | |
| 24 | "data": JSON.stringify(data), | |
| 25 | "dataType": "json", | |
| 26 | "contentType": "application/json", | |
| 27 | "timeout": opts.timeout || app_props.timeout, | |
| 28 | "headers": fhparams.getFHHeaders(), | |
| 29 | success: function(res){ | |
| 30 | if(success){ | |
| 31 | 0 | return success(res); |
| 32 | } | |
| 33 | }, | |
| 34 | error: function(req, statusText, error){ | |
| 35 | 0 | logger.error('got error when calling ' + endpoint, req.responseText || req, error); |
| 36 | if(fail){ | |
| 37 | 0 | fail(req, statusText, error); |
| 38 | } | |
| 39 | } | |
| 40 | }); | |
| 41 | } | |
| 42 | ||
| 43 | 1 | var auth = function(opts, success, fail) { |
| 44 | if (!fail) { | |
| 45 | 0 | fail = function(msg, error) { |
| 46 | 0 | logger.debug(msg + ":" + JSON.stringify(error)); |
| 47 | }; | |
| 48 | } | |
| 49 | if (!opts.policyId) { | |
| 50 | 0 | return fail('auth_no_policyId', {}); |
| 51 | } | |
| 52 | if (!opts.clientToken) { | |
| 53 | 0 | return fail('auth_no_clientToken', {}); |
| 54 | } | |
| 55 | ||
| 56 | 0 | cloud.ready(function(err, data) { |
| 57 | if (err) { | |
| 58 | 0 | return fail(err.message, err); |
| 59 | } else { | |
| 60 | 0 | var req = {}; |
| 61 | 0 | req.policyId = opts.policyId; |
| 62 | 0 | req.clientToken = opts.clientToken; |
| 63 | 0 | var cloudHost = cloud.getCloudHost(); |
| 64 | if(cloudHost.getEnv()){ | |
| 65 | 0 | req.environment = cloudHost.getEnv(); |
| 66 | } | |
| 67 | if (opts.endRedirectUrl) { | |
| 68 | 0 | req.endRedirectUrl = opts.endRedirectUrl; |
| 69 | if (opts.authCallback) { | |
| 70 | 0 | req.endRedirectUrl += (/\?/.test(req.endRedirectUrl) ? "&" : "?") + "_fhAuthCallback=" + opts.authCallback; |
| 71 | } | |
| 72 | } | |
| 73 | 0 | req.params = {}; |
| 74 | if (opts.params) { | |
| 75 | 0 | req.params = opts.params; |
| 76 | } | |
| 77 | 0 | var endurl = opts.endRedirectUrl || "status=complete"; |
| 78 | 0 | req.device = device.getDeviceId(); |
| 79 | 0 | req = fhparams.addFHParams(req); |
| 80 | 0 | callAuthEndpoint('auth', req, opts, function(res){ |
| 81 | 0 | auth.authenticateHandler(endurl, res, success, fail); |
| 82 | }, function(req, statusText, error){ | |
| 83 | 0 | handleError(fail, req, statusText, error); |
| 84 | }); | |
| 85 | } | |
| 86 | }); | |
| 87 | }; | |
| 88 | ||
| 89 | 1 | auth.hasSession = function(cb){ |
| 90 | 0 | data.sessionManager.exists(cb); |
| 91 | }; | |
| 92 | ||
| 93 | 1 | auth.clearSession = function(cb){ |
| 94 | 0 | data.sessionManager.read(function(err, session){ |
| 95 | if(err){ | |
| 96 | 0 | return cb(err); |
| 97 | } | |
| 98 | if(session){ | |
| 99 | //try the best to delete the remote session | |
| 100 | 0 | callAuthEndpoint('revokesession', session, {}); |
| 101 | } | |
| 102 | 0 | data.sessionManager.remove(cb); |
| 103 | 0 | fhparams.setAuthSessionToken(undefined); |
| 104 | }); | |
| 105 | }; | |
| 106 | ||
| 107 | 1 | auth.authenticateHandler = checkAuth.handleAuthResponse; |
| 108 | ||
| 109 | 1 | auth.verify = function(cb){ |
| 110 | 0 | data.sessionManager.read(function(err, session){ |
| 111 | if(err){ | |
| 112 | 0 | return cb(err); |
| 113 | } | |
| 114 | if(session){ | |
| 115 | //try the best to delete the session in remote | |
| 116 | 0 | callAuthEndpoint('verifysession', session, {}, function(res){ |
| 117 | 0 | return cb(null, res.isValid); |
| 118 | }, function(req, statusText, error){ | |
| 119 | 0 | return cb('network_error'); |
| 120 | }); | |
| 121 | } else { | |
| 122 | 0 | return cb('no_session'); |
| 123 | } | |
| 124 | }); | |
| 125 | }; | |
| 126 | ||
| 127 | 1 | module.exports = auth; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var logger =require("./logger"); |
| 2 | 1 | var cloud = require("./waitForCloud"); |
| 3 | 1 | var fhparams = require("./fhparams"); |
| 4 | 1 | var ajax = require("./ajax"); |
| 5 | 1 | var handleError = require("./handleError"); |
| 6 | 1 | var appProps = require("./appProps"); |
| 7 | 1 | var _ = require('underscore'); |
| 8 | ||
| 9 | function doCloudCall(opts, success, fail){ | |
| 10 | 0 | var cloud_host = cloud.getCloudHost(); |
| 11 | 0 | var url = cloud_host.getCloudUrl(opts.path); |
| 12 | 0 | var params = opts.data || {}; |
| 13 | 0 | var type = opts.method || "POST"; |
| 14 | 0 | var data; |
| 15 | if (["POST", "PUT", "PATCH", "DELETE"].indexOf(type.toUpperCase()) !== -1) { | |
| 16 | 0 | data = JSON.stringify(params); |
| 17 | } else { | |
| 18 | 0 | data = params; |
| 19 | } | |
| 20 | ||
| 21 | 0 | var headers = fhparams.getFHHeaders(); |
| 22 | if (opts.headers) { | |
| 23 | 0 | headers = _.extend(headers, opts.headers); |
| 24 | } | |
| 25 | ||
| 26 | 0 | return ajax({ |
| 27 | "url": url, | |
| 28 | "type": type, | |
| 29 | "dataType": opts.dataType || "json", | |
| 30 | "data": data, | |
| 31 | "contentType": opts.contentType || "application/json", | |
| 32 | "timeout": opts.timeout || appProps.timeout, | |
| 33 | "headers": headers, | |
| 34 | "success": success, | |
| 35 | "error": function(req, statusText, error){ | |
| 36 | 0 | return handleError(fail, req, statusText, error); |
| 37 | } | |
| 38 | }); | |
| 39 | } | |
| 40 | ||
| 41 | 1 | module.exports = function(opts, success, fail){ |
| 42 | 0 | logger.debug("cloud is called"); |
| 43 | if(!fail){ | |
| 44 | 0 | fail = function(msg, error){ |
| 45 | 0 | logger.debug(msg + ":" + JSON.stringify(error)); |
| 46 | }; | |
| 47 | } | |
| 48 | ||
| 49 | 0 | cloud.ready(function(err, cloudHost){ |
| 50 | 0 | logger.debug("Calling fhact now"); |
| 51 | if(err){ | |
| 52 | 0 | return fail(err.message, err); |
| 53 | } else { | |
| 54 | 0 | doCloudCall(opts, success, fail); |
| 55 | } | |
| 56 | }); | |
| 57 | }; | |
| 58 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var hashImpl = require("./security/hash"); |
| 2 | ||
| 3 | 1 | module.exports = function(p, s, f){ |
| 4 | 0 | var params = {}; |
| 5 | if(typeof p.algorithm === "undefined"){ | |
| 6 | 0 | p.algorithm = "MD5"; |
| 7 | } | |
| 8 | 0 | params.act = "hash"; |
| 9 | 0 | params.params = p; |
| 10 | 0 | hashImpl(params, s, f); |
| 11 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var logger =require("./logger"); |
| 2 | 1 | var cloud = require("./waitForCloud"); |
| 3 | 1 | var fhparams = require("./fhparams"); |
| 4 | 1 | var ajax = require("./ajax"); |
| 5 | 1 | var handleError = require("./handleError"); |
| 6 | 1 | var consts = require("./constants"); |
| 7 | 1 | var appProps = require("./appProps"); |
| 8 | ||
| 9 | 1 | module.exports = function(opts, success, fail){ |
| 10 | 0 | logger.debug("mbaas is called."); |
| 11 | if(!fail){ | |
| 12 | 0 | fail = function(msg, error){ |
| 13 | 0 | console.debug(msg + ":" + JSON.stringify(error)); |
| 14 | }; | |
| 15 | } | |
| 16 | ||
| 17 | 0 | var mbaas = opts.service; |
| 18 | 0 | var params = opts.params; |
| 19 | ||
| 20 | 0 | cloud.ready(function(err, cloudHost){ |
| 21 | 0 | logger.debug("Calling mbaas now"); |
| 22 | if(err){ | |
| 23 | 0 | return fail(err.message, err); |
| 24 | } else { | |
| 25 | 0 | var cloud_host = cloud.getCloudHost(); |
| 26 | 0 | var url = cloud_host.getMBAASUrl(mbaas); |
| 27 | 0 | params = fhparams.addFHParams(params); |
| 28 | 0 | return ajax({ |
| 29 | "url": url, | |
| 30 | "tryJSONP": true, | |
| 31 | "type": "POST", | |
| 32 | "dataType": "json", | |
| 33 | "data": JSON.stringify(params), | |
| 34 | "headers": fhparams.getFHHeaders(), | |
| 35 | "contentType": "application/json", | |
| 36 | "timeout": opts.timeout || appProps.timeout, | |
| 37 | "success": success, | |
| 38 | "error": function(req, statusText, error){ | |
| 39 | 0 | return handleError(fail, req, statusText, error); |
| 40 | } | |
| 41 | }); | |
| 42 | } | |
| 43 | }); | |
| 44 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var logger = require("./logger"); |
| 2 | 1 | var appProps = require("./appProps"); |
| 3 | 1 | var cloud = require("./waitForCloud"); |
| 4 | ||
| 5 | 1 | module.exports = function (onNotification, success, fail, config) { |
| 6 | if (!fail) { | |
| 7 | 0 | fail = function (msg, error) { |
| 8 | 0 | logger.debug(msg + ":" + JSON.stringify(error)); |
| 9 | }; | |
| 10 | } | |
| 11 | ||
| 12 | 0 | cloud.ready(function(err, cloudHost){ |
| 13 | 0 | logger.debug("push is called"); |
| 14 | if(err){ | |
| 15 | 0 | return fail(err.message, err); |
| 16 | } else { | |
| 17 | if (window.push) { | |
| 18 | 0 | var props = appProps.getAppProps(); |
| 19 | 0 | props.pushServerURL = props.host + '/api/v2/ag-push'; |
| 20 | if (config) { | |
| 21 | for(var key in config) { | |
| 22 | 0 | props[key] = config[key]; |
| 23 | } | |
| 24 | } | |
| 25 | 0 | window.push.register(onNotification, success, fail, props); |
| 26 | } else { | |
| 27 | 0 | fail('push plugin not installed'); |
| 28 | } | |
| 29 | } | |
| 30 | }); | |
| 31 | }; | |
| 32 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var keygen = require("./security/aes-keygen"); |
| 2 | 1 | var aes = require("./security/aes-node"); |
| 3 | 1 | var rsa = require("./security/rsa-node"); |
| 4 | 1 | var hash = require("./security/hash"); |
| 5 | ||
| 6 | 1 | module.exports = function(p, s, f){ |
| 7 | if (!p.act) { | |
| 8 | 0 | f('bad_act', {}, p); |
| 9 | 0 | return; |
| 10 | } | |
| 11 | if (!p.params) { | |
| 12 | 0 | f('no_params', {}, p); |
| 13 | 0 | return; |
| 14 | } | |
| 15 | if (!p.params.algorithm) { | |
| 16 | 0 | f('no_params_algorithm', {}, p); |
| 17 | 0 | return; |
| 18 | } | |
| 19 | 0 | p.params.algorithm = p.params.algorithm.toLowerCase(); |
| 20 | if(p.act === "hash"){ | |
| 21 | 0 | return hash(p, s, f); |
| 22 | } else if(p.act === "encrypt"){ | |
| 23 | if(p.params.algorithm === "aes"){ | |
| 24 | 0 | return aes.encrypt(p, s, f); |
| 25 | } else if(p.params.algorithm === "rsa"){ | |
| 26 | 0 | return rsa.encrypt(p, s, f); |
| 27 | } else { | |
| 28 | 0 | return f('encrypt_bad_algorithm:' + p.params.algorithm, {}, p); |
| 29 | } | |
| 30 | } else if(p.act === "decrypt"){ | |
| 31 | if(p.params.algorithm === "aes"){ | |
| 32 | 0 | return aes.decrypt(p, s, f); |
| 33 | } else { | |
| 34 | 0 | return f('decrypt_bad_algorithm:' + p.params.algorithm, {}, p); |
| 35 | } | |
| 36 | } else if(p.act === "keygen"){ | |
| 37 | if(p.params.algorithm === "aes"){ | |
| 38 | 0 | return keygen(p, s, f); |
| 39 | } else { | |
| 40 | 0 | return f('keygen_bad_algorithm:' + p.params.algorithm, {}, p); |
| 41 | } | |
| 42 | } | |
| 43 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var consts = require("./constants"); |
| 2 | 1 | var ajax = require("./ajax"); |
| 3 | 1 | var logger = require("./logger"); |
| 4 | 1 | var qs = require("./queryMap"); |
| 5 | 1 | var _ = require('underscore'); |
| 6 | ||
| 7 | 1 | var app_props = null; |
| 8 | ||
| 9 | 1 | var load = function(cb) { |
| 10 | 1 | var doc_url = document.location.href; |
| 11 | 1 | var url_params = qs(doc_url.replace(/#.*?$/g, '')); |
| 12 | 1 | var url_props = {}; |
| 13 | ||
| 14 | //only use fh_ prefixed params | |
| 15 | for(var key in url_params){ | |
| 16 | if(url_params.hasOwnProperty(key) ){ | |
| 17 | if(key.indexOf('fh_') === 0){ | |
| 18 | 0 | url_props[key.substr(3)] = decodeURI(url_params[key]); |
| 19 | } | |
| 20 | } | |
| 21 | } | |
| 22 | ||
| 23 | //default properties | |
| 24 | 1 | app_props = { |
| 25 | appid: "000000000000000000000000", | |
| 26 | appkey: "0000000000000000000000000000000000000000", | |
| 27 | projectid: "000000000000000000000000", | |
| 28 | connectiontag: "0.0.1" | |
| 29 | }; | |
| 30 | ||
| 31 | function setProps(props){ | |
| 32 | 1 | _.extend(app_props, props, url_props); |
| 33 | ||
| 34 | if(typeof url_params.url !== 'undefined'){ | |
| 35 | 1 | app_props.host = url_params.url; |
| 36 | } | |
| 37 | ||
| 38 | 1 | app_props.local = !!(url_props.host || url_params.url || (props.local && props.host)); |
| 39 | 1 | cb(null, app_props); |
| 40 | } | |
| 41 | ||
| 42 | 1 | var config_url = url_params.fhconfig || consts.config_js; |
| 43 | 1 | ajax({ |
| 44 | url: config_url, | |
| 45 | dataType: "json", | |
| 46 | success: function(data) { | |
| 47 | 1 | logger.debug("fhconfig = " + JSON.stringify(data)); |
| 48 | //when load the config file on device, because file:// protocol is used, it will never call fail call back. The success callback will be called but the data value will be null. | |
| 49 | if (null == data) { | |
| 50 | //fh v2 only | |
| 51 | if(window.fh_app_props){ | |
| 52 | 0 | return setProps(window.fh_app_props); |
| 53 | } | |
| 54 | 0 | return cb(new Error("app_config_missing")); |
| 55 | } else { | |
| 56 | ||
| 57 | 1 | setProps(data); |
| 58 | } | |
| 59 | }, | |
| 60 | error: function(req, statusText, error) { | |
| 61 | //fh v2 only | |
| 62 | if(window.fh_app_props){ | |
| 63 | 0 | return setProps(window.fh_app_props); |
| 64 | } | |
| 65 | 0 | logger.error(consts.config_js + " Not Found"); |
| 66 | 0 | cb(new Error("app_config_missing")); |
| 67 | } | |
| 68 | }); | |
| 69 | }; | |
| 70 | ||
| 71 | 1 | var setAppProps = function(props) { |
| 72 | 0 | app_props = props; |
| 73 | }; | |
| 74 | ||
| 75 | 1 | var getAppProps = function() { |
| 76 | 3 | return app_props; |
| 77 | }; | |
| 78 | ||
| 79 | 1 | module.exports = { |
| 80 | load: load, | |
| 81 | getAppProps: getAppProps, | |
| 82 | setAppProps: setAppProps | |
| 83 | }; | |
| 84 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var logger = require("./logger"); |
| 2 | 1 | var queryMap = require("./queryMap"); |
| 3 | 1 | var fhparams = require("./fhparams"); |
| 4 | 1 | var data = require('./data'); |
| 5 | ||
| 6 | 1 | var checkAuth = function(url) { |
| 7 | if (/\_fhAuthCallback/.test(url)) { | |
| 8 | 0 | var qmap = queryMap(url); |
| 9 | if (qmap) { | |
| 10 | 0 | var fhCallback = qmap["_fhAuthCallback"]; |
| 11 | if (fhCallback) { | |
| 12 | if (qmap['result'] && qmap['result'] === 'success') { | |
| 13 | 0 | var sucRes = {'sessionToken': qmap['fh_auth_session'], 'authResponse' : JSON.parse(decodeURIComponent(decodeURIComponent(qmap['authResponse'])))}; |
| 14 | 0 | fhparams.setAuthSessionToken(qmap['fh_auth_session']); |
| 15 | 0 | data.sessionManager.save(qmap['fh_auth_session']); |
| 16 | 0 | window[fhCallback](null, sucRes); |
| 17 | } else { | |
| 18 | 0 | window[fhCallback]({'message':qmap['message']}); |
| 19 | } | |
| 20 | } | |
| 21 | } | |
| 22 | } | |
| 23 | }; | |
| 24 | ||
| 25 | 1 | var handleAuthResponse = function(endurl, res, success, fail){ |
| 26 | if(res.status && res.status === "ok"){ | |
| 27 | ||
| 28 | 0 | var onComplete = function(res){ |
| 29 | if(res.sessionToken){ | |
| 30 | 0 | fhparams.setAuthSessionToken(res.sessionToken); |
| 31 | 0 | data.sessionManager.save(res.sessionToken, function(){ |
| 32 | 0 | return success(res); |
| 33 | }); | |
| 34 | } else { | |
| 35 | 0 | return success(res); |
| 36 | } | |
| 37 | }; | |
| 38 | //for OAuth, a url will be returned which means the user should be directed to that url to authenticate. | |
| 39 | //we try to use the ChildBrower plugin if it can be found. Otherwise send the url to the success function to allow developer to handle it. | |
| 40 | if(res.url){ | |
| 41 | 0 | var inappBrowserWindow = null; |
| 42 | 0 | var locationChange = function(new_url){ |
| 43 | if(new_url.indexOf(endurl) > -1){ | |
| 44 | if(inappBrowserWindow){ | |
| 45 | 0 | inappBrowserWindow.close(); |
| 46 | } | |
| 47 | 0 | var qmap = queryMap(new_url); |
| 48 | if(qmap) { | |
| 49 | if(qmap['result'] && qmap['result'] === 'success'){ | |
| 50 | 0 | var sucRes = {'sessionToken': qmap['fh_auth_session'], 'authResponse' : JSON.parse(decodeURIComponent(decodeURIComponent(qmap['authResponse'])))}; |
| 51 | 0 | onComplete(sucRes); |
| 52 | } else { | |
| 53 | if(fail){ | |
| 54 | 0 | fail("auth_failed", {'message':qmap['message']}); |
| 55 | } | |
| 56 | } | |
| 57 | } else { | |
| 58 | if(fail){ | |
| 59 | 0 | fail("auth_failed", {'message':qmap['message']}); |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | }; | |
| 64 | if(window.PhoneGap || window.cordova){ | |
| 65 | if(window.plugins && window.plugins.childBrowser){ | |
| 66 | //found childbrowser plugin,add the event listener and load it | |
| 67 | //we need to know when the OAuth process is finished by checking for the presence of endurl. If the endurl is found, it means the authentication finished and we should find if it's successful. | |
| 68 | if(typeof window.plugins.childBrowser.showWebPage === "function"){ | |
| 69 | 0 | window.plugins.childBrowser.onLocationChange = locationChange; |
| 70 | 0 | window.plugins.childBrowser.showWebPage(res.url); |
| 71 | 0 | inappBrowserWindow = window.plugins.childBrowser; |
| 72 | } | |
| 73 | } else { | |
| 74 | try { | |
| 75 | 0 | inappBrowserWindow = window.open(res.url, "_blank", 'location=yes'); |
| 76 | 0 | inappBrowserWindow.addEventListener("loadstart", function(ev){ |
| 77 | 0 | locationChange(ev.url); |
| 78 | }); | |
| 79 | } catch(e){ | |
| 80 | 0 | logger.info("InAppBrowser plugin is not intalled."); |
| 81 | 0 | onComplete(res); |
| 82 | } | |
| 83 | } | |
| 84 | } else { | |
| 85 | 0 | document.location.href = res.url; |
| 86 | } | |
| 87 | } else { | |
| 88 | 0 | onComplete(res); |
| 89 | } | |
| 90 | } else { | |
| 91 | if(fail){ | |
| 92 | 0 | fail("auth_failed", res); |
| 93 | } | |
| 94 | } | |
| 95 | }; | |
| 96 | ||
| 97 | //This is mainly for using $fh.auth inside browsers. If the authentication method is OAuth, at the end of the process, the user will be re-directed to | |
| 98 | //a url that we specified for checking if the auth is successful. So we always check the url to see if we are on the re-directed page. | |
| 99 | if (window.addEventListener) { | |
| 100 | 1 | window.addEventListener('load', function(){ |
| 101 | 1 | checkAuth(window.location.href); |
| 102 | }, false); //W3C | |
| 103 | } else if (window.attachEvent) { | |
| 104 | 0 | window.attachEvent('onload', function(){ |
| 105 | 0 | checkAuth(window.location.href); |
| 106 | }); //IE | |
| 107 | } | |
| 108 | ||
| 109 | 1 | module.exports = { |
| 110 | "handleAuthResponse": handleAuthResponse | |
| 111 | }; | |
| 112 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | module.exports = { |
| 2 | "boxprefix": "/box/srv/1.1/", | |
| 3 | "sdk_version": "BUILD_VERSION", | |
| 4 | "config_js": "fhconfig.json", | |
| 5 | "INIT_EVENT": "fhinit", | |
| 6 | "INTERNAL_CONFIG_LOADED_EVENT": "internalfhconfigloaded", | |
| 7 | "CONFIG_LOADED_EVENT": "fhconfigloaded", | |
| 8 | "SESSION_TOKEN_STORAGE_NAME": "fh_session_token", | |
| 9 | "SESSION_TOKEN_KEY_NAME":"sessionToken" | |
| 10 | }; | |
| 11 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | module.exports = { |
| 2 | readCookieValue : function (cookie_name) { | |
| 3 | 0 | var name_str = cookie_name + "="; |
| 4 | 0 | var cookies = document.cookie.split(";"); |
| 5 | for (var i = 0; i < cookies.length; i++) { | |
| 6 | 0 | var c = cookies[i]; |
| 7 | while (c.charAt(0) === ' ') { | |
| 8 | 0 | c = c.substring(1, c.length); |
| 9 | } | |
| 10 | if (c.indexOf(name_str) === 0) { | |
| 11 | 0 | return c.substring(name_str.length, c.length); |
| 12 | } | |
| 13 | } | |
| 14 | 0 | return null; |
| 15 | }, | |
| 16 | ||
| 17 | createCookie : function (cookie_name, cookie_value) { | |
| 18 | 0 | var date = new Date(); |
| 19 | 0 | date.setTime(date.getTime() + 36500 * 24 * 60 * 60 * 1000); //100 years |
| 20 | 0 | var expires = "; expires=" + date.toGMTString(); |
| 21 | 0 | document.cookie = cookie_name + "=" + cookie_value + expires + "; path = /"; |
| 22 | } | |
| 23 | }; | |
| 24 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var Lawnchair = require('../../libs/generated/lawnchair'); |
| 2 | 1 | var lawnchairext = require('./lawnchair-ext'); |
| 3 | 1 | var logger = require('./logger'); |
| 4 | 1 | var constants = require("./constants"); |
| 5 | ||
| 6 | 1 | var data = { |
| 7 | //dom adapter doens't work on windows phone, so don't specify the adapter if the dom one failed | |
| 8 | //we specify the order of lawnchair adapters to use, lawnchair will find the right one to use, to keep backward compatibility, keep the order | |
| 9 | //as dom, webkit-sqlite, localFileStorage, window-name | |
| 10 | DEFAULT_ADAPTERS : ["dom", "webkit-sqlite", "window-name", "titanium"], | |
| 11 | getStorage: function(name, adapters, fail){ | |
| 12 | 1 | var adpts = data.DEFAULT_ADAPTERS; |
| 13 | 1 | var errorHandler = fail || function(){}; |
| 14 | if(adapters && adapters.length > 0){ | |
| 15 | 0 | adpts = (typeof adapters === 'string'?[adapters]: adapters); |
| 16 | } | |
| 17 | 1 | var conf = { |
| 18 | name: name, | |
| 19 | adapter: adpts, | |
| 20 | fail: function(msg, err){ | |
| 21 | 0 | var error_message = 'read/save from/to local storage failed msg:' + msg + ' err:' + err; |
| 22 | 0 | logger.error(error_message, err); |
| 23 | 0 | errorHandler(error_message, {}); |
| 24 | } | |
| 25 | }; | |
| 26 | 1 | var store = Lawnchair(conf, function(){}); |
| 27 | 1 | return store; |
| 28 | }, | |
| 29 | addFileStorageAdapter: function(appProps, hashFunc){ | |
| 30 | 0 | Lawnchair.adapter('localFileStorage', lawnchairext.fileStorageAdapter(appProps, hashFunc)); |
| 31 | }, | |
| 32 | sessionManager: { | |
| 33 | read: function(cb){ | |
| 34 | 1 | data.getStorage(constants.SESSION_TOKEN_STORAGE_NAME).get(constants.SESSION_TOKEN_KEY_NAME, function(session){ |
| 35 | if(cb){ | |
| 36 | 1 | return cb(null, session); |
| 37 | } | |
| 38 | }); | |
| 39 | }, | |
| 40 | exists: function(cb){ | |
| 41 | 0 | data.getStorage(constants.SESSION_TOKEN_STORAGE_NAME).exists(constants.SESSION_TOKEN_KEY_NAME, function(exist){ |
| 42 | if(cb){ | |
| 43 | 0 | return cb(null, exist); |
| 44 | } | |
| 45 | }); | |
| 46 | }, | |
| 47 | remove: function(cb){ | |
| 48 | 0 | data.getStorage(constants.SESSION_TOKEN_STORAGE_NAME).remove(constants.SESSION_TOKEN_KEY_NAME, function(){ |
| 49 | if(cb){ | |
| 50 | 0 | return cb(); |
| 51 | } | |
| 52 | }); | |
| 53 | }, | |
| 54 | save: function(sessionToken, cb){ | |
| 55 | 0 | data.getStorage(constants.SESSION_TOKEN_STORAGE_NAME).save({key: constants.SESSION_TOKEN_KEY_NAME, sessionToken: sessionToken}, function(obj){ |
| 56 | if(cb){ | |
| 57 | 0 | return cb(); |
| 58 | } | |
| 59 | }); | |
| 60 | } | |
| 61 | } | |
| 62 | }; | |
| 63 | ||
| 64 | 1 | module.exports = data; |
| 65 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var cookies = require("./cookies"); |
| 2 | 1 | var uuidModule = require("./uuid"); |
| 3 | 1 | var logger = require("./logger"); |
| 4 | ||
| 5 | 1 | module.exports = { |
| 6 | //try to get the unique device identifier | |
| 7 | "getDeviceId": function(){ | |
| 8 | //check for cordova/phonegap first | |
| 9 | if(typeof window.fhdevice !== "undefined" && typeof window.fhdevice.uuid !== "undefined"){ | |
| 10 | 0 | return window.fhdevice.uuid; |
| 11 | } else if(typeof window.device !== "undefined" && typeof window.device.uuid !== "undefined"){ | |
| 12 | 0 | return window.device.uuid; |
| 13 | } else if(typeof navigator.device !== "undefined" && typeof navigator.device.uuid !== "undefined"){ | |
| 14 | 0 | return navigator.device.uuid; |
| 15 | } else { | |
| 16 | 0 | var _mock_uuid_cookie_name = "mock_uuid"; |
| 17 | 0 | var uuid = cookies.readCookieValue(_mock_uuid_cookie_name); |
| 18 | if(!uuid){ | |
| 19 | 0 | uuid = uuidModule.createUUID(); |
| 20 | 0 | cookies.createCookie(_mock_uuid_cookie_name, uuid); |
| 21 | } | |
| 22 | 0 | return uuid; |
| 23 | } | |
| 24 | }, | |
| 25 | ||
| 26 | //this is for fixing analytics issues when upgrading from io6 to ios7. Probably can be deprecated now | |
| 27 | "getCuidMap": function(){ | |
| 28 | if(typeof window.fhdevice !== "undefined" && typeof window.fhdevice.cuidMap !== "undefined"){ | |
| 29 | 0 | return window.fhdevice.cuidMap; |
| 30 | } else if(typeof window.device !== "undefined" && typeof window.device.cuidMap !== "undefined"){ | |
| 31 | 0 | return window.device.cuidMap; |
| 32 | } else if(typeof navigator.device !== "undefined" && typeof navigator.device.cuidMap !== "undefined"){ | |
| 33 | 0 | return navigator.device.cuidMap; |
| 34 | } | |
| 35 | ||
| 36 | 0 | return null; |
| 37 | }, | |
| 38 | ||
| 39 | "getDestination": function(){ | |
| 40 | 0 | var destination = null; |
| 41 | 0 | var platformsToTest = require("./platformsMap"); |
| 42 | ||
| 43 | ||
| 44 | 0 | var userAgent = navigator.userAgent; |
| 45 | ||
| 46 | 0 | var dest_override = document.location.search.split("fh_destination_code="); |
| 47 | if (dest_override.length > 1) { | |
| 48 | 0 | destination = dest_override[1]; |
| 49 | } else if (typeof window.fh_destination_code !== 'undefined') { | |
| 50 | 0 | destination = window.fh_destination_code; |
| 51 | } else { | |
| 52 | 0 | platformsToTest.forEach(function(testDestination){ |
| 53 | 0 | testDestination.test.forEach(function(destinationTest){ |
| 54 | if(userAgent.indexOf(destinationTest) > -1){ | |
| 55 | 0 | destination = testDestination.destination; |
| 56 | } | |
| 57 | }); | |
| 58 | }); | |
| 59 | } | |
| 60 | ||
| 61 | if(destination == null){ //No user agents were found, set to default web | |
| 62 | 0 | destination = "web"; |
| 63 | } | |
| 64 | ||
| 65 | 0 | logger.debug("destination = " + destination); |
| 66 | ||
| 67 | 0 | return destination; |
| 68 | } | |
| 69 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 2 | var EventEmitter = require('events').EventEmitter; |
| 2 | ||
| 3 | 2 | var emitter = new EventEmitter(); |
| 4 | 2 | emitter.setMaxListeners(0); |
| 5 | ||
| 6 | 2 | module.exports = emitter; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var device = require("./device"); |
| 2 | 1 | var sdkversion = require("./sdkversion"); |
| 3 | 1 | var appProps = require("./appProps"); |
| 4 | 1 | var logger = require("./logger"); |
| 5 | ||
| 6 | 1 | var defaultParams = null; |
| 7 | 1 | var authSessionToken = null; |
| 8 | //TODO: review these options, we probably only needs all of them for init calls, but we shouldn't need all of them for act calls | |
| 9 | 1 | var buildFHParams = function(){ |
| 10 | if(defaultParams){ | |
| 11 | 0 | return defaultParams; |
| 12 | } | |
| 13 | 0 | var fhparams = {}; |
| 14 | 0 | fhparams.cuid = device.getDeviceId(); |
| 15 | 0 | fhparams.cuidMap = device.getCuidMap(); |
| 16 | 0 | fhparams.destination = device.getDestination(); |
| 17 | ||
| 18 | if(window.device || navigator.device){ | |
| 19 | 0 | fhparams.device = window.device || navigator.device; |
| 20 | } | |
| 21 | ||
| 22 | //backward compatible | |
| 23 | if (typeof window.fh_app_version !== 'undefined'){ | |
| 24 | 0 | fhparams.app_version = fh_app_version; |
| 25 | } | |
| 26 | if (typeof window.fh_project_version !== 'undefined'){ | |
| 27 | 0 | fhparams.project_version = fh_project_version; |
| 28 | } | |
| 29 | if (typeof window.fh_project_app_version !== 'undefined'){ | |
| 30 | 0 | fhparams.project_app_version = fh_project_app_version; |
| 31 | } | |
| 32 | 0 | fhparams.sdk_version = sdkversion(); |
| 33 | if(authSessionToken){ | |
| 34 | 0 | fhparams.sessionToken = authSessionToken; |
| 35 | } | |
| 36 | ||
| 37 | 0 | var app_props = appProps.getAppProps(); |
| 38 | if(app_props){ | |
| 39 | 0 | fhparams.appid = app_props.appid; |
| 40 | 0 | fhparams.appkey = app_props.appkey; |
| 41 | 0 | fhparams.projectid = app_props.projectid; |
| 42 | 0 | fhparams.analyticsTag = app_props.analyticsTag; |
| 43 | 0 | fhparams.connectiontag = app_props.connectiontag; |
| 44 | if(app_props.init){ | |
| 45 | 0 | fhparams.init = typeof(app_props.init) === "string" ? JSON.parse(app_props.init) : app_props.init; |
| 46 | } | |
| 47 | } | |
| 48 | ||
| 49 | 0 | defaultParams = fhparams; |
| 50 | 0 | logger.debug("fhparams = ", defaultParams); |
| 51 | 0 | return fhparams; |
| 52 | }; | |
| 53 | ||
| 54 | //TODO: deprecate this. Move to use headers instead | |
| 55 | 1 | var addFHParams = function(params){ |
| 56 | 0 | var p = params || {}; |
| 57 | 0 | p.__fh = buildFHParams(); |
| 58 | 0 | return p; |
| 59 | }; | |
| 60 | ||
| 61 | 1 | var getFHHeaders = function(){ |
| 62 | 0 | var headers = {}; |
| 63 | 0 | var params = buildFHParams(); |
| 64 | for(var name in params){ | |
| 65 | if(params.hasOwnProperty(name)){ | |
| 66 | 0 | headers['X-FH-' + name] = params[name]; |
| 67 | } | |
| 68 | } | |
| 69 | 0 | return headers; |
| 70 | }; | |
| 71 | ||
| 72 | 1 | var setAuthSessionToken = function(sessionToken){ |
| 73 | 0 | authSessionToken = sessionToken; |
| 74 | 0 | defaultParams = null; |
| 75 | }; | |
| 76 | ||
| 77 | 1 | module.exports = { |
| 78 | "buildFHParams": buildFHParams, | |
| 79 | "addFHParams": addFHParams, | |
| 80 | "setAuthSessionToken":setAuthSessionToken, | |
| 81 | "getFHHeaders": getFHHeaders | |
| 82 | }; | |
| 83 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | module.exports = function(fail, req, resStatus, error){ |
| 2 | 0 | var errraw; |
| 3 | 0 | var statusCode = 0; |
| 4 | if(req){ | |
| 5 | try{ | |
| 6 | 0 | statusCode = req.status; |
| 7 | 0 | var res = JSON.parse(req.responseText); |
| 8 | 0 | errraw = res.error || res.msg || res; |
| 9 | if (errraw instanceof Array) { | |
| 10 | 0 | errraw = errraw.join('\n'); |
| 11 | } | |
| 12 | } catch(e){ | |
| 13 | 0 | errraw = req.responseText; |
| 14 | } | |
| 15 | } | |
| 16 | if(fail){ | |
| 17 | 0 | fail(errraw, { |
| 18 | status: statusCode, | |
| 19 | message: resStatus, | |
| 20 | error: error | |
| 21 | }); | |
| 22 | } | |
| 23 | }; | |
| 24 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var constants = require("./constants"); |
| 2 | 1 | var appProps = require("./appProps"); |
| 3 | ||
| 4 | function removeEndSlash(input){ | |
| 5 | 1 | var ret = input; |
| 6 | if(ret.charAt(ret.length - 1) === "/"){ | |
| 7 | 0 | ret = ret.substring(0, ret.length-1); |
| 8 | } | |
| 9 | 1 | return ret; |
| 10 | } | |
| 11 | ||
| 12 | function removeStartSlash(input){ | |
| 13 | 0 | var ret = input; |
| 14 | if(ret.length > 1 && ret.charAt(0) === "/"){ | |
| 15 | 0 | ret = ret.substring(1, ret.length); |
| 16 | } | |
| 17 | 0 | return ret; |
| 18 | } | |
| 19 | ||
| 20 | function CloudHost(cloud_props){ | |
| 21 | 1 | this.cloud_props = cloud_props; |
| 22 | 1 | this.cloud_host = undefined; |
| 23 | 1 | this.app_env = null; |
| 24 | 1 | this.isLegacy = false; |
| 25 | } | |
| 26 | ||
| 27 | 1 | CloudHost.prototype.getHost = function(appType){ |
| 28 | if(this.cloud_host){ | |
| 29 | 0 | return this.cloud_host; |
| 30 | } else { | |
| 31 | 1 | var url; |
| 32 | 1 | var app_type; |
| 33 | if(this.cloud_props && this.cloud_props.hosts){ | |
| 34 | 1 | url = this.cloud_props.hosts.url; |
| 35 | ||
| 36 | if (typeof url === 'undefined') { | |
| 37 | // resolve url the old way i.e. depending on | |
| 38 | // -burnt in app mode | |
| 39 | // -returned dev or live url | |
| 40 | // -returned dev or live type (node or fh(rhino or proxying)) | |
| 41 | 0 | var cloud_host = this.cloud_props.hosts.releaseCloudUrl; |
| 42 | 0 | app_type = this.cloud_props.hosts.releaseCloudType; |
| 43 | ||
| 44 | if(typeof appType !== "undefined" && appType.indexOf("dev") > -1){ | |
| 45 | 0 | cloud_host = this.cloud_props.hosts.debugCloudUrl; |
| 46 | 0 | app_type = this.cloud_props.hosts.debugCloudType; |
| 47 | } | |
| 48 | 0 | url = cloud_host; |
| 49 | } | |
| 50 | } | |
| 51 | 1 | url = removeEndSlash(url); |
| 52 | 1 | this.cloud_host = url; |
| 53 | if(app_type === "fh"){ | |
| 54 | 0 | this.isLegacy = true; |
| 55 | } | |
| 56 | 1 | return url; |
| 57 | } | |
| 58 | }; | |
| 59 | ||
| 60 | 1 | CloudHost.prototype.getActUrl = function(act){ |
| 61 | 0 | var app_props = appProps.getAppProps() || {}; |
| 62 | if(typeof this.cloud_host === "undefined"){ | |
| 63 | 0 | this.getHost(app_props.mode); |
| 64 | } | |
| 65 | if(this.isLegacy){ | |
| 66 | 0 | return this.cloud_host + constants.boxprefix + "act/" + this.cloud_props.domain + "/" + app_props.appid + "/" + act + "/" + app_props.appid; |
| 67 | } else { | |
| 68 | 0 | return this.cloud_host + "/cloud/" + act; |
| 69 | } | |
| 70 | }; | |
| 71 | ||
| 72 | 1 | CloudHost.prototype.getMBAASUrl = function(service){ |
| 73 | 0 | var app_props = appProps.getAppProps() || {}; |
| 74 | if(typeof this.cloud_host === "undefined"){ | |
| 75 | 0 | this.getHost(app_props.mode); |
| 76 | } | |
| 77 | 0 | return this.cloud_host + "/mbaas/" + service; |
| 78 | }; | |
| 79 | ||
| 80 | 1 | CloudHost.prototype.getCloudUrl = function(path){ |
| 81 | 0 | var app_props = appProps.getAppProps() || {}; |
| 82 | if(typeof this.cloud_host === "undefined"){ | |
| 83 | 0 | this.getHost(app_props.mode); |
| 84 | } | |
| 85 | 0 | return this.cloud_host + "/" + removeStartSlash(path); |
| 86 | }; | |
| 87 | ||
| 88 | 1 | CloudHost.prototype.getEnv = function(){ |
| 89 | if(this.app_env){ | |
| 90 | 0 | return this.app_env; |
| 91 | } else { | |
| 92 | if(this.cloud_props && this.cloud_props.hosts){ | |
| 93 | 0 | this.app_env = this.cloud_props.hosts.environment; |
| 94 | } | |
| 95 | } | |
| 96 | 0 | return this.app_env; |
| 97 | }; | |
| 98 | ||
| 99 | 1 | module.exports = CloudHost; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var loadScript = require("./loadScript"); |
| 2 | 1 | var consts = require("./constants"); |
| 3 | 1 | var fhparams = require("./fhparams"); |
| 4 | 1 | var ajax = require("./ajax"); |
| 5 | 1 | var handleError = require("./handleError"); |
| 6 | 1 | var logger = require("./logger"); |
| 7 | 1 | var hashFunc = require("./security/hash"); |
| 8 | 1 | var appProps = require("./appProps"); |
| 9 | 1 | var constants = require("./constants"); |
| 10 | 1 | var events = require("./events"); |
| 11 | 1 | var data = require('./data'); |
| 12 | ||
| 13 | 1 | var init = function(cb) { |
| 14 | 1 | appProps.load(function(err, data) { |
| 15 | if (err) { | |
| 16 | 0 | return cb(err); |
| 17 | } | |
| 18 | // Emit internal config loaded event - SDK will now set appprops | |
| 19 | 1 | events.emit(constants.INTERNAL_CONFIG_LOADED_EVENT, null, data); |
| 20 | 1 | return loadCloudProps(data, cb); |
| 21 | }); | |
| 22 | }; | |
| 23 | ||
| 24 | 1 | var loadCloudProps = function(app_props, callback) { |
| 25 | if (app_props.loglevel) { | |
| 26 | 0 | logger.setLevel(app_props.loglevel); |
| 27 | } | |
| 28 | // If local - shortcircuit the init - just return the host | |
| 29 | if (app_props.local) { | |
| 30 | 1 | var res = { |
| 31 | "domain": "local", | |
| 32 | "firstTime": false, | |
| 33 | "hosts": { | |
| 34 | "debugCloudType": "node", | |
| 35 | "debugCloudUrl": app_props.host, | |
| 36 | "releaseCloudType": "node", | |
| 37 | "releaseCloudUrl": app_props.host, | |
| 38 | "type": "cloud_nodejs", | |
| 39 | "url": app_props.host | |
| 40 | }, | |
| 41 | "init": { | |
| 42 | "trackId": "000000000000000000000000" | |
| 43 | }, | |
| 44 | "status": "ok" | |
| 45 | }; | |
| 46 | ||
| 47 | 1 | return callback(null, { |
| 48 | cloud: res | |
| 49 | }); | |
| 50 | } | |
| 51 | ||
| 52 | ||
| 53 | //now we have app props, add the fileStorageAdapter | |
| 54 | 0 | data.addFileStorageAdapter(app_props, hashFunc); |
| 55 | 0 | var doInit = function(path, appProps, savedHost, storage) { |
| 56 | 0 | var data = fhparams.buildFHParams(); |
| 57 | ||
| 58 | 0 | ajax({ |
| 59 | "url": path, | |
| 60 | "type": "POST", | |
| 61 | "tryJSONP": true, | |
| 62 | "dataType": "json", | |
| 63 | "contentType": "application/json", | |
| 64 | "data": JSON.stringify(data), | |
| 65 | "timeout": appProps.timeout, | |
| 66 | "success": function(initRes) { | |
| 67 | if (storage) { | |
| 68 | 0 | storage.save({ |
| 69 | key: "fh_init", | |
| 70 | value: initRes | |
| 71 | }, function() {}); | |
| 72 | } | |
| 73 | if (callback) { | |
| 74 | 0 | callback(null, { |
| 75 | cloud: initRes | |
| 76 | }); | |
| 77 | } | |
| 78 | }, | |
| 79 | "error": function(req, statusText, error) { | |
| 80 | 0 | var errormsg = "unknown"; |
| 81 | if (req) { | |
| 82 | 0 | errormsg = req.status + " - " + req.responseText; |
| 83 | } | |
| 84 | 0 | logger.error("App init returned error : " + errormsg); |
| 85 | //use the cached host if we have a copy | |
| 86 | if (savedHost && req.status !== 400) { | |
| 87 | 0 | logger.info("Using cached host: " + JSON.stringify(savedHost)); |
| 88 | if (callback) { | |
| 89 | 0 | callback(null, { |
| 90 | cloud: savedHost | |
| 91 | }); | |
| 92 | } | |
| 93 | } else { | |
| 94 | if (req.status === 400) { | |
| 95 | 0 | logger.error(req.responseText); |
| 96 | } else { | |
| 97 | 0 | logger.error("No cached host found. Init failed."); |
| 98 | } | |
| 99 | 0 | handleError(function(msg, err) { |
| 100 | if (callback) { | |
| 101 | 0 | callback({ |
| 102 | error: err, | |
| 103 | message: msg | |
| 104 | }); | |
| 105 | } | |
| 106 | }, req, statusText, error); | |
| 107 | } | |
| 108 | } | |
| 109 | }); | |
| 110 | }; | |
| 111 | ||
| 112 | 0 | var storage = null; |
| 113 | 0 | var path = app_props.host + consts.boxprefix + "app/init"; |
| 114 | try { | |
| 115 | 0 | storage = data.getStorage("fh_init_storage", typeof Titanium !== "undefined"?['titanium']:null); |
| 116 | 0 | storage.get('fh_init', function(storage_res) { |
| 117 | 0 | var savedHost = null; |
| 118 | if (storage_res && storage_res.value !== null && typeof(storage_res.value) !== "undefined" && storage_res !== "") { | |
| 119 | 0 | storage_res = typeof(storage_res) === "string" ? JSON.parse(storage_res) : storage_res; |
| 120 | 0 | storage_res.value = typeof(storage_res.value) === "string" ? JSON.parse(storage_res.value) : storage_res.value; |
| 121 | if (storage_res.value.init) { | |
| 122 | 0 | app_props.init = storage_res.value.init; |
| 123 | } else { | |
| 124 | //keep it backward compatible. | |
| 125 | 0 | app_props.init = typeof(storage_res.value) === "string" ? JSON.parse(storage_res.value) : storage_res.value; |
| 126 | } | |
| 127 | if (storage_res.value.hosts) { | |
| 128 | 0 | savedHost = storage_res.value; |
| 129 | } | |
| 130 | } | |
| 131 | ||
| 132 | 0 | doInit(path, app_props, savedHost, storage); |
| 133 | }); | |
| 134 | } catch (e) { | |
| 135 | //for whatever reason (e.g. localStorage is disabled) Lawnchair is failed to init, just do the init | |
| 136 | 0 | doInit(path, app_props, null, null); |
| 137 | } | |
| 138 | }; | |
| 139 | ||
| 140 | 1 | module.exports = { |
| 141 | "init": init, | |
| 142 | "loadCloudProps": loadCloudProps | |
| 143 | }; | |
| 144 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var fileStorageAdapter = function (app_props, hashFunc) { |
| 2 | // private methods | |
| 3 | ||
| 4 | function doLog(mess){ | |
| 5 | if(console){ | |
| 6 | 0 | console.log(mess); |
| 7 | } | |
| 8 | } | |
| 9 | ||
| 10 | 0 | var fail = function (e, i) { |
| 11 | if(console) { | |
| 12 | 0 | console.log('error in file system adapter !', e, i); |
| 13 | } else { | |
| 14 | 0 | throw e; |
| 15 | } | |
| 16 | }; | |
| 17 | ||
| 18 | ||
| 19 | function filenameForKey(key, cb) { | |
| 20 | 0 | key = app_props.appid + key; |
| 21 | ||
| 22 | 0 | hashFunc({ |
| 23 | algorithm: "MD5", | |
| 24 | text: key | |
| 25 | }, function(result) { | |
| 26 | 0 | var filename = result.hashvalue + '.txt'; |
| 27 | if (typeof navigator.externalstorage !== "undefined") { | |
| 28 | 0 | navigator.externalstorage.enable(function handleSuccess(res){ |
| 29 | 0 | var path = filename; |
| 30 | if(res.path ) { | |
| 31 | 0 | path = res.path; |
| 32 | if(!path.match(/\/$/)) { | |
| 33 | 0 | path += '/'; |
| 34 | } | |
| 35 | 0 | path += filename; |
| 36 | } | |
| 37 | 0 | filename = path; |
| 38 | 0 | return cb(filename); |
| 39 | },function handleError(err){ | |
| 40 | 0 | return cb(filename); |
| 41 | }); | |
| 42 | } else { | |
| 43 | 0 | doLog('filenameForKey key=' + key+ ' , Filename: ' + filename); |
| 44 | 0 | return cb(filename); |
| 45 | } | |
| 46 | }); | |
| 47 | } | |
| 48 | ||
| 49 | 0 | return { |
| 50 | ||
| 51 | 0 | valid: function () { return !!(window.requestFileSystem); }, |
| 52 | ||
| 53 | init : function (options, callback){ | |
| 54 | //calls the parent function fn and applies this scope | |
| 55 | if(options && 'function' === typeof options.fail ) { | |
| 56 | 0 | fail = options.fail; |
| 57 | } | |
| 58 | if (callback) { | |
| 59 | 0 | this.fn(this.name, callback).call(this, this); |
| 60 | } | |
| 61 | }, | |
| 62 | ||
| 63 | keys: function (callback){ | |
| 64 | 0 | throw "Currently not supported"; |
| 65 | }, | |
| 66 | ||
| 67 | save : function (obj, callback){ | |
| 68 | 0 | var key = obj.key; |
| 69 | 0 | var value = obj.val||obj.value; |
| 70 | 0 | filenameForKey(key, function(hash) { |
| 71 | 0 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function gotFS(fileSystem) { |
| 72 | ||
| 73 | 0 | fileSystem.root.getFile(hash, { |
| 74 | create: true | |
| 75 | }, function gotFileEntry(fileEntry) { | |
| 76 | 0 | fileEntry.createWriter(function gotFileWriter(writer) { |
| 77 | 0 | writer.onwrite = function() { |
| 78 | 0 | return callback({ |
| 79 | key: key, | |
| 80 | val: value | |
| 81 | }); | |
| 82 | }; | |
| 83 | 0 | writer.write(value); |
| 84 | }, function() { | |
| 85 | 0 | fail('[save] Failed to create file writer'); |
| 86 | }); | |
| 87 | }, function() { | |
| 88 | 0 | fail('[save] Failed to getFile'); |
| 89 | }); | |
| 90 | }, function() { | |
| 91 | 0 | fail('[save] Failed to requestFileSystem'); |
| 92 | }); | |
| 93 | }); | |
| 94 | }, | |
| 95 | ||
| 96 | batch : function (records, callback){ | |
| 97 | 0 | throw "Currently not supported"; |
| 98 | }, | |
| 99 | ||
| 100 | get : function (key, callback){ | |
| 101 | 0 | filenameForKey(key, function(hash) { |
| 102 | 0 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function gotFS(fileSystem) { |
| 103 | 0 | fileSystem.root.getFile(hash, {}, function gotFileEntry(fileEntry) { |
| 104 | 0 | fileEntry.file(function gotFile(file) { |
| 105 | 0 | var reader = new FileReader(); |
| 106 | 0 | reader.onloadend = function (evt) { |
| 107 | 0 | var text = evt.target.result; |
| 108 | // Check for URLencoded | |
| 109 | // PG 2.2 bug in readAsText() | |
| 110 | try { | |
| 111 | 0 | text = decodeURIComponent(text); |
| 112 | } catch (e) { | |
| 113 | // Swallow exception if not URLencoded | |
| 114 | // Just use the result | |
| 115 | } | |
| 116 | 0 | return callback({ |
| 117 | key: key, | |
| 118 | val: text | |
| 119 | }); | |
| 120 | }; | |
| 121 | 0 | reader.readAsText(file); |
| 122 | }, function() { | |
| 123 | 0 | fail('[load] Failed to getFile'); |
| 124 | }); | |
| 125 | }, function() { | |
| 126 | // Success callback on key load failure | |
| 127 | 0 | callback({ |
| 128 | key: key, | |
| 129 | val: null | |
| 130 | }); | |
| 131 | }); | |
| 132 | }, function() { | |
| 133 | 0 | fail('[load] Failed to get fileSystem'); |
| 134 | }); | |
| 135 | }); | |
| 136 | }, | |
| 137 | ||
| 138 | exists : function (key, callback){ | |
| 139 | 0 | filenameForKey(key,function (hash){ |
| 140 | 0 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function gotFS(fileSystem) { |
| 141 | 0 | fileSystem.root.getFile(hash, {}, |
| 142 | function gotFileEntry(fileEntry) { | |
| 143 | 0 | return callback(true); |
| 144 | }, function (err){ | |
| 145 | 0 | return callback(false); |
| 146 | }); | |
| 147 | }); | |
| 148 | }); | |
| 149 | }, | |
| 150 | ||
| 151 | all : function (callback){ | |
| 152 | 0 | throw "Currently not supported"; |
| 153 | }, | |
| 154 | ||
| 155 | remove : function (key, callback){ | |
| 156 | 0 | filenameForKey(key, function(hash) { |
| 157 | ||
| 158 | 0 | window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function gotFS(fileSystem) { |
| 159 | 0 | fileSystem.root.getFile(hash, {}, function gotFileEntry(fileEntry) { |
| 160 | ||
| 161 | 0 | fileEntry.remove(function() { |
| 162 | 0 | return callback({ |
| 163 | key: key, | |
| 164 | val: null | |
| 165 | }); | |
| 166 | }, function() { | |
| 167 | 0 | fail('[remove] Failed to remove file'); |
| 168 | }); | |
| 169 | }, function() { | |
| 170 | 0 | fail('[remove] Failed to getFile'); |
| 171 | }); | |
| 172 | }, function() { | |
| 173 | 0 | fail('[remove] Failed to get fileSystem'); |
| 174 | }); | |
| 175 | }); | |
| 176 | }, | |
| 177 | ||
| 178 | nuke : function (callback){ | |
| 179 | 0 | throw "Currently not supported"; |
| 180 | } | |
| 181 | ||
| 182 | ||
| 183 | }; | |
| 184 | }; | |
| 185 | ||
| 186 | 1 | module.exports = { |
| 187 | fileStorageAdapter: fileStorageAdapter | |
| 188 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | module.exports = function (url, callback) { |
| 2 | 0 | var script; |
| 3 | 0 | var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement; |
| 4 | 0 | script = document.createElement("script"); |
| 5 | 0 | script.async = "async"; |
| 6 | 0 | script.src = url; |
| 7 | 0 | script.type = "text/javascript"; |
| 8 | 0 | script.onload = script.onreadystatechange = function () { |
| 9 | if (!script.readyState || /loaded|complete/.test(script.readyState)) { | |
| 10 | 0 | script.onload = script.onreadystatechange = null; |
| 11 | if (head && script.parentNode) { | |
| 12 | 0 | head.removeChild(script); |
| 13 | } | |
| 14 | 0 | script = undefined; |
| 15 | if (callback && typeof callback === "function") { | |
| 16 | 0 | callback(); |
| 17 | } | |
| 18 | } | |
| 19 | }; | |
| 20 | 0 | head.insertBefore(script, head.firstChild); |
| 21 | }; | |
| 22 |
| Line | Hits | Source |
|---|---|---|
| 1 | 2 | var console = require('console'); |
| 2 | 2 | var log = require('loglevel'); |
| 3 | ||
| 4 | 2 | log.setLevel('info'); |
| 5 | ||
| 6 | /** | |
| 7 | * APIs: | |
| 8 | * see https://github.com/pimterry/loglevel. | |
| 9 | * In short, you can use: | |
| 10 | * log.setLevel(loglevel) - default to info | |
| 11 | * log.enableAll() - enable all log messages | |
| 12 | * log.disableAll() - disable all log messages | |
| 13 | * | |
| 14 | * log.trace(msg) | |
| 15 | * log.debug(msg) | |
| 16 | * log.info(msg) | |
| 17 | * log.warn(msg) | |
| 18 | * log.error(msg) | |
| 19 | * | |
| 20 | * Available levels: { "TRACE": 0, "DEBUG": 1, "INFO": 2, "WARN": 3, "ERROR": 4, "SILENT": 5} | |
| 21 | * Use either string or integer value | |
| 22 | */ | |
| 23 | 2 | module.exports = log; |
| Line | Hits | Source |
|---|---|---|
| 1 | 2 | module.exports = function(url) { |
| 2 | 4 | var qmap = {}; |
| 3 | 4 | var i = url.split("?"); |
| 4 | if (i.length === 2) { | |
| 5 | 4 | var queryString = i[1]; |
| 6 | 4 | var pairs = queryString.split("&"); |
| 7 | 4 | qmap = {}; |
| 8 | for (var p = 0; p < pairs.length; p++) { | |
| 9 | 8 | var q = pairs[p]; |
| 10 | 8 | var qp = q.split("="); |
| 11 | 8 | qmap[qp[0]] = qp[1]; |
| 12 | } | |
| 13 | } | |
| 14 | 4 | return qmap; |
| 15 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var constants = require("./constants"); |
| 2 | ||
| 3 | 1 | module.exports = function() { |
| 4 | 0 | var type = "FH_JS_SDK"; |
| 5 | if (typeof window.fh_destination_code !== 'undefined') { | |
| 6 | 0 | type = "FH_HYBRID_SDK"; |
| 7 | } else if(window.PhoneGap || window.cordova) { | |
| 8 | 0 | type = "FH_PHONEGAP_SDK"; |
| 9 | } | |
| 10 | 0 | return type + "/" + constants.sdk_version; |
| 11 | }; | |
| 12 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var rsa = require("../../../libs/rsa"); |
| 2 | 1 | var SecureRandom = rsa.SecureRandom; |
| 3 | 1 | var byte2Hex = rsa.byte2Hex; |
| 4 | ||
| 5 | 1 | var generateRandomKey = function(keysize){ |
| 6 | 0 | var r = new SecureRandom(); |
| 7 | 0 | var key = new Array(keysize); |
| 8 | 0 | r.nextBytes(key); |
| 9 | 0 | var result = ""; |
| 10 | for(var i=0;i<key.length;i++){ | |
| 11 | 0 | result += byte2Hex(key[i]); |
| 12 | } | |
| 13 | 0 | return result; |
| 14 | }; | |
| 15 | ||
| 16 | 1 | var aes_keygen = function(p, s, f){ |
| 17 | if (!p.params.keysize) { | |
| 18 | 0 | f('no_params_keysize', {}, p); |
| 19 | 0 | return; |
| 20 | } | |
| 21 | if (p.params.algorithm.toLowerCase() !== "aes") { | |
| 22 | 0 | f('keygen_bad_algorithm', {}, p); |
| 23 | 0 | return; |
| 24 | } | |
| 25 | 0 | var keysize = parseInt(p.params.keysize, 10); |
| 26 | //keysize is in bit, need to convert to bytes to generate random key | |
| 27 | //but the legacy code has a bug, it doesn't do the convert, so if the keysize is less than 100, don't convert | |
| 28 | if(keysize > 100){ | |
| 29 | 0 | keysize = keysize/8; |
| 30 | } | |
| 31 | if(typeof SecureRandom === "undefined"){ | |
| 32 | 0 | return f("security library is not loaded."); |
| 33 | } | |
| 34 | 0 | return s({ |
| 35 | 'algorithm': 'AES', | |
| 36 | 'secretkey': generateRandomKey(keysize), | |
| 37 | 'iv': generateRandomKey(keysize) | |
| 38 | }); | |
| 39 | }; | |
| 40 | ||
| 41 | 1 | module.exports = aes_keygen; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var CryptoJS = require("../../../libs/generated/crypto"); |
| 2 | ||
| 3 | 1 | var encrypt = function(p, s, f){ |
| 4 | 0 | var fields = ['key', 'plaintext', 'iv']; |
| 5 | if(p.params.algorithm.toLowerCase() !== "aes"){ | |
| 6 | 0 | return f('encrypt_bad_algorithm', {}, p); |
| 7 | } | |
| 8 | for (var i = 0; i < fields; i++) { | |
| 9 | 0 | var field = fields[i]; |
| 10 | if (!p.params[field]) { | |
| 11 | 0 | return f('no_params_' + field, {}, p); |
| 12 | } | |
| 13 | } | |
| 14 | 0 | var encrypted = CryptoJS.AES.encrypt(p.params.plaintext, CryptoJS.enc.Hex.parse(p.params.key), {iv: CryptoJS.enc.Hex.parse(p.params.iv)}); |
| 15 | 0 | cipher_text = CryptoJS.enc.Hex.stringify(encrypted.ciphertext); |
| 16 | 0 | return s({ciphertext: cipher_text}); |
| 17 | }; | |
| 18 | ||
| 19 | 1 | var decrypt = function(p, s, f){ |
| 20 | 0 | var fields = ['key', 'ciphertext', 'iv']; |
| 21 | if(p.params.algorithm.toLowerCase() !== "aes"){ | |
| 22 | 0 | return f('decrypt_bad_algorithm', {}, p); |
| 23 | } | |
| 24 | for (var i = 0; i < fields; i++) { | |
| 25 | 0 | var field = fields[i]; |
| 26 | if (!p.params[field]) { | |
| 27 | 0 | return f('no_params_' + field, {}, p); |
| 28 | } | |
| 29 | } | |
| 30 | 0 | var data = CryptoJS.enc.Hex.parse(p.params.ciphertext); |
| 31 | 0 | var encodeData = CryptoJS.enc.Base64.stringify(data); |
| 32 | 0 | var decrypted = CryptoJS.AES.decrypt(encodeData, CryptoJS.enc.Hex.parse(p.params.key), {iv: CryptoJS.enc.Hex.parse(p.params.iv)}); |
| 33 | ||
| 34 | try { | |
| 35 | 0 | return s({plaintext:decrypted.toString(CryptoJS.enc.Utf8)}); |
| 36 | } catch (e) { | |
| 37 | 0 | return f(e); |
| 38 | } | |
| 39 | }; | |
| 40 | ||
| 41 | 1 | module.exports = { |
| 42 | encrypt: encrypt, | |
| 43 | decrypt: decrypt | |
| 44 | }; | |
| 45 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var CryptoJS = require("../../../libs/generated/crypto"); |
| 2 | ||
| 3 | ||
| 4 | 1 | var hash = function(p, s, f){ |
| 5 | if (!p.params.text) { | |
| 6 | 0 | f('hash_no_text', {}, p); |
| 7 | 0 | return; |
| 8 | } | |
| 9 | 0 | var hashValue; |
| 10 | if (p.params.algorithm.toLowerCase() === "md5") { | |
| 11 | 0 | hashValue = CryptoJS.MD5(p.params.text).toString(CryptoJS.enc.Hex); |
| 12 | } else if(p.params.algorithm.toLowerCase() === "sha1"){ | |
| 13 | 0 | hashValue = CryptoJS.SHA1(p.params.text).toString(CryptoJS.enc.Hex); |
| 14 | } else if(p.params.algorithm.toLowerCase() === "sha256"){ | |
| 15 | 0 | hashValue = CryptoJS.SHA256(p.params.text).toString(CryptoJS.enc.Hex); |
| 16 | } else if(p.params.algorithm.toLowerCase() === "sha512"){ | |
| 17 | 0 | hashValue = CryptoJS.SHA512(p.params.text).toString(CryptoJS.enc.Hex); |
| 18 | } else { | |
| 19 | 0 | return f("hash_unsupported_algorithm: " + p.params.algorithm); |
| 20 | } | |
| 21 | 0 | return s({"hashvalue": hashValue}); |
| 22 | }; | |
| 23 | ||
| 24 | 1 | module.exports = hash; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var rsa = require("../../../libs/rsa"); |
| 2 | 1 | var RSAKey = rsa.RSAKey; |
| 3 | ||
| 4 | 1 | var encrypt = function(p, s, f){ |
| 5 | 0 | var fields = ['modulu', 'plaintext']; |
| 6 | if(p.params.algorithm.toLowerCase() !== "rsa"){ | |
| 7 | 0 | return f('encrypt_bad_algorithm', {}, p); |
| 8 | } | |
| 9 | for (var i = 0; i < fields; i++) { | |
| 10 | 0 | var field = fields[i]; |
| 11 | if (!p.params[field]) { | |
| 12 | 0 | return f('no_params_' + field, {}, p); |
| 13 | } | |
| 14 | } | |
| 15 | 0 | var key = new RSAKey(); |
| 16 | 0 | key.setPublic(p.params.modulu, "10001"); |
| 17 | 0 | var ori_text = p.params.plaintext; |
| 18 | 0 | cipher_text = key.encrypt(ori_text); |
| 19 | 0 | return s({ciphertext:cipher_text}); |
| 20 | }; | |
| 21 | ||
| 22 | 1 | module.exports = { |
| 23 | encrypt: encrypt | |
| 24 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var cloudAPI = require("./api_cloud"); |
| 2 | ||
| 3 | 1 | module.exports = function (params, success, failure) { |
| 4 | 0 | cloudAPI({ |
| 5 | 'path': '/mbaas/sync/' + params.dataset_id, | |
| 6 | 'method': 'post', | |
| 7 | 'data': params.req | |
| 8 | }, success, failure); | |
| 9 | }; |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | module.exports = { |
| 2 | createUUID : function () { | |
| 3 | //from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript | |
| 4 | //based on RFC 4122, section 4.4 (Algorithms for creating UUID from truely random pr pseudo-random number) | |
| 5 | 0 | var s = []; |
| 6 | 0 | var hexDigitals = "0123456789ABCDEF"; |
| 7 | for (var i = 0; i < 32; i++) { | |
| 8 | 0 | s[i] = hexDigitals.substr(Math.floor(Math.random() * 0x10), 1); |
| 9 | } | |
| 10 | 0 | s[12] = "4"; |
| 11 | 0 | s[16] = hexDigitals.substr((s[16] & 0x3) | 0x8, 1); |
| 12 | 0 | var uuid = s.join(""); |
| 13 | 0 | return uuid; |
| 14 | } | |
| 15 | }; | |
| 16 |
| Line | Hits | Source |
|---|---|---|
| 1 | 1 | var initializer = require("./initializer"); |
| 2 | 1 | var events = require("./events"); |
| 3 | 1 | var CloudHost = require("./hosts"); |
| 4 | 1 | var constants = require("./constants"); |
| 5 | 1 | var logger = require("./logger"); |
| 6 | 1 | var data = require('./data'); |
| 7 | 1 | var fhparams = require('./fhparams'); |
| 8 | ||
| 9 | //the cloud configurations | |
| 10 | 1 | var cloud_host; |
| 11 | ||
| 12 | 1 | var is_initialising = false; |
| 13 | 1 | var is_cloud_ready = false; |
| 14 | 1 | var init_error = null; |
| 15 | ||
| 16 | ||
| 17 | 1 | var ready = function(cb){ |
| 18 | if(is_cloud_ready){ | |
| 19 | 0 | return cb(null, {host: getCloudHostUrl()}); |
| 20 | } else { | |
| 21 | 1 | events.once(constants.INIT_EVENT, function(err, host){ |
| 22 | 1 | return cb(err, host); |
| 23 | }); | |
| 24 | if(!is_initialising){ | |
| 25 | 1 | is_initialising = true; |
| 26 | 1 | var fhinit = function(){ |
| 27 | 1 | data.sessionManager.read(function(err, session){ |
| 28 | //load the persisted sessionToken and set it for the session | |
| 29 | if(session && session.sessionToken){ | |
| 30 | 0 | fhparams.setAuthSessionToken(session.sessionToken); |
| 31 | } | |
| 32 | 1 | initializer.init(function(err, initRes){ |
| 33 | 1 | is_initialising = false; |
| 34 | if(err){ | |
| 35 | 0 | init_error = err; |
| 36 | 0 | return events.emit(constants.INIT_EVENT, err); |
| 37 | } else { | |
| 38 | 1 | init_error = null; |
| 39 | 1 | is_cloud_ready = true; |
| 40 | 1 | cloud_host = new CloudHost(initRes.cloud); |
| 41 | 1 | return events.emit(constants.INIT_EVENT, null, {host: getCloudHostUrl()}); |
| 42 | } | |
| 43 | }); | |
| 44 | }); | |
| 45 | }; | |
| 46 | if(typeof window.cordova !== "undefined" || typeof window.phonegap !== "undefined"){ | |
| 47 | //if we are running inside cordova/phonegap, only init after device is ready to ensure the device id is the right one | |
| 48 | 0 | document.addEventListener("deviceready", fhinit, false); |
| 49 | } else { | |
| 50 | 1 | fhinit(); |
| 51 | } | |
| 52 | } | |
| 53 | } | |
| 54 | }; | |
| 55 | ||
| 56 | 1 | var getCloudHost = function(){ |
| 57 | 0 | return cloud_host; |
| 58 | }; | |
| 59 | ||
| 60 | 1 | var getCloudHostUrl = function(){ |
| 61 | if(typeof cloud_host !== "undefined"){ | |
| 62 | 1 | var appProps = require("./appProps").getAppProps(); |
| 63 | 1 | return cloud_host.getHost(appProps.mode); |
| 64 | } else { | |
| 65 | 0 | return undefined; |
| 66 | } | |
| 67 | }; | |
| 68 | ||
| 69 | 1 | var isReady = function(){ |
| 70 | 1 | return is_cloud_ready; |
| 71 | }; | |
| 72 | ||
| 73 | 1 | var getInitError = function(){ |
| 74 | 1 | return init_error; |
| 75 | }; | |
| 76 | ||
| 77 | //for test | |
| 78 | 1 | var reset = function(){ |
| 79 | 0 | is_cloud_ready = false; |
| 80 | 0 | is_initialising = false; |
| 81 | 0 | cloud_host = undefined; |
| 82 | 0 | init_error = undefined; |
| 83 | 0 | ready(function(){ |
| 84 | ||
| 85 | }); | |
| 86 | }; | |
| 87 | ||
| 88 | 1 | ready(function(error, host){ |
| 89 | if(error){ | |
| 90 | if(error.message !== "app_config_missing"){ | |
| 91 | 0 | logger.error("Failed to initialise fh."); |
| 92 | } else { | |
| 93 | 0 | logger.info("No fh config file"); |
| 94 | } | |
| 95 | } else { | |
| 96 | 1 | logger.info("fh cloud is ready"); |
| 97 | } | |
| 98 | }); | |
| 99 | ||
| 100 | 1 | module.exports = { |
| 101 | ready: ready, | |
| 102 | isReady: isReady, | |
| 103 | getCloudHost: getCloudHost, | |
| 104 | getCloudHostUrl: getCloudHostUrl, | |
| 105 | getInitError: getInitError, | |
| 106 | reset: reset | |
| 107 | }; |