gitoria
All repositories: gitoria
7.6 KB
// lib/users.hl — WHO MAY CREATE (ticket gitoria#6; CONCEPT.md "login via ident"). Reading is public;// creating a repo needs a login through ident. Login is ident's LOGIN BUTTON flow (ident README// "How apps use ident", way 2): <ident>/login?key=&return=<public url>/login/callback → ?ident_code=// → the server exchanges it (key + secret) for the per-app identity id. Way 3, the IDENTITY SELECTOR// (gitoria#14, ident README "The identity selector"), sits beside the button; ident checks its caller's origin// against the app's registered origins, and a repo address (<slug>.<domain>) is not one — so the selector shows// on the main address only (login.js) and the button works from every address (it returns through the main one).//// usersTable pk @id index !identity { identity, name, created } storage/mpackdb/users.db// identity = what ident's exchange answers: the identity's public SHORT id since ident#23 (`a68sz`; old 32-hex per-app// ids are rewritten once by tools/migrate-short-ids.hl) — stays SERVER SIDE, never sent to a page.// name = the display name asked once at the first login ('' until chosen); it is what// a repo shows as its owner.// The session (hl:web) carries `user = { id = <users @id> }` only.//// Config (environment, or `.env` beside project.hl — never read or printed by workers):// IDENT_URL, IDENT_EXCHANGE_URL, IDENT_API_KEY, IDENT_API_SECRET as in tickets// (GITORIA_PUBLIC_URL and GITORIA_STORAGE: lib/util.hl)import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { fetch } from 'hl:fetch'import { envOr, storageDir, publicUrl, scheme, hostPort, firstOf, plainError, isHex } from './util.hl'static identUrl = envOr('IDENT_URL', 'https://ident.worldapi.org')static identExchangeUrl = envOr('IDENT_EXCHANGE_URL', identUrl)static identKey = envOr('IDENT_API_KEY', '')static identSecret = envOr('IDENT_API_SECRET', '')static usersTable = new MPackDB(file = storageDir + '/users.db', primaryKey = '@id', indexes = ['!identity'])static selectorScript = identUrl + '/selector.js'static callbackUrl = publicUrl.replaceAll('/', '') == '' ? '' : scheme + '://' + hostPort + '/login/callback'static loginHref = identUrl + '/login?key=' + identKey + '&return=' + encodeURIComponent(callbackUrl)// an identity id as ident answers it: its public SHORT ID since ident#23 (5 characters like `a68sz`: 2-9 and a-z),// before that the old per-app id (32 hex) — lower case letters and digits, at most 64 (mission 039; isHex refused `a68sz`)static isIdentId = (s) => {if (s == null || hlTypeName(s) != 'String' || s.length == 0 || s.length > 64) { return false }let i = 0while (i < s.length) {c = s.charCodeAt(i)if (!((c >= 48 && c <= 57) || (c >= 97 && c <= 122))) { return false }i = i + 1}return true}// THE EXCHANGE: POST <ident>/api/exchange { key, secret, code } → { identity } | { error }// (a failed fetch is an `Error` event, absorbed by project.hl's `on Error`; the fetch then yields null)static exchangeCode = (code) => {if (identKey == '' || identSecret == '') { return { error = 'login is not set up on this server (IDENT_API_KEY / IDENT_API_SECRET missing)' } }if (!isHex(code, 200)) { return { error = 'that is not an ident login code' } }r = fetch(identExchangeUrl + '/api/exchange', { method = 'POST' json = { key = identKey secret = identSecret code = code } headers = { 'user-agent' = 'gitoria.worldapi.org (ident exchange)' } timeoutMs = 10000 })if (r == null || r.status == null || r.status == 0) { return { error = 'ident did not answer' } }j = r.status == 200 ? r.json() : nullif (j == null || j.identity == null || !isIdentId(j.identity)) {let why = ''if (r.status != 200) {e = r.json()why = e != null && e.error != null ? ': ' + e.error : ''}return { error = 'ident refused the login (' + r.status + why + ')' }}return { identity = j.identity }}// ---- users --------------------------------------------------------------------------------// a face's trailing `session` is always the server's since hybriel #16 (a peer's extra argument is refused)static userRecord = (userId) => {if (userId == null || hlTypeName(userId) != 'String' || userId == '') { return null }return usersTable.fetch(userId)}// the user of an identity id, made at its first login (name '' = not chosen yet)static ensureUser = (identity) => {u = firstOf(usersTable.find('identity', identity))if (u != null) { return u }id = usersTable.put({ identity = identity name = '' created = now() })if (id == null) { return null }return usersTable.fetch(id)}// what a page may know about a user: NEVER the identity idstatic userInfo = (u) => { return { name = u.name named = u.name != '' } }static userOfSession = (session) => {if (session == null || session.user == null) { return null }return userRecord(session.user.id)}// the users @id of a session's user, or null — for a page that must NOT carry the whole record (it holds the ident identity, which// never goes to a page: a page's values are sent to the browser, mission SG)static userIdOfSession = (session) => {u = userOfSession(session)return u == null ? null : u.id}static infoOfSession = (session) => {u = userOfSession(session)return u == null ? null : userInfo(u)}// the display name of an owner, 'someone' for a user without onestatic nameOfUser = (userId) => {u = userRecord(userId)if (u == null || u.name == '') { return 'someone' }return u.name}// the display name: 1–60 characters, one line, asked ONCEstatic setUserName = (userId, name) => {u = userRecord(userId)if (u == null) { return { error = 'not logged in' } }if (u.name != '') { return { error = 'your display name is already set' } }bad = plainError(name, 60, 'the display name')if (bad != null) { return { error = bad } }n = name.trim()if (n == '') { return { error = 'the display name must not be empty' } }usersTable.update(u.id, { id = u.id identity = u.identity name = n created = u.created })after = usersTable.fetch(u.id)if (after == null || after.name != n) { return { error = 'could not store the name: ' + usersTable.lastError() } }return { user = after }}// A LOGIN (the login button's return, lib/api.hl loginCallback, and the selector's face gitoriaLogin, components/main.hl):// ident's one-time code → { user } (made at the first login) or { error }. The caller puts the user into the session.static userOfLoginCode = (code) => {x = exchangeCode(code)if (x.error != null) { return { error = x.error } }u = ensureUser(x.identity)if (u == null) { return { error = 'could not store the user' } }return { user = u }}// the random tag of a session's login (session.data.tag, set at login): `gitoriaSignedIn` / `gitoriaSignedOut` reach the// tabs of the session that carries it (project.hl audience)static tagOf = (session) => { return session != null && session.data != null ? session.data.tag : null }// the users @ids with exactly this display name (member lists add people by name)static usersByName = (name) => {out = []if (name == null || name == '') { return out }all = usersTable.find(null, null)if (all == null || all.length == null) { return out }for (u of all) { if (u.name == name) { out.push(u.id) } }return out}// A repository API member names a display name or an ident public short id.// Ambiguous display names are refused, just like the web member form.static userForMember = (name) => {if (name == null || hlTypeName(name) != 'String' || name.trim() == '') { return null }n = name.trim()ids = usersByName(n)if (ids.length == 1) { return ids[0] }if (ids.length > 1) { return null }u = firstOf(usersTable.find('identity', n))return u == null ? null : u.id}
Branches
- f1-broken-json
- f2-api-bugs
- gp-gate-ports
- heap-revert
- hl25-records
- ident-chooser-selector
- mainmain branch
- p51-public-pr-reads
- records-1112
- s25-no-session-on-login-error
- v59
- w52-person-approval
- w52b-approval-followup
- w52c-header-phone
- w53-pulls-speed-pr
- w54-repo-rename-pr
- w55-rename-org
- w56-owner-members
- w62-pr-close
- w63-force-push
- w64-org-slash
Latest commits
- f4c9440ddeploy.sh: tar --hard-dereference for the live data backupmre
- 1a687b15Merge branch 'w48-member-merge'mre
- 166c775fW48 report, logmre
- 1b4623f0Members merge pull requests: API route, button on /pulls and /pulls/N, one shared merge pathmre
- 1075c38cW48 mission, logmre
- 17bdd0d6V37/V39 missions, logmre
- 146de5f8Re-vendor gitoria to Hybriel set-day39 (51e921d7)mre
- e874e44dtests/status-keyed: app token lives 60 s so it outlives the gate's two restartsmre
- 270b8d61deploy.sh: DEPLOY_STOP_START=1 stops, waits for the old pid, then starts (mpackdb freeze, replaces the standby swap)mre
- 3f9235b8project.hl: drop the dummy /pulls/:n component route (hybriel#294 is fixed in day37)mre
- c90daf19Re-vendor gitoria to Hybriel set-day37 (f6b3d45b)mre
- 03b5bb08W39 report to reports/, drop scratch repro, mission + logmre
- 154d333fMerge w39-create-api: POST /api/repos with a user token (#39)mre
- 149dd512Rewrite W39 reportmre
- e5385075Expect 401 for POST /api/repos without a tokenmre
- 1a76055cDocument repository creation APImre
- 4d779b3bAdd create-api gatemre
- 88563122Add POST /api/repos for user tokensmre
- b00860e3Create repositories with member checks in createRepoInmre
- 0a38f236Report repository creation API blockermre