gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit97f77b0797f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre97f77b07/components/apps.hl

7.8 KB

  1. // components/apps.hl — `/apps`: THE ACCOUNT'S APPS (piece 2 of 6, ticket #25; CONCEPT.md
  2. // "Apps: any ident user can register apps. An app gets an API key.").
  3. // Register (name + the origins it runs on), list, edit, new secret, delete. An app has a
  4. // public API key and a secret for its server; the secret is shown ONCE, right after it is
  5. // made — ident stores only its sha256.
  6. //
  7. // The faces' names start with `app…`: a face is found by its EVENT NAME across all
  8. // files (hl:web), so they must not collide with home.hl's.
  9. parent './main.hl'
  10. import { accountOfSession } from '../lib/accounts.hl'
  11. import { appRows, createApp, updateApp, regenerateSecret, deleteApp } from '../lib/apps.hl'
  12. import { siteName } from '../project.hl'
  13. session = null
  14. me = accountOfSession(session)
  15. signedIn = me != null
  16. signedOut = me == null
  17. meEmail = me != null ? me.email : ''
  18. apps = me != null ? appRows(me.id) : []
  19. hasApps = apps.length > 0
  20. noApps = apps.length == 0
  21. current = '/apps'
  22. __title = siteName + ' | your apps'
  23. // THE APP FORM — new and edit
  24. editing = false
  25. editId = '' // '' = a new app, else the app's id (a UUID)
  26. editHeading = ''
  27. fName = ''
  28. fOrigins = ''
  29. // THE SECRET, shown once
  30. showSecret = false
  31. secretFor = ''
  32. secret = ''
  33. message = ''
  34. notice = ''
  35. View {
  36. identCard {
  37. if (signedOut) {
  38. h1 { "Your apps" }
  39. p { id = "signinfirst" class = "lead" "Sign in to ident first: " a { href = "/" "sign in" } "." }
  40. }
  41. if (signedIn) {
  42. accountBar {
  43. userEmail { id = "meemail" meEmail }
  44. a { id = "toidentities" href = "/" "Your identities" }
  45. }
  46. if (showSecret) {
  47. secretBox { id = "secretbox"
  48. h2 { "Secret of " secretFor }
  49. p { class = "lead" "Shown only now — copy it to your app's server. Its API calls (the code exchange) use it; it never goes into a web page. Lost it? Make a new one." }
  50. code { id = "secret" secret }
  51. button { id = "secretdone" type = "button" class = "small" "I copied it" on click(e) { emit hideSecret(e) } }
  52. }
  53. }
  54. if (editing) {
  55. form { id = "appform"
  56. on submit(e) {
  57. e.preventDefault()
  58. emit saveApp(e)
  59. }
  60. h1 { id = "appformheading" editHeading }
  61. label { "Name"
  62. input { id = "fappname" name = "name" maxlength = "60" autocomplete = "off" required = "required" value = fName on input(e) { emit setName(e.target.value) } }
  63. }
  64. label { "Origins " fieldHint { "(where the app runs, like https://tickets.worldapi.org — several separated by spaces; the login button only returns there)" }
  65. input { id = "forigins" name = "origins" autocomplete = "off" required = "required" value = fOrigins on input(e) { emit setOrigins(e.target.value) } }
  66. }
  67. formButtons {
  68. button { id = "saveapp" type = "submit" "Save" }
  69. button { id = "cancelapp" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } }
  70. }
  71. }
  72. }
  73. section { id = "appssection"
  74. sectionHead {
  75. h1 { "Your apps" }
  76. button { id = "newapp" type = "button" class = "small" "Register an app" on click(e) { emit openNew(e) } }
  77. }
  78. if (noApps) { p { id = "noapps" class = "lead" "No apps yet. An app you register gets an API key for its “login with ident” button." } }
  79. ul { id = "apps"
  80. for (row of apps) {
  81. li { class = "app"
  82. appMain {
  83. appName { row.name }
  84. appOrigins { row.originsText }
  85. appKey { "API key " code { class = "apikey" row.apiKey } }
  86. }
  87. identityActions {
  88. button { type = "button" class = "quiet small edit" value = row.id "Edit" on click(e) { emit openEdit(e.target.value) } }
  89. button { type = "button" class = "quiet small newsecret" value = row.id "New secret" on click(e) { emit renew(e.target.value) } }
  90. button { type = "button" class = "quiet small danger delete" value = row.id "Delete" on click(e) { emit remove(e.target.value) } }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. }
  97. p { id = "notice" class = "notice" notice }
  98. p { id = "message" class = "message" message }
  99. }
  100. }
  101. on setName(v) { fName = v }
  102. on setOrigins(v) { fOrigins = v }
  103. rowOf = (id) => {
  104. for (row of apps) { if ('' + row.id == '' + id) { return row } }
  105. return null
  106. }
  107. // the origins field: words separated by spaces or commas
  108. originList = (text) => {
  109. out = []
  110. for (w of text.replaceAll(',', ' ').split(' ')) {
  111. let t = w.trim()
  112. if (t != '') { out.push(t) }
  113. }
  114. return out
  115. }
  116. setApps = (list) => {
  117. apps = list
  118. hasApps = list.length > 0
  119. noApps = list.length == 0
  120. return null
  121. }
  122. on openNew(e) {
  123. message = ''
  124. notice = ''
  125. editId = ''
  126. fName = ''
  127. fOrigins = ''
  128. editHeading = 'Register an app'
  129. editing = true
  130. }
  131. on openEdit(id) {
  132. message = ''
  133. notice = ''
  134. row = rowOf(id)
  135. if (row == null) { return null }
  136. editId = row.id
  137. fName = row.name
  138. fOrigins = row.originsText
  139. editHeading = 'Edit ' + row.name
  140. editing = true
  141. }
  142. on closeForm(e) {
  143. editing = false
  144. message = ''
  145. }
  146. on hideSecret(e) {
  147. showSecret = false
  148. secret = ''
  149. secretFor = ''
  150. }
  151. on saveApp(e) {
  152. message = ''
  153. notice = ''
  154. input = { name = fName origins = originList(fOrigins) }
  155. let r = null
  156. if (editId == '') {
  157. r = emit server appCreate(input)
  158. } else {
  159. r = emit server appUpdate('' + editId, input)
  160. }
  161. if (r == null) {
  162. message = 'the server did not answer — try again'
  163. return null
  164. }
  165. if (r.error != null) {
  166. message = r.error
  167. return null
  168. }
  169. setApps(r.apps)
  170. editing = false
  171. if (r.secret != null) {
  172. secretFor = r.app.name
  173. secret = r.secret
  174. showSecret = true
  175. notice = 'App registered.'
  176. } else {
  177. notice = 'App saved.'
  178. }
  179. }
  180. on renew(id) {
  181. message = ''
  182. notice = ''
  183. row = rowOf(id)
  184. if (row == null) { return null }
  185. if (!confirm('Make a new secret for “' + row.name + '”? The current one stops working at once.')) { return null }
  186. r = emit server appNewSecret('' + id)
  187. if (r == null) {
  188. message = 'the server did not answer — try again'
  189. return null
  190. }
  191. if (r.error != null) {
  192. message = r.error
  193. return null
  194. }
  195. setApps(r.apps)
  196. secretFor = r.app.name
  197. secret = r.secret
  198. showSecret = true
  199. notice = 'New secret made.'
  200. }
  201. on remove(id) {
  202. message = ''
  203. notice = ''
  204. row = rowOf(id)
  205. if (row == null) { return null }
  206. if (!confirm('Delete the app “' + row.name + '”? Its API key stops working and the ids its users had in it are gone.')) { return null }
  207. r = emit server appDelete('' + id)
  208. if (r == null) {
  209. message = 'the server did not answer — try again'
  210. return null
  211. }
  212. if (r.error != null) {
  213. message = r.error
  214. return null
  215. }
  216. setApps(r.apps)
  217. if ('' + editId == '' + id) { editing = false }
  218. showSecret = false
  219. secret = ''
  220. notice = 'App deleted.'
  221. }
  222. // ---- the faces ------------------------------------------------------------------------
  223. // The LAST parameter is the session (the framework appends it); accountOfSession only
  224. // gets the framework's own Session (hl:web never takes it from the peer, hybriel#16/#31).
  225. on server appCreate(input, session) {
  226. let me = accountOfSession(session)
  227. if (me == null) { return { error = 'you are not signed in' } }
  228. r = createApp(me.id, input)
  229. if (r.error != null) { return r }
  230. return { app = r.app secret = r.secret apps = appRows(me.id) }
  231. }
  232. on server appUpdate(id, input, session) {
  233. let me = accountOfSession(session)
  234. if (me == null) { return { error = 'you are not signed in' } }
  235. r = updateApp(me.id, id, input)
  236. if (r.error != null) { return r }
  237. return { app = r.app apps = appRows(me.id) }
  238. }
  239. on server appNewSecret(id, session) {
  240. let me = accountOfSession(session)
  241. if (me == null) { return { error = 'you are not signed in' } }
  242. r = regenerateSecret(me.id, id)
  243. if (r.error != null) { return r }
  244. return { app = r.app secret = r.secret apps = appRows(me.id) }
  245. }
  246. on server appDelete(id, session) {
  247. let me = accountOfSession(session)
  248. if (me == null) { return { error = 'you are not signed in' } }
  249. r = deleteApp(me.id, id)
  250. if (r.error != null) { return r }
  251. return { deleted = r.deleted apps = appRows(me.id) }
  252. }

Branches

Latest commits

  • 97f77b07ident mission 012 (WIP): onto the layouts theme dbe3de7, shell on AppHeader/MainMenu/UserMenu/Sidebar + ContentFirst, styles cut, check-theme gatemre
  • 8e4f435bpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • fa288f63ident mission 011: report wording (Intl zone names)mre
  • 961fae01ident mission 011: report, LOG, STATUSmre
  • 65348039ident#7: delivery gate — console check split at the gate's own server restart (49 checks)mre
  • dfa7b783ident#7: README (Delivery, env, gate, files, data), deploy.sh runs tests/delivery.mjsmre
  • ab6331f4ident#7: tests/delivery.mjs — fake clock gate: Vienna/New York/Tokyo/UTC 17:00, urgent, mail off, restart, DST days, zone changemre
  • 7869f57bident#7: delivery — the daily mail at 17:00 in the user's zone (digests table, once per local day, test clock IDENT_TEST_CLOCK), urgent mailed at once, push hookmre
  • 371ef1b5ident#7: lib/mail.hl sendNotificationMail (sink: <sink>.mail, one JSON line per mail)mre
  • 39749e0bident#7: time zones through hl:time — server checks isZone, an account without a known zone reads as UTC, allAccountsmre
  • 7a54fe72mission brief 011mre
  • a6a874cdident mission 010: vendor hybriel 0f396652 (plugins + compose cache volume/tmpfs + deploy URL wait + report)mre
  • 4e7cf78emission brief 010mre
  • e8b2b0f7docker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 5fdbb6b2ident mission 009: report — scratch folder notemre
  • 74235815ident mission 009 (4/4): docs (README files map + same-output test, STATUS, LOG), report, tests/letcount.py + realdata-baseline/comparemre
  • fe183516ident mission 009 (3/4): let only where reassigned — 293 never-reassigned lets are plain declarations; kept: reassigned, loop bodies, names of a file member, a name declared twice in one function; same outputmre
  • d2e7f91bident mission 009 (2/4): one lib file per topic (login, accounts, identities, apps, invites, selector, notify + helpers, util), function routes as thin wrappers in lib/api.hl, project.hl = the map; same outputmre
  • 91017164ident mission 009 (1/4): file moves — the root .hl files into lib/ (api.hl → lib/api-helpers.hl), styles.hl → components/styles.hl; imports adjusted, no other changemre
  • f8bdcbc2ident: Hybriel master 06617221 (plugin allocators 3a781359 + 413f60e4, mpackdb 2cb7ae5e, http1 773de63e); all gates greenmre