Skip to content

Commit

Permalink
Add support for SameSite cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
tsujiguchitky committed Oct 14, 2020
1 parent de1d252 commit 910dd9b
Showing 1 changed file with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2011-2016 ForgeRock AS.
* Portions copyright 2020 Open Source Solution Technology Corporation
*/

define([
Expand All @@ -31,22 +32,25 @@ define([
* @param {String} [path] - cookie path.
* @param {String|String[]} [domain] - cookie domain(s).
* @param {Boolean} [secure] - is cookie secure.
* @param {String} [samesite] - cookie samesite.
* @returns {String} created cookie.
*/
obj.createCookie = function (name, value, expirationDate, path, domain, secure) {
obj.createCookie = function (name, value, expirationDate, path, domain, secure, samesite) {
var expirationDatePart,
nameValuePart,
pathPart,
domainPart,
securePart;
securePart,
samesitePart;

expirationDatePart = expirationDate ? ";expires=" + expirationDate.toGMTString() : "";
nameValuePart = name + "=" + value;
pathPart = path ? ";path=" + path : "";
domainPart = domain ? ";domain=" + domain : "";
securePart = secure ? ";secure" : "";
samesitePart = samesite ? "; SameSite=" + samesite : "";

return nameValuePart + expirationDatePart + pathPart + domainPart + securePart;
return nameValuePart + expirationDatePart + pathPart + domainPart + securePart + samesitePart;
};

/**
Expand All @@ -57,17 +61,18 @@ define([
* @param {String} [path] - cookie path.
* @param {String|String[]} [domain] - cookie domain(s). Use empty array for creating host-only cookies.
* @param {Boolean} [secure] - is cookie secure.
* @param {String} [samesite] - cookie samesite.
*/
obj.setCookie = function (name, value, expirationDate, path, domains, secure) {
obj.setCookie = function (name, value, expirationDate, path, domains, secure, samesite) {
if (!_.isArray(domains)) {
domains = [domains];
}

if (domains.length === 0) {
document.cookie = obj.createCookie(name, value, expirationDate, path, undefined, secure);
document.cookie = obj.createCookie(name, value, expirationDate, path, undefined, secure, samesite);
} else {
_.each(domains, function(domain) {
document.cookie = obj.createCookie(name, value, expirationDate, path, domain, secure);
document.cookie = obj.createCookie(name, value, expirationDate, path, domain, secure, samesite);
});
}
};
Expand Down

0 comments on commit 910dd9b

Please sign in to comment.