Skip to content

Commit 86cc621

Browse files
authored
Merge pull request #133 from SupahNickie/33-consent-passing
checking Lambda function into version control
2 parents ad37573 + fdf0fb9 commit 86cc621

1 file changed

Lines changed: 144 additions & 0 deletions

File tree

src/lambdaFunction.js

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
const VENDOR_COOKIE_NAME = "euconsent";
2+
const GDPR_COUNTRIES = new Set([
3+
"GB",
4+
"DE",
5+
"PL",
6+
"FR",
7+
"ES",
8+
"NO",
9+
"IT",
10+
"IS",
11+
"RO",
12+
"SE",
13+
"BG",
14+
"GR",
15+
"NL",
16+
"HR",
17+
"IE",
18+
"CH",
19+
"CZ",
20+
"AT",
21+
"HU",
22+
"FI",
23+
"DK",
24+
"BE",
25+
"LI",
26+
"PT",
27+
"MT",
28+
"LU",
29+
"CY",
30+
"LT",
31+
"SK",
32+
"SI",
33+
"EE",
34+
"LV"
35+
]);
36+
37+
function parseCookies(headers) {
38+
const parsedCookie = {};
39+
if (headers.cookie) {
40+
headers.cookie[0].value.split(';').forEach((cookie) => {
41+
if (cookie) {
42+
const parts = cookie.split('=');
43+
parsedCookie[parts[0].trim()] = parts[1].trim();
44+
}
45+
});
46+
}
47+
return parsedCookie;
48+
}
49+
50+
function parseQueryString(string) {
51+
const parsedQueryString = {};
52+
if (string) {
53+
string.split('&').forEach((param) => {
54+
if (param) {
55+
const parts = param.split('=');
56+
parsedQueryString[parts[0].trim()] = parts[1].trim();
57+
}
58+
});
59+
}
60+
return parsedQueryString;
61+
}
62+
63+
function containsMacros(queryString) {
64+
return /(\{gdpr\}|\{gdpr_consent\})/.test(queryString);
65+
}
66+
67+
function performMacroSubstitution(queryString, gdprApplies, consentString) {
68+
queryString = decodeURIComponent(queryString);
69+
queryString = queryString.replace(/\{gdpr\}/, gdprApplies);
70+
queryString = queryString.replace(/\{gdpr_consent\}/, consentString);
71+
return queryString;
72+
}
73+
74+
function encodeQueryString(gdprApplies, consentString, addParms, redirectContainsMacros) {
75+
if ( (addParms && addParms === '1') || !redirectContainsMacros ) {
76+
return `?gdpr=${gdprApplies}&gdpr_consent=${consentString}`;
77+
}
78+
}
79+
80+
exports.handler = (event, context, callback) => {
81+
const request = event.Records[0].cf.request;
82+
const headers = request.headers;
83+
const queryString = request.querystring;
84+
const parsedCookies = parseCookies(headers);
85+
const parsedQueryString = parseQueryString(queryString);
86+
87+
let origin = '';
88+
if (headers['origin'] && (headers['origin'].length > 0)) {
89+
origin = headers['origin'][0]['value'];
90+
}
91+
92+
let countryCode = '';
93+
if (headers['cloudfront-viewer-country'] && (headers['cloudfront-viewer-country'].length > 0)) {
94+
countryCode = headers['cloudfront-viewer-country'][0].value.toUpperCase();
95+
}
96+
97+
const gdprApplies = parsedQueryString.gdpr === '1' || GDPR_COUNTRIES.has(countryCode) ? 1 : 0;
98+
const consentString = parsedCookies[VENDOR_COOKIE_NAME];
99+
100+
let response;
101+
if (!parsedQueryString.redirect) {
102+
response = {
103+
status: '200',
104+
statusDescription: 'OK',
105+
headers: {
106+
'access-control-allow-credentials': [{
107+
key: 'Access-Control-Allow-Credentials',
108+
value: 'true'
109+
}],
110+
'access-control-allow-methods': [{
111+
key: 'Access-Control-Allow-Methods',
112+
value: 'GET, OPTIONS'
113+
}],
114+
'access-control-allow-origin': [{
115+
key: 'Access-Control-Allow-Origin',
116+
value: origin
117+
}],
118+
'content-type': [{
119+
key: 'Content-Type',
120+
value: 'application/json'
121+
}],
122+
},
123+
body: JSON.stringify({
124+
gdpr: gdprApplies,
125+
gdpr_consent: consentString,
126+
})
127+
};
128+
} else {
129+
const redirectContainsMacros = containsMacros(parsedQueryString.redirect);
130+
response = {
131+
status: '302',
132+
statusDescription: 'Found',
133+
headers: {
134+
location: [{
135+
key: 'Location',
136+
value: performMacroSubstitution(parsedQueryString.redirect, gdprApplies, consentString) +
137+
encodeQueryString(gdprApplies, consentString, parsedQueryString.add_parms, redirectContainsMacros),
138+
}]
139+
}
140+
};
141+
}
142+
143+
callback(null, response);
144+
};

0 commit comments

Comments
 (0)