gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita81abdc9a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmrea81abdc9/lib/tokens.hl

3.0 KB

  1. // lib/tokens.hl — ACCESS TOKENS for git over HTTPS (ticket gitoria#7; docs/git-backend.md "Credentials"). A developer's
  2. // git client cannot do the ident login, so a logged-in user makes a token here and gives it to git as the PASSWORD
  3. // (the user name is ignored). Git's HTTP Basic login is not deprecated by git itself; GitHub's ban on account
  4. // passwords is GitHub's. Only the sha256 of a token is stored: the token is shown ONCE, at its creation.
  5. // tokensTable pk @id index !hash, user { user (users @id), name, hash, created } storage/mpackdb/tokens.db
  6. // A token is `gtr_` + 40 hex characters from the kernel's random source (160 bits — the fast sha256 is enough).
  7. import { MPackDB } from 'hl:mpackdb'
  8. import { now } from 'hl:time'
  9. import { randomBytes, sha256 } from 'hl:crypto'
  10. import { storageDir, countOfList, firstOf, plainError, isHex, newestFirst, localStamp } from './util.hl'
  11. import { userRecord } from './users.hl'
  12. static tokensTable = new MPackDB(file = storageDir + '/tokens.db', primaryKey = '@id', indexes = ['!hash', 'user'])
  13. static maxTokens = 20
  14. static rowOfToken = (t) => { return { id = t.id name = t.name created = localStamp(t.created) createdMs = t.created } }
  15. // a user's tokens, newest first — never the token itself, only what identifies it to the owner
  16. static tokenRows = (userId) => {
  17. out = []
  18. all = tokensTable.find('user', userId)
  19. if (countOfList(all) == 0) { return out }
  20. for (t of all) { out.push(rowOfToken(t)) }
  21. return newestFirst(out)
  22. }
  23. // a new token: { token (the secret, this once), row } or { error }
  24. static createToken = (userId, name) => {
  25. u = userRecord(userId)
  26. if (u == null) { return { error = 'log in with ident (top right) first' } }
  27. n = name == null ? '' : ('' + name).trim()
  28. bad = plainError(n, 60, 'the token name')
  29. if (bad != null) { return { error = bad } }
  30. if (n == '') { return { error = 'give the token a name (for example the computer it is for)' } }
  31. if (countOfList(tokensTable.find('user', u.id)) >= maxTokens) { return { error = 'you have ' + maxTokens + ' tokens already — remove one first' } }
  32. secret = 'gtr_' + randomBytes(20, 'hex')
  33. id = tokensTable.put({ user = u.id name = n hash = sha256(secret) created = now() })
  34. if (id == null) { return { error = 'could not store the token: ' + tokensTable.lastError() } }
  35. return { token = secret row = rowOfToken(tokensTable.fetch(id)) }
  36. }
  37. // remove one of the user's own tokens
  38. static revokeToken = (userId, tokenId) => {
  39. if (userId == null || tokenId == null || hlTypeName(tokenId) != 'String') { return { error = 'no such token' } }
  40. t = tokensTable.fetch(tokenId)
  41. if (t == null || t.user != userId) { return { error = 'no such token' } }
  42. tokensTable.delete(tokenId)
  43. return { ok = true }
  44. }
  45. // the user a token belongs to (users @id) or null; anything that is not shaped like a token is null without a lookup
  46. static userOfToken = (secret) => {
  47. if (secret == null || hlTypeName(secret) != 'String' || secret.length != 44 || !secret.startsWith('gtr_') || !isHex(secret.slice(4), 40)) { return null }
  48. t = firstOf(tokensTable.find('hash', sha256(secret)))
  49. return t == null ? null : t.user
  50. }

Branches

Latest commits

  • a81abdc9gitoria#34: org page via function route, member-reachable orgs, selects, reportmre
  • 17f031f0gitoria#34: private repos, organizations, members, move tool, gatemre
  • 71b69343gitoria#23: the sidebar's Repositories link was a 404 after any in-page navigation (static menu re-evaluated publicUrl in the browser); the menu is an instance property now; gate taps it after a tab, folder, filemre
  • 90b4f728gitoria#25: restore the homepage cache and the parsed-blocks cache (they store data under immutable keys, no HTML); the page key, the framework readme key and the textHash key stay removedmre
  • a487248cgitoria#25: remove every cache that hid the page build (page key, Markdown render key, homepage cache, parsed-blocks cache) and the gate that only tested themmre
  • 12f2f4fegitoria: re-vendor Hybriel set-day27b, a startup fault in on Error exits 1mre
  • c21c084egitoria: write the standby service out in full so the boot gate can read the main servicemre
  • e307d69agitoria: say what is per process during the hand-overmre
  • f0cbe6a6gitoria: standby service on port 45014 and a deploy that hands over through it without a gapmre
  • 727cd083gitoria#25: README: vendor notes for Hybriel c1b1010f and the page cachemre
  • 61b72dedgitoria#25: re-vendor Hybriel master c1b1010f (W149 page cache), key the repo Readme page by slug and main commit, extend the readme-cache gatemre
  • 5b0b4b9ereadme-cache gate: use the security gate's ports when no own ports are givenmre
  • bc8f20d9gitoria#25: re-vendor Hybriel c09538bb with the W141 render cache, cache the homepage per commit, add the readme-cache gatemre
  • 9feebbd3Re-vendor Hybriel to ded47c13 (W145: Firefox start script fix)mre
  • 28ed41e5gitoria#24: one sign-in comes from hl:web batch 20, the /session/share workaround is gonemre
  • 3b2669e4gs: drop the unbuildable legacy check, fix pwa fetchmre
  • 2a2fdd77gs: C2 check before cookie experimentsmre
  • 76e0587aWIP gs 2mre
  • a80da7bdWIP gs: security gate for lazy sessions, g24 browser checksmre
  • 23fb05d8re-vendor Hybriel aaa39e69 (hl:web batch 20: login emit and Sessions.mint, tab follows a login, signed-in session cookie wins among duplicates and is re-issued with the Domain, W85 faster render, lazy sessions)mre