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
- 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
- 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