Skip to content

Commit

Permalink
Allow storing secure cookies (HTTPS)
Browse files Browse the repository at this point in the history
  • Loading branch information
jherax committed Apr 24, 2017
1 parent 6df8575 commit d88c5e0
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 26 deletions.
10 changes: 9 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

<!-- markdownlint-disable MD024 MD033 -->

## 2.1.3

### Improvements

1. Allow storing secure cookies (HTTPS). The property `secure` is available through the `options` parameter for `setItem()` and `removeItem()`

---

## 2.1.2

### Improvements

1. Added the `options` parameter to the API method `removeItem()`, in order to allow passing metadata to the cookie to delete.
When using `"cookieStorage"` the new signature is: `instance.removeItem(key, options)`
1. Checks if the cookie was created on `setItem()`, or delete it if the domain or path are not valid.
1. When calling `setItem()`, checks if the cookie was created, or deletes it if the domain or path are not valid.

---

Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $ yarn add proxy-storage
<script src="https://unpkg.com/proxy-storage/dist/proxy-storage.min.js"></script>

<!-- or from rawgit.com -->
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.1.2/dist/proxy-storage.min.js"></script>
<script src="https://cdn.rawgit.com/jherax/proxy-storage/2.1.3/dist/proxy-storage.min.js"></script>
```

In the above case, [`proxyStorage`](#api) is included as a global object
Expand Down Expand Up @@ -295,6 +295,7 @@ Where the **`options`** parameter is an `object` with the following properties:

- `domain`_`{string}`_: the domain or subdomain where the cookie will be valid.
- `path`_`{string}`_: relative path where the cookie is valid. _Default `"/"`_
- `secure`_`{boolean}`_: if provided, sets a secure cookie (HTTPS).
- `expires`_`{Date, object}`_: the expiration date of the cookie.
You can pass an object describing the expiration:
- `date`_`{Date}`_: if provided, this date will be applied, otherwise the
Expand Down Expand Up @@ -323,12 +324,13 @@ cookieStore.setItem('activity', data, {
});

cookieStore.setItem('testing1', true, {
path: '/profile',
expires: new Date('2018/03/06'),
secure: true,
path: '/jherax',
expires: new Date('2017/12/31'),
});

cookieStore.setItem('testing2', [1,4,7], {
domain: '.wordpress.com',
domain: '.github.com',
expires: { days: 1 },
});

Expand Down Expand Up @@ -426,9 +428,8 @@ function clearAllStorages() {

The [`WebStorage`](#webstorage) class exposes the static member `interceptors`
which lets you to register callback functions upon the prototype methods
`setItem`, `getItem`, `removeItem`, and `clear`.
It is very useful when you need to perform an action to intercept the value
to read, write, or delete.
`setItem`, `getItem`, `removeItem`, and `clear`. It is very useful when you
need to perform an action to intercept the value to read, write, or delete.

- **`WebStorage.interceptors`**`(command, action)`: adds an interceptor to
the API method.
Expand Down Expand Up @@ -473,7 +474,7 @@ WebStorage.interceptors('removeItem', (key/*, options*/) => {
});

// uses the default storage mechanism (usually localStorage)
storage.setItem('storage-test', { id: 1040, text: 'it works!' });
storage.setItem('storage-test', {id: 1040, text: 'it works!'});
let data = storage.getItem('storage-test');
console.log(data);

Expand Down
29 changes: 22 additions & 7 deletions dist/proxy-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! [email protected].2. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
/*! [email protected].3. Jherax 2017. Visit https://github.com/jherax/proxy-storage */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down Expand Up @@ -562,15 +562,31 @@ function buildExpirationString(date) {
/**
* @private
*
* Builds the string for the cookie's metadata.
* Builds the string for the cookie metadata.
*
* @param {string} key: name of the metadata
* @param {object} data: metadata of the cookie
* @return {string}
*/
function buildMetadataFor(key, data) {
if (!data[key]) return '';
return '; ' + key + '=' + data[key];
return ';' + key + '=' + data[key];
}

/**
* @private
*
* Builds the whole string for the cookie metadata.
*
* @param {object} data: metadata of the cookie
* @return {string}
*/
function formatMetadata(data) {
var expires = buildMetadataFor('expires', data);
var domain = buildMetadataFor('domain', data);
var path = buildMetadataFor('path', data);
var secure = data.secure ? ';secure' : '';
return '' + expires + domain + path + secure;
}

/**
Expand Down Expand Up @@ -608,10 +624,9 @@ function cookieStorage() {
if (options.domain && typeof options.domain === 'string') {
metadata.domain = options.domain.trim();
}
var expires = buildMetadataFor('expires', metadata);
var domain = buildMetadataFor('domain', metadata);
var path = buildMetadataFor('path', metadata);
var cookie = key + '=' + encodeURIComponent(value) + expires + domain + path;
if (options.secure === true) metadata.secure = true;
var cookie = key + '=' + encodeURIComponent(value) + formatMetadata(metadata);
// TODO: should encodeURIComponent(key) ?
$cookie.set(cookie);
},
getItem: function getItem(key) {
Expand Down
Loading

0 comments on commit d88c5e0

Please sign in to comment.