gitoria
All repositories: gitoria
3.0 KB
// lib/tokens.hl — ACCESS TOKENS for git over HTTPS (ticket gitoria#7; docs/git-backend.md "Credentials"). A developer's// git client cannot do the ident login, so a logged-in user makes a token here and gives it to git as the PASSWORD// (the user name is ignored). Git's HTTP Basic login is not deprecated by git itself; GitHub's ban on account// passwords is GitHub's. Only the sha256 of a token is stored: the token is shown ONCE, at its creation.// tokensTable pk @id index !hash, user { user (users @id), name, hash, created } storage/mpackdb/tokens.db// A token is `gtr_` + 40 hex characters from the kernel's random source (160 bits — the fast sha256 is enough).import { MPackDB } from 'hl:mpackdb'import { now } from 'hl:time'import { randomBytes, sha256 } from 'hl:crypto'import { storageDir, countOfList, firstOf, plainError, isHex, newestFirst, localStamp } from './util.hl'import { userRecord } from './users.hl'static tokensTable = new MPackDB(file = storageDir + '/tokens.db', primaryKey = '@id', indexes = ['!hash', 'user'])static maxTokens = 20static rowOfToken = (t) => { return { @id = t.id name = t.name created = localStamp(t.created) createdMs = t.created } }// a user's tokens, newest first — never the token itself, only what identifies it to the ownerstatic tokenRows = (userId) => {out = []all = tokensTable.find('user', userId)if (countOfList(all) == 0) { return out }for (t of all) { out.push(rowOfToken(t)) }return newestFirst(out)}// a new token: { token (the secret, this once), row } or { error }static createToken = (userId, name) => {u = userRecord(userId)if (u == null) { return { error = 'log in with ident (top right) first' } }n = name == null ? '' : ('' + name).trim()bad = plainError(n, 60, 'the token name')if (bad != null) { return { error = bad } }if (n == '') { return { error = 'give the token a name (for example the computer it is for)' } }if (countOfList(tokensTable.find('user', u.id)) >= maxTokens) { return { error = 'you have ' + maxTokens + ' tokens already — remove one first' } }secret = 'gtr_' + randomBytes(20, 'hex')id = tokensTable.put({ user = u.id name = n hash = sha256(secret) created = now() })if (id == null) { return { error = 'could not store the token: ' + tokensTable.lastError() } }return { token = secret row = rowOfToken(tokensTable.fetch(id)) }}// remove one of the user's own tokensstatic revokeToken = (userId, tokenId) => {if (userId == null || tokenId == null || hlTypeName(tokenId) != 'String') { return { error = 'no such token' } }t = tokensTable.fetch(tokenId)if (t == null || t.user != userId) { return { error = 'no such token' } }tokensTable.delete(tokenId)return { ok = true }}// the user a token belongs to (users @id) or null; anything that is not shaped like a token is null without a lookupstatic userOfToken = (secret) => {if (secret == null || hlTypeName(secret) != 'String' || secret.length != 44 || !secret.startsWith('gtr_') || !isHex(secret.slice(4), 40)) { return null }t = firstOf(tokensTable.find('hash', sha256(secret)))return t == null ? null : t.user}
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
- 477d7bdcMerge branch 'p50'mre
- 00efc06fP50 reportmre
- e3f85afblog: commit mention slipmre
- 4d1a4cf4log: #25 numbersmre
- 1af0a60ddocs: H25 run() correctionmre
- eefa2dbcRepo setting: refuse direct pushes to the main branch (pre-receive over HTTPS and ssh)mre
- 8c8ba2f1log: PR flow announcedmre
- fa00cd35log: Kunibert pings provenmre
- 3a68cfaclog: components decisionmre
- 87de8e31log: Kunibert connected to 9 app reposmre
- c22aff69log: PR-only rollout startedmre
- 25309981log: STOP and paused workers 2026-11-08mre
- 5286803alog: worker briefs and reports of 2026-11-08mre
- 85c86605log: 2026-11-08 Arachne on list shapemre
- cb634512log: 2026-11-08 keyed lists livemre
- 07c98d4aLists: keyed rows with @id, remove(id) and one whole-list assignment instead of hand-rebuilt loopsmre
- 0db0d81elog: 2026-11-08 day52 live, W30 startedmre
- b05f6d92tests/status: the gate's 3 s app token expired during the restart; it lives 60 s like status-keyedmre
- 1e8f4f0avendor: Hybriel set-day52 (fault-path leak fix #335, keyed rows #331)mre
- f5f0fb7blog: 2026-11-08 day50/#29 live, H25 correction, V52 startedmre