Newer
Older
veriq / frontend / assets / verify.js
/* VerIQ verify page logic — public verification of a signed result */
(function () {
  "use strict";
  var $ = function (id) { return document.getElementById(id); };

  function showError(msg) {
    $("errorArea").textContent = msg;
    $("errorArea").style.display = "";
    $("loading").style.display = "none";
  }

  var m = window.location.pathname.match(/^\/v\/([A-Za-z0-9]{8})$/);
  if (!m) { showError("Invalid verification link."); return; }
  var rid = m[1];

  fetch("/api/result/" + rid).then(function (r) {
    if (!r.ok) return r.json().then(function (e) { throw new Error(e.detail || r.status); });
    return r.json();
  }).then(function (d) {
    $("loading").style.display = "none";
    $("verifyArea").style.display = "";

    $("iqScore").textContent = d.iq;
    $("percentileLine").textContent =
      "higher than " + d.percentile + "% of test takers" +
      (d.flagged ? " · FLAGGED: implausible timing" : "");

    var badges = $("badges");
    badges.innerHTML = "";
    var b1 = document.createElement("span");
    b1.className = "badge " + (d.signature_valid ? "ok" : "bad");
    b1.textContent = d.signature_valid ? "✓ signature valid" : "✗ SIGNATURE INVALID";
    badges.appendChild(b1);
    if (d.identity) {
      var bi = document.createElement("span");
      bi.className = "badge ok";
      bi.textContent = "identity: " + d.identity;
      badges.appendChild(bi);
    } else {
      var bn = document.createElement("span");
      bn.className = "badge";
      bn.textContent = "anonymous result";
      badges.appendChild(bn);
      var cs = $("claimSection");
      if (cs) cs.style.display = "";
      var cb = $("claimX");
      if (cb) cb.addEventListener("click", function () {
        cb.disabled = true;
        fetch("/auth/x/start?result_id=" + rid)
          .then(function (r) {
            if (r.status === 503) throw new Error("not_configured");
            if (!r.ok) throw new Error("http " + r.status);
            return r.json();
          })
          .then(function (data) { window.location.href = data.authorize_url; })
          .catch(function (e) {
            cb.disabled = false;
            var err = $("claimErr");
            err.style.display = "";
            err.textContent = e.message === "not_configured"
              ? "Identity binding is being set up — try again later."
              : "Claim failed: " + e.message;
          });
      });
    }

    $("kvId").textContent = d.result_id;
    $("kvDate").textContent = d.completed_at;
    $("kvIdentity").textContent = d.identity || "not claimed (anonymous)";
    $("kvBank").textContent = "v" + d.bank_version;
    $("kvSig").textContent = d.signature;
    $("kvSig").style.color = d.signature_valid ? "var(--good)" : "var(--bad)";

    fetch("/.well-known/veriq-pubkey.txt").then(function (r) { return r.text(); })
      .then(function (pk) { $("kvPub").textContent = pk.trim(); })
      .catch(function () { $("kvPub").textContent = "unavailable"; });
  }).catch(function (e) {
    showError("Could not load result: " + e.message);
  });
})();