gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb37cc1dab37cc1da|||PR Vendor Hybriel day59Atlasb37cc1da/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

  • 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
  • e9fec7bb|||PR HL-STANDARD clean-up: typed parameters, plain locals, by-reference cacheAtlas
  • eff166a4Re-vendor complete Hybriel plugins and runtime to day44mre
  • cef81394Wait for the DST fixture page instead of an unused socketmre
  • 42434f47Check lazy-session cookies after login with a stored anonymous fixturemre
  • 728e84e3Re-vendor complete Hybriel day41 plugin directoriesmre
  • cbd7ff43Backup tar: --hard-dereference, so mpackdb sidecars are never stored as hard links (Tracker's finding)mre
  • 59d1da0acalendar: plain declarations instead of needless let in 9 files (calendar#17): 49 declarations that are never reassigned; reassigned ones and the timer closure's p stay letmre
  • f5415cf9calendar: the Style blocks in the language's nested form (calendar#16): rules named after the View's own elements with their children nested, pseudo-classes and @media nested, merged duplicate select rule; same CSS, shots identical in Chrome at 4 sizesmre
  • edb7c947calendar: the sign-in callback keeps the session it finds and signs it in (same cookie, nothing signed out; ident#28); security gate 56+4 failing → 60 passedmre
  • bf8cc0fcStyles move into their components: the shell's styles into main.hl, the events and the editors into the month view's Style block; styles.hl keeps only the theme inherit, the accent and the message line; .gitignore ignores a bin symlink too; tests/styles-shots.mjs for the before/after pixel comparemre
  • 5d8579d7README: login.js stays by caramboleyo's explicit rulemre
  • cc759328Merge sb3-standby: calendar deploys through its standbymre
  • f2fcdc6dcalendar: a standby container (port 45015, profile standby, reminder clock off) and a /health page; deploy hands over through standby-swap.sh without a gapmre