gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf2d9f79ff2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemref2d9f79f/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

  • f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
  • 8370b748ident#33: the app request box moves into home.hl's Stylemre
  • 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre
  • 40627a89ident#33: the time zone section's spacing moves into timezone.hlmre
  • d389db08ident#33: the identity page's rules and the Notif. chip move into identity.hl's Stylemre
  • ddda0b5bident#33: the identity row's parts move into identitylist.hl's Stylemre
  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
  • c512fb96Merge branch 'f4-homesplit' into mainIdent
  • 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
  • 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
  • e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre
  • e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
  • 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
  • 6fad3bd2Merge branch 'f3-codestep' into mainIdent
  • 51e387f2|||PR ident#40 the code step is one component (components/codestep.hl) placed by home.hl and embed.hl; its verify face, setCode, browserZone and verify call exist once (#40.2)mre
  • c28bddeeMerge branch 'f2-shell' into mainIdent
  • c3a40ff6|||PR ident#40 one shell for the app and /start (account part off on /start, #40.1); startshell.hl removed; unused signOut faces gone (#40.4)mre
  • 060f3870ident#40.4: use the shell sign-out faces in regression gatesmre