From 0412b971702624100239ff5784dda5dcb12fce5a Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Sat, 13 Apr 2024 13:22:59 -0400 Subject: [PATCH] WIP managing a per-app current identity from the Tilde Friends navigation bar. --- core/app.js | 6 ++++++ core/client.js | 49 ++++++++++++++++++++++++++++++++++++++++++++---- core/core.js | 51 +++++++++++++++++++++++++++++++++++++++++--------- src/httpd.js.c | 2 +- 4 files changed, 94 insertions(+), 14 deletions(-) diff --git a/core/app.js b/core/app.js index 2735e9e4..b4a894c3 100644 --- a/core/app.js +++ b/core/app.js @@ -144,6 +144,8 @@ function socket(request, response, client) { JSON.stringify({ action: 'session', credentials: credentials, + identities: await ssb.getIdentities(credentials?.session?.name), + identity: await core.getActiveIdentity(credentials?.session?.name, packageOwner, packageName), parentApp: parentApp, id: blobId, }), @@ -210,6 +212,10 @@ function socket(request, response, client) { if (process) { process.resetPermission(message.permission); } + } else if (message.action == 'setActiveIdentity') { + process.setActiveIdentity(message.identity); + } else if (message.action == 'createIdentity') { + process.createIdentity(); } else if (message.message == 'tfrpc') { if (message.id && g_calls[message.id]) { if (message.error !== undefined) { diff --git a/core/client.js b/core/client.js index 4646d93d..1bf10760 100644 --- a/core/client.js +++ b/core/client.js @@ -56,6 +56,8 @@ class TfNavigationElement extends LitElement { spark_lines: {type: Object}, version: {type: Object}, show_version: {type: Boolean}, + identity: {type: String}, + identities: {type: Array}, }; } @@ -65,6 +67,7 @@ class TfNavigationElement extends LitElement { this.show_permissions = false; this.status = {}; this.spark_lines = {}; + this.identities = []; } /** @@ -128,6 +131,37 @@ class TfNavigationElement extends LitElement { } } + set_active_identity(event) { + send({action: 'setActiveIdentity', identity: event.srcElement.value}); + } + + create_identity(event) { + send({action: 'createIdentity'}); + } + + render_identity() { + if (this.identities?.length) { + return html` + + + `; + } else { + return html` + + + `; + } + } + /** * TODOC * @returns @@ -257,6 +291,7 @@ class TfNavigationElement extends LitElement { x, ])} + ${this.render_identity()} ${this.render_login()} @@ -1125,11 +1160,17 @@ function api_setHash(hash) { function _receive_websocket_message(message) { if (message && message.action == 'session') { setStatusMessage('🟢 Executing...', kStatusColor); - document.getElementsByTagName('tf-navigation')[0].credentials = - message.credentials; + let navigation = document.getElementsByTagName('tf-navigation')[0]; + navigation.credentials = message.credentials; + navigation.identities = message.identities; + navigation.identity = message.identity; } else if (message && message.action == 'permissions') { - document.getElementsByTagName('tf-navigation')[0].permissions = - message.permissions ?? {}; + let navigation = document.getElementsByTagName('tf-navigation')[0]; + navigation.permissions = message.permissions ?? {}; + } else if (message && message.action == 'identities') { + let navigation = document.getElementsByTagName('tf-navigation')[0]; + navigation.identities = message.identities; + navigation.identity = message.identity; } else if (message && message.action == 'ready') { setStatusMessage(null); if (window.location.hash) { diff --git a/core/core.js b/core/core.js index f2698f59..fe3f6a2c 100644 --- a/core/core.js +++ b/core/core.js @@ -508,6 +508,30 @@ async function getProcessBlob(blobId, key, options) { url: options?.url, }, }; + process.sendIdentities = async function() { + process.app.send({ + action: 'identities', + identities: await ssb.getIdentities(process?.credentials?.session?.name), + identity: await getActiveIdentity(process?.credentials?.session?.name, options.packageOwner, options.packageName), + }); + }; + process.setActiveIdentity = async function(identity) { + if (process?.credentials?.session?.name && options.packageOwner && options.packageName) { + await new Database(process?.credentials?.session?.name).set(`id:${options.packageOwner}:${options.packageName}`, identity); + } + process.sendIdentities(); + }; + process.createIdentity = async function() { + if ( + process.credentials && + process.credentials.session && + process.credentials.session.name + ) { + let id = ssb.createIdentity(process.credentials.session.name); + await process.sendIdentities(); + return id; + } + } if (process.credentials?.permissions?.administration) { imports.core.globalSettingsDescriptions = function () { let settings = Object.assign({}, k_global_settings); @@ -578,15 +602,7 @@ async function getProcessBlob(blobId, key, options) { Object.keys(ssb).map((key) => [key, ssb[key].bind(ssb)]) ); imports.ssb.port = tildefriends.ssb_port; - imports.ssb.createIdentity = function () { - if ( - process.credentials && - process.credentials.session && - process.credentials.session.name - ) { - return ssb.createIdentity(process.credentials.session.name); - } - }; + imports.ssb.createIdentity = () => process.createIdentity(); imports.ssb.addIdentity = function (id) { if ( process.credentials && @@ -613,6 +629,8 @@ async function getProcessBlob(blobId, key, options) { }); } }; + imports.ssb.setActiveIdentity = id => process.setActiveIdentity(id); + imports.ssb.getActiveIdentity = () => getActiveIdentity(process.credentials?.session?.name, options.packageOwner, options.packageName); imports.ssb.getOwnerIdentities = function () { if (options.packageOwner) { return ssb.getIdentities(options.packageOwner); @@ -1458,10 +1476,25 @@ function storePermission(user, packageOwner, packageName, permission, allow) { } } + +async function getActiveIdentity(user, packageOwner, packageName) { + if (user && packageOwner && packageName) { + let id = await new Database(user).get(`id:${packageOwner}:${packageName}`); + if (!id) { + let ids = await ssb.getIdentities(user); + if (ids) { + id = ids[0]; + } + } + return id; + } +} + export { gGlobalSettings as globalSettings, setGlobalSettings, enableStats, invoke, getSessionProcessBlob, + getActiveIdentity, }; diff --git a/src/httpd.js.c b/src/httpd.js.c index 0e9bf859..4b764e84 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -474,7 +474,7 @@ static JSValue _httpd_auth_query(JSContext* context, JSValueConst this_val, int JSValue settings_value = settings ? JS_ParseJSON(context, settings, strlen(settings), NULL) : JS_UNDEFINED; JSValue permissions = JS_GetPropertyStr(context, settings_value, "permissions"); JSValue user_permissions = JS_GetPropertyStr(context, permissions, name_string); - int length = tf_util_get_length(context, user_permissions); + int length = !JS_IsUndefined(user_permissions) ? tf_util_get_length(context, user_permissions) : 0 ; for (int i = 0; i < length; i++) { JSValue permission = JS_GetPropertyUint32(context, user_permissions, i);