gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit991641d4991641d4Merge branch 'f10-access' into mainIdent991641d4/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. setApps = (list) => {
  160. apps = list
  161. hasApps = list.length > 0
  162. noApps = list.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. setApps(r.apps)
  213. editing = false
  214. if (r.secret != null) {
  215. secretFor = r.app.name
  216. secret = r.secret
  217. showSecret = true
  218. notice = 'App registered.'
  219. } else {
  220. notice = 'App saved.'
  221. }
  222. }
  223. on renew(id) {
  224. message = ''
  225. notice = ''
  226. row = rowOf(id)
  227. if (row == null) { return null }
  228. if (!confirm('Make a new secret for “' + row.name + '”? The current one stops working at once.')) { return null }
  229. r = emit server appNewSecret('' + id)
  230. if (r == null) {
  231. message = 'the server did not answer — try again'
  232. return null
  233. }
  234. if (r.error != null) {
  235. message = r.error
  236. return null
  237. }
  238. setApps(r.apps)
  239. secretFor = r.app.name
  240. secret = r.secret
  241. showSecret = true
  242. notice = 'New secret made.'
  243. }
  244. on remove(id) {
  245. message = ''
  246. notice = ''
  247. row = rowOf(id)
  248. if (row == null) { return null }
  249. if (!confirm('Delete the app “' + row.name + '”? Its API key stops working and the ids its users had in it are gone.')) { return null }
  250. r = emit server appDelete('' + id)
  251. if (r == null) {
  252. message = 'the server did not answer — try again'
  253. return null
  254. }
  255. if (r.error != null) {
  256. message = r.error
  257. return null
  258. }
  259. setApps(r.apps)
  260. if ('' + editId == '' + id) { editing = false }
  261. showSecret = false
  262. secret = ''
  263. notice = 'App deleted.'
  264. }
  265. // ---- the faces ------------------------------------------------------------------------
  266. // The LAST parameter is the session (the framework appends it); accountOfSession only
  267. // gets the framework's own Session (hl:web never takes it from the peer, hybriel#16/#31).
  268. on server appCreate(input, session) {
  269. let me = accountOfSession(&session)
  270. if (me == null) { return { error = 'you are not signed in' } }
  271. r = createApp(me.id, input)
  272. if (r.error != null) { return r }
  273. return { app = r.app secret = r.secret apps = appRows(me.id) }
  274. }
  275. on server appUpdate(id, input, session) {
  276. let me = accountOfSession(&session)
  277. if (me == null) { return { error = 'you are not signed in' } }
  278. r = updateApp(me.id, id, input)
  279. if (r.error != null) { return r }
  280. return { app = r.app apps = appRows(me.id) }
  281. }
  282. on server appNewSecret(id, session) {
  283. let me = accountOfSession(&session)
  284. if (me == null) { return { error = 'you are not signed in' } }
  285. r = regenerateSecret(me.id, id)
  286. if (r.error != null) { return r }
  287. return { app = r.app secret = r.secret apps = appRows(me.id) }
  288. }
  289. on server appDelete(id, session) {
  290. let me = accountOfSession(&session)
  291. if (me == null) { return { error = 'you are not signed in' } }
  292. r = deleteApp(me.id, id)
  293. if (r.error != null) { return r }
  294. return { deleted = r.deleted apps = appRows(me.id) }
  295. }

Branches

Latest commits

  • 991641d4Merge branch 'f10-access' into mainIdent
  • e56d8273|||PR ident#34 part 4: access and inbox fixed classesmre
  • df7696dbident#34: tests: access ticks selector quoting fixmre
  • 7215903eident#34: access: the unstyled classes all, app, tick, apptick, plus are dropped; the gate finds the app ticks by input[name=app]mre
  • 69b0a0ceident#34: access: the pick buttons use Style.pickCard instead of class pickmre
  • dc702419Merge branch 'f9-choose' into mainIdent
  • 8caec771|||PR ident#34 restore .choose hookmre
  • 0dbff960Merge branch 'f8-shared' into mainIdent
  • d69665f6|||PR ident#34 part 3: shared look classes (wide, avatar, chip/used, conn, choose, who, inboxlink) moved to statics or droppedmre
  • 3a64dc48ident#34: chipLook: radius as the same var() string (a bare token name in a static does not compile); selector gate selectormre
  • eb039073ident#34: tests: signin and migration find the choice without the classmre
  • 4b94a594ident#34: tests: fix selector of the identity choicemre
  • bebc88e4ident#34: main.hl drops the unstyled classes inboxlink and whomre
  • b4e7ad04ident#34: the identity choice link/button has no class (rule on #chooselist a)mre
  • 3c33d6eaident#34: the connection link in inbox has no class (rule on #conns a)mre
  • dc97ec0dident#34: the Notif. chip and the Used in link share chipLook (Style.pChip) instead of the classes chip/used/notifmre
  • 378bd432ident#34: the identity picture look is a static in styles.hl (Style.pAvatar); sizes stay with each listmre
  • 1cf79e7eident#34: the wide page column is a static in styles.hl (Style.pWide), not the class "wide"mre
  • ddc7d135Merge branch 'f7-hooks' into mainIdent
  • eee0b513|||PR ident#34 part: test-hook classes replaced by name/element/attribute selectors (tests adjusted), the shared lead/message/notice looks are statics in styles.hlmre