-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
379 lines (369 loc) · 11.9 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>NFC onboarder</title>
<style>
body {
background-color: #f0f0f0;
color: #000;
font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
sans-serif;
line-height: 1.25;
}
.screen {
position: fixed;
inset: 0;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 1.5rem;
}
.screen:not([data-showing="true"]) {
display: none;
}
.info {
color: #555;
font-size: 1.8em;
}
.red {
background-color: #c00;
color: #fff;
}
.green {
background-color: #080;
color: #fff;
}
.center {
text-align: center;
}
body[data-debug="true"] #screens {
display: flex;
flex-wrap: wrap;
gap: 1em;
}
body[data-debug="true"] .screen::before {
position: absolute;
content: attr(id);
bottom: 3px;
right: 3px;
}
body[data-debug="true"] .screen {
display: flex;
position: relative;
width: 360px;
height: 640px;
border: 1px solid #f00;
}
.unsupported {
font-weight: 700;
font-size: 3em;
margin-bottom: 1rem;
}
success-warning-message {
display: block;
background: #2224;
color: #ff6;
padding: 0.5rem 0.75rem;
border-radius: 0.25rem;
}
h1 {
text-align: center;
font-size: 1.5em;
margin: 0 0 0.5rem;
}
p {
margin: 0;
}
.v-stack {
display: flex;
flex-direction: column;
gap: 1rem;
}
.form-control input {
width: 100%;
box-sizing: border-box;
}
.hint {
font-size: 0.8em;
color: #0008;
}
.form-control input {
padding: 0.5rem;
border: 1px solid #888;
border-radius: 0.25rem;
}
.form-control.is-success {
color: #080;
}
.form-control.is-success input {
border-color: #080;
color: #080;
}
button {
padding: 0.5rem 1rem;
border: 1px solid #888;
border-radius: 0.25rem;
cursor: pointer;
font-size: 1em;
font-family: inherit;
background: linear-gradient(180deg, #fff, #eee);
box-shadow: 0 1px 2px #888;
}
#errorText {
width: 100%;
box-sizing: border-box;
background: #000;
color: #fff;
padding: 0.5rem 0.75rem;
}
.dangerous {
color: #e11;
}
</style>
</head>
<body data-debug="false">
<div id="screens">
<div id="welcome" class="screen">
<div class="v-stack">
<h1>NFC onboarder</h1>
<p>
You can use this web application to write URLs to blank NFC tags.
Once onboarded, when someone taps the tag with their phone, it will
open the URL you specify.
</p>
<form class="v-stack">
<p class="form-control">
<label for="url">Configure URL:</label>
<input type="text" id="url" value="" name="url" />
<span class="hint"
>{sn} will be replaced with the tag's serial number,
{hmac:<i>key</i>} will be replaced with the HMAC of the tag's
serial number using the specified key</span
>
</p>
<button type="submit" id="begin">Begin</button>
</form>
</div>
</div>
<div id="begin" class="screen">
<div class="v-stack">
<h1>NFC onboarder</h1>
<p>
This web application will onboard blank NFC tags by writing the
following URL when you tap the tag:
</p>
<div class="form-control is-success">
<label for="targetUrl">Target URL configured:</label>
<input type="text" id="targetUrl" readonly value="https://" />
<span class="hint"
>{sn} will be replaced with the tag's serial number,
{hmac:<i>key</i>} will be replaced with the HMAC of the tag's
serial number using the specified key</span
>
</div>
<button id="scan">Activate NFC reader</button>
</div>
</div>
<div id="activating" class="screen info">
<div>Activating NFC reader...</div>
</div>
<div id="onboarding" class="screen info">
<div>Onboarding tag...</div>
</div>
<div id="locking" class="screen info">
<div>Locking tag...</div>
</div>
<div id="waiting" class="screen info">
<div>Tap your NFC tag...</div>
</div>
<div id="already" class="screen green">
<div class="v-stack">
<h1>NFC tag already onboarded.</h1>
<div class="center">Serial number: <strong class="sn"></strong></div>
<div><success-warning-message></success-warning-message></div>
</div>
</div>
<div id="success" class="screen green">
<div class="v-stack">
<h1>NFC tag has been onboarded.</h1>
<div class="center">Serial number: <strong class="sn"></strong></div>
<div><success-warning-message></success-warning-message></div>
</div>
</div>
<div id="waitingLock" class="screen info">
<div class="v-stack">
<div class="center dangerous">
Tap your NFC tag<br />to make it read-only
</div>
<button type="submit" id="cancelLock">Cancel</button>
</div>
</div>
<div id="locked" class="screen green">
<div class="v-stack">
<h1>NFC tag has been locked.</h1>
<div class="center">Serial number: <strong class="sn"></strong></div>
</div>
</div>
<div id="error" class="screen red">
<div class="v-stack">
<div>Something went wrong!</div>
<textarea id="errorText" readonly rows="10" cols="36"></textarea>
</div>
</div>
</div>
<script type="module">
import { compileUrlTemplate } from "./compileUrlTemplate.js";
const $ = (selector) => document.querySelector(selector);
const searchParams = new URLSearchParams(location.search);
const url = searchParams.get("url") || "";
let lastExpectedUrl = "";
let lockUrl = "";
const setScreen = (id) => {
document.querySelectorAll(".screen").forEach((screen) => {
screen.dataset.showing = screen.id === id;
});
};
for (const snSpan of document.querySelectorAll(".sn")) {
snSpan.textContent = "01:23:45:67:89:AB:CD";
}
if (typeof NDEFReader === "undefined") {
document.body.dataset.debug = true;
const msg = document.createElement("div");
msg.className = "unsupported";
msg.innerHTML = "NFC is not supported on this device.";
document.body.insertBefore(msg, document.body.firstChild);
}
if (url) {
$("#targetUrl").value = url;
setScreen("begin");
} else {
setScreen("welcome");
}
function showError(message) {
setScreen("error");
$("#errorText").value = message;
}
$("#scan").addEventListener("click", async () => {
try {
const reader = new NDEFReader();
setScreen("activating");
await reader.scan();
setScreen("waiting");
reader.onreading = async (event) => {
const { timeStamp, message, serialNumber } = event;
const stringifyRecords = () =>
JSON.stringify(
Array.from(message.records, (r) => {
let data = r.data;
if (r.recordType === "mime") {
const prefix = `data:${r.mediaType};base64,`;
const bytes = new Uint8Array(
data.buffer,
data.byteOffset,
data.byteLength
);
data = prefix + btoa(String.fromCharCode(...bytes));
} else if (data instanceof DataView) {
const decoder = new TextDecoder();
data = decoder.decode(data);
}
if (typeof data !== "string") data = String(data);
return { recordType: r.recordType, data: data };
}),
null,
2
);
const sn = serialNumber.replace(/:/g, "");
const template = await compileUrlTemplate(url, { serialNumber });
const expectedUrl = template.generateUrl();
for (const snSpan of document.querySelectorAll(".sn")) {
snSpan.textContent = serialNumber;
}
lastExpectedUrl = expectedUrl;
let scannedUrl;
if (
message.records.length === 1 &&
message.records[0].recordType === "url" &&
template.check(
(scannedUrl = new TextDecoder().decode(message.records[0].data))
)
) {
if (lockUrl === scannedUrl) {
lockUrl = "";
setScreen("locking");
try {
await reader.makeReadOnly();
setScreen("locked");
} catch (e) {
showError(`Unable to lock: ${e}`);
}
} else if (lockUrl) {
showError(
"The tag being locked is not the same as the tag being onboarded. Cancelling lock."
);
} else {
setScreen("already");
}
} else if (lockUrl) {
lockUrl = "";
showError(
"The tag being locked is not the same as the tag being onboarded. Cancelling lock."
);
} else if (
message.records.length === 0 ||
(message.records.length === 1 &&
message.records[0].recordType === "empty")
) {
setScreen("onboarding");
try {
await reader.write({
records: [{ recordType: "url", data: expectedUrl }],
});
setScreen("success");
} catch (e) {
showError(`Unable to onboard: ${e}`);
}
} else {
setScreen("error");
showError(
`Non-empty tag scanned, refusing to replace. Please clear the tag first.`
);
}
};
} catch (error) {
alert(`Unable to scan NFC: ${error}`);
}
});
$("#cancelLock").addEventListener("click", () => {
lockUrl = "";
setScreen("waiting");
});
customElements.define(
"success-warning-message",
class extends HTMLElement {
constructor() {
super();
this.innerHTML = `
<div class="v-stack" style="gap: 0.5em">
<small>
<strong>Note:</strong> This NFC tag may still be writable.
You can permanently lock this tag to prevent further writes,
or use the “NFC Tools” app to set up a write password.
</small>
<button class="js-lock-tag">Permanently lock</button>
</div>
`;
this.querySelector(".js-lock-tag").addEventListener("click", () => {
setScreen("waitingLock");
lockUrl = lastExpectedUrl;
});
}
}
);
</script>
</body>
</html>