notes
All repositories: gitoria
5.1 KB
// COPY of tickets lib/ident-access.hl (tickets afb9a40, itself a copy of components b27b df95f69 components/ident-access/ident-access.hl), unchanged for notes: the settings are notes' own names// The null fetch (ident unreachable, caught by project.hl's `on Error`) answers 503.// ident-access.hl — an app asks ident whether the request's access key is good (ident#27, the app's side).// Server side only, one file, no View. Copy this file into the app's components/ folder (README.md has the wiring).//// import { check, needWrite, refuse } from './ident-access.hl'// who = check(req) // req is a function route's request// if (!who.ok) { return refuse(who) } // who.ok false: a ready 401 / 403 / 503 answer// who.identity, who.name, who.access // 'read' or 'write'//// The key is read from `Authorization: Bearer idt_…` or from the PASSWORD of `Authorization: Basic …` (git sends it so; the user// name is ignored). Each call asks ident (POST <IDENT_URL>/api/introspect with the app's own key and secret) and nothing is// remembered: a key revoked in ident is refused at the very next call. When ident cannot be reached, or does not accept the// app's own key and secret, the answer is a refusal (503), never an allowance.//// A refused or timed-out connection to ident is an Error event (hybriel#240): the app's project.hl needs an `on Error(e)` handler// (a handler in this file does not catch it); the fetch then gives null and check answers 503. Without one the framework answers 500.//// Settings (environment): IDENT_API_KEY and IDENT_API_SECRET (the app's registration in ident), IDENT_EXCHANGE_URL / IDENT_URL.import { env } from 'hl:proc'import { fetch } from 'hl:fetch'import { fromBase64 } from 'hl:crypto'import { Response } from 'hl:http1'import { envOr } from './util.hl'static hexDigits = '0123456789abcdef'static asciiLead = '01234567'// does the text have the shape of an ident key: idt_ and 48 lower-case hex digitsstatic shaped = (text) => {if (text == null || text.length != 52 || !text.startsWith('idt_')) { return false }rest = text.slice(4)for (c of rest.split('')) {if (!hexDigits.includes(c)) { return false }}return true}// the key out of the Authorization header, or null (no header, another scheme, or not decodable)static keyOf = (req) => {given = req.headers['authorization']if (given == null) { return null }head = given.trim()if (head.length > 8 && head.slice(0, 7).toLowerCase() == 'bearer ') { return head.slice(7).trim() }if (head.length > 7 && head.slice(0, 6).toLowerCase() == 'basic ') {raw = fromBase64(head.slice(6).trim())if (raw == null) { return null }hex = raw.hex()let at = -1for (let i = 0; i + 1 < hex.length && at < 0; i = i + 2) {if (hex.slice(i, i + 2) == '3a') { at = i / 2 }}if (at < 0) { return null }pass = raw.slice(at + 1)ph = pass.hex()// only plain ASCII goes on to toString (a byte that is not UTF-8 would be a located error, not a refusal)for (let i = 0; i < ph.length; i = i + 2) {if (!asciiLead.includes(ph.slice(i, i + 1))) { return null }}return pass.toString()}return null}// ask ident about the request: { ok = true identity name access } or { ok = false status error }static check = (req) => {token = keyOf(req)if (token == null || !shaped(token)) {return { ok = false status = 401 error = 'An ident access key is needed (Authorization: Bearer idt_…).' }}base = envOr('IDENT_EXCHANGE_URL', envOr('IDENT_URL', 'https://ident.worldapi.org'))appKey = env('IDENT_API_KEY')appSecret = env('IDENT_API_SECRET')if (appKey == null || appKey == '' || appSecret == null || appSecret == '') {return { ok = false status = 503 error = 'This app has no ident key and secret set, so it cannot check access keys.' }}res = fetch(base + '/api/introspect', { method = 'POST' json = { key = appKey secret = appSecret token = token } timeoutMs = 8000 })if (res == null || res.status != 200) {return { ok = false status = 503 error = 'ident could not check the key right now.' }}answer = res.json()if (answer == null || answer.active != true) {return { ok = false status = 401 error = 'This access key is not valid for this app.' }}if (answer.access != 'read' && answer.access != 'write') {return { ok = false status = 503 error = 'ident could not check the key right now.' }}return { ok = true identity = answer.identity name = answer.name access = answer.access }}// the same, for a request that changes something: a read-only key is refused with 403static needWrite = (req) => {who = check(req)if (who.ok && who.access != 'write') {return { ok = false status = 403 error = 'This access key is read-only.' }}return who}// the answer for a refused check (who.ok false); a 401 asks for credentials, so git and browsers prompt for themstatic refuse = (who) => {h = { 'Content-Type' = 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' }if (who.status == 401) { h['WWW-Authenticate'] = 'Basic realm="ident access key", charset="UTF-8"' }return new Response(JSON.stringify({ error = who.error }), { status = who.status headers = h })}
Branches
Latest commits
- 24c95433Merge branch 'notes17-ghost-note' into mainAtlas
- ae1075b2Preserve rejected drafts during note ID adoptionAtlas
- 383cce23|||PR Prevent saved local edits from restoring deleted notesAtlas
- 99ca1cf6Merge branch 'vendor-day56' into mainAtlas
- 649cef37|||PR Vendor Hybriel day56Atlas
- aadd0abcMerge branch 'hl-standard-2' into mainAtlas
- 52ffac9f|||PR Keyboard and reload checks for the notes gate (HL-STANDARD F21, F5)Atlas
- 617ee8c0Merge branch 'hl-standard' into mainAtlas
- 452458ee|||PR Type the function parameters of notes' own .hl files (HL-STANDARD L10)Atlas
- 23758cd6Re-vendor complete Hybriel plugins and runtime to day44mre
- 5d7cc700Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
- 3e07b1cfVendor complete Hybriel day39 plugin sources and librariesmre
- d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmre
- 16bc8fd8Re-vendor Hybriel day38b with the mpackdb store-open race fixmre
- d860dc2dnotes#12: the markup names its own rules instead of hand-written class strings: the selected row is `aria-current="page"` (the list-first theme rule styles it, no class), the delete question is `Style.pAsk` and follows its own `open` attribute (bound to `asking`), the error line is `Style.pMessage` (body once in lib/looks.hl, rule in notes.hl, main.hl and loginfailed.hl). The CSS declarations are the same, only selectors changed (.ask -> p.ask, .ask.open -> p.ask[open], .message -> p.message); the pages look the same (AE 0 at 4 sizes). main.hl's selectorClass in/out stays (login.js reads it).mre
- 2e7f8664notes#13: the list and back links pass the note key from the loop (emit pick(e, r.key)) instead of writing data-note into the View and reading it back with getAttribute; the two window.history.replaceState calls and the identcode e.target.value read stay (hl:web has no replace-navigation, a bound hidden input is not adopted on a script's change event), see NO13 reportmre
- 4dc98282notes#15: a deleted note made in this tab leaves the list at once (the delete now removes this tab's edit under every key the row has: local id, server id); gate: browser.mjs notes#15 section, red on the old codemre
- e4e7402fnotes#11: plain declarations instead of let for variables never reassigned (components/notes.hl 3, lib/jsoncheck.hl 5, lib/util.hl 2, tools/migrate-short-ids.hl 8)mre
- 27b6a09enotes#8: Style blocks in the nested form (note-list, note-editor, .ask with their children nested, :empty as a nested pseudo-class); the same CSS rules, the pages look the samemre
- 71a9538bnotes: B27W report, log linemre