-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyzer.html
More file actions
177 lines (155 loc) · 6.5 KB
/
analyzer.html
File metadata and controls
177 lines (155 loc) · 6.5 KB
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
176
177
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Download Experiment</title>
<script>
const fail = (message) => {
alert(message);
throw new Error(message);
};
const hash = async (str, salt) => {
const buf = window.crypto.subtle.digest(
'SHA-512',
(new TextEncoder()).encode(str + new String(salt))
);
return Array.from(new Uint8Array(await buf)).map((b) => b.toString(16).padStart(2, "0")).join("");
};
const getPrivKey = (keyData) => {
return window.crypto.subtle.importKey(
"jwk", keyData,
{ name: "RSA-OAEP", hash: { name: "SHA-256" }, },
false, ["decrypt", "unwrapKey"]
);
};
const getPubKey = (keyData) => {
return window.crypto.subtle.importKey(
"jwk", keyData,
{ name: "RSA-OAEP", hash: { name: "SHA-256" }, },
true, ["encrypt", "wrapKey"]
);
};
const loadValue = (domID) => {
const value = document.getElementById(domID).value;
if (!value) { fail("Expected some value at " + domID); }
return value;
};
</script>
<!-- import Bootstrap: -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
</head>
<body>
<div id="base-div" class="container">
<div id="fetch-view">
<script>
const decrypt = async (data, privKey) => {
const { ciphertext, wrappedKey, encryptedCounter } = data;
const counter = await window.crypto.subtle.decrypt(
{ name: "RSA-OAEP" }, privKey, new Uint8Array(encryptedCounter)
);
const aesKey = await window.crypto.subtle.unwrapKey(
"jwk", new Uint8Array(wrappedKey), privKey,
{ name: "RSA-OAEP" }, { name: "AES-CTR" }, false, ["decrypt"]
);
const AES_CTR_options = { name: "AES-CTR", counter: counter, length: 64 };
const dataDecrypted = await window.crypto.subtle.decrypt(
AES_CTR_options, aesKey, new Uint8Array(ciphertext)
);
const jsonEncodedData = (new TextDecoder()).decode(dataDecrypted);
return JSON.parse(jsonEncodedData);
};
/**
* Turns a list of uploads into a single CSV
* @param {{
[key: string] : {
[key: string] : any;
};
}[]} jsonFormList - a list of data uploads, each with an entry per participant, each entry containing a json object of relevant data
*/
const produceCsv = (jsonFormList) => {
const bigJsonForm = jsonFormList.reduce((prevJsonForm, currentJsonForm, idx) => {
// for each data upload
Object.entries(currentJsonForm).forEach(([studentName, data]) => {
// for each student
// rename the columns
// TODO: handle the case where a column requires escaping (e.g. "interactions,andstuff \"")
const dataRenamed = Object.fromEntries(
Object.entries(data).map(
([fieldName, v])=> [(fieldName + "_" + new String(idx)), v]
)
);
// and union it back into the ongoing object (default empty)
prevJsonForm[studentName] = { ...(prevJsonForm[studentName] ?? {}), ...dataRenamed};
});
return prevJsonForm;
});
const tupleForm = Object.entries(bigJsonForm);
const columnNames = Object.keys(tupleForm[0][1]);
const csvRows = tupleForm.map(([studentName, rowData]) => {
const colData = columnNames.map((colName) => rowData[colName] ?? "");
const csvRow = ([studentName].concat(colData)).join(",");
return csvRow;
});
const headerRow = ["Name"].concat(columnNames);
return ([headerRow].concat(csvRows)).join("\n");
};
document.addEventListener("DOMContentLoaded", () => {
document.getElementById("submitButton").addEventListener("click", async () => {
const ENDPOINT = loadValue("endpointURI");
const expName = loadValue("nameInput");
const privJwkRaw = JSON.parse(window.atob(loadValue("privKeyInput")));
const privJwk = await getPrivKey(privJwkRaw);
console.log("GET", ENDPOINT + "?name=" + expName);
const { data } = await fetch(ENDPOINT + "?name=" + expName, { method: "get", mode: 'cors' }).then(r => r.json());
const decryptedData = await Promise.all(
data.map(async (encryptedUpload) => {
// Get { pseudonym : encryptedFields }
const upload = await decrypt(encryptedUpload, privJwk);
// Get [ pseudonym : unencryptedFields ][]
const rowDataTuples = await Promise.all(
Object.entries(upload).map(
async ([pseudonym, encryptedRow]) =>
[pseudonym, await decrypt(encryptedRow, privJwk)]
)
);
const decryptedUpload = Object.fromEntries(rowDataTuples);
return decryptedUpload;
})
);
const csvData = produceCsv(decryptedData);
const fileEncoding = window.btoa(csvData);
// turn the decrypted data into a downloadable file
document.getElementById("outfileATag").href = "data:text/plain;base64," + await fileEncoding;
document.getElementById("display").hidden = false;
});
});
</script>
<h2>Download data for an Experiment:</h2>
<div id="dialog">
<!-- -->
<label>
Endpoint URL:
<input type="url" id="endpointURI" value="https://anonymization.nelson-lojo.workers.dev/experiment"/>
</label>
<br />
<label>
Experiment Name:
<input id="nameInput" />
</label>
<br />
<p>Private Key:</p>
<textarea id="privKeyInput"
placeholder="Enter the private key that was generated when you created the experiment">
</textarea>
<br />
<button id="submitButton" aria-label="Download data from this experiment">
Download Data
</button>
</div>
<div id="display" hidden>
<a id="outfileATag" download>download your data</a>
</div>
</div>
</body>
</html>