diff --git a/plugin.xml b/plugin.xml index 9ba1ff9..d9e0d89 100644 --- a/plugin.xml +++ b/plugin.xml @@ -16,12 +16,16 @@ - + + + + + diff --git a/www/cookie-handler.js b/www/cookie-handler.js index 549d84d..a4e3ad9 100644 --- a/www/cookie-handler.js +++ b/www/cookie-handler.js @@ -1,6 +1,8 @@ var pluginId = module.id.slice(0, module.id.indexOf('.')); var ToughCookie = require(pluginId + '.tough-cookie'); -var cookieJar = new ToughCookie.CookieJar(); +var WebStorageCookieStore = require(pluginId + '.local-storage-store'); +var store = new WebStorageCookieStore(); +var cookieJar = new ToughCookie.CookieJar(store); module.exports = { setCookie: setCookie, diff --git a/www/local-storage-store.js b/www/local-storage-store.js new file mode 100644 index 0000000..76d686c --- /dev/null +++ b/www/local-storage-store.js @@ -0,0 +1,186 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Exponent + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + * Based on "tough-cookie-web-storage-store" v1.0.0 + * Thanks James Ide: https://github.com/exponentjs/tough-cookie-web-storage-store + * + * Modified by Sefa Ilkimen for cordova plugin integration + * + */ + +'use strict'; + +var pluginId = module.id.slice(0, module.id.indexOf('.')); +var ToughCookie = require(pluginId + '.tough-cookie'); +var _ = require(pluginId + '.lodash'); + +var Cookie = ToughCookie.Cookie; + +var STORE_KEY = '__cookieStore__'; + +function WebStorageCookieStore(storage) { + ToughCookie.Store.call(this); + this._storage = storage || window.localStorage; + this.synchronous = true; +} + +WebStorageCookieStore.prototype = Object.create(ToughCookie.Store); + +WebStorageCookieStore.prototype.findCookie = function(domain, path, key, callback) { + var store = this._readStore(); + var cookie = _.get(store, [domain, path, key], null); + + callback(null, Cookie.fromJSON(cookie)); +}; + +WebStorageCookieStore.prototype.findCookies = function(domain, path, callback) { + if (!domain) { + callback(null, []); + return; + } + + var that = this; + var cookies = []; + var store = this._readStore(); + var domains = ToughCookie.permuteDomain(domain) || [domain]; + + domains.forEach(function(domain) { + if (!store[domain]) { + return; + } + + var matchingPaths = Object.keys(store[domain]); + + if (path != null) { + matchingPaths = matchingPaths.filter(function(cookiePath) { + return that._isOnPath(cookiePath, path); + }); + } + + matchingPaths.forEach(function(path) { + Array.prototype.push.apply(cookies, _.values(store[domain][path])); + }); + }); + + cookies = cookies.map(function(cookie) { + return Cookie.fromJSON(cookie); + }); + + callback(null, cookies); +}; + +/** + * Returns whether `cookiePath` is on the given `urlPath` + */ +WebStorageCookieStore.prototype._isOnPath = function(cookiePath, urlPath) { + if (!cookiePath) { + return false; + } + + if (cookiePath === urlPath) { + return true; + } + + if (!urlPath.startsWith(cookiePath)) { + return false; + } + + if (cookiePath[cookiePath.length - 1] !== '/' && urlPath[cookiePath.length] !== '/') { + return false; + } + + return true; +}; + +WebStorageCookieStore.prototype.putCookie = function(cookie, callback) { + var store = this._readStore(); + + _.set(store, [cookie.domain, cookie.path, cookie.key], cookie); + this._writeStore(store); + callback(null); +}; + +WebStorageCookieStore.prototype.updateCookie = function(oldCookie, newCookie, callback) { + this.putCookie(newCookie, callback); +}; + + +WebStorageCookieStore.prototype.removeCookie = function(domain, path, key, callback) { + var store = this._readStore(); + + _.unset(store, [domain, path, key]); + this._writeStore(store); + callback(null); +}; + +WebStorageCookieStore.prototype.removeCookies = function(domain, path, callback) { + var store = this._readStore(); + + if (path == null) { + _.unset(store, [domain]); + } else { + _.unset(store, [domain, path]); + } + + this._writeStore(store); + callback(null); +}; + +WebStorageCookieStore.prototype.getAllCookies = function(callback) { + var cookies = []; + var store = this._readStore(); + + Object.keys(store).forEach(function(domain) { + Object.keys(store[domain]).forEach(function(path) { + Array.protype.push.apply(cookies, _.values(store[domain][path])); + }); + }); + + cookies = cookies.map(function(cookie) { + Cookie.fromJSON(cookie) + }); + + cookies.sort(function(c1, c2) { + return (c1.creationIndex || 0) - (c2.creationIndex || 0); + }); + + callback(null, cookies); +}; + +WebStorageCookieStore.prototype._readStore = function() { + var json = this._storage.getItem(STORE_KEY); + + if (json !== null) { + try { + return JSON.parse(json); + } catch (e) { } + } + + return {}; +}; + +WebStorageCookieStore.prototype._writeStore = function(store) { + this._storage.setItem(STORE_KEY, JSON.stringify(store)); +}; + +module.exports = WebStorageCookieStore; diff --git a/www/lodash.js b/www/lodash.js new file mode 100644 index 0000000..7f48eec --- /dev/null +++ b/www/lodash.js @@ -0,0 +1,20 @@ +/** + * @license + * Lodash (Custom Build) lodash.com/license | Underscore.js 1.8.3 underscorejs.org/LICENSE + * Build: `lodash include="get,set,unset,values"` + */ +;(function(){function t(t,e){for(var n=-1,r=null==t?0:t.length,o=Array(r);++n=t}function S(t){var e=typeof t; +return null!=t&&("object"==e||"function"==e)}function w(t){return null!=t&&typeof t=="object"}function x(t){return typeof t=="symbol"||w(t)&&"[object Symbol]"==l(t)}function F(t){return null==t?"":p(t)}function $(t){if(A(t)){var e=st(t),n=!e&&ft(t),r=!e&&!n&&pt(t),o=!e&&!n&&!r&&ht(t);if(e=e||n||r||o){for(var n=t.length,u=String,i=-1,c=Array(n);++it)&&(t==e.length-1?e.pop():nt.call(e,t,1),--this.size,true)},u.prototype.get=function(t){var e=this.__data__;return t=c(e,t),0>t?E:e[t][1]},u.prototype.has=function(t){ +return-1r?(++this.size,n.push([t,e])):n[r][1]=e,this},i.prototype.clear=function(){this.size=0,this.__data__={hash:new o,map:new(it||u),string:new o}},i.prototype.delete=function(t){return t=y(this,t).delete(t),this.size-=t?1:0,t},i.prototype.get=function(t){return y(this,t).get(t)},i.prototype.has=function(t){return y(this,t).has(t)},i.prototype.set=function(t,e){var n=y(this,t),r=n.size;return n.set(t,e),this.size+=n.size==r?0:1, +this};var lt=function(t){t=v(t,function(t){return 500===e.size&&e.clear(),t});var e=t.cache;return t}(function(t){var e=[];return P.test(t)&&e.push(""),t.replace(U,function(t,n,r,o){e.push(r?o.replace(C,"$1"):n||t)}),e});v.Cache=i;var ft=f(function(){return arguments}())?f:function(t){return w(t)&&Q.call(t,"callee")&&!et.call(t,"callee")},st=Array.isArray,pt=D||k,ht=N?e(N):s;r.keys=$,r.memoize=v,r.set=function(t,e,n){if(null!=t&&S(t)){e=h(e,t);for(var r=-1,o=e.length,u=o-1,i=t;null!=i&&++ro.length)){var u=0,i=-1,c=-1,l=o.length;for(0>u&&(u=-u>l?0:l+u),i=i>l?l:i,0>i&&(i+=l),l=u>i?0:i-u>>>0,u>>>=0,i=Array(l);++c