gitoria

gitoria

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit097c52ba097c52baStyle the login error through its View idmre097c52ba/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

  • 097c52baStyle the login error through its View idmre
  • c6d9c86aOwn the not-found code rule in its componentmre
  • 055424c2Record W61 replacement theme gates, sidebar repro and baselinemre
  • 2ed09441Use layouts theme 17e0f03 for W61 compatibilitymre
  • b8609a91Merge branch 'main' into w61-stylemre
  • 4639caf7Report W61 theme startup blocker and day39 Style probemre
  • 81f0a0bbREADME: two caches were restored by 90b4f72; log W62mre
  • 4a3f557eImport layouts theme ebd00b6 for W61 (startup blocked)mre
  • ab12a257Merge W62: #25 commit page measurements and proc repromre
  • 41323e49gitoria#25: measure commit request costs and reproduce blocking process waitsmre
  • 53ca42b2log: W60 merged, W61 W62 startedmre
  • 80870a33mission W61: style passmre
  • 560fd208Merge W60: helpers, bound forms, login in topics, two form fixes (gitoria#28 #32 #33)mre
  • ac5ba6a4log: W60, #25 measuredmre
  • 220f6342W60: record corrected form proof, full AE and remaining gapsmre
  • 7e31cac5gitoria#28: restrict reply form layout to reply formsmre
  • ae373cfdgitoria#28: keep each reply draft in its thread componentmre
  • eeeebd5agitoria: insert a newly created organization oncemre
  • 34d2d5cagitoria: clear the bound public-key textarea after creationmre
  • 119e54c0W60: restore conductor-owned tracking filesmre