gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit384d3d23384d3d23|||PR give calendar months their own addressAtlas384d3d23/lib/api.hl

3.4 KB

  1. // lib/api.hl — THE FUNCTION ROUTES (calendar mission 001, code order): thin wrappers — check the input and the session,
  2. // call the topic (lib/users.hl), answer. The calendar's own reads and writes are faces (components/calendar.hl).
  3. //
  4. // THE LOGIN BUTTON'S RETURN (ident README "How apps use ident"): /login/callback?ident_code=&next= → the user
  5. // (lib/users.hl userOfLoginCode) → the session is signed in → back to `next` (lib/api-helpers.hl safePath). A FAILED
  6. // LOGIN is a page (components/loginfailed.hl): the reason is parked in the session, then → /login/failed.
  7. // These two write the session: project.hl hands them the request and the session store BY REFERENCE (&req, &sessions);
  8. // a by-value copy would lose the write (tickets mission 010, gitoria mission 002, notes mission 002).
  9. import { Response } from 'hl:http1'
  10. import { randomBytes } from 'hl:crypto'
  11. import { userOfLoginCode, usersTable } from './users.hl'
  12. import { eventsTable } from './events.hl'
  13. import { countOfList } from './util.hl'
  14. import { safePath } from './api-helpers.hl'
  15. import { tooManyLogins } from './login-limit.hl'
  16. static loginFailed = (&req, &sessions, String why) => {
  17. let s = req.session
  18. fresh = s == null
  19. if (fresh) { s = sessions.mint() }
  20. s.data.loginError = why
  21. sessions.save(s)
  22. res = new Response('login failed: ' + why, { status = 302 headers = { 'Location' = '/login/failed' 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' 'Referrer-Policy' = 'no-referrer' } })
  23. if (fresh) { res.headers['Set-Cookie'] = sessions.cookieHeader(s.id) }
  24. return res
  25. }
  26. // the function route gets the cookie's session as req.session (hybriel #11); none yet → minted here
  27. static loginCallback = (Hybrid route, &req, &sessions) => {
  28. if (req.method != 'GET') { return loginFailed(&req, &sessions, 'GET only') }
  29. q = req.query != null ? req.query : {}
  30. code = q.ident_code
  31. if (code == null || code == '') { return loginFailed(&req, &sessions, 'ident sent no login code') }
  32. // the per-client limit comes before the exchange (which calls ident); nginx sets X-Client-IP
  33. if (tooManyLogins(req.headers['x-client-ip'])) { return loginFailed(&req, &sessions, 'too many login attempts from your network — wait a few minutes') }
  34. x = userOfLoginCode(code)
  35. if (x.error != null) { return loginFailed(&req, &sessions, x.error) }
  36. // THE SAME SESSION, NEVER SIGNED OUT (ident#28): a good code signs the session the browser already has in (a second tab or an
  37. // installed app may not store a new cookie, and a session we signed out would leave it anonymous); none yet → minted here
  38. let s = req.session
  39. if (s == null) { s = sessions.mint() }
  40. s.user = { id = x.user.id }
  41. s.data.tag = randomBytes(16)
  42. s.data.loginError = null
  43. sessions.save(s)
  44. res = new Response('logged in', { status = 302 headers = { 'Location' = safePath(q.next) 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' 'Referrer-Policy' = 'no-referrer' 'Set-Cookie' = sessions.cookieHeader(s.id) } })
  45. return res
  46. }
  47. // /health: 200 only when both tables answer (they are loaded at start); deploys wait for it (standby-swap.sh) before they switch containers
  48. static health = () => {
  49. u = countOfList(usersTable.find(null, null))
  50. e = countOfList(eventsTable.find(null, null))
  51. return new Response('ok', { status = 200 headers = { 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' 'X-Content-Type-Options' = 'nosniff' } })
  52. }

Branches

Latest commits

  • 384d3d23|||PR give calendar months their own addressAtlas
  • fff2f537Merge branch 'calendar31-lib' into mainAtlas
  • 07cde3dc|||PR keep worker records out of the repo; README vendor is day72Atlas
  • a753b0ab|||PR move shared calendar logic to libAtlas
  • b32a921dMerge branch 'vendor-day72' into mainAtlas
  • 60a0dd19|||PR Hybriel day72 (00c9dbb8) instead of the withdrawn day73Atlas
  • 53f5464dMerge branch 'calendar26-day73' into mainAtlas
  • 338499ec|||PR Keep reminder settings open during editing and rejected savesAtlas
  • ad9e8d59|||PR Use framework page and popover state, Hybriel day73Atlas
  • cab224a1Re-vendor calendar to verified Hybriel day73Atlas
  • f9b8f918Merge branch 'calendar-vendor-day61' into mainAtlas
  • 66341793|||PR Vendor Hybriel day61Atlas
  • 36fc596bMerge branch 'vendor-day59' into mainAtlas
  • b37cc1da|||PR Vendor Hybriel day59Atlas
  • 05665102Merge branch 'vendor-day56' into mainAtlas
  • 687296fb|||PR Vendor Hybriel day56Atlas
  • a6166d4eMerge branch 'hl-standard-3' into mainAtlas
  • 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
  • add4b499|||PR Make calendar day actions keyboard accessibleAtlas
  • 79e49b4dMerge branch 'hl-standard' into mainAtlas