gitoriaLog in with ident

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit3251488a3251488atracker#7: /my/shows (followed shows, newest follow first, poster, title, last watched SxxEyy); gate can take screenshots (TRACKER_GATE_SHOTS)mre3251488a/project.hl

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

Branches

Latest commits

  • 3251488atracker#7: /my/shows (followed shows, newest follow first, poster, title, last watched SxxEyy); gate can take screenshots (TRACKER_GATE_SHOTS)mre
  • 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