gitoria

notes

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd7366702d7366702Check session cookie flags after login and assert anonymous GET stays cookielessmred7366702/lib/ident-access.hl

5.1 KB

  1. // 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
  2. // The null fetch (ident unreachable, caught by project.hl's `on Error`) answers 503.
  3. // ident-access.hl — an app asks ident whether the request's access key is good (ident#27, the app's side).
  4. // Server side only, one file, no View. Copy this file into the app's components/ folder (README.md has the wiring).
  5. //
  6. // import { check, needWrite, refuse } from './ident-access.hl'
  7. // who = check(req) // req is a function route's request
  8. // if (!who.ok) { return refuse(who) } // who.ok false: a ready 401 / 403 / 503 answer
  9. // who.identity, who.name, who.access // 'read' or 'write'
  10. //
  11. // The key is read from `Authorization: Bearer idt_…` or from the PASSWORD of `Authorization: Basic …` (git sends it so; the user
  12. // name is ignored). Each call asks ident (POST <IDENT_URL>/api/introspect with the app's own key and secret) and nothing is
  13. // remembered: a key revoked in ident is refused at the very next call. When ident cannot be reached, or does not accept the
  14. // app's own key and secret, the answer is a refusal (503), never an allowance.
  15. //
  16. // 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
  17. // (a handler in this file does not catch it); the fetch then gives null and check answers 503. Without one the framework answers 500.
  18. //
  19. // Settings (environment): IDENT_API_KEY and IDENT_API_SECRET (the app's registration in ident), IDENT_EXCHANGE_URL / IDENT_URL.
  20. import { env } from 'hl:proc'
  21. import { fetch } from 'hl:fetch'
  22. import { fromBase64 } from 'hl:crypto'
  23. import { Response } from 'hl:http1'
  24. import { envOr } from './util.hl'
  25. static hexDigits = '0123456789abcdef'
  26. static asciiLead = '01234567'
  27. // does the text have the shape of an ident key: idt_ and 48 lower-case hex digits
  28. static shaped = (text) => {
  29. if (text == null || text.length != 52 || !text.startsWith('idt_')) { return false }
  30. rest = text.slice(4)
  31. for (c of rest.split('')) {
  32. if (!hexDigits.includes(c)) { return false }
  33. }
  34. return true
  35. }
  36. // the key out of the Authorization header, or null (no header, another scheme, or not decodable)
  37. static keyOf = (req) => {
  38. given = req.headers['authorization']
  39. if (given == null) { return null }
  40. head = given.trim()
  41. if (head.length > 8 && head.slice(0, 7).toLowerCase() == 'bearer ') { return head.slice(7).trim() }
  42. if (head.length > 7 && head.slice(0, 6).toLowerCase() == 'basic ') {
  43. raw = fromBase64(head.slice(6).trim())
  44. if (raw == null) { return null }
  45. hex = raw.hex()
  46. let at = -1
  47. for (let i = 0; i + 1 < hex.length && at < 0; i = i + 2) {
  48. if (hex.slice(i, i + 2) == '3a') { at = i / 2 }
  49. }
  50. if (at < 0) { return null }
  51. pass = raw.slice(at + 1)
  52. ph = pass.hex()
  53. // only plain ASCII goes on to toString (a byte that is not UTF-8 would be a located error, not a refusal)
  54. for (let i = 0; i < ph.length; i = i + 2) {
  55. if (!asciiLead.includes(ph.slice(i, i + 1))) { return null }
  56. }
  57. return pass.toString()
  58. }
  59. return null
  60. }
  61. // ask ident about the request: { ok = true identity name access } or { ok = false status error }
  62. static check = (req) => {
  63. token = keyOf(req)
  64. if (token == null || !shaped(token)) {
  65. return { ok = false status = 401 error = 'An ident access key is needed (Authorization: Bearer idt_…).' }
  66. }
  67. base = envOr('IDENT_EXCHANGE_URL', envOr('IDENT_URL', 'https://ident.worldapi.org'))
  68. appKey = env('IDENT_API_KEY')
  69. appSecret = env('IDENT_API_SECRET')
  70. if (appKey == null || appKey == '' || appSecret == null || appSecret == '') {
  71. return { ok = false status = 503 error = 'This app has no ident key and secret set, so it cannot check access keys.' }
  72. }
  73. res = fetch(base + '/api/introspect', { method = 'POST' json = { key = appKey secret = appSecret token = token } timeoutMs = 8000 })
  74. if (res == null || res.status != 200) {
  75. return { ok = false status = 503 error = 'ident could not check the key right now.' }
  76. }
  77. answer = res.json()
  78. if (answer == null || answer.active != true) {
  79. return { ok = false status = 401 error = 'This access key is not valid for this app.' }
  80. }
  81. if (answer.access != 'read' && answer.access != 'write') {
  82. return { ok = false status = 503 error = 'ident could not check the key right now.' }
  83. }
  84. return { ok = true identity = answer.identity name = answer.name access = answer.access }
  85. }
  86. // the same, for a request that changes something: a read-only key is refused with 403
  87. static needWrite = (req) => {
  88. who = check(req)
  89. if (who.ok && who.access != 'write') {
  90. return { ok = false status = 403 error = 'This access key is read-only.' }
  91. }
  92. return who
  93. }
  94. // the answer for a refused check (who.ok false); a 401 asks for credentials, so git and browsers prompt for them
  95. static refuse = (who) => {
  96. h = { 'Content-Type' = 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' }
  97. if (who.status == 401) { h['WWW-Authenticate'] = 'Basic realm="ident access key", charset="UTF-8"' }
  98. return new Response(JSON.stringify({ error = who.error }), { status = who.status headers = h })
  99. }

Branches

Latest commits

  • 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
  • 78ba0073notes: an ident access key (Authorization: Bearer idt_…) reads and writes its identity's notes through /api/me and /api/notes; gate tests/ident-access-notes.mjs (ident#27)mre
  • 3de46dacnotes: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 73+5 failing → 78 passedmre
  • 5909d458notes: MdEditor from components 59704ce (text on the clipboard always pastes, a file manager copy still attaches; pipe rows without a delimiter row; tickets#49, notes#5)mre
  • 98bd6a98N5b: pipe rows without a delimiter row are a table (MdEditor ad4e929, plain copy); gate tests/dashboard-table.mjs; reportmre
  • dbf93319N5 report: components gate rerun 613 passed, 0 failedmre
  • 3a8bc0d2N5: empty Table-button table is written as a real pipe table (MdEditor 4bd1112, plain copy); gate tests/table-button.mjs; reportmre
  • c16bd443N5: step 1 report (tables pass locally; his DASHBOARD note has no delimiter row)mre
  • 6091320anotes: MdEditor 7437e32 (surface drawn at once when the page is claimed); gate measures claim-to-text on 5 note loads at 1280 and 390mre
  • 0aa9277enotes: move each component's styles into its own Style block (shell and header selector in main.hl, list, editor frame and delete question in notes.hl); styles.hl keeps the theme inherit, the accent and the message colour; .gitignore ignores a bin symlink too; tests/shots.mjs for the pixel comparemre
  • 9ba01de3notes: write the standby service out in full so the boot gate can read the main servicemre
  • 98cab40anotes: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
  • 1fadb473notes gate: the editor table now has 1 px cell lines from the notes thememre