gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8f3d9bc98f3d9bc9|||PR ident#42 keyed upsert and removemre8f3d9bc9/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. 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. 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

  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre
  • 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
  • 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
  • dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
  • 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre