Send a NIP-46 logout when disconnecting a remote signer
disconnect() now clears the local state and the stored client keypair first, then asks the remote signer to drop the session with a NIP-46 logout request (via applesauce's NostrConnectSigner.logout()). The request is a courtesy hint, so a failure is only logged and never blocks the local disconnect. Extension signers still just close their connection. Refs #125
This commit is contained in:
3 files changed
+107
-6
No files matched your search
@@ -19,6 +19,9 @@ const STORAGE_KEY_CONNECT_RELAY = 'marco:nostr_connect_relay';
|
||||
|
||||
const DEFAULT_CONNECT_RELAY = 'wss://nostr.kosmos.org';
|
||||
|
||||
/** How long to wait for a remote signer to acknowledge a NIP-46 logout. */
|
||||
export const REMOTE_SIGNER_LOGOUT_TIMEOUT = 10000;
|
||||
|
||||
export default class NostrAuthService extends Service {
|
||||
@service nostrRelay;
|
||||
@service nostrData;
|
||||
@@ -279,17 +282,15 @@ export default class NostrAuthService extends Service {
|
||||
}
|
||||
|
||||
async disconnect() {
|
||||
const instance = this._signerInstance;
|
||||
const wasConnect = this.signerType === 'connect';
|
||||
|
||||
// Clear local state immediately, regardless of what the remote signer does.
|
||||
this.pubkey = null;
|
||||
this.nostrData?.loadProfile(null);
|
||||
this.signerType = null;
|
||||
this.connectStatus = null;
|
||||
this.connectUri = null;
|
||||
if (
|
||||
this._signerInstance &&
|
||||
typeof this._signerInstance.close === 'function'
|
||||
) {
|
||||
await this._signerInstance.close();
|
||||
}
|
||||
this._signerInstance = null;
|
||||
|
||||
localStorage.removeItem(STORAGE_KEY);
|
||||
@@ -297,5 +298,20 @@ export default class NostrAuthService extends Service {
|
||||
localStorage.removeItem(STORAGE_KEY_CONNECT_LOCAL_KEY);
|
||||
localStorage.removeItem(STORAGE_KEY_CONNECT_REMOTE_PUBKEY);
|
||||
localStorage.removeItem(STORAGE_KEY_CONNECT_RELAY);
|
||||
|
||||
if (!instance) return;
|
||||
|
||||
if (wasConnect && typeof instance.logout === 'function') {
|
||||
// NIP-46: tell the remote signer to drop the session. logout() also
|
||||
// closes the local connection and is a no-op when not connected. It is
|
||||
// a courtesy hint, so a failure must not stop the local disconnect.
|
||||
try {
|
||||
await instance.logout({ timeout: REMOTE_SIGNER_LOGOUT_TIMEOUT });
|
||||
} catch (error) {
|
||||
console.warn('Failed to send NIP-46 logout to remote signer', error);
|
||||
}
|
||||
} else if (typeof instance.close === 'function') {
|
||||
await instance.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,6 +58,8 @@ export class MockNostrDataService extends Service {
|
||||
return this.profiles[pubkey];
|
||||
}
|
||||
|
||||
loadProfile() {}
|
||||
|
||||
getEventRelays() {
|
||||
return [];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
import { module, test } from 'qunit';
|
||||
import { setupTest } from 'marco/tests/helpers';
|
||||
import sinon from 'sinon';
|
||||
import NostrAuthService, {
|
||||
REMOTE_SIGNER_LOGOUT_TIMEOUT,
|
||||
} from 'marco/services/nostr-auth';
|
||||
|
||||
const STORAGE_KEY_CONNECT_REMOTE_PUBKEY = 'marco:nostr_connect_remote_pubkey';
|
||||
|
||||
module('Unit | Service | nostr-auth', function (hooks) {
|
||||
setupTest(hooks);
|
||||
|
||||
hooks.beforeEach(function () {
|
||||
localStorage.clear();
|
||||
|
||||
this.owner.register('service:nostrAuth', NostrAuthService);
|
||||
this.service = this.owner.lookup('service:nostrAuth');
|
||||
});
|
||||
|
||||
hooks.afterEach(function () {
|
||||
sinon.restore();
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
test('disconnect sends a NIP-46 logout for a remote signer', async function (assert) {
|
||||
const logout = sinon.spy(async () => {});
|
||||
const close = sinon.spy(async () => {});
|
||||
|
||||
this.service.signerType = 'connect';
|
||||
this.service.pubkey = 'a'.repeat(64);
|
||||
localStorage.setItem(STORAGE_KEY_CONNECT_REMOTE_PUBKEY, 'b'.repeat(64));
|
||||
this.service._signerInstance = { logout, close };
|
||||
|
||||
await this.service.disconnect();
|
||||
|
||||
assert.true(logout.calledOnce, 'sends the logout request');
|
||||
assert.deepEqual(
|
||||
logout.firstCall.args[0],
|
||||
{ timeout: REMOTE_SIGNER_LOGOUT_TIMEOUT },
|
||||
'waits for the acknowledgement with the configured timeout'
|
||||
);
|
||||
assert.false(close.called, 'does not close separately (logout does it)');
|
||||
assert.strictEqual(this.service.pubkey, null, 'clears the pubkey');
|
||||
assert.strictEqual(this.service.signerType, null, 'clears the signer type');
|
||||
assert.strictEqual(
|
||||
localStorage.getItem(STORAGE_KEY_CONNECT_REMOTE_PUBKEY),
|
||||
null,
|
||||
'clears the persisted connection state'
|
||||
);
|
||||
});
|
||||
|
||||
test('disconnect closes the connection for an extension signer', async function (assert) {
|
||||
const close = sinon.spy(async () => {});
|
||||
|
||||
this.service.signerType = 'extension';
|
||||
this.service.pubkey = 'a'.repeat(64);
|
||||
this.service._signerInstance = { close };
|
||||
|
||||
await this.service.disconnect();
|
||||
|
||||
assert.true(close.calledOnce, 'closes the extension connection');
|
||||
assert.strictEqual(this.service.pubkey, null, 'clears the pubkey');
|
||||
});
|
||||
|
||||
test('disconnect still resolves when the remote signer logout fails', async function (assert) {
|
||||
const logout = sinon.spy(async () => {
|
||||
throw new Error('signer offline');
|
||||
});
|
||||
|
||||
this.service.signerType = 'connect';
|
||||
this.service.pubkey = 'a'.repeat(64);
|
||||
this.service._signerInstance = { logout };
|
||||
|
||||
await this.service.disconnect();
|
||||
|
||||
assert.true(logout.calledOnce, 'attempts the logout');
|
||||
assert.strictEqual(
|
||||
this.service.pubkey,
|
||||
null,
|
||||
'local state is cleared even if the signer is unreachable'
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user