gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit97f77b0797f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre97f77b07/lib/login-helpers.hl

2.7 KB

  1. // lib/login-helpers.hl — what lib/login.hl looks up: making a six-digit code and its hash, and the client's IP
  2. // BUCKET of the per-IP limit. No table access. Statics only.
  3. import { randomBytes, sha256 } from 'hl:crypto'
  4. // ---- codes --------------------------------------------------------------------------
  5. // six digits from the kernel CSPRNG (randomBytes answers hex)
  6. static newOtp = () => {
  7. hex = randomBytes(8)
  8. let n = 0
  9. let i = 0
  10. while (i < 12) {
  11. n = (n * 16 + '0123456789abcdef'.indexOf(hex[i])) % 1000000
  12. i = i + 1
  13. }
  14. let s = '' + n
  15. while (s.length < 6) { s = '0' + s }
  16. return s
  17. }
  18. static otpHash = (email, code) => { return sha256(email + ':' + code) }
  19. // ---- the client's IP bucket (mission 006 (old 010)) -------------------------------------------
  20. // `ip` is the X-Client-IP header nginx sets on Byrodin (CF-Connecting-IP when the request
  21. // really came through Cloudflare, else the TCP peer; nginx OVERWRITES what a client sent),
  22. // or null when there is none (dev without nginx: hl:web's req.remoteAddress would be nginx's; hl:http1 did not expose the
  23. // connection's peer address). The bucket:
  24. // no header / empty → 'direct' ONE SHARED BUCKET for every header-less request
  25. // IPv4 (also ::ffff:a.b.c.d) → the address
  26. // IPv6 → its /64 prefix ('2a01:4f9:3080:1126::/64'): one client
  27. // usually owns a whole /64, so per-address would not limit it
  28. // anything else → the text itself (lowercased, cut to 64 chars)
  29. static hexDigits = '0123456789abcdef'
  30. static stripZeros = (g) => {
  31. let t = g
  32. while (t.length > 1 && t[0] == '0') { t = t.slice(1, t.length) }
  33. return t
  34. }
  35. static ipBucket = (ip) => {
  36. if (ip == null || hlTypeName(ip) != 'String') { return 'direct' }
  37. let t = ip.trim().toLowerCase()
  38. if (t == '') { return 'direct' }
  39. if (t.length > 64) { t = t.slice(0, 64) }
  40. if (!t.includes(':')) { return t }
  41. // an IPv4-mapped IPv6 address counts as its IPv4 address
  42. if (t.includes('.')) { return t.slice(t.lastIndexOf(':') + 1, t.length) }
  43. let head = t
  44. let tail = ''
  45. gap = t.indexOf('::')
  46. if (gap >= 0) {
  47. head = t.slice(0, gap)
  48. tail = t.slice(gap + 2, t.length)
  49. }
  50. groups = head == '' ? [] : head.split(':')
  51. rest = tail == '' ? [] : tail.split(':')
  52. if (gap >= 0) {
  53. let fill = 8 - groups.length - rest.length
  54. while (fill > 0) {
  55. groups.push('0')
  56. fill = fill - 1
  57. }
  58. }
  59. for (g of rest) { groups.push(g) }
  60. if (groups.length != 8) { return t }
  61. out = []
  62. let i = 0
  63. while (i < 4) {
  64. let g = groups[i]
  65. if (g == '' || g.length > 4) { return t }
  66. let j = 0
  67. while (j < g.length) {
  68. if (!hexDigits.includes(g[j])) { return t }
  69. j = j + 1
  70. }
  71. out.push(stripZeros(g))
  72. i = i + 1
  73. }
  74. return out.join(':') + '::/64'
  75. }

Branches

Latest commits

  • 97f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre
  • 8e4f435bpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • fa288f63ident mission 011: report wording (Intl zone names)mre
  • 961fae01ident mission 011: report, LOG, STATUSmre
  • 65348039ident#7: delivery gate — console check split at the gate's own server restart (49 checks)mre
  • dfa7b783ident#7: README (Delivery, env, gate, files, data), deploy.sh runs tests/delivery.mjsmre
  • ab6331f4ident#7: tests/delivery.mjs — fake clock gate: Vienna/New York/Tokyo/UTC 17:00, urgent, mail off, restart, DST days, zone changemre
  • 7869f57bident#7: delivery — the daily mail at 17:00 in the user's zone (digests table, once per local day, test clock IDENT_TEST_CLOCK), urgent mailed at once, push hookmre
  • 371ef1b5ident#7: lib/mail.hl sendNotificationMail (sink: <sink>.mail, one JSON line per mail)mre
  • 39749e0bident#7: time zones through hl:time — server checks isZone, an account without a known zone reads as UTC, allAccountsmre
  • 7a54fe72mission brief 011mre
  • a6a874cdident mission 010: vendor hybriel 0f396652 (plugins + compose cache volume/tmpfs + deploy URL wait + report)mre
  • 4e7cf78emission brief 010mre
  • e8b2b0f7docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 5fdbb6b2ident mission 009: report — scratch folder notemre
  • 74235815ident mission 009 (4/4): docs (README files map + same-output test, STATUS, LOG), report, tests/letcount.py + realdata-baseline/comparemre
  • fe183516ident mission 009 (3/4): let only where reassigned — 293 never-reassigned lets are plain declarations; kept: reassigned, loop bodies, names of a file member, a name declared twice in one function; same outputmre
  • d2e7f91bident mission 009 (2/4): one lib file per topic (login, accounts, identities, apps, invites, selector, notify + helpers, util), function routes as thin wrappers in lib/api.hl, project.hl = the map; same outputmre
  • 91017164ident mission 009 (1/4): file moves — the root .hl files into lib/ (api.hl → lib/api-helpers.hl), styles.hl → components/styles.hl; imports adjusted, no other changemre
  • f8bdcbc2ident: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); all gates greenmre