gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit44b7d9f944b7d9f9tracker#6: /schedule — upcoming episodes of followed shows, soonest firstmre44b7d9f9/project.hl

7.5 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 { readBytes, exists } from 'hl:fs'
  22. import Styles from './styles.hl'
  23. import { exchangeCode, ensureUser } from './users.hl'
  24. import Home from './components/home.hl'
  25. import LoginFailed from './components/loginfailed.hl'
  26. import Show from './components/show.hl'
  27. import Unwatched from './components/unwatched.hl'
  28. import Schedule from './components/schedule.hl'
  29. static appTitle = "tracker"
  30. styles = Styles
  31. port = env('TRACKER_PORT') != null ? toNumber(env('TRACKER_PORT')) : 45008
  32. watching = env('TRACKER_WATCH') != '0'
  33. // read by hl:web's own manifest config (WebFramework.hl `cfg.sessionDir`), not the constructor call below
  34. sessionDir = env('TRACKER_SESSIONS') != null ? env('TRACKER_SESSIONS') : null
  35. // ---- THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"), copied from calendar.worldapi.org's project.hl
  36. // BACK TO THE PAGE: /login.js puts `?next=` into the button's return URL at the click. Only a same-origin PATH
  37. // goes (one `/`, URL-safe characters, ≤ 500). Anything else → `/`.
  38. nextChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;@!$()*:'
  39. safePath = (want) => {
  40. if (want == null || hlTypeName(want) != 'String' || want == '' || want.length > 500) { return '/' }
  41. if (want.slice(0, 1) != '/' || want.slice(0, 2) == '//' || want.slice(0, 7) == '/login/') { return '/' }
  42. let i = 0
  43. while (i < want.length) {
  44. if (!nextChars.includes(want[i])) { return '/' }
  45. i = i + 1
  46. }
  47. return want
  48. }
  49. // A FAILED LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed
  50. failed = (req, why) => {
  51. let s = req.session
  52. let fresh = s == null
  53. if (fresh) { s = server.sessions.mint() }
  54. s.data.loginError = why
  55. server.sessions.save(s)
  56. let res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })
  57. if (fresh) { res.headers['Set-Cookie'] = server.sessions.cookieHeader(s.id) }
  58. return res
  59. }
  60. // the function route gets the cookie's session as req.session (hybriel #11); none yet → minted here
  61. loginCallback = (route, req) => {
  62. if (req.method != 'GET') { return failed(req, 'GET only') }
  63. let q = req.query != null ? req.query : {}
  64. let code = q.ident_code
  65. if (code == null || code == '') { return failed(req, 'ident sent no login code') }
  66. let x = exchangeCode(code)
  67. if (x.error != null) { return failed(req, x.error) }
  68. let u = ensureUser(x.identity)
  69. if (u == null) { return failed(req, 'could not store the user') }
  70. let s = req.session
  71. let fresh = s == null
  72. if (fresh) { s = server.sessions.mint() }
  73. s.user = { id = u.id }
  74. s.data.tag = randomBytes(16)
  75. s.data.loginError = null
  76. server.sessions.save(s)
  77. let res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' } })
  78. if (fresh) { res.headers['Set-Cookie'] = server.sessions.cookieHeader(s.id) }
  79. return res
  80. }
  81. // ---- /posters/<name> (tracker.worldapi.org#4): a show's poster image, served dynamically from
  82. // storage/mpackdb/posters/ (the poster's file name is the show's oldId + its migrated extension,
  83. // shows.hl posterName) — falls back to a placeholder svg when the real file isn't there yet (the
  84. // mongo export never included the actual poster bytes, see the report's `open`).
  85. postersDir = (env('TRACKER_STORAGE') != null ? env('TRACKER_STORAGE') : './storage/mpackdb') + '/posters'
  86. placeholderPoster = './assets/poster-placeholder.svg'
  87. posterNameChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_'
  88. isSafePosterName = (name) => {
  89. if (name == null || hlTypeName(name) != 'String' || name == '' || name.length > 100) { return false }
  90. let i = 0
  91. while (i < name.length) {
  92. if (!posterNameChars.includes(name[i])) { return false }
  93. i = i + 1
  94. }
  95. return true
  96. }
  97. endsWithStr = (s, suffix) => { return s.length >= suffix.length && s.slice(s.length - suffix.length) == suffix }
  98. mimeOfPoster = (name) => {
  99. if (endsWithStr(name, '.png')) { return 'image/png' }
  100. if (endsWithStr(name, '.webp')) { return 'image/webp' }
  101. if (endsWithStr(name, '.svg')) { return 'image/svg+xml' }
  102. return 'image/jpeg'
  103. }
  104. posterRoute = (route, req) => {
  105. let name = route.params != null ? route.params.name : null
  106. if (!isSafePosterName(name)) { return new Response('bad poster name', { status = 400 headers = { 'Content-Type' = 'text/plain; charset=utf-8' } }) }
  107. let path = postersDir + '/' + name
  108. if (exists(path)) { return new Response(readBytes(path), { headers = { 'Content-Type' = mimeOfPoster(name) 'Cache-Control' = 'public, max-age=86400' } }) }
  109. return new Response(readBytes(placeholderPoster), { headers = { 'Content-Type' = 'image/svg+xml' 'Cache-Control' = 'public, max-age=3600' } })
  110. }
  111. routes = [
  112. { pattern = "/favicon.ico" direct = "" }
  113. { pattern = "/login/callback" function = loginCallback }
  114. { pattern = "/login/failed" component = LoginFailed }
  115. { pattern = "/login.js" file = "./login.js" headers = { 'Cache-Control' = 'no-cache' } }
  116. { pattern = "/posters/:name" function = posterRoute }
  117. { pattern = "/shows/:slug" component = Show }
  118. { pattern = "/unwatched" component = Unwatched }
  119. { pattern = "/schedule" component = Schedule }
  120. { pattern = "/" component = Home }
  121. ]
  122. // WHO GETS THE PUSH: the login state reaches the tabs of one session (components/main.hl trackerSignedIn/Out).
  123. tagOf = (session) => { return session != null && session.data != null ? session.data.tag : null }
  124. audience = {
  125. trackerSignedIn = (tag, session) => { return tag != null && tagOf(session) == tag }
  126. trackerSignedOut = (tag, session) => { return tag != null && tagOf(session) == tag }
  127. }
  128. // cookies are per host, not per port: an own name keeps this app's session apart from
  129. // ident's own (and from any other worldapi app sharing a dev host), see ident README "Design
  130. // tokens" / hybriel#10 hybriel#17.
  131. sessionCookie = 'trackersid'
  132. server = new WebFramework(routes = routes, styles = styles, port = port, sessionCookie = sessionCookie, watchMode = watching)
  133. on Error(e) { console.log('error absorbed: ' + e.message) }

Branches

Latest commits

  • 44b7d9f9tracker#6: /schedule — upcoming episodes of followed shows, soonest firstmre
  • 91c9fc8ctracker#5: /unwatched — unwatched released episodes of followed shows, newest firstmre
  • a97c0295tracker#4: show page /shows/:slug (header, seasons, episodes, watch checks) + tools/relink-episode-seasons.hlmre
  • 05f407c5tracker: no border on any button except inverted ones (Log out, ident status and identities too); header brand weight 100mre
  • 17375427tracker#2: tools/migrate.hl + tools/verify.hl — old MongoDB data into mpackdb with new idsmre
  • 31aac936tracker#3: no border on the header and on filled buttons; inverted buttons keep theirsmre
  • 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