-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
299 lines (271 loc) · 10.1 KB
/
index.html
File metadata and controls
299 lines (271 loc) · 10.1 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!--
Copyright 2026 Digital Bazaar, Inc.
SPDX-License-Identifier: BSD-3-Clause
-->
<html>
<head>
<meta http-equiv="content-security-policy" content="frame-src 'none'">
<style>
.flex-container {
display: flex;
/* Optional: Adds space between the items */
gap: 20px;
@media (width <= 1080px) {
flex-direction: column;
}
}
.column-item {
/* Items will grow to fill equal available horizontal space */
flex: 1;
/* Add other styling */
padding: 15px;
background-color: #f0f0f0;
border: 1px solid #ccc;
}
.show {
display: block !important;
}
@media (width >= 1080px) {
.expander {
display: none;
}
.expand-content {
display: block;
}
}
@media (width <= 1080px) {
.expander {
display: block;
}
.expand-content {
display: none;
}
}
</style>
<script type="module">
import {render} from './render.js';
let useButton = false;
document.addEventListener('DOMContentLoaded', () => {
// expand interface for raw/complete VC
const expandButton = document.querySelector('#expand-button');
expandButton.addEventListener('click', () => {
const content = document.querySelector('.expand-content');
if(content.classList.contains('show')) {
content.classList.remove('show');
expandButton.textContent = 'Show';
} else {
content.classList.add('show');
expandButton.textContent = 'Hide';
}
});
const renderButton = document.querySelector('#render');
const loading = document.querySelector('#loading');
renderButton.addEventListener('click', () => {
renderButton.style.display = 'none';
loading.style.display = 'block';
doRender().catch(() => {});
});
// render immediately if button=false is specified
const searchParams = new URLSearchParams(window.location.search);
useButton = searchParams.get('button') !== 'false';
if(!useButton) {
renderButton.style.display = 'none';
doRender().catch(() => {});
}
});
const credential = {
'@context': [
'https://www.w3.org/ns/credentials/v2',
'https://www.w3.org/ns/credentials/examples/v2'
],
type: ['VerifiableCredential', 'NameCredential'],
issuer: {
id: 'did:example:1234',
name: 'The Issuer',
},
credentialSubject: {
name: 'Example Name',
notRendered: 'should not appear'
},
renderMethod: {
type: 'TemplateRenderMethod',
renderEngine: 'html',
renderProperty: [
'/issuer/name',
'/credentialSubject/name'
],
template: {
id:
'https://test.example/credential-templates/NameCredential.html',
mediaType: 'text/html',
digestMultibase:
'zQmerWC85Wg6wFl9znFCwYxApG270iEu5h6JqWAPdhyxz2dR'
},
outputPreference: {
mode: ['visual', 'audio'],
mediaType: 'application/html',
// FIXME: TBD
style: {width: '800px', height: '800px'}
}
}
};
const fullCredentialAsString = JSON.stringify(credential, null, 2);
const fullCredentialTextarea =
document.querySelector('#fullCredential');
fullCredentialTextarea.innerHTML = fullCredentialAsString;
fullCredentialTextarea.style.width = '100%';
fullCredentialTextarea.style.height =
fullCredentialAsString.split('\n').length - 1 + 'rem';
async function doRender() {
const renderMethod = credential.renderMethod;
// fetch mustache source; note that this source would be injected into
// the template referenced in the credential by a build process run
// by the issuer, and would therefore appear in the full template
// retrieved from `renderMethod.id`; it is only fetched here for
// convenience and to demonstrate the assembly details)
let mustacheSrc;
try {
const response = await fetch('./mustache-4.2.js');
mustacheSrc = await response.text();
} catch(e) {
console.error(e);
throw e;
}
// FIXME: dereference template from `renderMethod.id`
const template = `
<div>
<h1 id="credentialSubject-name"></h1>
<p>Issued by: <span id="issuer-name"></span></p>
<div>
<label for="content">Selectively Disclosed Credential:</label>
<div><textarea id="content"></textarea></div>
</div>
<div>
The outer/Wallet HTML uses
<abbr title="Content-Security-Policy">CSP</abbr> set in its
<code><meta></code> tag to prevent the use of an
<code>src</code> attribute being used (i.e., "frame-src 'none'").
Consequently, clicking the following link will result in an error
(i.e., it won't hit the server at all):
<ul>
<li><a href="https://example.com/link-blocked">
Remote Link (should be blocked)</a>
</li>
<li>
<a href="${window.location.href}/?exfiltration=attempt">
Local Link (should be blocked)
</a>
</li>
</ul>
<div id="mustache-target">
</div>
</div>
<!-- embedded mustache library -->
<script>
${mustacheSrc}
<\/script>
<!-- mustache template -->
<script id="mustache-template" type="x-tmpl-mustache">
This part of the template is rendered using an embedded template
rendering engine, mustache. It includes this value from the
credential as a mustache variable:
<p>
Issuer name: {{ credential.issuer.name }}
</p>
Note that the whole rendering, if desired, could be generated by
an embedded rendering engine of choice (or more than one!).
<\/script>
<!-- main script -->
<script>
console.log('running template render script');
// modals should be blocked
alert('should be blocked');
// display credential as JSON as an example renderer; anything
// could be done here instead, including mustache/other-style
// template processing to generate the HTML for display
const credential = JSON.parse(document.querySelector(
'head > script[name="credential"]').innerHTML);
const contentDiv = document.querySelector('#content');
const credentialAsString = JSON.stringify(credential, null, 2);
contentDiv.innerHTML = credentialAsString;
contentDiv.style.width = '100%';
contentDiv.style.height = credentialAsString.split('\\n').length - 1 + 'rem';
document.querySelector('#credentialSubject-name').innerText =
credential.credentialSubject.name;
document.querySelector('#issuer-name').innerText =
credential.issuer.name;
// run mustache
const tpl = document.getElementById(
'mustache-template').innerText;
const rendered = Mustache.render(tpl, {credential});
document.getElementById('mustache-target').innerHTML = rendered;
// demonstrate blocked network access
fetch('./index.html').then(result => {
console.error('fetch not blocked', result);
}).catch(e => {
console.log('network access intentionally blocked', e);
});
setTimeout(() => {
window.renderMethodReady()
}, ${useButton ? 2000 : 0});
<\/script>
</div>`;
const {iframe, ready} = await render({
credential,
renderProperty: renderMethod.renderProperty,
template
});
ready.then(() => {
console.log('rendering ready');
const loading = document.querySelector('#loading');
loading.style.display = 'none';
iframe.style.display = 'block';
});
const {style} = renderMethod.outputPreference;
for(const key in style) {
iframe.style[key] = style[key];
}
iframe.style.display = 'none';
const display = document.querySelector('#display');
display.appendChild(iframe);
}
</script>
</head>
<body>
<h1>HTML Render Method Test</h1>
<p>This page would live inside a Wallet (or other Verifiable Credential
rendering client).
</p>
<div class="flex-container">
<div class="column-item">
<h2>Raw/Complete Verifiable Credential</h2>
<div class="expander">
<button id="expand-button">Show</button>
</div>
<div class="expand-content">
<p>
Below is the "raw" Credential data prior to being selectively disclosed
and rendered by the iframe (to the right / below):
</p>
<textarea id="fullCredential"></textarea>
</div>
</div>
<div class="column-item">
<h2>Rendered Credential (in a restricted <code>iframe</code>)</h2>
<p>
Below is the rendered Credential after selective disclosure and
rendering via the HTML Render Method within a sandboxed
<code>iframe</code>:
</p>
<div id="display">
<button id="render">
Render
</button>
<div id="loading" style="display: none">
Waiting 2 seconds to demonstrate async capability...
</div>
</div>
</div>
</div>
</body>
</html>