tickets
All repositories: gitoria
28.5 KB
// lib/api.hl — THE FUNCTION ROUTES (project.hl `routes`): the JSON API for the scheduler, a CLI and connected apps,// and the three page routes that are not components (/login/callback, /connect, /tickets/:ref). Thin wrappers:// check the method, the token / key and the body (lib/api-helpers.hl), call the topic function, push what// changed (the same frames the web faces push, so an open browser follows an API write live), answer.// See README.md "API" for the shapes.//// Every POST body goes through readBody: invalid JSON, unknown field, missing/empty required field, non-string// value → 400. WRITES NEED A TOKEN (ticket #7): no / bad / revoked token → 401, checked BEFORE the body. The// author is the token's user; a body with `author` → 400 naming it. Reads stay public.// THE MARKDOWN READ VIEW (ticket #6): `Accept: text/markdown` on the ticket GETs (the two lists,// the two single-ticket forms) answers a compact Markdown document (mdview.hl); JSON otherwise, unchanged.// TWO WAYS TO NAME A TICKET (ticket #38): `/api/tickets/:ref` — :ref is the OLD global// number of a migrated ticket (docs say "#38") or the ticket's UUID (`id`) — and the// per-project form `/api/projects/:slug/tickets/:number`. Both answer the same shapes.import { Response } from 'hl:http1'import { randomBytes } from 'hl:crypto'import { reply, hdr, fail, queryOf, readBody, refuse, unauthorized, unauthorizedFor, authDenied, denied, apiUser, apiAuth, actorIn, stateFilter, filterLabel, wantsMarkdown, markdownReply, htmlPage, moved, missing, commentFormOf, fileReply } from './api-helpers.hl'import { isMultipart } from './multipart.hl'import { attachmentFor } from './attachments.hl'import { exchangeCode, ensureUser, userOfSession } from './users.hl'import { projectNames, projectByAnySlug, projectByRef, slugOfRef, projectRecords, projectRowOf, projectSlugOf, createProject, updateProject, memberRows, setMemberRole, userByRef, roles } from './projects.hl'import { states } from './tickets-helpers.hl'import { pageEventOf } from './events.hl'import { ticketRows, ticketWithEvents, ticketByRef, ticketAt, createTicket, addComment, changeComment, changeState, assignTicket, editTicket, setParent, changeBlocker, relatedViews, removeMember, inboxRows } from './tickets.hl'import { mayInvite, createInvite, joinByInvite } from './invites.hl'import { createRequest, exchangeConnectCode, connectionRows, disconnect } from './connections.hl'import { listDocument, ticketDocument } from './mdview.hl'import { subscriptionOf, openStream } from './stream.hl'import { ticketHref, projectHrefOf } from './util.hl'// ---- tickets ------------------------------------------------------------------------------------static byRef = (route) => { return ticketByRef(route.params.ref) }// the project of an /api/projects/:slug/… route: its slug (also an old one) or its id (ticket #25) → the slugstatic slugOf = (route) => { return slugOfRef(route.params.slug) }static byNumber = (route) => { return ticketAt(slugOf(route), route.params.number) }static listTickets = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }// hl:http1 hands the parsed query string as `req.query`; the path carries noneq = req.query != null ? req.query : queryOf(req.path)sf = stateFilter(q)if (sf.bad != null) { return sf.bad }pr = q.project != null && q.project != '' ? q.project : nullrows = ticketRows(pr, sf.state)if (wantsMarkdown(req)) { return markdownReply(listDocument(rows, filterLabel(pr, sf.state))) }return reply(200, { tickets = rows })}// POST /api/tickets { project, … } and POST /api/projects/:slug/tickets { … }static createFromApi = (project, b, auth, req) => {pr = projectByRef(project)w = actorIn(auth, req, pr != null ? pr.id : null)if (w.response != null) { return w.response }user = w.userr = createTicket(project, b.subject, b.summary, user, b.source, b.assignee)if (r.forbidden == true) { return reply(403, { error = r.error field = r.field }) }if (r.error != null) { return refuse({ error = r.error field = r.field }) }if (!r.existed) { emit client ticketCreated(r.ticket) }return reply(r.existed ? 200 : 201, { ticket = r.ticket existed = r.existed })}static postTicket = (route, req) => {if (req.method == 'GET') { return listTickets(route, req) }if (req.method != 'POST') { return fail(405, 'GET or POST') }u = apiAuth(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['project' 'subject'], ['summary' 'source' 'assignee'])if (rb.bad != null) { return refuse(rb.bad) }return createFromApi(rb.body.project, rb.body, u, req)}static projectTickets = (route, req) => {slug = slugOf(route)if (req.method == 'GET') {if (projectByAnySlug(slug) == null) { return fail(404, 'no such project') }q = req.query != null ? req.query : queryOf(req.path)sf = stateFilter(q)if (sf.bad != null) { return sf.bad }rows = ticketRows(slug, sf.state)if (wantsMarkdown(req)) { return markdownReply(listDocument(rows, filterLabel(slug, sf.state))) }return reply(200, { tickets = rows })}if (req.method != 'POST') { return fail(405, 'GET or POST') }u = apiAuth(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['subject'], ['summary' 'source' 'assignee'])if (rb.bad != null) { return refuse(rb.bad) }return createFromApi(slug, rb.body, u, req)}static getOne = (t, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }if (t == null) { return fail(404, 'no such ticket') }data = ticketWithEvents(t)if (wantsMarkdown(req)) { return markdownReply(ticketDocument(data)) }return reply(200, data)}// tickets#27 (mission 017): a comment WITH FILES is a multipart/form-data body (`text` + file parts, lib/api-helpers.hl// commentFormOf); JSON { text } stays as it was. Over a size limit → 413, a file that is not what it claims → 400.static commentOnTicket = (t, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }auth = apiAuth(req)if (auth == null || auth.response != null) { return authDenied(auth, 'an API write') }let text = ''let files = nullif (isMultipart(req)) {mp = commentFormOf(req)if (mp.bad != null) { return mp.bad }text = mp.textfiles = mp.files} else {rb = readBody(req, ['text'], [])if (rb.bad != null) { return refuse(rb.bad) }text = rb.body.text}if (t == null) { return fail(404, 'no such ticket') }w = actorIn(auth, req, t.project)if (w.response != null) { return w.response }u = w.userr = addComment(t.id, u, text, files)if (r.error == 'no such ticket') { return fail(404, r.error) }if (r.forbidden == true) { return reply(403, { error = r.error field = r.field }) }if (r.tooBig == true) { return reply(413, { error = r.error field = r.field }) }if (r.error != null) { return refuse({ error = r.error field = r.field }) }emit client ticketEvent(r.ticket.id, pageEventOf(r.event), r.ticket)return reply(201, r)}// mission 016: a comment's author EDITS it (POST …/comments/:comment/edit { text }) or DELETES it (…/delete, body empty or// {}); any member LIKES it (…/like, again = take it back). `:comment` = the comment's event `id`. 200 { ticket, event (the// comment as it is now), liked (like only) }; 401 no token, 403 not the author / not a member, 404 no such ticket / comment,// 400 empty or unchanged text, a deleted comment, a bad body. An open ticket page follows (push commentChanged).static changeOfComment = (t, route, req, action) => {if (req.method != 'POST') { return fail(405, 'POST only') }auth = apiAuth(req)if (auth == null || auth.response != null) { return authDenied(auth, 'an API write') }let b = {}if (action == 'edit' || (req.body != null && req.body != '')) {rb = readBody(req, action == 'edit' ? ['text'] : [], [])if (rb.bad != null) { return refuse(rb.bad) }b = rb.body}if (t == null) { return fail(404, 'no such ticket') }w = actorIn(auth, req, t.project)if (w.response != null) { return w.response }r = changeComment(t.id, route.params.comment, w.user, action, b.text)if (r.error == 'no such ticket' || r.error == 'no such comment on this ticket') { return fail(404, r.error) }if (r.error != null) { return denied(r) }emit client commentChanged(r.ticket.id, pageEventOf(r.event))return reply(200, r)}static stateOfTicket = (t, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }auth = apiAuth(req)if (auth == null || auth.response != null) { return authDenied(auth, 'an API write') }rb = readBody(req, ['state'], ['text'])if (rb.bad != null) { return refuse(rb.bad) }if (t == null) { return fail(404, 'no such ticket') }w = actorIn(auth, req, t.project)if (w.response != null) { return w.response }u = w.userb = rb.bodyr = changeState(t.id, b.state, u, b.text)if (r.error == 'no such ticket') { return fail(404, r.error) }if (r.forbidden == true) { return reply(403, { error = r.error field = r.field }) }if (r.error != null) { return refuse({ error = r.error field = r.field }) }emit client ticketEvent(r.ticket.id, pageEventOf(r.event), r.ticket)if (r.assignEvent != null) { emit client ticketEvent(r.ticket.id, pageEventOf(r.assignEvent), r.ticket) }emit client ticketsRelated(relatedViews(r.ticket.id, []))return reply(201, r)}// POST …/assign { assignee } (ticket #20): a member's display name (or users id, or ident id); '' = nobody.// Needs the role edit or admin → else 403.static assignOfTicket = (t, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, [], ['assignee'])if (rb.bad != null) { return refuse(rb.bad) }if (rb.body.assignee == null) { return refuse({ error = "field 'assignee' is required: a member's name, or an empty string for nobody" field = 'assignee' }) }if (t == null) { return fail(404, 'no such ticket') }let who = ''if (rb.body.assignee.trim() != '') {f = userByRef(rb.body.assignee, t.project)if (f.error != null) { return refuse({ error = f.error field = 'assignee' }) }who = f.user.id}r = assignTicket(t.id, u, who)if (r.forbidden == true) { return reply(403, { error = r.error field = r.field }) }if (r.error != null) { return refuse({ error = r.error field = r.field }) }emit client ticketEvent(r.ticket.id, pageEventOf(r.event), r.ticket)return reply(201, r)}// POST …/edit { subject?, summary? } (ticket #8): a member with the role edit or admin → else 403;// the history keeps the previous valuesstatic editOfTicket = (t, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, [], ['subject' 'summary'])if (rb.bad != null) { return refuse(rb.bad) }if (t == null) { return fail(404, 'no such ticket') }b = rb.bodyr = editTicket(t.id, u, b.subject, b.summary)if (r.error == 'no such ticket') { return fail(404, r.error) }if (r.forbidden == true) { return reply(403, { error = r.error }) }if (r.error != null) { return refuse({ error = r.error field = r.field }) }emit client ticketEvent(r.ticket.id, pageEventOf(r.event), r.ticket)emit client ticketsRelated(relatedViews(r.ticket.id, []))return reply(201, r)}// RELATIONS (ticket #4, mission 017; tickets.hl "relations"): a ticket is named as// `<project>#<number>` or by its UUID. Who may: a member with the role edit or admin in THIS ticket's project (the one whose relation changes; mission ST) → else 403.// POST …/parent { parent }: set the parent; `"parent": ""` removes it.// POST …/blocked-by { add } or { remove }: this ticket is (no longer) blocked by that one.// 201 { ticket, event (kind link) }; the ticket rows and every open page of the tickets involved follow.static linkReply = (r) => {if (r.error == 'no such ticket') { return fail(404, r.error) }if (r.forbidden == true) { return reply(403, { error = r.error }) }if (r.error != null) { return refuse({ error = r.error field = r.field }) }emit client ticketEvent(r.ticket.id, pageEventOf(r.event), r.ticket)emit client ticketsRelated(r.related)return reply(201, { ticket = r.ticket event = r.event })}static parentOfTicket = (t, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, [], ['parent'])if (rb.bad != null) { return refuse(rb.bad) }if (rb.body.parent == null) { return refuse({ error = "field 'parent' is required: <project>#<number>, or an empty string to remove the parent" field = 'parent' }) }if (t == null) { return fail(404, 'no such ticket') }return linkReply(setParent(t.id, u, rb.body.parent))}static blockersOfTicket = (t, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, [], ['add' 'remove'])if (rb.bad != null) { return refuse(rb.bad) }b = rb.bodyif ((b.add == null) == (b.remove == null)) { return refuse({ error = "give exactly one of 'add' or 'remove' (<project>#<number>)" field = b.add == null ? 'add' : 'remove' }) }if (t == null) { return fail(404, 'no such ticket') }adding = b.add != nullreturn linkReply(changeBlocker(t.id, u, adding ? b.add : b.remove, adding, adding ? 'add' : 'remove'))}// ---- the event stream for programs (ticket #26, lib/stream.hl) ----------------------------------------// GET /api/events[?project=a,b&assignee=<name|id|me>¬Mine=1&after=<id>] with `Authorization: Bearer <token>`// (a user's token, or a project key tktc_ = that project only) → text/event-stream, held open. 401 without a// valid token, 400 a bad filter, 403 a project the key does not reach. `Last-Event-ID` wins over `after`.static getEvents = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }auth = apiAuth(req)if (auth == null || auth.response != null) { return authDenied(auth, 'the event stream') }who = auth.user != null ? { user = auth.user } : { project = auth.conn.project }q = req.query != null ? req.query : queryOf(req.path)r = subscriptionOf(q, req.headers['last-event-id'], who)if (r.error != null) { return denied(r) }if (openStream(req.respond, r.sub) == null) { return fail(500, 'the event stream could not start') }return null}// GET /api/attachments/:id (tickets#27, opened by tickets#37): an attachment's bytes, for everyone who may see its comment — and a// ticket's comments are public, so no sign-in is needed. 404 for an unknown file or one whose comment was deleted.static getAttachment = (route, req) => {if (req.method != 'GET' && req.method != 'HEAD') { return fail(405, 'GET only') }r = attachmentFor(route.params.id)if (r.error != null) { return fail(r.status, r.error) }return fileReply(r.file)}static getTicket = (route, req) => { return getOne(byRef(route), req) }static postComment = (route, req) => { return commentOnTicket(byRef(route), req) }static postState = (route, req) => { return stateOfTicket(byRef(route), req) }static postCommentEdit = (route, req) => { return changeOfComment(byRef(route), route, req, 'edit') }static postCommentDelete = (route, req) => { return changeOfComment(byRef(route), route, req, 'delete') }static postCommentLike = (route, req) => { return changeOfComment(byRef(route), route, req, 'like') }static postProjectCommentEdit = (route, req) => { return changeOfComment(byNumber(route), route, req, 'edit') }static postProjectCommentDelete = (route, req) => { return changeOfComment(byNumber(route), route, req, 'delete') }static postProjectCommentLike = (route, req) => { return changeOfComment(byNumber(route), route, req, 'like') }static getProjectTicket = (route, req) => { return getOne(byNumber(route), req) }static postProjectComment = (route, req) => { return commentOnTicket(byNumber(route), req) }static postProjectState = (route, req) => { return stateOfTicket(byNumber(route), req) }static postAssign = (route, req) => { return assignOfTicket(byRef(route), req) }static postProjectAssign = (route, req) => { return assignOfTicket(byNumber(route), req) }static postEdit = (route, req) => { return editOfTicket(byRef(route), req) }static postProjectEdit = (route, req) => { return editOfTicket(byNumber(route), req) }static postParent = (route, req) => { return parentOfTicket(byRef(route), req) }static postProjectParent = (route, req) => { return parentOfTicket(byNumber(route), req) }static postBlockers = (route, req) => { return blockersOfTicket(byRef(route), req) }static postProjectBlockers = (route, req) => { return blockersOfTicket(byNumber(route), req) }// ---- projects and members (ticket #20; projects.hl) ----------------------------------------------// GET /api/projects → { projects (the slugs), details (title, slug, description, id …), states, roles }// POST /api/projects { title, slug?, description? } → 201 { project, members } — any logged-in user opens// a project and is its first admin; the slug is generated from the title unless given// GET /api/projects/:slug → { project, members } (the slug may be an old one)// POST /api/projects/:slug { title?, slug?, description? } → { project } — admin only; a new slug keeps the old one working// POST /api/projects/:slug/members { user, role } — admin only: `user` = a display name, an ident id or a user id of someone// who logged in here; sets (adds / changes) the role: use | edit | admin// POST /api/projects/:slug/members/remove { user } — admin only// POST /api/projects/:slug/invites { role, uses?, days?, email? } — admin only: an ident invite link → { url, id, expires, mailed }// GET /api/inbox — the token's user: { pending, review }, the tickets assigned to themstatic getProjects = (route, req) => {if (req.method == 'POST') { return postNewProject(req) }if (req.method != 'GET') { return fail(405, 'GET or POST') }let details = []for (p of projectRecords()) { details.push(projectRowOf(p)) }return reply(200, { projects = projectNames() details = details states = states roles = roles })}static postNewProject = (req) => {u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['title'], ['slug' 'description'])if (rb.bad != null) { return refuse(rb.bad) }r = createProject(u, rb.body.title, rb.body.slug, rb.body.description)if (r.error != null) { return denied(r) }return reply(201, r)}static getProject = (route, req) => {p = projectByAnySlug(slugOf(route))if (req.method == 'GET') {if (p == null) { return fail(404, 'no such project') }return reply(200, { project = projectRowOf(p) members = memberRows(p.id) })}if (req.method != 'POST') { return fail(405, 'GET or POST') }u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, [], ['title' 'slug' 'description'])if (rb.bad != null) { return refuse(rb.bad) }if (p == null) { return fail(404, 'no such project') }r = updateProject(p.id, u, rb.body.title, rb.body.slug, rb.body.description)if (r.error != null) { return denied(r) }return reply(200, r)}static postMembers = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }p = projectByAnySlug(slugOf(route))u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['user' 'role'], [])if (rb.bad != null) { return refuse(rb.bad) }if (p == null) { return fail(404, 'no such project') }f = userByRef(rb.body.user, null)if (f.error != null) { return refuse({ error = f.error field = 'user' }) }r = setMemberRole(p.id, u, f.user.id, rb.body.role)if (r.error != null) { return denied(r) }return reply(200, r)}static postMemberRemove = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }p = projectByAnySlug(slugOf(route))u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['user'], [])if (rb.bad != null) { return refuse(rb.bad) }if (p == null) { return fail(404, 'no such project') }f = userByRef(rb.body.user, p.id)if (f.error != null) { return refuse({ error = f.error field = 'user' }) }r = removeMember(p.id, u, f.user.id)if (r.error != null) { return denied(r) }return reply(200, r)}// an invite is only for an admin (invites.hl mayInvite); ident makes the linkstatic postInvites = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }p = projectByAnySlug(slugOf(route))u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['role'], ['uses' 'days' 'email'])if (rb.bad != null) { return refuse(rb.bad) }if (p == null) { return fail(404, 'no such project') }b = rb.bodyno = mayInvite(p, u, b.role)if (no != null) { return denied(no) }n = b.uses == null ? 1 : toNumber(b.uses)d = b.days == null ? 7 : toNumber(b.days)if (n == null || n < 1 || n > 1000) { return denied({ error = "'uses' must be a number from 1 to 1000" field = 'uses' }) }if (d == null || d < 1 || d > 90) { return denied({ error = "'days' must be a number from 1 to 90" field = 'days' }) }r = createInvite(p.id, b.role, n, d, b.email)if (r.error != null) { return denied(r) }return reply(201, r)}static getInbox = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }return new Response(JSON.stringify(inboxRows(u)), { status = 200 headers = hdr({ 'Content-Type' = 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' }) })}// ---- connecting an app (ticket #21; connections.hl) ------------------------------------------------// GET /connect?app=&label=&return=&state= — the app sends the person here; the request is stored and the browser goes to// the page /connect/<nonce> (components/connect.hl), where they pick or make a project and confirm.// POST /api/connect/exchange { code } — the app's SERVER swaps the one-time code for the project's key:// 200 { key, project (slug), title, api }; 400 for an unknown, used or expired code. The key is shown here ONCE.// GET /api/projects/:slug/connections — who is connected (public, like the project page)// POST /api/projects/:slug/connections/remove { id } — an admin disconnects; the key is dead at oncestatic connectStart = (route, req) => {if (req.method != 'GET') { return htmlPage(405, 'Connect', 'GET only') }q = req.query != null ? req.query : {}r = createRequest(q.app, q.label, q['return'], q.state)if (r.error != null) { return htmlPage(r.busy == true ? 429 : 400, 'Connect', r.error) }to = '/connect/' + r.noncereturn new Response('continue at ' + to, { status = 302 headers = hdr({ 'Location' = to 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' }) })}static connectExchange = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }rb = readBody(req, ['code'], [])if (rb.bad != null) { return refuse(rb.bad) }r = exchangeConnectCode(rb.body.code)if (r.error != null) { return refuse({ error = r.error field = 'code' }) }emit client connectionsChanged(r.project)return new Response(JSON.stringify(r), { status = 200 headers = hdr({ 'Content-Type' = 'application/json; charset=utf-8' 'Cache-Control' = 'no-store' }) })}static projectConnections = (route, req) => {if (req.method != 'GET') { return fail(405, 'GET only') }p = projectByAnySlug(slugOf(route))if (p == null) { return fail(404, 'no such project') }return reply(200, { connections = connectionRows(p.id) })}static projectDisconnect = (route, req) => {if (req.method != 'POST') { return fail(405, 'POST only') }p = projectByAnySlug(slugOf(route))u = apiUser(req)if (u == null || u.response != null) { return authDenied(u, 'an API write') }rb = readBody(req, ['id'], [])if (rb.bad != null) { return refuse(rb.bad) }if (p == null) { return fail(404, 'no such project') }r = disconnect(p.id, u, rb.body.id)if (r.error != null) { return denied(r) }emit client connectionsChanged(p.slug)return reply(200, r)}// ---- old page URLs -----------------------------------------------------------------------------// They move for good (301) to the short page URLs of ticket #25, `/<slug>` and `/<slug>/<number>`, with the// project's CURRENT slug; what names nothing → 404.// `/tickets/<old global number>` (also `/tickets/<uuid>`)static oldTicketPage = (route, req) => {t = ticketByRef(route.params.ref)if (t == null) { return missing('no such ticket') }return moved(ticketHref(projectSlugOf(t), t.number))}// `/projects/<slug>` (ticket #25)static oldProjectPage = (route, req) => {p = projectByAnySlug(route.params.slug)if (p == null) { return missing('no such project') }return moved(projectHrefOf(p.slug))}// `/projects/<slug>/<number>` and `/projects/<slug>/tickets/<number>` (the form the conductor sent in messages)static oldProjectTicketPage = (route, req) => {t = ticketAt(route.params.slug, route.params.number)if (t == null) { return missing('no such ticket') }return moved(ticketHref(projectSlugOf(t), t.number))}// ---- THE LOGIN BUTTON'S RETURN (ticket #7; ident README "How apps use ident") -------------// The shell's "Log in with ident" goes to <ident>/login?key=<IDENT_API_KEY>&return=// <TICKETS_PUBLIC_URL>/login/callback; ident sends the browser back here with ?ident_code=.// The code is exchanged SERVER SIDE (users.hl exchangeCode, key + secret) for the per-app// identity id; its tickets user (made at the first login) goes into THIS browser's hl:web// session (`req.session`, the cookie's — hybriel#11; minted from `sessions` when the browser brought none), then →// the page the login started from (`?next=`, ticket #10, safeNext) or `/`, where the shell asks for a display name// if there is none yet. project.hl hands `req` AND the framework's session store BY REFERENCE: copied into this// second call, the session inside `req` would be a copy too and the login would never reach the session the// framework keeps (mission 010: the gate's logins failed that way).// (The identity selector logs in through the shell's face `identLogin` instead — no reload.)static loginCallback = (route, &req, &sessions) => {if (req.method != 'GET') { return htmlPage(405, 'Login failed', 'GET only') }q = req.query != null ? req.query : {}code = q.ident_codeif (code == null || code == '') { return htmlPage(400, 'Login failed', 'ident sent no login code') }x = exchangeCode(code)if (x.error != null) { return htmlPage(400, 'Login failed', x.error) }u = ensureUser(x.identity)if (u == null) { return htmlPage(500, 'Login failed', 'could not store the user') }// an INVITE (ident#22, invites.hl): ident sends `invite=<id>` with the codelet joined = nullif (q.invite != null && q.invite != '') {j = joinByInvite(q.invite, x.identity, u.id)if (j.error != null) { return htmlPage(400, 'Invite failed', j.error) }joined = j.project}// THE CALLBACK NEVER SIGNS OUT THE SESSION IT FINDS (ident#28): only a valid ident code gets here (a missing, unknown or reused// code answered 400 above and touched nothing). The new user goes onto the SAME session, the browser's own cookie and id; a// session is minted only when the browser brought none. The old code logged the presented session out first, so a repeated// or raced callback could leave the browser signed out.let s = req.sessionif (s == null) { s = sessions.mint() }s.user = { id = u.id }s.data.tag = randomBytes(16)sessions.save(s)to = joined != null ? projectHrefOf(joined.slug) : safeNext(q.next)res = new Response('logged in', { status = 302 headers = hdr({ 'Location' = to 'Cache-Control' = 'no-store' 'Content-Type' = 'text/plain; charset=utf-8' }) })res.headers['Set-Cookie'] = sessions.cookieHeader(s.id)return res}// BACK TO THE PAGE (ticket #10): login.js puts `?next=<path + query of the page>` into the// button's return URL. Only a same-origin PATH goes: it starts with ONE `/` (not `//`, no// backslash — `/\host` is another host to some browsers), only URL-safe characters (no// scheme, no spaces, no control characters), at most 500 chars, never /login/… itself.// Anything else → `/`.static nextChars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~/?&=%+,;@!$()*:'static safeNext = (want) => {if (want == null || hlTypeName(want) != 'String' || want == '' || want.length > 500) { return '/' }if (want.slice(0, 1) != '/' || want.slice(0, 2) == '//' || want.slice(0, 7) == '/login/') { return '/' }let i = 0while (i < want.length) {if (!nextChars.includes(want[i])) { return '/' }i = i + 1}return want}
Branches
Latest commits
- 090ee809tickets: every component's styles in its own nested Style {} block; styles.hl keeps only the theme inherit, the accent and the site-wide rules (tickets#36)mre
- d395763ftickets: ident-access gate ports 8769/8768 (8797 is taken on loreana)mre
- afb9a403tickets: an ident access key (Authorization: Bearer idt_…) works in the API (ident#27 app side): lib/ident-access.hl copied from components, read keys GET only, ident down 503; gate tests/ident-access-tickets.mjsmre
- 48c1ba79tickets: md-paste-files gate with the file manager casesmre
- f04afc43tickets: MdEditor from components 59704ce (a file manager copy still attaches, pipe rows, tickets#49)mre
- 06599fa0tickets: MdEditor from components b0f1ec2 (a clipboard with text always pastes the text, tickets#49); real Ctrl+V gatemre
- 2e1b116ftickets: re-vendor Hybriel master acaceaf3 (framework ec8d6c1d): plugins/web patch queue while a page is built, anonymous session cookies refresh; gate waits for the comment editors to draw (W174) and clicks the form editor when it stands still; security gate: main's code runs on its own binary, lazy-session cookie checksmre
- 361bc0fetickets: re-vendor Hybriel 7a684920 (WIP, not deployed); on Error exits 1 at startup; migrate passes its log by reference; gate checks follow lazy sessions and the 413 refusalmre
- 8a69999cident#28: /login/callback never signs out the session it finds; a valid code puts the user on the same sessionmre
- 337e9b21tickets#37: a comment's files are open to everyone who can read the commentmre
- 1800600ftickets: write the standby service out in full so the boot gate can read the main servicemre
- 8179d287tickets: standby service on its port plus 10 and a deploy that hands over through it without a gapmre
- 0398145ctickets: the table grid rule is marked functional for check-thememre
- 40fddc20tickets takes components e54f83a: tables get thin grey lines from the theme, desktop toolbar stays one row with the Table button, F4 check counts 11 buttonsmre
- c1b77d61tickets#35: text typed before the editor is built is taken in once by the build (the input handler no longer writes it to the field first, which doubled the first letters); the gate failure shows the surfacemre
- 32bf1873tickets#35: the gate types right after the load with the CPU slowed, no waitingmre
- 64ea9ca2tickets#35: text typed into the reply editor before the page has finished starting is kept; the gate types right after loadmre
- ccd3a327tickets: the security gate expects frame-src to be exactly the ident address (the sign-in frame), still no wildcard or selfmre
- 4348dbabident#31: the selector's sign-in returns to the page the user started on (return attribute = the login callback with next), gate checkmre
- 66ef3005Merge branch 'iw-ident-widget'mre