This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
173 lines (137 loc) · 6.54 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Virtru SDK for JavaScript - Form</title>
<link href="https://sdk.virtru.com/js/latest/auth-widget/index.css" rel="stylesheet" />
<script src="https://sdk.virtru.com/js/latest/auth-widget/index.js"></script>
<script src="https://sdk.virtru.com/js/latest/virtru-sdk.min.js"></script>
<script src="https://sdk.amazonaws.com/js/aws-sdk-2.283.1.min.js"></script>
<script src="js/aws_s3.js"></script>
<script src="js/util.js"></script>
<link href="css/form.css" rel="stylesheet" />
</head>
<body>
<div class="form-style-2" id="formContainer">
<div class="form-style-2-heading">Form Example</div>
<div class="form-style-2"><strong>Security Note</strong>
<ul>
<li>All data will be owned by form submitter</li>
<li>User [email protected] will be granted access</li>
<li>Form data can be managed by visiting: <a href="https://secure.virtru.com/dashboard">Virtru Dashboard</a></li>
<li>Data can be revoked at any time</li>
</ul>
</div>
<form id="myForm" onsubmit="return false;">
<label for="form_firstname"><span>First Name </span><input type="text"
class="input-field" name="form_firstname" value="" /></label>
<label for="form_lastname"><span>Last Name</span><input type="text"
class="input-field" name="form_lastname" value="" /></label>
<label for="form_email"><span>Email</span><input type="text"
class="input-field" name="form_email" value="" /></label>
<label for="form_file"><span>File</span><input type="file"
class="input-field" id="form_file" name="form_file" value="" /></label>
</form>
<button id="btnSubmit">Submit</button>
</div>
<div id="spinner" class="virtru-auth-loader" style="display: none;"></div>
<div id="success" style="display: none;"><h1>Success!</h1></div>
<div id="authModal" style="display:none;"></div>
<div id="app"></div>
<script>
// Owner of the form. This address will be
// granted rights to the data submitted via the form
const formOwner = "[email protected]";
// Show and Hide UI elements
function showForm(){
document.querySelector('#formContainer').style = 'display:block;';
}
function hideForm(){
document.querySelector('#formContainer').style = 'display:none;';
}
function showSpinner() {
document.querySelector('#spinner').style = 'display:block;';
}
function hideSpinner() {
document.querySelector('#spinner').style = 'display:none;';
}
function showAuth(){
document.querySelector('#authModal').style = 'display:block;';
}
function hideAuth(){
document.querySelector('#authModal').style = 'display:none;';
}
function showSuccess(){
document.querySelector('#success').style = 'display:block;';
}
function hideSuccess(){
document.querySelector('#success').style = 'display:none;';
}
function triggerSubmit() {
showAuth();
return false;
};
// Build JSON form data
function buildJson(formFields) {
let formData = {};
formFields.forEach(fieldName => {
formData[fieldName] = document.querySelector(`#myForm [name="${fieldName}"]`).value;
});
return formData;
}
// Encrypt a string (TDF) then Upload to S3
async function EncryptUploadWithString(uuid,formData,policyOwner) {
const plainFilename = uuid + '.form.json';
const tdfFilename = uuid + '.form.tdf3.html';
const client = new Virtru.Client({email:policyOwner});
const encryptParams = new Virtru.EncryptParamsBuilder()
.withStringSource(JSON.stringify(formData))
.withDisplayFilename(plainFilename)
.withUsersWithAccess([formOwner])
.build();
const ct = await client.encrypt(encryptParams);
const ctString = await ct.toString();
uploadFile(ctString,tdfFilename);
}
// Encrypt a file to TDF then Upload to S3
async function EncryptUploadWithBufferedArray(uuid,fileObject,fileOriginalName,policyOwner) {
let fileBlob = new Blob([fileObject]);
let fileArrayBuffer = null;
fileArrayBuffer = await new Response(fileBlob).arrayBuffer();
const plainFilename1 = fileOriginalName;
const tdfFilename1 = uuid + '.file.1.tdf3.html';
const client1 = new Virtru.Client({email:policyOwner});
const encryptParams1 = new Virtru.EncryptParamsBuilder()
.withArrayBufferSource(fileArrayBuffer)
.withDisplayFilename(plainFilename1)
.withUsersWithAccess([formOwner])
.build();
const encryptedPayload1 = await client1.encrypt(encryptParams1);
const encryptedString1 = await encryptedPayload1.toString();
uploadFile(encryptedString1,tdfFilename1);
}
async function submitFormData(policyOwner) {
let formFields = ["form_firstname","form_lastname","form_email"];
let formData = buildJson(formFields);
const uuid = uuidv4();
console.log(uuid);
EncryptUploadWithString(uuid,formData,policyOwner);
const fileName1 = document.querySelector('#myForm [name="form_file"]').files[0].name;
const fileObject1 = document.querySelector('#myForm [name="form_file"]').files[0];
EncryptUploadWithBufferedArray(uuid,fileObject1,fileName1,policyOwner)
}
// Hide Virtru Auth and submit form data
async function afterAuth(email) {
hideAuth();
await submitFormData(email);
hideForm();
showSuccess();
}
// Add event listener to the submit button
document.getElementById("btnSubmit").addEventListener('click', () => {
triggerSubmit();
})
// Initialize Virtru Authorization Widget
Virtru.AuthWidget('authModal', { afterAuth });
</script>
</body>
</html>