Files
akkounts/app/controllers/settings/nostr_signers_controller.rb
raucao 70faa4f329 Update the signer section in place instead of reloading the page
Disconnecting, cancelling and (when a key already exists) connecting now swap the Nostr Connect section via Turbo Streams, so the page's relay/profile/media lookups are not re-fetched. A replace still reloads because it changes the key, and a first-time connect reloads to reveal the pubkey and metadata.
2026-11-28 13:42:38 +04:00

202 lines
7.0 KiB
Ruby

module Settings
class NostrSignersController < ApplicationController
before_action :authenticate_user!
RATE_LIMIT = 8
RATE_WINDOW = 10.minutes
STATUS_RATE_LIMIT = 240
STATUS_RATE_WINDOW = 5.minutes
MAX_PENDING = 20
def create
return render_error("Nostr is not enabled", :forbidden) unless Setting.nostr_enabled?
return render_error("Remote signing is not configured", :service_unavailable) unless
NostrSignerSession.encryption_available?
return render_error("Too many connection attempts. Please try again later.", :too_many_requests) if
rate_limited?
return render_error("Too many pending connections. Please try again later.", :too_many_requests) if
pending_connections_exceeded?
signer_session =
if params[:bunker_uri].present?
parsed = NostrManager::RemoteSigner::ParseBunkerUri.call(uri: params[:bunker_uri])
NostrManager::RemoteSigner::CreateSession.call(
flow: "settings",
user: current_user,
relays: parsed[:relays],
connection_method: "bunker",
remote_signer_pubkey: parsed[:remote_signer_pubkey],
secret: parsed[:secret]
)
else
NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: current_user)
end
session[:nostr_signer_token] = signer_session.token
NostrConnectListenerJob.perform_later(signer_session.id)
respond_to do |format|
format.html { redirect_to setting_path(:nostr) }
format.turbo_stream do
@pending_signer_session = signer_session
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
end
format.json do
render json: {
token: signer_session.token,
uri: signer_session.nostrconnect_uri,
expires_at: signer_session.expires_at
}
end
end
rescue NostrManager::RemoteSigner::Error => e
render_error(e.message, :unprocessable_entity)
end
def status
return render_error("Too many status requests. Please try again later.", :too_many_requests) if
status_rate_limited?
signer_session = NostrSignerSession.find_by_token(params[:token])
return render_error("Unknown connection", :not_found) if signer_session.nil? ||
signer_session.user_id != current_user.id
signer_session.update_column(:last_status_poll_at, Time.current)
if signer_session.status == "pending" && signer_session.expires_at < Time.current
signer_session.expire!
return render json: { status: "expired", error: signer_session.error }
end
if signer_session.verified? || signer_session.connected?
connect_account!(signer_session)
elsif signer_session.disconnecting?
render json: { status: "disconnected", error: "Remote signer disconnected." }
elsif signer_session.failed? || signer_session.expired?
render json: { status: signer_session.status, error: signer_session.error }
else
render json: { status: "pending", auth_url: signer_session.auth_url }
end
end
def section
respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
end
format.html { redirect_to setting_path(:nostr) }
end
end
def replace
signer_session = NostrSignerSession.find_by_token(params[:token])
return render_error("Unknown connection", :not_found) if signer_session.nil? ||
signer_session.user_id != current_user.id
unless signer_session.verified? || signer_session.connected?
return render_error("This connection can no longer be confirmed. Please try again.",
:unprocessable_entity)
end
connect_account!(signer_session, replace: true)
end
def destroy
NostrManager::RemoteSigner::DisconnectSessions.call(
sessions: current_user.nostr_signer_sessions.where(status: %w[pending verified connected])
)
forget_nostr_account
session.delete(:nostr_signer_token)
respond_to do |format|
format.html { redirect_to setting_path(:nostr) }
format.json { render json: { ok: true } }
format.turbo_stream do
if current_user.nostr_pubkey.present?
render turbo_stream: turbo_stream.replace("nostr-signer-section", partial: "settings/nostr_signer")
else
redirect_to setting_path(:nostr)
end
end
end
end
private
def connect_account!(signer_session, replace: false)
conflict = nil
signer_session.with_lock do
next if signer_session.consumed_at.present?
other_user = LdapManager::FetchUserByNostrKey.call(pubkey: signer_session.user_pubkey)
if other_user.present? && other_user != current_user
conflict = :other_account
else
current_pubkey = current_user.nostr_pubkey
if !replace && current_pubkey.present? && current_pubkey != signer_session.user_pubkey
conflict = :existing_key
else
unless current_pubkey.present? && current_pubkey == signer_session.user_pubkey
LdapManager::SetNostrKey.call(user: current_user, pubkey: signer_session.user_pubkey)
end
signer_session.update!(status: "connected", consumed_at: Time.current)
remember_nostr_account(current_user, session_record: signer_session)
end
end
end
case conflict
when :other_account
render_error("Public key already in use for a different account", :unprocessable_entity)
when :existing_key
render json: {
status: "replace_required",
existing_npub: current_user.nostr_pubkey_bech32,
signer_npub: signer_session.user_pubkey_bech32
}
else
session.delete(:nostr_signer_token)
render json: { status: "connected", npub: signer_session.user_pubkey_bech32 }
end
end
def rate_limited?
key = "nostr_signer:#{current_user.id}"
count = Rails.cache.read(key).to_i
return true if count >= RATE_LIMIT
Rails.cache.write(key, count + 1, expires_in: RATE_WINDOW)
false
end
def status_rate_limited?
key = "nostr_signer_status:#{current_user.id}"
count = Rails.cache.read(key).to_i
return true if count >= STATUS_RATE_LIMIT
Rails.cache.write(key, count + 1, expires_in: STATUS_RATE_WINDOW)
false
end
def pending_connections_exceeded?
NostrSignerSession
.where(status: "pending")
.where("expires_at > ?", Time.current)
.count >= MAX_PENDING
end
def render_error(message, status)
respond_to do |format|
format.html do
redirect_to setting_path(:nostr), flash: { alert: message }
end
format.json { render json: { error: message }, status: status }
end
end
end
end