Support disconnecting NIP-46 sessions with logout

This commit is contained in:
raucao committed 2026-11-27 15:43:00 +04:00
1 parent 9fdc87e605
commit 9b24946d6d
8 files changed
+160 -1

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
class NostrLogoutJob < ApplicationJob
queue_as :nostr_rpc
def perform(session_id)
signer_session = NostrSignerSession.find_by(id: session_id)
return if signer_session.nil?
NostrManager::RemoteSigner::Disconnect.call(session: signer_session)
ensure
NostrSignerSession.find_by(id: session_id)&.destroy!
end
end
+11 -1
View File
@@ -7,9 +7,19 @@ class NostrSignerCleanupJob < ApplicationJob
.where("expires_at < ?", Time.current)
.delete_all
NostrSignerSession
.where(status: "disconnecting")
.where("updated_at < ?", 10.minutes.ago)
.delete_all
NostrSignRequest
.where(status: %w[pending signed failed])
.where(status: %w[pending failed])
.where("expires_at < ?", Time.current)
.delete_all
NostrSignRequest
.where(status: "signed")
.where("expires_at < ?", 10.minutes.ago)
.delete_all
end
end
@@ -67,6 +67,10 @@ module NostrManager
request("ping", [], timeout: timeout)
end
def logout(timeout: 10)
request("logout", [], timeout: timeout)
end
def request(method, params, timeout:, on_auth_url: nil)
raise RequestError, "The remote signer is not known yet" if @remote_signer_pubkey.blank?
@@ -0,0 +1,41 @@
module NostrManager
module RemoteSigner
class Disconnect < ApplicationService
def initialize(session:)
@session = session
end
def call
return false if @session.remote_signer_pubkey.blank? || Array(@session.relays).empty?
NostrManager::Relay::Pool.with(
relays: @session.relays,
timeout: 20,
connect_timeout: 5
) do |pool|
pool.subscribe(
Nostr::Filter.new(
kinds: [KIND],
p: [@session.client_public_key],
since: Time.current.to_i - 60
)
)
client = Client.new(
pool: pool,
client_private_key: @session.client_private_key,
client_public_key: @session.client_public_key,
remote_signer_pubkey: @session.remote_signer_pubkey
)
client.logout(timeout: 10)
end
true
rescue StandardError => e
Rails.logger.info("[nostr][signer] logout failed: #{e.class}: #{e.message}")
false
end
end
end
end
+31
View File
@@ -0,0 +1,31 @@
require "rails_helper"
RSpec.describe NostrLogoutJob, type: :job do
let(:signer_keypair) { Nostr::Keygen.new.generate_key_pair }
let(:session) do
NostrManager::RemoteSigner::CreateSession.call(
flow: "settings",
relays: ["wss://relay.example.com"]
).tap do |record|
record.complete_connection!(
remote_signer_pubkey: signer_keypair.public_key.to_s,
user_pubkey: SecureRandom.hex(32)
)
record.update!(status: "disconnecting")
end
end
it "destroys the session after attempting logout" do
expect(NostrManager::RemoteSigner::Disconnect).to receive(:call).with(session: session).and_return(false)
described_class.perform_now(session.id)
expect(NostrSignerSession.exists?(session.id)).to be(false)
end
it "does nothing for a missing session" do
expect(NostrManager::RemoteSigner::Disconnect).not_to receive(:call)
described_class.perform_now(0)
end
end
@@ -31,4 +31,14 @@ RSpec.describe NostrSignerCleanupJob, type: :job do
expect(NostrSignerSession.exists?(session.id)).to be(true)
end
it "removes stuck disconnecting sessions" do
allow(Setting).to receive(:nostr_signer_relays).and_return(["wss://relay.example.com"])
session = NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user)
session.update_columns(status: "disconnecting", updated_at: 11.minutes.ago)
described_class.perform_now
expect(NostrSignerSession.exists?(session.id)).to be(false)
end
end
@@ -0,0 +1,49 @@
require "rails_helper"
require "support/fake_signer_pool"
RSpec.describe NostrManager::RemoteSigner::Disconnect, type: :model do
let(:signer_keypair) { Nostr::Keygen.new.generate_key_pair }
let(:user_keypair) { Nostr::Keygen.new.generate_key_pair }
let(:session) do
NostrManager::RemoteSigner::CreateSession.call(
flow: "settings",
relays: ["wss://relay.example.com"]
).tap do |record|
record.complete_connection!(
remote_signer_pubkey: signer_keypair.public_key.to_s,
user_pubkey: user_keypair.public_key.to_s
)
end
end
let(:pool) do
FakeSignerPool.new(
client_public_key: session.client_public_key,
signer_private_key: signer_keypair.private_key.to_s,
user_private_key: user_keypair.private_key.to_s
)
end
before do
allow(NostrManager::Relay::Pool).to receive(:with) do |relays:, timeout:, connect_timeout:, &block|
block.call(pool)
end
end
it "sends the NIP-46 logout request" do
expect(described_class.call(session: session)).to be(true)
expect(pool.published.length).to eq(1)
end
it "returns false when there is no remote signer" do
session.update!(remote_signer_pubkey: nil)
expect(described_class.call(session: session)).to be(false)
end
it "tolerates relay failures" do
allow(NostrManager::Relay::Pool).to receive(:with).and_raise(StandardError, "offline")
expect(described_class.call(session: session)).to be(false)
end
end
+2
View File
@@ -82,6 +82,8 @@ class FakeSignerPool
user_pubkey
when "ping"
"pong"
when "logout"
"ack"
when "sign_event"
template = JSON.parse(payload["params"].first)
signed =