-
Notifications
You must be signed in to change notification settings - Fork 125
/
check-token.html
181 lines (160 loc) · 6.23 KB
/
check-token.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
<!doctype html>
<meta charset="utf-8">
<title>Check an Origin Trial token</title>
<meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">
<style>
th { text-align: right; }
#validity { color: #000; background-color: #8f8; }
#validity.invalid { color: #fff; background-color: #f00; }
</style>
<h1>Check an Origin Trial token</h1>
<p>
<label>Paste the token here<br>
<textarea id="token" placeholder="Token" autofocus cols="50" rows="6"></textarea>
</label>
</p>
<p>
<label>Is this a test token? (generated by Chromium utility, not Origin Trials developer console)<br>
<input id="istesttoken" type="checkbox"></input>
</label>
</p>
<table>
<tr><th>Valid?</th><td><span id="validity"></span></td></tr>
<tr><th>Version</th><td><span id="version"></span></td></tr>
<tr><th>Origin</th><td><span id="origin"></span></td></tr>
<tr><th>Matches Subdomains?</th><td><span id="subdomain"></span></td></tr>
<tr><th>Matches Third-party?</th><td><span id="thirdparty"></span></td></tr>
<tr><th>Usage Restriction</th><td><span id="usage"></span></td></tr>
<tr><th>Feature</th><td><span id="feature"></span></td></tr>
<tr><th>Expires</th><td><span id="expiry"></span></td></tr>
</table>
<script type="text/javascript"
src="https://cdn.rawgit.com/tonyg/js-nacl/09d04c712117961a21aebf9c4735eb04f05ea79e/lib/nacl_factory.js"
crossorigin="anonymous"
integrity="sha384-k1LT12bBTLMRe+VRFdE0F4iaPU3RO5oSILEsw4TJ8eNtzZLlMuWY4MShH79NAmU8">
</script>
<script>
// Production key for validating signatures on tokens issued by the production OT console.
// From https://source.chromium.org/chromium/chromium/src/+/master:components/embedder_support/origin_trials/origin_trial_policy_impl.cc;l=23;drc=073e74ade285c0c22ef01bccdf260854d7b1aa85
// as of 2021-01-14.
const kProductionPublicKey = new Uint8Array([
0x7c, 0xc4, 0xb8, 0x9a, 0x93, 0xba, 0x6e, 0xe2, 0xd0, 0xfd, 0x03,
0x1d, 0xfb, 0x32, 0x66, 0xc7, 0x3b, 0x72, 0xfd, 0x54, 0x3a, 0x07,
0x51, 0x14, 0x66, 0xaa, 0x02, 0x53, 0x4e, 0x33, 0xa1, 0x15,
]);
// Test key for validating signatures on tokens issued for Chromium test purposes.
// From https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/common/origin_trials/trial_token_unittest.cc;l=40;drc=dbd8b9731136091384f48592a664b3c01be1a822
// as of 2021-01-14.
const kTestPublicKey = new Uint8Array([
0x75, 0x10, 0xac, 0xf9, 0x3a, 0x1c, 0xb8, 0xa9, 0x28, 0x70, 0xd2,
0x9a, 0xd0, 0x0b, 0x59, 0xe1, 0xac, 0x2b, 0xb7, 0xd5, 0xca, 0x1f,
0x64, 0x90, 0x08, 0x8e, 0xa8, 0xe0, 0x56, 0x3a, 0x04, 0xd0,
]);
const tokenElem = document.getElementById("token");
const isTestElem = document.getElementById("istesttoken");
const validityElem = document.getElementById("validity");
const versionElem = document.getElementById("version");
const originElem = document.getElementById("origin");
const subdomainElem = document.getElementById("subdomain");
const thirdpartyElem = document.getElementById("thirdparty");
const usageElem = document.getElementById("usage");
const featureElem = document.getElementById("feature");
const expiryElem = document.getElementById("expiry");
let nacl;
nacl_factory.instantiate(readyNacl => { nacl = readyNacl; });
const utf8Decoder = new TextDecoder("utf-8", {fatal: true});
function validate() {
validityElem.textContent = "";
validityElem.classList.add("invalid");
originElem.textContent = "";
subdomainElem.textContent = "";
thirdpartyElem.textContent = "";
featureElem.textContent = "";
expiryElem.textContent = "";
// Base64-decode the token into a Uint8Array.
let tokenStr;
try {
tokenStr = atob(tokenElem.value);
} catch(e) {
console.error(e);
validityElem.textContent = "Invalid Base64";
return;
}
const token = new Uint8Array(tokenStr.length);
for (let i = 0; i < token.length; i++) {
token[i] = tokenStr.charCodeAt(i);
}
// Check that the version number is 2 or 3.
console.log(token);
const version = token[0];
versionElem.textContent = "" + version;
if (version !== 2 && version !== 3) {
validityElem.textContent = "Unknown version";
return;
}
// Pull the fields out of the token.
if (token.length < 69) {
validityElem.textContent = "Token is too short";
return;
}
const signature = new Uint8Array(token.buffer, 1, 64);
const payloadLength = new DataView(token.buffer, 65, 4).getInt32(0, /*littleEndian=*/false);
const payload = new Uint8Array(token.buffer, 69);
if (payload.length !== payloadLength) {
validityElem.textContent =
"Token is " + payload.length + " bytes; expected " + payloadLength;
return;
}
// The version + length + payload is signed.
const signedData = new Uint8Array(token.buffer.slice(64));
signedData[0] = token[0];
// Check the ED25519 signature.
const publicKey = isTestElem.checked ? kTestPublicKey : kProductionPublicKey;
if (!nacl.crypto_sign_verify_detached(signature, signedData, publicKey)) {
validityElem.textContent = "Invalid signature";
return;
}
// Pull the fields out of the JSON payload.
let json;
try {
json = utf8Decoder.decode(payload);
} catch(e) {
console.error(e);
validityElem.textContent = "Invalid UTF-8";
return;
}
console.log("Token JSON", json);
let obj;
try {
obj = JSON.parse(json);
} catch(e) {
console.error(e);
validityElem.textContent = "Invalid JSON";
return;
}
originElem.textContent = obj.origin;
subdomainElem.textContent = obj.isSubdomain ? "Yes" : "No";
thirdpartyElem.textContent = obj.isThirdParty ? "Yes" : "No";
usageElem.textContent = obj.usage;
featureElem.textContent = obj.feature;
let expiry;
try {
expiry = parseInt(obj.expiry);
} catch(e) {
console.error(e);
validityElem.textContent = "Expiry value wasn't an integer";
expiryElem.textContent = obj.expiry;
return;
}
let expiryDate = new Date(expiry * 1000);
expiryElem.textContent = expiryDate.toLocaleString();
if (expiryDate < new Date()) {
validityElem.textContent = "Expired"
return;
}
validityElem.classList.remove("invalid");
validityElem.textContent = "Valid";
}
tokenElem.oninput = validate;
isTestElem.oninput = validate;
</script>