04 plugin.js (1/4)

File: examples/plugins/device-code-catalog/plugin.js (108 lines)

// device-code-catalog: web side of an OAuth 2.0 device-code catalog.
//
// The reader can sign in on its own (device.json "auth"), so this page is
// optional. It offers:
//   - the server address (config.json -> {cfg.server}); a real service would
//     hard-code its URLs in device.json and drop this field,
//   - the SAME sign-in from the browser (handy when the reader is in your bag),
//   - sign out.
// Both sides write token.json as {"token": "..."}, so either sign-in works for both.

const CLIENT_ID = 'crosspoint-example';

CrossPoint.registerPlugin(async (container, api) => {
  const $ = (s) => container.querySelector(s);
  const b64 = (t) => { let s = ''; for (const c of new TextEncoder().encode(t)) s += String.fromCharCode(c); return btoa(s); };
  const write = (name, obj) => api.writeFile(`${api.dir}/${name}`, b64(JSON.stringify(obj)));
  const read = async (name) => {
    try { const r = await fetch(api.pluginFile(name)); return r.ok ? await r.json() : {}; } catch (e) { return {}; }
  };
  const form = (o) => new URLSearchParams(o).toString();
  const FORM = { 'Content-Type': 'application/x-www-form-urlencoded' };
  const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
‹ PreviousReader file (.txt)Next ›