var EzOAuth2; EzOAuth2 = (function() { function EzOAuth2(root_url, customer_id, customer_secret, endpoint_uri) { var id; this.root_url = root_url; this.customer_id = customer_id; this.customer_secret = customer_secret; this.endpoint_uri = endpoint_uri; id = Ti.Utils.md5HexDigest(this.root_url + this.customer_id + this.customer_secret + this.endpoint_uri); this.access_token = Ti.App.Properties.getString(id + '_access', null); this.refresh_token = Ti.App.Properties.getString(id + '_refresh', null); this.update_stamp = Ti.App.Properties.getString(id + '_updated', null); this.expiry_time = Ti.App.Properties.getInt(id + '_expires', 3600); Ti.API.debug('EzOAuth2 object initialized.'); } EzOAuth2.prototype.saveSettings = function() { var id; id = Ti.Utils.md5HexDigest(this.root_url + this.customer_id + this.customer_secret + this.endpoint_uri); Ti.App.Properties.setString(id + '_access', this.access_token); Ti.App.Properties.setString(id + '_refresh', this.refresh_token); Ti.App.Properties.setString(id + '_updated', this.update_stamp); return Ti.App.Properties.setInt(id + '_expires', this.expiry_time); }; EzOAuth2.prototype.getAuthView = function(success_function, cancel_function) { var authUrl, wv; authUrl = this.root_url + '/oauth/authorize' + '?response_type=token' + '&client_id=' + this.customer_id + '&redirect_uri=' + encodeURIComponent(this.endpoint_uri); Ti.API.debug('authUrl: ' + authUrl); wv = Ti.UI.createWebView({ url: authUrl }); wv.parent_obj = this; wv.success_function = success_function; wv.cancel_function = cancel_function; wv.addEventListener('beforeload', this.processOAuth2Response); this.auth_web = wv; this.auth_btn = Ti.UI.createButton({ left: 0, top: 0, width: 50, height: 30, title: "Cancel" }); this.auth_btn.click_function = cancel_function; this.auth_btn.addEventListener('click', function(e) { e.source.auth_view.hide(); return e.source.click_function(e); }); this.auth_view = Ti.UI.createView(); this.auth_view.add(this.auth_web); this.auth_view.add(this.auth_btn); this.auth_web.auth_view = this.auth_view; this.auth_btn.auth_view = this.auth_view; return this.auth_view; }; EzOAuth2.prototype.doRequest = function(url, success_function, type) { var xhr; if (type == null) { type = 'GET'; } if (this.access_token === null) { Ti.API.warning('No access token.'); return false; } if (Ti.Network.networkType === Ti.Network.NETWORK_NONE) { Ti.API.warning('No Internet connection.'); return false; } xhr = Ti.Network.createHTTPClient({ onerror: function() { return Ti.API.error('ERROR communicating. (' + this.responseText + ')'); }, onload: success_function }); xhr.open(type, this.root_url + '/api/ezp/v1' + url); xhr.setRequestHeader('Authorization', 'OAuth ' + this.access_token); xhr.setRequestHeader('Accept', 'application/json'); return xhr.send(); }; EzOAuth2.prototype.getNode = function(node_id, success_function) { return this.doRequest('/content/node/' + node_id, success_function); }; EzOAuth2.prototype.getNodeFields = function(node_id, success_function) { return this.doRequest('/content/node/' + node_id + '/fields', success_function); }; EzOAuth2.prototype.getNodeField = function(node_id, field_id, success_function) { return this.doRequest('/content/node/' + node_id + '/field/' + field_id, success_function); }; EzOAuth2.prototype.getNodeList = function(root_node_id, success_function, offset, limit, sortKey, sortType) { var req_url; if (offset == null) { offset = null; } if (limit == null) { limit = null; } if (sortKey == null) { sortKey = null; } if (sortType == null) { sortType = null; } req_url = '/content/node/' + root_node_id + '/list'; if (offset != null) { req_url += '/offset/' + offset; } if (limit != null) { req_url += '/limit/' + limit; } if (sortKey != null) { req_url += '/sort/' + sortKey; if (sortType != null) { req_url += '/' + sortType; } } return this.doRequest(req_url, success_function); }; EzOAuth2.prototype.getGetParameters = function(url) { var pair, pair_split, pairs, vars, _i, _len; vars = {}; pairs = url.slice(url.indexOf('?') + 1).split('&'); for (_i = 0, _len = pairs.length; _i < _len; _i++) { pair = pairs[_i]; Ti.API.debug('Now splitting: "' + pair + '"'); pair_split = pair.split('='); vars[pair_split[0]] = pair_split[1]; } return vars; }; EzOAuth2.prototype.processOAuth2Response = function(e) { var params; Ti.API.debug('[beforeload] URL: ' + e.url); Ti.API.debug('Endpoint URI: ' + e.source.parent_obj.endpoint_uri); Ti.API.debug('Index:' + e.url.indexOf(e.source.parent_obj.endpoint_uri)); if (e.url.indexOf(e.source.parent_obj.endpoint_uri) === 0) { Ti.API.info('ENDPOINT URL DETECTED!!!!'); e.source.parent_obj.final_url = e.url; params = e.source.parent_obj.getGetParameters(e.url); e.source.parent_obj.access_token = params['access_token']; e.source.parent_obj.refresh_token = params['refresh_token']; e.source.parent_obj.expiry_time = params['expires_in']; e.source.parent_obj.update_stamp = Math.round(+new Date() / 1000); e.source.auth_view.hide(); Ti.API.info('Got new token. Will expire in ' + params['expires_in'] + ' seconds.'); return e.source.success_function(e); } }; return EzOAuth2; })();