gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit644f9775644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre644f9775/lib/identities-helpers.hl

5.9 KB

  1. // lib/identities-helpers.hl — what lib/identities.hl looks up: the strict identity fields, the avatar check, the
  2. // short id's alphabet, the label a list shows. No table access. Statics only.
  3. import { randomBytes } from 'hl:crypto'
  4. import { str, hasControl } from './util.hl'
  5. static maxField = 60 // chars per identity field
  6. static fieldNames = ['identityName' 'nickname' 'firstname' 'lastname']
  7. // THE PROFILE FIELDS an app may ask for (ident#11): a subset of fieldNames. The identity name is the person's own label inside ident and stays private.
  8. static profileFieldNames = ['nickname' 'firstname' 'lastname']
  9. // THE AVATAR (ticket #15): an uploaded picture, stored as a small data URL (the page scales it to
  10. // 128×128) — not a text field, so it is checked and stored separately from fieldNames/maxField
  11. static maxAvatarUrl = 60000
  12. static avatarChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/='
  13. // AN AVATAR: empty (none) or the uploaded picture as a data URL — data:image/(png|jpeg|webp);base64,
  14. // with the file's own magic bytes at the start of the base64 text (the page scales the upload to
  15. // 128×128 first, avatar.js), ≤ maxAvatarUrl characters, base64 characters only
  16. static checkAvatarUrl = (v) => {
  17. if (v == null || hlTypeName(v) != 'String') { return { error = 'field avatar must be a string' } }
  18. t = v.trim()
  19. if (t == '') { return { avatar = '' } }
  20. if (t.length > maxAvatarUrl) { return { error = 'field avatar is longer than ' + maxAvatarUrl + ' characters' } }
  21. let kind = ''
  22. let start = 0
  23. if (t.startsWith('data:image/png;base64,')) { kind = 'iVBORw0KGgo' start = 22 }
  24. else if (t.startsWith('data:image/jpeg;base64,')) { kind = '/9j/' start = 23 }
  25. else if (t.startsWith('data:image/webp;base64,')) { kind = 'UklGR' start = 23 }
  26. if (kind == '') { return { error = 'field avatar must be an uploaded PNG, JPEG or WebP picture' } }
  27. data = t.slice(start, t.length)
  28. if (!data.startsWith(kind)) { return { error = 'field avatar is not a valid picture' } }
  29. let i = 0
  30. while (i < data.length) {
  31. if (!avatarChars.includes(data[i])) { return { error = 'field avatar is not valid base64' } }
  32. i = i + 1
  33. }
  34. return { avatar = t }
  35. }
  36. // the avatar to show: only an uploaded picture (a data URL). Before the upload existed an avatar
  37. // was an http(s) URL; those are no longer shown (never fetched from a third-party address).
  38. static shownAvatar = (r) => { return str(r.avatar).startsWith('data:image/') ? str(r.avatar) : '' }
  39. // THE IDENTITY FIELDS, STRICT: `fields` is an object whose keys are among fieldNames plus
  40. // `avatar`. A name field is a string (trimmed, at most 60 chars, no control characters);
  41. // `avatar` is checked by checkAvatarUrl. Absent keys are absent from the answer.
  42. // Answers { error, field } or { fields }.
  43. static checkFields = (fields) => {
  44. if (fields == null || hlTypeName(fields) != 'Hybrid' || fields.length != null) { return { error = 'the identity fields must be an object' field = '' } }
  45. out = {}
  46. for (k of fields.keys()) {
  47. if (k == 'avatar') {
  48. let c = checkAvatarUrl(fields[k])
  49. if (c.error != null) { return { error = c.error field = 'avatar' } }
  50. out.avatar = c.avatar
  51. } else if (!fieldNames.includes(k)) { return { error = 'unknown field: ' + k field = k } }
  52. else {
  53. let v = fields[k]
  54. if (v == null || hlTypeName(v) != 'String') { return { error = 'field ' + k + ' must be a string' field = k } }
  55. let t = v.trim()
  56. if (t.length > maxField) { return { error = 'field ' + k + ' is longer than ' + maxField + ' characters' field = k } }
  57. if (hasControl(t)) { return { error = 'field ' + k + ' contains a control character' field = k } }
  58. out[k] = t
  59. }
  60. }
  61. return { fields = out }
  62. }
  63. // WHAT A LIST SHOWS: the identity name; without one, nickname, then first + last name,
  64. // then "Identity <n>" (n = its place in the list)
  65. static labelOf = (r, n) => {
  66. if (str(r.identityName) != '') { return r.identityName }
  67. if (str(r.nickname) != '') { return r.nickname }
  68. full = (str(r.firstname) + ' ' + str(r.lastname)).trim()
  69. if (full != '') { return full }
  70. return 'Identity ' + n
  71. }
  72. // ---- THE SHORT ID's shape (ident#23; lib/identities.hl makes and finds them) -------------------
  73. // 5 characters from an alphabet without look-alikes — digits 2-9 and the letters minus i, l, o
  74. // (no 0/O, 1/l/I): 31 characters, 28.6 million ids
  75. static shortAlphabet = '23456789abcdefghjkmnpqrstuvwxyz'
  76. static shortLength = 5
  77. static newShortId = () => {
  78. let out = ''
  79. while (out.length < shortLength) {
  80. let hex = randomBytes(16)
  81. let i = 0
  82. while (i < 32 && out.length < shortLength) {
  83. let b = '0123456789abcdef'.indexOf(hex[i]) * 16 + '0123456789abcdef'.indexOf(hex[i + 1])
  84. // 248 = 8 * 31: bytes above it are dropped so every character is equally likely
  85. if (b < 248) { out = out + shortAlphabet[b % 31] }
  86. i = i + 2
  87. }
  88. }
  89. return out
  90. }
  91. // what a person typed → the lowercase form, or null when it cannot be a short id
  92. static normShortId = (v) => {
  93. if (v == null || hlTypeName(v) != 'String') { return null }
  94. t = v.trim().toLowerCase()
  95. if (t.length != shortLength) { return null }
  96. let i = 0
  97. while (i < t.length) {
  98. if (shortAlphabet.indexOf(t[i]) < 0) { return null }
  99. i = i + 1
  100. }
  101. return t
  102. }
  103. // THE FIELDS AN APP ASKED FOR (ident#11): answers { error } (naming the field) or { fields } (the names, each once, in the order asked)
  104. static checkProfileFields = (list) => {
  105. let out = []
  106. for (f of list) {
  107. if (hlTypeName(f) != 'String') { return { error = 'fields must be a list of field names' } }
  108. if (!profileFieldNames.includes(f)) { return { error = 'unknown profile field: ' + f + ' (you can ask for ' + profileFieldNames.join(', ') + ')' } }
  109. if (!out.includes(f)) { out.push(f) }
  110. }
  111. return { fields = out }
  112. }
  113. // THE VALUES: one key per asked field, the text the person entered, null when empty or never filled in
  114. static profileValues = (rec, fields) => {
  115. let out = {}
  116. for (f of fields) {
  117. let v = str(rec[f]).trim()
  118. out[f] = v == '' ? null : v
  119. }
  120. return out
  121. }

Branches

Latest commits

  • 644f9775|||PR ident#36 navigate() instead of window.location in the shell, start shell and homemre
  • 4ca2f921deploy.sh: tar --hard-dereference so backups hold no hard linksmre
  • 23971fb7ident#49: media queries in the language's @media formmre
  • 73f4f97dident: re-vendor to hybriel day38b (mpackdb store-open fix)mre
  • aa056c03ident: a startup error prints its message before exit 1 (hybriel#247)mre
  • ee37a861ident#G48: gitoria's registration gets the wildcard origin of its repo addresses at load (gate tests/gitoria-origin.mjs)mre
  • dc6d486fident#27: the docs gate expects the new Access entry in the main menumre
  • d277633cident#27: README: identity access and the introspect endpointmre
  • e999b0d3ident#27: the Access tab — one key per identity for all its apps: give access in three steps, the overview matrix, a cell's sheet (since, last use, level, rotate, revoke), the app's check POST /api/introspect, agent identities hidden from the login picker, an inbox icon in the header's icon style (gate: tests/access.mjs)mre
  • 436206fcident#47: deploy.sh runs the sign-in gate on the deploy's own port slot, like the sessionwrites and docs gates (the default 8700 can be taken by another process)mre
  • a4887ea3ident#47: the selector's sign-in frame no longer has a Continue button; the identity row itself is the link (gate tests/signin.mjs fails on the old code)mre
  • d8ec703dident#45: /docs shows docs/IDENT.md (hl:markdown, vendored from ac1ed9da), one Docs entry in the main menu, README for developers only, gates tests/docs.mjs and tests/sessionwrites.mjs in deploy.shmre
  • ec6aad35Merge branch 'a28g-amp-callsites' into d45-docsmre
  • 6bfbd3f2ident#45: docs/IDENT.md, what ident is and how it works; CONCEPT.md marks one id per app as droppedmre
  • c0edca26ident#28 A28G: write & at every call that hands a session to an &-parameter; gate tests/sessionwrites.mjs pins that sign-in, pending address, embed sign-in and selector land in the session filesmre
  • 80965c08ident#28: README says the deploy goes through the standbymre
  • 2f41670cident#28: ident-standby service on port 45012 (no background round in the standby) and deploy.sh hands over through standby-swap.shmre
  • a544b261ident#28: README names the new vendored Hybrielmre
  • 716860b0ident#28: gates follow Hybriel ac1ed9da (lazy sessions, meta attribute order, JSON key order, default X-Frame-Options switched off for the embed page)mre
  • 889e00fbident#28: Hybriel master ac1ed9da (sessions reload a newer file, a replayed reconnect carries the server session); a startup fault exits 1 (on Error)mre