gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit1986821819868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre19868218/components/apps.hl

8.6 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. import { leadLook, messageLook, noticeLook } from './styles.hl'
  14. session = null
  15. me = accountOfSession(&session)
  16. signedIn = me != null
  17. signedOut = me == null
  18. meEmail = me != null ? me.email : ''
  19. apps = me != null ? appRows(me.id) : []
  20. hasApps = apps.length > 0
  21. noApps = apps.length == 0
  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. Style {
  36. pLead = leadLook
  37. pMessage = messageLook
  38. pNotice = noticeLook
  39. appMain {
  40. flex = '1 1 14rem'
  41. display = 'grid'
  42. gap = '.15rem'
  43. minWidth = '0'
  44. }
  45. appName {
  46. fontWeight = '700'
  47. color = 'var(--color-text-strong)'
  48. overflowWrap = 'anywhere'
  49. }
  50. appOrigins {
  51. color = 'var(--color-text-muted)'
  52. fontSize = '.875rem'
  53. overflowWrap = 'anywhere'
  54. }
  55. appKey {
  56. fontSize = '.875rem'
  57. color = 'var(--color-text-muted)'
  58. overflowWrap = 'anywhere'
  59. }
  60. // the secret, shown once
  61. secretBox {
  62. display = 'grid'
  63. gap = '.6rem'
  64. padding = '1rem'
  65. background = 'var(--color-surface)'
  66. borderRadius = 'var(--radius-large)'
  67. h2 {
  68. color = 'var(--color-warning)'
  69. }
  70. code {
  71. display = 'block'
  72. padding = '.5rem .6rem'
  73. background = 'var(--color-background-sunken)'
  74. userSelect = 'all'
  75. }
  76. }
  77. }
  78. View {
  79. identCard {
  80. if (signedOut) {
  81. h1 { "Your apps" }
  82. p { id = "signinfirst" Style.pLead "Sign in to ident first: " a { href = "/" "sign in" } "." }
  83. }
  84. if (signedIn) {
  85. accountBar {
  86. userEmail { id = "meemail" meEmail }
  87. a { id = "toidentities" href = "/" "Your identities" }
  88. }
  89. if (showSecret) {
  90. secretBox { id = "secretbox"
  91. h2 { "Secret of " secretFor }
  92. p { Style.pLead "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." }
  93. code { id = "secret" secret }
  94. button { id = "secretdone" type = "button" class = "small" "I copied it" on click(e) { emit hideSecret(e) } }
  95. }
  96. }
  97. if (editing) {
  98. form { id = "appform"
  99. on submit(e) {
  100. e.preventDefault()
  101. emit saveApp(e)
  102. }
  103. h1 { id = "appformheading" editHeading }
  104. label { "Name"
  105. input { id = "fappname" name = "name" maxlength = "60" autocomplete = "off" required = "required" value = fName on input(e) { emit setName(e.target.value) } }
  106. }
  107. label { "Origins " fieldHint { "(where the app runs, like https://tickets.worldapi.org — several separated by spaces; the login button only returns there)" }
  108. input { id = "forigins" name = "origins" autocomplete = "off" required = "required" value = fOrigins on input(e) { emit setOrigins(e.target.value) } }
  109. }
  110. formButtons {
  111. button { id = "saveapp" type = "submit" "Save" }
  112. button { id = "cancelapp" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } }
  113. }
  114. }
  115. }
  116. section { id = "appssection"
  117. sectionHead {
  118. h1 { "Your apps" }
  119. button { id = "newapp" type = "button" class = "small" "Register an app" on click(e) { emit openNew(e) } }
  120. }
  121. if (noApps) { p { id = "noapps" Style.pLead "No apps yet. An app you register gets an API key for its “login with ident” button." } }
  122. ul { id = "apps"
  123. for (row of apps) {
  124. li {
  125. appMain {
  126. appName { row.name }
  127. appOrigins { row.originsText }
  128. appKey { "API key " code { row.apiKey } }
  129. }
  130. identityActions {
  131. button { type = "button" class = "quiet small" name = "edit" value = row.id "Edit" on click(e) { emit openEdit(e.target.value) } }
  132. button { type = "button" class = "quiet small" name = "newsecret" value = row.id "New secret" on click(e) { emit renew(e.target.value) } }
  133. button { type = "button" class = "quiet small danger" name = "delete" value = row.id "Delete" on click(e) { emit remove(e.target.value) } }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. p { id = "notice" Style.pNotice notice }
  141. p { id = "message" Style.pMessage message }
  142. }
  143. }
  144. on setName(v) { fName = v }
  145. on setOrigins(v) { fOrigins = v }
  146. rowOf = (id) => {
  147. for (row of apps) { if ('' + row.id == '' + id) { return row } }
  148. return null
  149. }
  150. // the origins field: words separated by spaces or commas
  151. originList = (text) => {
  152. out = []
  153. for (w of text.replaceAll(',', ' ').split(' ')) {
  154. let t = w.trim()
  155. if (t != '') { out.push(t) }
  156. }
  157. return out
  158. }
  159. // the list changed by one row (upsert / remove): the empty-list line and the count follow
  160. appsChanged = () => {
  161. hasApps = apps.length > 0
  162. noApps = apps.length == 0
  163. return null
  164. }
  165. on openNew(e) {
  166. message = ''
  167. notice = ''
  168. editId = ''
  169. fName = ''
  170. fOrigins = ''
  171. editHeading = 'Register an app'
  172. editing = true
  173. }
  174. on openEdit(id) {
  175. message = ''
  176. notice = ''
  177. row = rowOf(id)
  178. if (row == null) { return null }
  179. editId = row.id
  180. fName = row.name
  181. fOrigins = row.originsText
  182. editHeading = 'Edit ' + row.name
  183. editing = true
  184. }
  185. on closeForm(e) {
  186. editing = false
  187. message = ''
  188. }
  189. on hideSecret(e) {
  190. showSecret = false
  191. secret = ''
  192. secretFor = ''
  193. }
  194. on saveApp(e) {
  195. message = ''
  196. notice = ''
  197. input = { name = fName origins = originList(fOrigins) }
  198. let r = null
  199. if (editId == '') {
  200. r = emit server appCreate(input)
  201. } else {
  202. r = emit server appUpdate('' + editId, input)
  203. }
  204. if (r == null) {
  205. message = 'the server did not answer — try again'
  206. return null
  207. }
  208. if (r.error != null) {
  209. message = r.error
  210. return null
  211. }
  212. apps.upsert(r.app)
  213. appsChanged()
  214. editing = false
  215. if (r.secret != null) {
  216. secretFor = r.app.name
  217. secret = r.secret
  218. showSecret = true
  219. notice = 'App registered.'
  220. } else {
  221. notice = 'App saved.'
  222. }
  223. }
  224. on renew(id) {
  225. message = ''
  226. notice = ''
  227. row = rowOf(id)
  228. if (row == null) { return null }
  229. if (!confirm('Make a new secret for “' + row.name + '”? The current one stops working at once.')) { return null }
  230. r = emit server appNewSecret('' + id)
  231. if (r == null) {
  232. message = 'the server did not answer — try again'
  233. return null
  234. }
  235. if (r.error != null) {
  236. message = r.error
  237. return null
  238. }
  239. apps.upsert(r.app)
  240. appsChanged()
  241. secretFor = r.app.name
  242. secret = r.secret
  243. showSecret = true
  244. notice = 'New secret made.'
  245. }
  246. on remove(id) {
  247. message = ''
  248. notice = ''
  249. row = rowOf(id)
  250. if (row == null) { return null }
  251. if (!confirm('Delete the app “' + row.name + '”? Its API key stops working and the ids its users had in it are gone.')) { return null }
  252. r = emit server appDelete('' + id)
  253. if (r == null) {
  254. message = 'the server did not answer — try again'
  255. return null
  256. }
  257. if (r.error != null) {
  258. message = r.error
  259. return null
  260. }
  261. apps.remove('' + id)
  262. appsChanged()
  263. if ('' + editId == '' + id) { editing = false }
  264. showSecret = false
  265. secret = ''
  266. notice = 'App deleted.'
  267. }
  268. // ---- the faces ------------------------------------------------------------------------
  269. // The LAST parameter is the session (the framework appends it); accountOfSession only
  270. // gets the framework's own Session (hl:web never takes it from the peer, hybriel#16/#31).
  271. on server appCreate(input, session) {
  272. let me = accountOfSession(&session)
  273. if (me == null) { return { error = 'you are not signed in' } }
  274. r = createApp(me.id, input)
  275. if (r.error != null) { return r }
  276. return { app = r.app secret = r.secret }
  277. }
  278. on server appUpdate(id, input, session) {
  279. let me = accountOfSession(&session)
  280. if (me == null) { return { error = 'you are not signed in' } }
  281. r = updateApp(me.id, id, input)
  282. if (r.error != null) { return r }
  283. return { app = r.app }
  284. }
  285. on server appNewSecret(id, session) {
  286. let me = accountOfSession(&session)
  287. if (me == null) { return { error = 'you are not signed in' } }
  288. r = regenerateSecret(me.id, id)
  289. if (r.error != null) { return r }
  290. return { app = r.app secret = r.secret }
  291. }
  292. on server appDelete(id, session) {
  293. let me = accountOfSession(&session)
  294. if (me == null) { return { error = 'you are not signed in' } }
  295. r = deleteApp(me.id, id)
  296. if (r.error != null) { return r }
  297. return { deleted = r.deleted }
  298. }

Branches

Latest commits

  • 19868218|||PR ident#39 shell reads its session member instead of a cross-file emitmre
  • 5d1b3fedMerge branch 'v61-day61' into mainIdent
  • 83faeb15|||PR ident: vendor Hybriel day61mre
  • b2572b34Merge branch 'v59-day59' into mainIdent
  • 67cf016d|||PR ident: vendor Hybriel day59mre
  • 600c043dMerge branch 'v58-day58' into mainIdent
  • 365f8c80|||PR ident: vendor Hybriel day58mre
  • 585126e5Merge branch 'f16-setf' into mainIdent
  • 953bf03a|||PR ident setF mirror dropped (hybriel#370)mre
  • a1419d9cMerge branch 'f15-navigate' into mainIdent
  • 3b3e451f|||PR ident#36 navigate to the app instead of location.assignmre
  • e36f4516Merge branch 'f14-online' into mainIdent
  • 2c5eac91|||PR ident#38 online state without probe timermre
  • bf1980f2Merge branch 'v56-day56' into mainIdent
  • 62ae0adf|||PR ident: vendor Hybriel day56mre
  • 0d18abc4Merge branch 'f13-avatar' into mainIdent
  • 2a99730a|||PR ident#37 avatar in hlmre
  • fa4ed25bMerge branch 'f11-upsert' into mainIdent
  • 8f3d9bc9|||PR ident#42 keyed upsert and removemre
  • 675493b1ident#42: editing an identity upserts the one edited row (face editIdentity answers { identity, row }); a create or delete keeps the whole list because the rows' position-dependent parts (default, Delete, Identity n) changemre