gitoria

ident

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf28333e4f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemref28333e4/components/apps.hl

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

Branches

Latest commits

  • f28333e4ident#33 ident#35: styles.hl keeps only the accent, the page column and the shared patterns; the account bar is written phone first (base rule + one min-width: 60rem block) instead of a max-width overridemre
  • 5b4541fcident#33: the selector sign-in frame's rules move into embed.hl's Stylemre
  • 00235f21ident#33: the per-app notification switches move into appsettings.hl's Stylemre
  • 2249945cident#33: the inbox and connection list rules move into inbox.hl's Stylemre
  • f2d9f79fident#33: the apps list and the secret box move into apps.hl's Stylemre
  • 8370b748ident#33: the app request box moves into home.hl's Stylemre
  • 40bf3b97ident#33: the 404 page's home link rule moves into notfound.hlmre
  • 40627a89ident#33: the time zone section's spacing moves into timezone.hlmre
  • d389db08ident#33: the identity page's rules and the Notif. chip move into identity.hl's Stylemre
  • ddda0b5bident#33: the identity row's parts move into identitylist.hl's Stylemre
  • 47e516afident#33 ident#34: the identity form's avatar rules move into identityform.hl; the preview and Remove button are hidden with the hidden attribute instead of built class strings (avatarPreviewClass, avatarRemoveClass gone)mre
  • 5cb5cf0eident#33: the code field's look moves into codestep.hl's Stylemre
  • f421cd60ident#33: the shell's own rules (user menu address, offline note, network probe) move into main.hl's Stylemre
  • c512fb96Merge branch 'f4-homesplit' into mainIdent
  • 07d1717c|||PR ident#40 home.hl is split into components, one per area: email step, identity form, identity list, app identity choice, time zone; each moves its faces with it, the email step is shared with embed.hl (#40.3, rest of #40.2)mre
  • 59e8fcc1ident#40.3: the app's identity choice is one component (components/chooseidentity.hl); its face chooseIdentity moves with itmre
  • e23efdfcident#40.3: the identity list is one component (components/identitylist.hl); its delete and face dropIdentity move with itmre
  • e24b59a6ident#40.3: the time zone is one component (components/timezone.hl); its face changeTimeZone moves with itmre
  • 8cbc5717ident#40.3: the email step is one component (components/emailstep.hl) placed by home.hl and embed.hlmre
  • 6fad3bd2Merge branch 'f3-codestep' into mainIdent