Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
319 lines (248 loc) · 7.9 KB
/
Copy pathscript.js
File metadata and controls
319 lines (248 loc) · 7.9 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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
const form = document.getElementById("verification-form");
const certificateInput = document.getElementById("certificate-id");
const resultContainer = document.getElementById("verification-result");
let certificates = [];
// Load certificate records
async function loadCertificates() {
try {
const response = await fetch("data/certificates.json");
if (!response.ok) {
throw new Error("Could not load certificate database.");
}
certificates = await response.json();
} catch (error) {
console.error("Certificate database error:", error);
showSystemError(
"The verification database could not be loaded. Please try again later."
);
}
}
// Normalize Certificate ID
function normalizeCertificateId(id) {
return id.trim().toUpperCase();
}
// Find certificate
function findCertificate(id) {
const normalizedId = normalizeCertificateId(id);
return certificates.find(
certificate =>
normalizeCertificateId(certificate.certificate_id) === normalizedId
);
}
// Format issue date
function formatDate(dateString) {
if (!dateString) {
return "Not specified";
}
const date = new Date(dateString + "T00:00:00");
return date.toLocaleDateString("en-GB", {
day: "2-digit",
month: "long",
year: "numeric"
});
}
// Show a certificate that passed ID and token verification
function showValidCertificate(certificate) {
resultContainer.style.display = "block";
resultContainer.innerHTML = `
<div style="
border: 1px solid #b8dec9;
border-left: 5px solid #16834b;
background: #f4fbf7;
padding: 22px;
border-radius: 8px;
">
<div style="
color: #16834b;
font-weight: bold;
font-size: 18px;
margin-bottom: 18px;
">
✓ Verified Certificate
</div>
<div style="line-height: 1.8; font-size: 14px;">
<div>
<strong>Student:</strong>
${escapeHtml(certificate.student_name)}
</div>
<div>
<strong>Course:</strong>
${escapeHtml(certificate.course)}
</div>
<div>
<strong>Certificate ID:</strong>
${escapeHtml(certificate.certificate_id)}
</div>
<div>
<strong>Issue Date:</strong>
${formatDate(certificate.issue_date)}
</div>
<div>
<strong>Issued by:</strong>
${escapeHtml(certificate.issuer)}
</div>
<div>
<strong>Institution:</strong>
${escapeHtml(certificate.institution)}
</div>
</div>
</div>
`;
}
// Show a certificate found by ID without QR token verification
function showRecordFound(certificate) {
resultContainer.style.display = "block";
resultContainer.innerHTML = `
<div style="
border: 1px solid #efd6a4;
border-left: 5px solid #c99324;
background: #fffaf0;
padding: 22px;
border-radius: 8px;
">
<div style="
color: #9a6b0f;
font-weight: bold;
font-size: 18px;
margin-bottom: 10px;
">
Certificate Record Found
</div>
<div style="font-size: 14px; line-height: 1.6;">
Certificate ID
<strong>${escapeHtml(certificate.certificate_id)}</strong>
exists in the PcIST certificate registry. This result is not a secure QR verification.
</div>
</div>
`;
}
// Show revoked certificate
function showRevokedCertificate(certificate) {
resultContainer.style.display = "block";
resultContainer.innerHTML = `
<div style="
border: 1px solid #efc3c3;
border-left: 5px solid #c43b3b;
background: #fff7f7;
padding: 22px;
border-radius: 8px;
">
<div style="
color: #c43b3b;
font-weight: bold;
font-size: 18px;
margin-bottom: 10px;
">
Certificate Revoked
</div>
<div style="font-size: 14px; line-height: 1.6;">
Certificate ID
<strong>${escapeHtml(certificate.certificate_id)}</strong>
exists in the PcIST certificate registry but is no longer valid.
</div>
</div>
`;
}
// Show certificate not found
function showNotFound(id) {
resultContainer.style.display = "block";
resultContainer.innerHTML = `
<div style="
border: 1px solid #efc3c3;
border-left: 5px solid #c43b3b;
background: #fff7f7;
padding: 22px;
border-radius: 8px;
">
<div style="
color: #c43b3b;
font-weight: bold;
font-size: 18px;
margin-bottom: 10px;
">
Certificate Not Found
</div>
<div style="font-size: 14px; line-height: 1.6;">
No certificate matching
<strong>${escapeHtml(id)}</strong>
was found in the official PcIST certificate registry.
</div>
</div>
`;
}
// System/database error
function showSystemError(message) {
resultContainer.style.display = "block";
resultContainer.innerHTML = `
<div style="
border: 1px solid #efd6a4;
border-left: 5px solid #c99324;
background: #fffaf0;
padding: 22px;
border-radius: 8px;
">
${escapeHtml(message)}
</div>
`;
}
// Prevent HTML injection from certificate data
function escapeHtml(value) {
const div = document.createElement("div");
div.textContent = value ?? "";
return div.innerHTML;
}
// Main verification
function verifyCertificate(id, token = null, isQrVerification = false) {
const normalizedId = normalizeCertificateId(id);
if (!normalizedId) {
return;
}
certificateInput.value = normalizedId;
const certificate = findCertificate(normalizedId);
if (!certificate) {
showNotFound(normalizedId);
return;
}
const tokenMatches =
isQrVerification &&
typeof token === "string" &&
typeof certificate.verification_token === "string" &&
token === certificate.verification_token;
if (certificate.status?.toLowerCase() === "revoked") {
if (!isQrVerification || tokenMatches) {
showRevokedCertificate(certificate);
} else {
showRecordFound(certificate);
}
return;
}
if (certificate.status?.toLowerCase() === "valid") {
if (tokenMatches) {
showValidCertificate(certificate);
} else {
showRecordFound(certificate);
}
return;
}
showSystemError(
"This certificate record has an unknown verification status."
);
}
// Manual verification
form.addEventListener("submit", event => {
event.preventDefault();
verifyCertificate(certificateInput.value);
});
// Start application
async function initializeVerification() {
await loadCertificates();
// Read Certificate ID from QR URL:
// ?id=PCIST-DSAI-2026-001&token=...
const params = new URLSearchParams(window.location.search);
const certificateId = params.get("id");
const verificationToken = params.get("token");
if (certificateId) {
verifyCertificate(certificateId, verificationToken, true);
}
}
initializeVerification();