gitoria

tickets

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0814ce6b0814ce6bTickets conductor: brief 025 tickets#36 style formmre0814ce6b/lib/ident-access.hl

5.2 KB

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

Branches

Latest commits

  • 0814ce6bTickets conductor: brief 025 tickets#36 style formmre
  • 1b6916c4Tickets conductor: Atlas handover tickets#36mre
  • aab95226Tickets conductor: worldapi-conductor paths, deploy blocker = read-only diskmre
  • 6372b06dTickets conductor: worker port slotmre
  • 836af31cTickets conductor: token pathsmre
  • b03f11b6Tickets conductor: CONDUCTOR.md (project part), short STATUS.md, old STATUS to archive/2026-10-31mre
  • a4123831tickets#42: the /connect and /login/callback error pages are app pages: the function routes redirect to /connect/failed/<code> and /login/failed/<code> (components/problem.hl, sentences in lib/problems.hl), htmlPage and its hand-written stylesheet are gone; gate tests/error-pages.mjsmre
  • cefc7b4atickets: a startup error prints its message before exit 1 (hybriel#247)mre
  • 99bd6570tickets: lib/migrate.hl writes no table of another topic any more: projects.hl rewriteSlug / addProjectNamed / hasMembers, tickets.hl moveToProject / ticketRecords / ticketRecordsOf, events.hl renameStates / eventRecords / eventRecordsOf; the migration calls them (tickets#47)mre
  • b57161e3tickets: the markup names its own rules (Style.pMessage, Style.olTickets, ...) instead of hand-written class strings that copy a stylesheet rule; the shared bodies sit in lib/looks.hl (tickets#38)mre
  • f767b24dtickets: 111 needless let became plain declarations (3 in ticket.hl, ticket_list.hl, comments.hl and 108 never-reassigned, not captured loop-body ones); the 212 really reassigned lets stay (tickets#44)mre
  • 16c0d3b5tickets: the modal window is phone first: the full-screen sheet is the base rule, the centred window sits in the min-width 60rem block (tickets#39)mre
  • 090ee809tickets: every component's styles in its own nested Style {} block; styles.hl keeps only the theme inherit, the accent and the site-wide rules (tickets#36)mre
  • d395763ftickets: ident-access gate ports 8769/8768 (8797 is taken on loreana)mre
  • afb9a403tickets: an ident access key (Authorization: Bearer idt_…) works in the API (ident#27 app side): lib/ident-access.hl copied from components, read keys GET only, ident down 503; gate tests/ident-access-tickets.mjsmre
  • 48c1ba79tickets: md-paste-files gate with the file manager casesmre
  • f04afc43tickets: MdEditor from components 59704ce (a file manager copy still attaches, pipe rows, tickets#49)mre
  • 06599fa0tickets: MdEditor from components b0f1ec2 (a clipboard with text always pastes the text, tickets#49); real Ctrl+V gatemre
  • 2e1b116ftickets: re-vendor Hybriel master acaceaf3 (framework ec8d6c1d): plugins/web patch queue while a page is built, anonymous session cookies refresh; gate waits for the comment editors to draw (W174) and clicks the form editor when it stands still; security gate: main's code runs on its own binary, lazy-session cookie checksmre
  • 361bc0fetickets: re-vendor Hybriel 7a684920 (WIP, not deployed); on Error exits 1 at startup; migrate passes its log by reference; gate checks follow lazy sessions and the 413 refusalmre