-
Notifications
You must be signed in to change notification settings - Fork 4
/
redeemer.js
98 lines (78 loc) · 2.38 KB
/
redeemer.js
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
/* Copyright 2020 Google LLC. SPDX-License-Identifier: Apache-2.0 */
"use strict";
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
EventTarget.prototype.on = EventTarget.prototype.addEventListener;
function base64decode(str) {
return new Uint8Array([...atob(str)].map(a => a.charCodeAt(0)));
}
function sleep(ms) {
return new Promise((done, fail) => {
setTimeout(done, ms);
});
}
async function progress(message) {
$(message).style.display = "revert";
await sleep(1000);
}
document.on("DOMContentLoaded", async e => {
console.log(e);
const ISSUER = "https://trust-token-issuer-demo.glitch.me";
async function verify_human(e) {
e.preventDefault();
$("dialog").showModal();
await progress("#checking");
// check token exists
const token = await document.hasTrustToken(ISSUER);
console.log(token);
await progress("#hasTrustToken");
if (token === false) {
// no token
await progress("#go2issuer");
} else {
await progress("#found");
try {
await progress("#redemption");
// redemption request
await fetch(`${ISSUER}/.well-known/trust-token/redemption`, {
method: "POST",
trustToken: {
type: "token-redemption",
issuer: ISSUER,
refreshPolicy: "none"
}
});
} catch (err) {
await progress("#cached");
console.info(err);
}
await progress("#verify");
// send RR and echo Sec-Redemption-Record
const res = await fetch(`/.well-known/trust-token/send-rr`, {
method: "POST",
headers: new Headers({
"Signed-Headers": "sec-redemption-record, sec-time"
}),
trustToken: {
type: "send-redemption-record",
issuers: [ISSUER],
refreshPolicy: "none",
includeTimestampHeader: true,
signRequestData: "include",
additionalSigningData: "additional_signing_data"
}
});
const body = await res.json();
console.log(JSON.stringify(body, " ", " "));
if (body.sig_verify) {
await progress("#finish");
$("dialog").close();
$("summary").removeEventListener("click", verify_human);
e.target.click();
} else {
await progress("#failed");
}
}
}
$("summary").on("click", verify_human);
});