Skip to content

Commit

Permalink
Merge pull request #286 from PerimeterX/feature/custom_cookie_header_…
Browse files Browse the repository at this point in the history
…allign

[SDKNEW-2873]custom cookie header support
  • Loading branch information
DanBezalelpx authored May 16, 2023
2 parents 0d055b9 + 9720cd7 commit 5c49749
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/pxcontext.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { v4: uuidv4 } = require('uuid');
const { CookieOrigin } = require('./enums/CookieOrigin');
const pxUtil = require('./pxutil');
const pxJWT = require('./pxjwt');
const constants = require('./utils/constants');

class PxContext {
constructor(config, req, additionalFields) {
Expand Down Expand Up @@ -43,7 +44,13 @@ class PxContext {
} else {
let cookies = req.cookies;
if (config.CUSTOM_COOKIE_HEADER && req.headers[config.CUSTOM_COOKIE_HEADER]) {
cookies = pxUtil.parseCookieHeader(req.headers[config.CUSTOM_COOKIE_HEADER]);
const customCookies = pxUtil.parseCookieHeader(req.headers[config.CUSTOM_COOKIE_HEADER]);
if (customCookies) {
const customCookieString = JSON.stringify(customCookies);
const cookiesHeaderString = JSON.stringify(cookies);
const cookiesString = [cookiesHeaderString, customCookieString].filter(Boolean).join(constants.COOKIE_SEPARATOR);
cookies = this.cookieStringToUniqueObject(cookiesString);
}
}

this.requestCookieNames = Object.keys(cookies);
Expand Down Expand Up @@ -147,6 +154,19 @@ class PxContext {
// eslint-disable-next-line eqeqeq
return (this.pxde && this.pxde['breached_account'] && this.pxdeVerified) != null;
}

cookieStringToUniqueObject(cookiesString) {
if (!cookiesString) {
return {};
}
const mergedString = this.mergeCookieStrings(cookiesString);
return Object.assign({}, ...Object.entries(JSON.parse(mergedString)).map(([key, value]) => ({ [key]: value })));
}

mergeCookieStrings(cookieString) {
const [cookiesHeader, customCookies] = cookieString.split(constants.COOKIE_SEPARATOR).map(JSON.parse);
return JSON.stringify({ ...cookiesHeader, ...customCookies });
}
}

module.exports = PxContext;
2 changes: 2 additions & 0 deletions lib/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const TOKEN_SEPARATOR = '.';
const APP_USER_ID_FIELD_NAME = 'app_user_id';
const JWT_ADDITIONAL_FIELDS_FIELD_NAME = 'jwt_additional_fields';
const CROSS_TAB_SESSION = 'cross_tab_session';
const COOKIE_SEPARATOR = ';';

module.exports = {
MILLISECONDS_IN_SECOND,
Expand Down Expand Up @@ -64,4 +65,5 @@ module.exports = {
APP_USER_ID_FIELD_NAME,
JWT_ADDITIONAL_FIELDS_FIELD_NAME,
CROSS_TAB_SESSION,
COOKIE_SEPARATOR,
};

0 comments on commit 5c49749

Please sign in to comment.