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 82dab93 commit d8affe0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 22 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,10 @@

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

## 1.0.0
## 1.0.1

### Features

1. Allows setting `{expires, domain, path, secure}` as metadata to configure the cookies.
1. Implements an API similar to [Web Storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage) interface.
1. Saves and retrieves data as `JSON`.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ $ yarn add cookie-storage-v2
<script src="https://unpkg.com/cookie-storage-v2/dist/cookie-storage.min.js"></script>

<!-- or from rawgit.com -->
<script src="https://cdn.rawgit.com/jherax/cookie-storage/1.0.0/dist/cookie-storage.min.js"></script>
<script src="https://cdn.rawgit.com/jherax/cookie-storage/1.0.1/dist/cookie-storage.min.js"></script>
```

In the above case, [`cookieStorage`](#api) is included as a global object
Expand Down Expand Up @@ -99,6 +99,7 @@ configures the way how the cookie is stored. It has 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 All @@ -125,12 +126,13 @@ cookieStorage.setItem('activity', data, {
});

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

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

Expand Down
29 changes: 22 additions & 7 deletions dist/cookie-storage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! cookieStorage@v1.0.0. Jherax 2017. Visit https://github.com/jherax/cookie-storage */
/*! cookieStorage@v1.0.1. Jherax 2017. Visit https://github.com/jherax/cookie-storage */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
Expand Down Expand Up @@ -211,15 +211,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 @@ -257,10 +273,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
4 changes: 2 additions & 2 deletions dist/cookie-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/cookie-storage.min.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cookie-storage-v2",
"version": "1.0.0",
"version": "1.0.1",
"description": "A lightweight JavaScript UMD to handle cookies through Web Storage Interface",
"author": "David Rivera <jherax@gmail.com>",
"main": "dist/cookie-storage.js",
Expand Down
27 changes: 21 additions & 6 deletions src/cookie-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,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) {
const expires = buildMetadataFor('expires', data);
const domain = buildMetadataFor('domain', data);
const path = buildMetadataFor('path', data);
const secure = data.secure ? ';secure' : '';
return `${expires}${domain}${path}${secure}`;
}

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

Expand Down

0 comments on commit d8affe0

Please sign in to comment.