-
Notifications
You must be signed in to change notification settings - Fork 27
/
cookie-utils.js
175 lines (160 loc) · 5.62 KB
/
cookie-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { nativeIsNaN } from './number-utils';
/**
* Checks whether the input path is supported
*
* @param {string} rawPath input path
* @returns {boolean} if cookie path is valid
*/
export const isValidCookiePath = (rawPath) => rawPath === '/' || rawPath === 'none';
/**
* Returns 'path=/' if rawPath is '/'
* or empty string '' for other cases, `rawPath === 'none'` included
*
* @param {string} rawPath path argument of *set-cookie-* scriptlets
* @returns {string} cookie path
*/
export const getCookiePath = (rawPath) => {
if (rawPath === '/') {
return 'path=/';
}
// otherwise do not set path as invalid
// the same for pathArg === 'none'
return '';
};
/**
* Combines input cookie name, value, and path into string.
*
* @param {string} rawName name argument of *set-cookie-* scriptlets
* @param {string} rawValue value argument of *set-cookie-* scriptlets
* @param {string} rawPath path argument of *set-cookie-* scriptlets
* @param {boolean} shouldEncode if cookie's name and value should be encoded
* @returns {string|null} string OR `null` if name or value is invalid
*/
export const concatCookieNameValuePath = (rawName, rawValue, rawPath, shouldEncode = true) => {
const COOKIE_BREAKER = ';';
// semicolon will cause the cookie to break
if (!shouldEncode && (rawName.includes(COOKIE_BREAKER) || `${rawValue}`.includes(COOKIE_BREAKER))) {
return null;
}
const name = shouldEncode ? encodeURIComponent(rawName) : rawName;
const value = shouldEncode ? encodeURIComponent(rawValue) : rawValue;
return `${name}=${value}; ${getCookiePath(rawPath)};`;
};
/**
* Gets supported cookie value
*
* @param {string} value input cookie value
* @returns {string|null} valid cookie string if ok OR null if not
*/
export const getLimitedCookieValue = (value) => {
if (!value) {
return null;
}
let validValue;
if (value === 'true') {
validValue = 'true';
} else if (value === 'True') {
validValue = 'True';
} else if (value === 'false') {
validValue = 'false';
} else if (value === 'False') {
validValue = 'False';
} else if (value === 'yes') {
validValue = 'yes';
} else if (value === 'Yes') {
validValue = 'Yes';
} else if (value === 'Y') {
validValue = 'Y';
} else if (value === 'no') {
validValue = 'no';
} else if (value === 'ok') {
validValue = 'ok';
} else if (value === 'OK') {
validValue = 'OK';
} else if (/^\d+$/.test(value)) {
validValue = parseFloat(value);
if (nativeIsNaN(validValue)) {
return null;
}
if (Math.abs(validValue) < 0 || Math.abs(validValue) > 15) {
return null;
}
} else {
return null;
}
return validValue;
};
/**
* Parses cookie string into object
*
* @param {string} cookieString string that conforms to document.cookie format
* @returns {Object} key:value object that corresponds with incoming cookies keys and values
*/
export const parseCookieString = (cookieString) => {
const COOKIE_DELIMITER = '=';
const COOKIE_PAIRS_DELIMITER = ';';
// Get raw cookies
const cookieChunks = cookieString.split(COOKIE_PAIRS_DELIMITER);
const cookieData = {};
cookieChunks.forEach((singleCookie) => {
let cookieKey;
let cookieValue;
const delimiterIndex = singleCookie.indexOf(COOKIE_DELIMITER);
if (delimiterIndex === -1) {
cookieKey = singleCookie.trim();
} else {
cookieKey = singleCookie.slice(0, delimiterIndex).trim();
cookieValue = singleCookie.slice(delimiterIndex + 1);
}
// Save cookie key=value data with null instead of empty ('') values
cookieData[cookieKey] = cookieValue || null;
});
return cookieData;
};
/**
* Check if cookie with specified name and value is present in a cookie string
*
* @param {string} cookieString 'document.cookie'-like string
* @param {string} name name argument of *set-cookie-* scriptlets
* @param {string} value value argument of *set-cookie-* scriptlets
* @returns {boolean} if cookie is already set
*/
export const isCookieSetWithValue = (cookieString, name, value) => {
return cookieString.split(';')
.some((cookieStr) => {
const pos = cookieStr.indexOf('=');
if (pos === -1) {
return false;
}
const cookieName = cookieStr.slice(0, pos).trim();
const cookieValue = cookieStr.slice(pos + 1).trim();
return name === cookieName && value === cookieValue;
});
};
/**
* Returns parsed offset expired number of ms or null if `offsetExpiresSec` is invalid
*
* @param {string} offsetExpiresSec input offset param in seconds
* @returns {number|null} number is milliseconds OR null
*/
export const getTrustedCookieOffsetMs = (offsetExpiresSec) => {
const ONE_YEAR_EXPIRATION_KEYWORD = '1year';
const ONE_DAY_EXPIRATION_KEYWORD = '1day';
const MS_IN_SEC = 1000;
const SECONDS_IN_YEAR = 365 * 24 * 60 * 60;
const SECONDS_IN_DAY = 24 * 60 * 60;
let parsedSec;
// Set predefined expire value if corresponding keyword was passed
if (offsetExpiresSec === ONE_YEAR_EXPIRATION_KEYWORD) {
parsedSec = SECONDS_IN_YEAR;
} else if (offsetExpiresSec === ONE_DAY_EXPIRATION_KEYWORD) {
parsedSec = SECONDS_IN_DAY;
} else {
parsedSec = Number.parseInt(offsetExpiresSec, 10);
// If offsetExpiresSec has been parsed to NaN - do not set cookie at all
if (Number.isNaN(parsedSec)) {
return null;
}
}
return parsedSec * MS_IN_SEC;
};