Skip to content

Commit

Permalink
add crpto-js for value encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
sayan-mitra committed Jul 22, 2020
1 parent 4c0d59e commit 0f92728
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 25 deletions.
25 changes: 25 additions & 0 deletions analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ class Analytics {
this.toBeProcessedArray = [];
this.toBeProcessedByIntegrationArray = [];
this.storage = Storage;
this.eventRepository = EventRepository;
this.sendAdblockPage = false;
this.sendAdblockPageOptions = {};
this.clientSuppliedCallbacks = {};
this.readyCallback = () => {};
this.executeReadyCallback = undefined;
this.methodToCallbackMapping = {
syncPixel: "syncPixelCallback",
};
}

/**
* initialize the user after load config
*/
initializeUser() {
this.userId =
this.storage.getUserId() != undefined ? this.storage.getUserId() : "";

Expand All @@ -78,7 +93,10 @@ class Analytics {
: {};

this.anonymousId = this.getAnonymousId();

// save once for storing older values to encrypted
this.storage.setUserId(this.userId);
<<<<<<< HEAD
this.eventRepository = EventRepository;
this.sendAdblockPage = false;
this.sendAdblockPageOptions = {};
Expand All @@ -89,6 +107,12 @@ class Analytics {
syncPixel: "syncPixelCallback",
};
this.loaded = false;
=======
this.storage.setAnonymousId(this.anonymousId);
this.storage.setGroupId(this.groupId);
this.storage.setUserTraits(this.userTraits);
this.storage.setGroupTraits(this.groupTraits);
>>>>>>> add crpto-js for value encryption
}

/**
Expand Down Expand Up @@ -816,6 +840,7 @@ class Analytics {
if (serverUrl) {
this.eventRepository.url = serverUrl;
}
this.initializeUser();
if (
options &&
options.valTrackingList &&
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"component-each": "^0.2.6",
"component-emitter": "github:component/emitter",
"component-url": "^0.2.1",
"crypto-js": "^4.0.0",
"each": "^1.2.2",
"fs": "0.0.1-security",
"gh-release": "^3.5.0",
Expand Down
16 changes: 3 additions & 13 deletions utils/storage/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cookie from "rudder-component-cookie";
import defaults from "@ndhoule/defaults";
import json from "json3";
import topDomain from "@segment/top-domain";
import logger from "../logUtil";

/**
* An object utility to persist values in cookies
Expand Down Expand Up @@ -46,10 +47,10 @@ class CookieLocal {
*/
set(key, value) {
try {
value = json.stringify(value);
cookie(key, value, clone(this._options));
return true;
} catch (e) {
logger.error(e);
return false;
}
}
Expand All @@ -59,18 +60,7 @@ class CookieLocal {
* @param {*} key
*/
get(key) {
// if not parseable, return as is without json parse
let value;
try {
value = cookie(key);
value = value ? json.parse(value) : null;
return value;
} catch (e) {
if (value) {
return value;
}
return null;
}
return cookie(key);
}

/**
Expand Down
115 changes: 103 additions & 12 deletions utils/storage/storage.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import CryptoJS from "crypto-js";
import logger from "../logUtil";
import { Cookie } from "./cookie";
import { Store } from "./store";
import { EventRepository } from "../EventRepository";

const defaults = {
user_storage_key: "rl_user_id",
user_storage_trait: "rl_trait",
user_storage_anonymousId: "rl_anonymous_id",
group_storage_key: "rl_group_id",
group_storage_trait: "rl_group_trait",
prefix: "RudderEncrypt:",
};

/**
Expand All @@ -30,13 +33,76 @@ class Storage {
}
}

/**
* Json stringify the given value
* @param {*} value
*/
stringify(value) {
return JSON.stringify(value);
}

/**
* JSON parse the value
* @param {*} value
*/
parse(value) {
// if not parseable, return as is without json parse
try {
return value ? JSON.parse(value) : null;
} catch (e) {
logger.error(e);
return value || null;
}
}

/**
* trim using regex for browser polyfill
* @param {*} value
*/
trim(value) {
return value.replace(/^\s+|\s+$/gm, "");
}

/**
* AES encrypt value with constant prefix
* @param {*} value
*/
encryptValue(value) {
if (this.trim(value) == "") {
return value;
}
const prefixedVal = `${defaults.prefix}${CryptoJS.AES.encrypt(
value,
EventRepository.writeKey
).toString()}`;

return prefixedVal;
}

/**
* decrypt value
* @param {*} value
*/
decryptValue(value) {
if (!value || (typeof value === "string" && this.trim(value) == "")) {
return value;
}
if (value.substring(0, defaults.prefix.length) == defaults.prefix) {
return CryptoJS.AES.decrypt(
value.substring(defaults.prefix.length),
EventRepository.writeKey
).toString(CryptoJS.enc.Utf8);
}
return value;
}

/**
*
* @param {*} key
* @param {*} value
*/
setItem(key, value) {
this.storage.set(key, value);
this.storage.set(key, this.encryptValue(this.stringify(value)));
}

/**
Expand All @@ -48,15 +114,21 @@ class Storage {
logger.error("[Storage] setUserId:: userId should be string");
return;
}
this.storage.set(defaults.user_storage_key, value);
this.storage.set(
defaults.user_storage_key,
this.encryptValue(this.stringify(value))
);
}

/**
*
* @param {*} value
*/
setUserTraits(value) {
this.storage.set(defaults.user_storage_trait, value);
this.storage.set(
defaults.user_storage_trait,
this.encryptValue(this.stringify(value))
);
}

/**
Expand All @@ -68,15 +140,21 @@ class Storage {
logger.error("[Storage] setGroupId:: groupId should be string");
return;
}
this.storage.set(defaults.group_storage_key, value);
this.storage.set(
defaults.group_storage_key,
this.encryptValue(this.stringify(value))
);
}

/**
*
* @param {*} value
*/
setGroupTraits(value) {
this.storage.set(defaults.group_storage_trait, value);
this.storage.set(
defaults.group_storage_trait,
this.encryptValue(this.stringify(value))
);
}

/**
Expand All @@ -88,50 +166,63 @@ class Storage {
logger.error("[Storage] setAnonymousId:: anonymousId should be string");
return;
}
this.storage.set(defaults.user_storage_anonymousId, value);
this.storage.set(
defaults.user_storage_anonymousId,
this.encryptValue(this.stringify(value))
);
}

/**
*
* @param {*} key
*/
getItem(key) {
return this.storage.get(key);
return this.parse(this.decryptValue(this.storage.get(key)));
}

/**
* get the stored userId
*/
getUserId() {
return this.storage.get(defaults.user_storage_key);
return this.parse(
this.decryptValue(this.storage.get(defaults.user_storage_key))
);
}

/**
* get the stored user traits
*/
getUserTraits() {
return this.storage.get(defaults.user_storage_trait);
return this.parse(
this.decryptValue(this.storage.get(defaults.user_storage_trait))
);
}

/**
* get the stored userId
*/
getGroupId() {
return this.storage.get(defaults.group_storage_key);
return this.parse(
this.decryptValue(this.storage.get(defaults.group_storage_key))
);
}

/**
* get the stored user traits
*/
getGroupTraits() {
return this.storage.get(defaults.group_storage_trait);
return this.parse(
this.decryptValue(this.storage.get(defaults.group_storage_trait))
);
}

/**
* get stored anonymous id
*/
getAnonymousId() {
return this.storage.get(defaults.user_storage_anonymousId);
return this.parse(
this.decryptValue(this.storage.get(defaults.user_storage_anonymousId))
);
}

/**
Expand Down

0 comments on commit 0f92728

Please sign in to comment.