gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchb27b3d43c187ident-access: server-side check of an ident access key (Bearer or Basic password), asks ident on every call, README, gate tests/ident-access.mjs (ident#27 app side)mreb27b/components/ident-access/ident-access.hl

4.5 KB

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

Branches

Latest commits

  • 3d43c187ident-access: server-side check of an ident access key (Bearer or Basic password), asks ident on every call, README, gate tests/ident-access.mjs (ident#27 app side)mre
  • 59704ce1Merge branch 'n5b-pipes'mre
  • 23b9164aMdEditor: a clipboard whose text is only the names or file:// paths of its files (a file manager copy) is about the files, as before; any other text wins (tickets#49); gate casesmre
  • b0f1ec28MdEditor: a clipboard with text always pastes the text, also when a picture is on it too (a spreadsheet or office app), files on or off; a picture alone still goes to the files; real Ctrl+V gate tests/md-paste-files.mjs (tickets#49)mre
  • ad4e929bMdEditor: pipe rows without a delimiter row are a table (first row = header, short rows padded, written back without an invented | --- | line) (notes#5)mre
  • 4bd11128MdEditor: the Table button's empty table is written as a real pipe table (empty cells), so it survives the source switch, a save and a reload (notes#7)mre
  • 7437e32bMdEditor draws its surface at once when claimed from server HTML: 1 ms mount animation, the first editor mounts all editors of the pagemre
  • 5de0264cRe-vendor onto Hybriel master acaceaf3 (set-day27b): binary, lib and plugins; exit 1 on a startup fault; .gitignore binmre
  • 79c77e7dMerge sb3-standby: components deploys through its standbymre
  • c5442043components: no jobs switch needed in the standbymre
  • 8d2fb24ccomponents: a standby container (port 45016, profile standby); deploy hands over through standby-swap.sh without a gapmre
  • c2834dd3Merge mr-fixes: media player state after a queue pick, Range handling, cover-less audio, accessibility labelsmre
  • 354fe3c6Media player: audio without a cover starts and shows its time (the mount tick sits on the seek bar, which every item has); the speed button names the current speed, the seek slider says its time in words; a gate page for the first render on cover-less audio and a fourth demo queue item without a covermre
  • ee4aee1eMedia player: a new queue item shows its own speed, mute, play state and time and starts when asked, also for two videos in a row; the media route reads suffix ranges and ignores range lists and garbage instead of answering 500; stronger gate checks (the 10 percent drag, the played bar, the queue tap)mre
  • 9a4f9b31Merge mp-player: media served from the committed filesmre
  • ab96e686Media route: serve the demo's media from the committed files in components/media-player (storage/ is not in git, the deploy gate found no files)mre
  • b2980734Merge mp-player: the media player (components/media-player), its demo and the media iconsmre
  • 1c41293eMediaPlayer: back / forward 10 s as replay / forward 10 icons (circular arrow with a 10), the play button keeps its surface background on audio and video, the media route's range end is a letmre
  • 9e1ab228MP: report, LOG line, STATUS entrymre
  • dd003e5dMediaPlayer: audio and video in one phone-first player (components/media-player), its demo /media-player with a queue, the media icons in components/icons, the demo's media files served with ranges (lib/media.hl), 35 gate checksmre