ident
All repositories: gitoria
5.9 KB
// lib/identities-helpers.hl — what lib/identities.hl looks up: the strict identity fields, the avatar check, the// short id's alphabet, the label a list shows. No table access. Statics only.import { randomBytes } from 'hl:crypto'import { str, hasControl } from './util.hl'static maxField = 60 // chars per identity fieldstatic fieldNames = ['identityName' 'nickname' 'firstname' 'lastname']// 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.static profileFieldNames = ['nickname' 'firstname' 'lastname']// THE AVATAR (ticket #15): an uploaded picture, stored as a small data URL (the page scales it to// 128×128) — not a text field, so it is checked and stored separately from fieldNames/maxFieldstatic maxAvatarUrl = 60000static avatarChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/='// AN AVATAR: empty (none) or the uploaded picture as a data URL — data:image/(png|jpeg|webp);base64,// with the file's own magic bytes at the start of the base64 text (the page scales the upload to// 128×128 first, avatar.js), ≤ maxAvatarUrl characters, base64 characters onlystatic checkAvatarUrl = (v) => {if (v == null || hlTypeName(v) != 'String') { return { error = 'field avatar must be a string' } }t = v.trim()if (t == '') { return { avatar = '' } }if (t.length > maxAvatarUrl) { return { error = 'field avatar is longer than ' + maxAvatarUrl + ' characters' } }let kind = ''let start = 0if (t.startsWith('data:image/png;base64,')) { kind = 'iVBORw0KGgo' start = 22 }else if (t.startsWith('data:image/jpeg;base64,')) { kind = '/9j/' start = 23 }else if (t.startsWith('data:image/webp;base64,')) { kind = 'UklGR' start = 23 }if (kind == '') { return { error = 'field avatar must be an uploaded PNG, JPEG or WebP picture' } }data = t.slice(start, t.length)if (!data.startsWith(kind)) { return { error = 'field avatar is not a valid picture' } }let i = 0while (i < data.length) {if (!avatarChars.includes(data[i])) { return { error = 'field avatar is not valid base64' } }i = i + 1}return { avatar = t }}// the avatar to show: only an uploaded picture (a data URL). Before the upload existed an avatar// was an http(s) URL; those are no longer shown (never fetched from a third-party address).static shownAvatar = (r) => { return str(r.avatar).startsWith('data:image/') ? str(r.avatar) : '' }// THE IDENTITY FIELDS, STRICT: `fields` is an object whose keys are among fieldNames plus// `avatar`. A name field is a string (trimmed, at most 60 chars, no control characters);// `avatar` is checked by checkAvatarUrl. Absent keys are absent from the answer.// Answers { error, field } or { fields }.static checkFields = (fields) => {if (fields == null || hlTypeName(fields) != 'Hybrid' || fields.length != null) { return { error = 'the identity fields must be an object' field = '' } }out = {}for (k of fields.keys()) {if (k == 'avatar') {let c = checkAvatarUrl(fields[k])if (c.error != null) { return { error = c.error field = 'avatar' } }out.avatar = c.avatar} else if (!fieldNames.includes(k)) { return { error = 'unknown field: ' + k field = k } }else {let v = fields[k]if (v == null || hlTypeName(v) != 'String') { return { error = 'field ' + k + ' must be a string' field = k } }let t = v.trim()if (t.length > maxField) { return { error = 'field ' + k + ' is longer than ' + maxField + ' characters' field = k } }if (hasControl(t)) { return { error = 'field ' + k + ' contains a control character' field = k } }out[k] = t}}return { fields = out }}// WHAT A LIST SHOWS: the identity name; without one, nickname, then first + last name,// then "Identity <n>" (n = its place in the list)static labelOf = (r, n) => {if (str(r.identityName) != '') { return r.identityName }if (str(r.nickname) != '') { return r.nickname }full = (str(r.firstname) + ' ' + str(r.lastname)).trim()if (full != '') { return full }return 'Identity ' + n}// ---- THE SHORT ID's shape (ident#23; lib/identities.hl makes and finds them) -------------------// 5 characters from an alphabet without look-alikes — digits 2-9 and the letters minus i, l, o// (no 0/O, 1/l/I): 31 characters, 28.6 million idsstatic shortAlphabet = '23456789abcdefghjkmnpqrstuvwxyz'static shortLength = 5static newShortId = () => {let out = ''while (out.length < shortLength) {let hex = randomBytes(16)let i = 0while (i < 32 && out.length < shortLength) {let b = '0123456789abcdef'.indexOf(hex[i]) * 16 + '0123456789abcdef'.indexOf(hex[i + 1])// 248 = 8 * 31: bytes above it are dropped so every character is equally likelyif (b < 248) { out = out + shortAlphabet[b % 31] }i = i + 2}}return out}// what a person typed → the lowercase form, or null when it cannot be a short idstatic normShortId = (v) => {if (v == null || hlTypeName(v) != 'String') { return null }t = v.trim().toLowerCase()if (t.length != shortLength) { return null }let i = 0while (i < t.length) {if (shortAlphabet.indexOf(t[i]) < 0) { return null }i = i + 1}return t}// THE FIELDS AN APP ASKED FOR (ident#11): answers { error } (naming the field) or { fields } (the names, each once, in the order asked)static checkProfileFields = (list) => {out = []for (f of list) {if (hlTypeName(f) != 'String') { return { error = 'fields must be a list of field names' } }if (!profileFieldNames.includes(f)) { return { error = 'unknown profile field: ' + f + ' (you can ask for ' + profileFieldNames.join(', ') + ')' } }if (!out.includes(f)) { out.push(f) }}return { fields = out }}// THE VALUES: one key per asked field, the text the person entered, null when empty or never filled instatic profileValues = (rec, fields) => {out = {}for (f of fields) {let v = str(rec[f]).trim()out[f] = v == '' ? null : v}return out}
Branches
Latest commits
- 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
- 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
- ddc7d135Merge branch 'f7-hooks' into mainIdent
- eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre
- ccd6d0c4ident#34: browser gate: rowButton finds the row's button by namemre
- 24dd414eident#34: the shared lead, message and notice looks are statics in components/styles.hl, imported by the components (lib/looks.hl gone)mre
- 03ed26c7ident#34: access.hl: the remaining lead classes droppedmre
- 1d066c42ident#34: access.hl drops its unstyled lead class (the shared lead rule would otherwise mute it)mre
- 1e1dd7b4ident#34: the access page keeps its lead class string (the page has no ident-card, so its lead line was never muted; a shared rule would change the look)mre
- 89ac6553ident#34: the lead, message and notice lines are named with Style.pLead / pMessage / pNotice (shared bodies in lib/looks.hl) instead of class stringsmre
- e84be3deident#34: browser gate lists the Edit/Delete buttons by namemre
- fb7111faident#34: access page: unstyled test-hook classes (idrow, never, who, whoname, appname, neverrevoke) dropped; the tests use element/name selectorsmre
- 9f2ad803ident#34: notification switches: push/email by name=..., locked by aria-disabled, the switch look is the role=switch button (no switch/push/email/locked classes)mre
- e0f0ddaaident#34: inbox: the Mark read/unread button is styled as the button of its note-foot, the icon is found as the row's img (no toggle/icon classes)mre
- 16b49756ident#34: unused row classes (choice, usedrow, kind, shortid) droppedmre
- f9d6fb38ident#34: apps and identity rows: the Edit/New secret/Delete buttons carry name=..., the API key is found by its element; unused row classes (app, identity, shortid) droppedmre
- b323265aMerge branch 'f5-styles' into mainIdent
- 9ced53c3|||PR ident#33 ident#35 every component's styles live in its own Style block; styles.hl keeps the page-wide rest; the account bar is phone first (min-width: 60rem block); the avatar preview is hidden with the hidden attribute (ident#34 in part)mre