ident
All repositories: gitoria
8.6 KB
// components/apps.hl — `/apps`: THE ACCOUNT'S APPS (piece 2 of 6, ticket #25; CONCEPT.md// "Apps: any ident user can register apps. An app gets an API key.").// Register (name + the origins it runs on), list, edit, new secret, delete. An app has a// public API key and a secret for its server; the secret is shown ONCE, right after it is// made — ident stores only its sha256.//// The faces' names start with `app…`: a face is found by its EVENT NAME across all// files (hl:web), so they must not collide with home.hl's.parent './main.hl'import { accountOfSession } from '../lib/accounts.hl'import { appRows, createApp, updateApp, regenerateSecret, deleteApp } from '../lib/apps.hl'import { siteName } from '../project.hl'import { leadLook, messageLook, noticeLook } from './styles.hl'session = nullme = accountOfSession(&session)signedIn = me != nullsignedOut = me == nullmeEmail = me != null ? me.email : ''apps = me != null ? appRows(me.id) : []hasApps = apps.length > 0noApps = apps.length == 0__title = siteName + ' | your apps'// THE APP FORM — new and editediting = falseeditId = '' // '' = a new app, else the app's id (a UUID)editHeading = ''fName = ''fOrigins = ''// THE SECRET, shown onceshowSecret = falsesecretFor = ''secret = ''message = ''notice = ''Style {pLead = leadLookpMessage = messageLookpNotice = noticeLookappMain {flex = '1 1 14rem'display = 'grid'gap = '.15rem'minWidth = '0'}appName {fontWeight = '700'color = 'var(--color-text-strong)'overflowWrap = 'anywhere'}appOrigins {color = 'var(--color-text-muted)'fontSize = '.875rem'overflowWrap = 'anywhere'}appKey {fontSize = '.875rem'color = 'var(--color-text-muted)'overflowWrap = 'anywhere'}// the secret, shown oncesecretBox {display = 'grid'gap = '.6rem'padding = '1rem'background = 'var(--color-surface)'borderRadius = 'var(--radius-large)'h2 {color = 'var(--color-warning)'}code {display = 'block'padding = '.5rem .6rem'background = 'var(--color-background-sunken)'userSelect = 'all'}}}View {identCard {if (signedOut) {h1 { "Your apps" }p { id = "signinfirst" Style.pLead "Sign in to ident first: " a { href = "/" "sign in" } "." }}if (signedIn) {accountBar {userEmail { id = "meemail" meEmail }a { id = "toidentities" href = "/" "Your identities" }}if (showSecret) {secretBox { id = "secretbox"h2 { "Secret of " secretFor }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." }code { id = "secret" secret }button { id = "secretdone" type = "button" class = "small" "I copied it" on click(e) { emit hideSecret(e) } }}}if (editing) {form { id = "appform"on submit(e) {e.preventDefault()emit saveApp(e)}h1 { id = "appformheading" editHeading }label { "Name"input { id = "fappname" name = "name" maxlength = "60" autocomplete = "off" required = "required" value = fName on input(e) { emit setName(e.target.value) } }}label { "Origins " fieldHint { "(where the app runs, like https://tickets.worldapi.org — several separated by spaces; the login button only returns there)" }input { id = "forigins" name = "origins" autocomplete = "off" required = "required" value = fOrigins on input(e) { emit setOrigins(e.target.value) } }}formButtons {button { id = "saveapp" type = "submit" "Save" }button { id = "cancelapp" type = "button" class = "quiet" "Cancel" on click(e) { emit closeForm(e) } }}}}section { id = "appssection"sectionHead {h1 { "Your apps" }button { id = "newapp" type = "button" class = "small" "Register an app" on click(e) { emit openNew(e) } }}if (noApps) { p { id = "noapps" Style.pLead "No apps yet. An app you register gets an API key for its “login with ident” button." } }ul { id = "apps"for (row of apps) {li {appMain {appName { row.name }appOrigins { row.originsText }appKey { "API key " code { row.apiKey } }}identityActions {button { type = "button" class = "quiet small" name = "edit" value = row.id "Edit" on click(e) { emit openEdit(e.target.value) } }button { type = "button" class = "quiet small" name = "newsecret" value = row.id "New secret" on click(e) { emit renew(e.target.value) } }button { type = "button" class = "quiet small danger" name = "delete" value = row.id "Delete" on click(e) { emit remove(e.target.value) } }}}}}}}p { id = "notice" Style.pNotice notice }p { id = "message" Style.pMessage message }}}on setName(v) { fName = v }on setOrigins(v) { fOrigins = v }rowOf = (id) => {for (row of apps) { if ('' + row.id == '' + id) { return row } }return null}// the origins field: words separated by spaces or commasoriginList = (text) => {out = []for (w of text.replaceAll(',', ' ').split(' ')) {let t = w.trim()if (t != '') { out.push(t) }}return out}// the list changed by one row (upsert / remove): the empty-list line and the count followappsChanged = () => {hasApps = apps.length > 0noApps = apps.length == 0return null}on openNew(e) {message = ''notice = ''editId = ''fName = ''fOrigins = ''editHeading = 'Register an app'editing = true}on openEdit(id) {message = ''notice = ''row = rowOf(id)if (row == null) { return null }editId = row.idfName = row.namefOrigins = row.originsTexteditHeading = 'Edit ' + row.nameediting = true}on closeForm(e) {editing = falsemessage = ''}on hideSecret(e) {showSecret = falsesecret = ''secretFor = ''}on saveApp(e) {message = ''notice = ''input = { name = fName origins = originList(fOrigins) }let r = nullif (editId == '') {r = emit server appCreate(input)} else {r = emit server appUpdate('' + editId, input)}if (r == null) {message = 'the server did not answer — try again'return null}if (r.error != null) {message = r.errorreturn null}apps.upsert(r.app)appsChanged()editing = falseif (r.secret != null) {secretFor = r.app.namesecret = r.secretshowSecret = truenotice = 'App registered.'} else {notice = 'App saved.'}}on renew(id) {message = ''notice = ''row = rowOf(id)if (row == null) { return null }if (!confirm('Make a new secret for “' + row.name + '”? The current one stops working at once.')) { return null }r = emit server appNewSecret('' + id)if (r == null) {message = 'the server did not answer — try again'return null}if (r.error != null) {message = r.errorreturn null}apps.upsert(r.app)appsChanged()secretFor = r.app.namesecret = r.secretshowSecret = truenotice = 'New secret made.'}on remove(id) {message = ''notice = ''row = rowOf(id)if (row == null) { return null }if (!confirm('Delete the app “' + row.name + '”? Its API key stops working and the ids its users had in it are gone.')) { return null }r = emit server appDelete('' + id)if (r == null) {message = 'the server did not answer — try again'return null}if (r.error != null) {message = r.errorreturn null}apps.remove('' + id)appsChanged()if ('' + editId == '' + id) { editing = false }showSecret = falsesecret = ''notice = 'App deleted.'}// ---- the faces ------------------------------------------------------------------------// The LAST parameter is the session (the framework appends it); accountOfSession only// gets the framework's own Session (hl:web never takes it from the peer, hybriel#16/#31).on server appCreate(input, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = createApp(me.id, input)if (r.error != null) { return r }return { app = r.app secret = r.secret }}on server appUpdate(id, input, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = updateApp(me.id, id, input)if (r.error != null) { return r }return { app = r.app }}on server appNewSecret(id, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = regenerateSecret(me.id, id)if (r.error != null) { return r }return { app = r.app secret = r.secret }}on server appDelete(id, session) {let me = accountOfSession(&session)if (me == null) { return { error = 'you are not signed in' } }r = deleteApp(me.id, id)if (r.error != null) { return r }return { deleted = r.deleted }}
Branches
Latest commits
- 45e55303ident#42: an app write patches one row: upsert on create, edit and new secret, one-argument remove on delete; the faces answer the changed app (or the deleted id), not the whole listmre
- 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