Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Linkspector
on: [pull_request]
jobs:
check-links:
name: runner / linkspector
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run linkspector
uses: umbrelladocs/action-linkspector@v1
with:
github_token: ${{ secrets.github_token }}
reporter: github-pr-review
fail_on_error: true
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*** doc under construction ***

require('cookie-master')
Cookies

toJson
toCookies
toCookieString
toSetCookieArray
toCookieStringUrlEncoded


require('cookie-master').Cookie
Cookie

toString
toFullString
parseFromString
parseFromObject
toJSON
105 changes: 105 additions & 0 deletions cookie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const _ = require('lodash');

class Cookie {

constructor(obj)
{
this.name = null;
this.value = null;
this.expires = null;
this.maxAge = null;
this.path = null;
this.domain = null;
this.secure = null;
this.httpOnly = null;
this.sameSite = null;

// if ((obj != null) && (obj instanceof Cookie)) this.parseFromString(obj.toFullString());
if ((obj != null) && (_.isObject(obj))) this.parseFromObject(obj);
if ((obj != null) && (_.isString(obj))) this.parseFromString(obj);
}

toString()
{
return `${this.name}=${this.value};`;
}

toFullString()
{
let c = this.toString() + ' ';
if (this.expires != null) c += `Expires=${(_.isDate(this.expires))?this.expires.toGMTString():(new Date(this.expires).toGMTString())}; `;
if (this.maxAge != null) c+= `Max-Age=${this.maxAge}; `;

if (_.startsWith(this.name,'__Host'))
c+= `Path=/; `;
else if (this.path != null)
c+= `Path=${this.path}; `;

if ((this.domain != null) && (!_.startsWith(this.name,'__Host'))) c+= `Domain=${this.domain}; `;
if ((this.secure == true) || (_.startsWith(this.name,'__Secure')) || (_.startsWith(this.name,'__Host'))) c+= `Secure; `;
if (this.httpOnly == true) c+= `HttpOnly; `;
if (this.sameSite != null) c+= `SameSite=${sameSite}; `;

return c.trim();
}

parseFromString(input)
{
let parts = input.split(";");
_(parts).forEach((item, index) => {
let items = item.split("=");
items[1] = item.slice(items[0].length + 1);
// console.log("'",items[0].toLowerCase().trim(), "' : ",items[0].toLowerCase().trim() === 'httponly', " ", items[1]);
if (index == 0)
{
this.name = items[0];
this.value = items[1];
}
else if (items[0].toLowerCase().trim() == 'expires') { this.expires = new Date(items[1]); }
else if (items[0].toLowerCase().trim() == 'max-age') { this.maxAge = items[1]; }
else if (items[0].toLowerCase().trim() == 'path') { this.path = items[1]; }
else if (items[0].toLowerCase().trim() == 'domain') { this.domain = items[1]; }
else if (items[0].toLowerCase().trim() == 'secure') { this.secure = true; }
else if (items[0].toLowerCase().trim() == 'httponly') { this.httpOnly = true; }
else if (items[0].toLowerCase().trim() == 'samesite') { this.sameSite = items[1]; }
});

if (_.startsWith(this.name,'__Secure')) { this.secure = true; }
if (_.startsWith(this.name,'__Host')) {
this.secure = true;
this.path = '/';
this.domain = null;
}
}

parseFromObject(obj)
{
this.name = obj.name;
this.value = obj.value;
this.expires = obj.expires;
this.maxAge = obj.maxAge;
this.path = obj.path;
this.domain = obj.domain;
this.secure = obj.secure;
this.httpOnly = obj.httpOnly;
this.sameSite = obj.sameSite;
}

toJSON()
{
return _.pickBy({
name: this.name,
value: this.value,
expires: this.expires,
maxAge: this.maxAge,
path: this.path,
domain: this.domain,
secure: this.secure,
httpOnly: this.httpOnly,
sameSite: this.sameSite
}, (v) => { return ((v !== '') && (v !== null)); });
}

}

module.exports=Cookie;
176 changes: 119 additions & 57 deletions cookieTester.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
var cookies = require('./cookies');
const Cookie = require('./cookie');
const _ = require('lodash');

var assert = require('assert');

describe('Cookies module', function() {

describe('Test core cookie handler', cookieHandlerTest);

function cookieHandlerTest() {
it('Should parse and output cookie objects', objectTest);

function objectTest()
{
let tester = [
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

_(tester).forEach(item => {
let tmp = new Cookie(item);
// tmp.parseFromString(item);
// console.log(tmp.toJSON());
// console.log(tmp.toFullString());
});
}
}

describe('Test toJson method', function() {

it('Should convert array of cookies to json object', function() {
it('Should convert array of cookies to array of json objects', function() {
var start = [
"JazzFormAuth=Form; Path=/ccm; Secure",
"WASPostParam=L2NjbS9zZWN1cmUvYXV0aGVudGljYXRlZC9pZGVudGl0eQ==.AAAAAAAAAAA=.AAAAIQ==.YXBwbGljYXRpb24veC13d3ctZm9ybS11cmxlbmNvZGVk; Path=/ccm/secure/authenticated/identity; Secure; HttpOnly",
Expand All @@ -14,60 +41,71 @@ describe('Cookies module', function() {
];

var result = cookies.toJson(start);
assert.equal(result.JazzFormAuth, "Form; Path=/ccm; Secure");
assert.equal(result.WASPostParam, "L2NjbS9zZWN1cmUvYXV0aGVudGljYXRlZC9pZGVudGl0eQ==.AAAAAAAAAAA=.AAAAIQ==.YXBwbGljYXRpb24veC13d3ctZm9ybS11cmxlbmNvZGVk; Path=/ccm/secure/authenticated/identity; Secure; HttpOnly");
});

it('Should convert string of cookies to json object', function() {
var start = "cookie1=someValue;cookie2=some other value;cookie3=another value";

var result = cookies.toJson(start);
assert.equal(result.cookie3, "another value");
// console.log(result);
assert.ok(_.isArray(result), "not an array");
_(result).forEach(obj => assert.ok(_.isObject(obj), "obj check failed"));
assert.equal(_.find(result,{name: "JazzFormAuth"}).secure, true, "Failed Jazz");
assert.equal(_.find(result,{name: "WASPostParam"}).value, "L2NjbS9zZWN1cmUvYXV0aGVudGljYXRlZC9pZGVudGl0eQ==.AAAAAAAAAAA=.AAAAIQ==.YXBwbGljYXRpb24veC13d3ctZm9ybS11cmxlbmNvZGVk");
});
});

describe('Test toCookieString method', function() {

it('Should convert a json object to a cookie string', function() {
var start = {
item1: "hello there",
item2: "hello there dude",
item3: "goodbye there",
item4: "ouch that hurt",
item5: "good night"
};
it('Should convert a cookie string array object to a header cookie string', function() {
var start = [
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

var result = cookies.toCookieString(start);
assert.equal(result, "item1=hello there;item2=hello there dude;item3=goodbye there;item4=ouch that hurt;item5=good night");
assert.equal(result, "JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; epricer_id=2700000261; epricer_email=rjalowiec@kc.rr.com; epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; ");
});

it('Should convert an array object to a cookie string', function() {
it('Should convert an array object of JSON cookies to a header cookie string', function() {
var start = [
"item1=hello there",
"item2=hello there dude",
"item3=goodbye there",
"item4=ouch that hurt",
"item5=good night"
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

var result = cookies.toCookieString(start);
assert.equal(result, "item1=hello there;item2=hello there dude;item3=goodbye there;item4=ouch that hurt;item5=good night");
var midpoint = cookies.toJson(start);
var result = cookies.toCookieString(midpoint);
assert.equal(result, "JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; epricer_id=2700000261; epricer_email=rjalowiec@kc.rr.com; epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; ");
});

it('Should convert an array object of JSON cookies to a header cookie string', function() {
var start = [
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

var result = cookies.toSimpleJSON(start);
assert.equal(result.epricer_id, "2700000261");
});
});

describe('Test toCookieStringUrlEncoded method', function() {

it('Should convert a json object to a cookie string and encode it', function() {
var start = {
item1: "hello there",
item2: "hello there dude",
item3: "goodbye there",
item4: "ouch that hurt",
item5: "good night"
};
var start = [
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

var result = cookies.toCookieStringUrlEncoded(start);
assert.equal(result, "item1=hello%20there;item2=hello%20there%20dude;item3=goodbye%20there;item4=ouch%20that%20hurt;item5=good%20night");
var midpoint = cookies.toJson(start);
var result = cookies.toCookieStringUrlEncoded(midpoint);
assert.equal(result, "JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4;%20epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8;%20epricer_id=2700000261;%20epricer_email=rjalowiec@kc.rr.com;%20epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection;%20");
});

it('Should convert an array object to a cookie string', function() {
Expand All @@ -80,30 +118,54 @@ describe('Cookies module', function() {
];

var result = cookies.toCookieStringUrlEncoded(start);
assert.equal(result, "item1=hello%20there;item2=hello%20there%20dude;item3=goodbye%20there;item4=ouch%20that%20hurt;item5=good%20night");
assert.equal(result, "item1=hello%20there;%20item2=hello%20there%20dude;%20item3=goodbye%20there;%20item4=ouch%20that%20hurt;%20item5=good%20night;%20");
});
});

describe('Test merge method', function() {

it('Should merge source json object into base json object', function() {
var base = {
item1: "hello there",
item2: "hello there dude",
item3: "goodbye there",
item4: "ouch that hurt",
item5: "good night"
};

var source = {
item1: "goodbye there",
item6: "rock it dude",
item7: "ZZZZzzzzzzz"
};

var result = cookies.merge(base, source);
assert.equal(result.item1, "goodbye there");
assert.equal(result.item6, "rock it dude");
describe('Test handling Cookie objects', function() {

it('Should convert an array of cookie strings to an array of cookie objects, then convert the cookie objects to an array of Strings using Cookie objects', function() {
var start = [
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

var midpoint = cookies.toCookies(start);
var result = midpoint.map(cookie => cookie.toFullString());
assert.equal(result[0], 'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly;');
assert.equal(result[1], 'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24 Sep 2017 20:50:30 GMT; Path=/services/partners/epricer/v2/api;');
});

it('Should convert an array of cookie strings to an array of cookie objects, then convert the cookie objects to an array of Strings using Cookies interface', function() {
var start = [
'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly',
'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_id=2700000261; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_email=rjalowiec@kc.rr.com; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api',
'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api'
];

var midpoint = cookies.toCookies(start);
var result = cookies.toSetCookieArray(midpoint);
assert.equal(result[0], 'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly;');
assert.equal(result[1], 'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24 Sep 2017 20:50:30 GMT; Path=/services/partners/epricer/v2/api;');
});
});
//
// describe('Test working with single Cookie', function() {
//
// it('Should convert an array of cookie strings to an array of cookie objects, then convert the cookie objects to an array of Strings using Cookie objects', function() {
// var start =
// 'epricer_urlredirection=https://wwwbeta-2.toronto.ca.ibm.com/partnerworld/commerce/programs/servers/EpricerRedirectionServlet.wss?command=epricerRedirection; Expires=Sun, 24-Sep-17 20:50:30 GMT; Path=/services/partners/epricer/v2/api; HttpOnly;';
//
// var midpoint = cookies.toCookies(start);
// var result = midpoint.map(cookie => cookie.toFullString());
// assert.equal(result[0], 'JSESSIONID=0000MgZyuP8CQGMz6Jnc5xFpOu8:1a0dgl3k4; Path=/services/partners/epricer/v2/bpgui; HttpOnly;');
// assert.equal(result[1], 'epricer_contextid=MgZyuP8CQGMz6Jnc5xFpOu8; Expires=Sun, 24 Sep 2017 20:50:30 GMT; Path=/services/partners/epricer/v2/api;');
// });
//
// });
});
Loading