gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit2ad9d29c2ad9d29ctracker#1: login exactly like calendar (identity selector in the header, empty homepage)mre2ad9d29c/project.hl

5.4 KB

  1. // project.hl — tracker.worldapi.org: STEP 1 (tracker.worldapi.org#1), an empty shell. Login copied unchanged from
  2. // calendar.worldapi.org (rejected once for a centered sign-in and no header selector — architect, 2026-09-27):
  3. // ident only, no own passwords (README "How apps use ident" of ident.worldapi.org), the identity selector in the
  4. // header (components/main.hl). No shows, no data yet — later steps come from the creator (CONCEPT.md).
  5. //
  6. // the header's ident-selector / "Log in with ident" -> <ident>/login?key=&return=<this app>/login/callback
  7. // /login/callback?ident_code=<code> the app's server exchanges the code for the identity's short id
  8. // (POST <ident>/api/exchange) and signs this app's OWN session in
  9. // (session.user = { id = <users @id> }, users.hl)
  10. // the header's "Log out" button signs this app's session out via the face trackerLogOut (components/main.hl)
  11. // — ident's own session, and the browser's ident cookie, are untouched
  12. //
  13. // Config (env; no committed secret — the first deploy registers this app in ident and sets these, done by the
  14. // architect, as with ident's own .env for SMTP):
  15. // TRACKER_PORT (45008), TRACKER_URL (this app's own public address, for the return URL),
  16. // IDENT_URL (https://ident.worldapi.org), TRACKER_KEY (pk_…), TRACKER_SECRET (sk_…)
  17. import WebFramework from 'hl:web'
  18. import { Response } from 'hl:http1'
  19. import { randomBytes } from 'hl:crypto'
  20. import { env } from 'hl:proc'
  21. import Styles from './styles.hl'
  22. import { exchangeCode, ensureUser } from './users.hl'
  23. import Home from './components/home.hl'
  24. import LoginFailed from './components/loginfailed.hl'
  25. static appTitle = "tracker"
  26. styles = Styles
  27. port = env('TRACKER_PORT') != null ? toNumber(env('TRACKER_PORT')) : 45008
  28. watching = env('TRACKER_WATCH') != '0'
  29. // read by hl:web's own manifest config (WebFramework.hl `cfg.sessionDir`), not the constructor call below
  30. sessionDir = env('TRACKER_SESSIONS') != null ? env('TRACKER_SESSIONS') : null
  31. // ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"), copied from calendar.worldapi.org's project.hl
  32. // BACK TO THE PAGE: /login.js puts `?next=` into the button's return URL at the click. Only a same-origin PATH
  33. // goes (one `/`, URL-safe characters, ≤ 500). Anything else → `/`.
  34. nextChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;@!$()*:'
  35. safePath = (want) => {
  36. if (want == null || hlTypeName(want) != 'String' || want == '' || want.length > 500) { return '/' }
  37. if (want.slice(0, 1) != '/' || want.slice(0, 2) == '//' || want.slice(0, 7) == '/login/') { return '/' }
  38. let i = 0
  39. while (i < want.length) {
  40. if (!nextChars.includes(want[i])) { return '/' }
  41. i = i + 1
  42. }
  43. return want
  44. }
  45. // A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed
  46. failed = (req, why) => {
  47. let s = req.session
  48. let fresh = s == null
  49. if (fresh) { s = server.sessions.mint() }
  50. s.data.loginError = why
  51. server.sessions.save(s)
  52. let res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })
  53. if (fresh) { res.headers['Set-Cookie'] = server.sessions.cookieHeader(s.id) }
  54. return res
  55. }
  56. // the function route gets the cookie's session as req.session (hybriel #11); none yet → minted here
  57. loginCallback = (route, req) => {
  58. if (req.method != 'GET') { return failed(req, 'GET only') }
  59. let q = req.query != null ? req.query : {}
  60. let code = q.ident_code
  61. if (code == null || code == '') { return failed(req, 'ident sent no login code') }
  62. let x = exchangeCode(code)
  63. if (x.error != null) { return failed(req, x.error) }
  64. let u = ensureUser(x.identity)
  65. if (u == null) { return failed(req, 'could not store the user') }
  66. let s = req.session
  67. let fresh = s == null
  68. if (fresh) { s = server.sessions.mint() }
  69. s.user = { id = u.id }
  70. s.data.tag = randomBytes(16)
  71. s.data.loginError = null
  72. server.sessions.save(s)
  73. let res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })
  74. if (fresh) { res.headers['Set-Cookie'] = server.sessions.cookieHeader(s.id) }
  75. return res
  76. }
  77. routes = [
  78. { pattern = "/favicon.ico" direct = "" }
  79. { pattern = "/login/callback" function = loginCallback }
  80. { pattern = "/login/failed" component = LoginFailed }
  81. { pattern = "/login.js" file = "./login.js" headers = { 'Cache-Control' = 'no-cache' } }
  82. { pattern = "/" component = Home }
  83. ]
  84. // WHO GETS THE PUSH: the login state reaches the tabs of one session (components/main.hl trackerSignedIn/Out).
  85. tagOf = (session) => { return session != null && session.data != null ? session.data.tag : null }
  86. audience = {
  87. trackerSignedIn = (tag, session) => { return tag != null && tagOf(session) == tag }
  88. trackerSignedOut = (tag, session) => { return tag != null && tagOf(session) == tag }
  89. }
  90. // cookies are per host, not per port: an own name keeps this app's session apart from
  91. // ident's own (and from any other worldapi app sharing a dev host), see ident README "Design
  92. // tokens" / hybriel#10 hybriel#17.
  93. sessionCookie = 'trackersid'
  94. server = new WebFramework(routes = routes, styles = styles, port = port, sessionCookie = sessionCookie, watchMode = watching)
  95. on Error(e) { console.log('error absorbed: ' + e.message) }

Branches

Latest commits

  • 2ad9d29ctracker#1: login exactly like calendar (identity selector in the header, empty homepage)mre
  • 3691e176tracker#1: empty tracker with the ident login (state of 2026-09-27)mre