Never silently change or orphan the account's Nostr key
Connecting a remote signer while a different account key already exists now asks for explicit confirmation instead of overwriting it, and every key change goes through a single LdapManager::SetNostrKey seam so step-up auth can be added in one place later. Removing the account key also disconnects the remote signer, since a signer session is bound to that key; disconnecting a signer still leaves the key intact.
This commit is contained in:
16 files changed
+506
-35
No files matched your search
@@ -27,7 +27,9 @@
|
||||
<% end %>
|
||||
<% elsif @pending_session.present? %>
|
||||
<div data-controller="nostr-signer-connect"
|
||||
data-nostr-signer-connect-status-url-value="<%= @status_url %>">
|
||||
data-nostr-signer-connect-status-url-value="<%= @status_url %>"
|
||||
data-nostr-signer-connect-replace-url-value="<%= @replace_url %>"
|
||||
data-nostr-signer-connect-destroy-url-value="<%= @destroy_url %>">
|
||||
<% unless @pending_session.bunker? %>
|
||||
<div data-nostr-signer-connect-target="details">
|
||||
<div class="<%= @title.present? ? "mt-4" : "" %> hidden justify-center sm:flex"
|
||||
@@ -93,6 +95,33 @@
|
||||
|
||||
<p class="mt-2 hidden <%= text_class %> text-red-700" data-nostr-signer-connect-target="error"></p>
|
||||
|
||||
<% if @replace_url.present? %>
|
||||
<div class="mt-4 hidden rounded-md border border-amber-300 bg-amber-50 p-4"
|
||||
data-nostr-signer-connect-target="replace">
|
||||
<p class="mb-2 font-bold text-amber-900">
|
||||
This account already has a Nostr public key
|
||||
</p>
|
||||
<p class="mb-1 <%= text_class %> text-amber-900">Existing key:</p>
|
||||
<p class="mb-3 break-all font-mono text-sm text-amber-900"
|
||||
data-nostr-signer-connect-target="replaceExisting"></p>
|
||||
<p class="mb-1 <%= text_class %> text-amber-900">Your signer's key:</p>
|
||||
<p class="mb-3 break-all font-mono text-sm text-amber-900"
|
||||
data-nostr-signer-connect-target="replaceSigner"></p>
|
||||
<p class="mb-3 <%= text_class %> text-amber-900">
|
||||
Connecting this signer would replace the existing key. Your signer
|
||||
must use the account key to sign.
|
||||
</p>
|
||||
<button type="button" class="<%= action_button_class %>"
|
||||
data-action="nostr-signer-connect#replace">
|
||||
Replace existing key
|
||||
</button>
|
||||
<button type="button" class="mt-2 <%= cancel_button_class %>"
|
||||
data-action="nostr-signer-connect#cancel">
|
||||
Cancel
|
||||
</button>
|
||||
</div>
|
||||
<% end %>
|
||||
|
||||
<div class="mt-4 hidden" data-nostr-signer-connect-target="retry">
|
||||
<%= button_to "Try again", @create_url, method: :post, class: action_button_class,
|
||||
form: button_form_options %>
|
||||
|
||||
@@ -2,13 +2,14 @@ require "rqrcode"
|
||||
|
||||
class NostrSignerConnectComponent < ViewComponent::Base
|
||||
def initialize(pending_session: nil, active_session: nil, create_url:, destroy_url: nil,
|
||||
status_url: nil, title: "Nostr Connect", description: nil,
|
||||
status_url: nil, replace_url: nil, title: "Nostr Connect", description: nil,
|
||||
show_disconnect: true, modal: false)
|
||||
@pending_session = pending_session
|
||||
@active_session = active_session
|
||||
@create_url = create_url
|
||||
@destroy_url = destroy_url
|
||||
@status_url = status_url
|
||||
@replace_url = replace_url
|
||||
@title = title
|
||||
@description = description
|
||||
@show_disconnect = show_disconnect
|
||||
|
||||
@@ -75,18 +75,23 @@ module Settings
|
||||
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
|
||||
current_user.nostr_signer_sessions
|
||||
.where(status: %w[pending verified connected])
|
||||
.find_each do |signer_session|
|
||||
if signer_session.remote_signer_pubkey.present? && signer_session.verified_at.present?
|
||||
signer_session.update!(status: "disconnecting")
|
||||
signer_session.clear_remember_token!
|
||||
NostrLogoutJob.perform_later(signer_session.id)
|
||||
else
|
||||
signer_session.destroy!
|
||||
end
|
||||
end
|
||||
NostrManager::RemoteSigner::DisconnectSessions.call(
|
||||
sessions: current_user.nostr_signer_sessions.where(status: %w[pending verified connected])
|
||||
)
|
||||
|
||||
forget_nostr_account
|
||||
session.delete(:nostr_signer_token)
|
||||
@@ -99,8 +104,8 @@ module Settings
|
||||
|
||||
private
|
||||
|
||||
def connect_account!(signer_session)
|
||||
conflict = false
|
||||
def connect_account!(signer_session, replace: false)
|
||||
conflict = nil
|
||||
|
||||
signer_session.with_lock do
|
||||
next if signer_session.consumed_at.present?
|
||||
@@ -108,16 +113,32 @@ module Settings
|
||||
other_user = LdapManager::FetchUserByNostrKey.call(pubkey: signer_session.user_pubkey)
|
||||
|
||||
if other_user.present? && other_user != current_user
|
||||
conflict = true
|
||||
conflict = :other_account
|
||||
else
|
||||
LdapManager::UpdateNostrKey.call(dn: current_user.dn, pubkey: signer_session.user_pubkey)
|
||||
signer_session.update!(status: "connected", consumed_at: Time.current)
|
||||
remember_nostr_account(current_user, session_record: signer_session)
|
||||
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
|
||||
|
||||
if conflict
|
||||
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 }
|
||||
|
||||
@@ -138,7 +138,16 @@ class SettingsController < ApplicationController
|
||||
http_status :unprocessable_entity and return
|
||||
end
|
||||
|
||||
LdapManager::UpdateNostrKey.call(dn: current_user.dn, pubkey: signed_event.pubkey)
|
||||
current_pubkey = current_user.nostr_pubkey
|
||||
|
||||
if current_pubkey.present? && current_pubkey != signed_event.pubkey &&
|
||||
params[:replace].to_s != "true"
|
||||
flash[:alert] = "This account already has a different Nostr public key. " \
|
||||
"Remove it first or confirm the replacement."
|
||||
http_status :unprocessable_entity and return
|
||||
end
|
||||
|
||||
LdapManager::SetNostrKey.call(user: current_user, pubkey: signed_event.pubkey)
|
||||
session[:shared_secret] = nil
|
||||
|
||||
flash[:success] = "Public key verification successful"
|
||||
@@ -148,7 +157,11 @@ class SettingsController < ApplicationController
|
||||
# DELETE /settings/nostr_pubkey
|
||||
def remove_nostr_pubkey
|
||||
# TODO require current pubkey or password to delete
|
||||
LdapManager::UpdateNostrKey.call(dn: current_user.dn, pubkey: nil)
|
||||
LdapManager::SetNostrKey.call(user: current_user, pubkey: nil)
|
||||
NostrManager::RemoteSigner::DisconnectSessions.call(
|
||||
sessions: current_user.nostr_signer_sessions.where(status: %w[pending verified connected])
|
||||
)
|
||||
forget_nostr_account
|
||||
|
||||
redirect_to setting_path(:nostr), flash: {
|
||||
success: 'Public key removed from account'
|
||||
|
||||
@@ -199,15 +199,7 @@ module Users
|
||||
end
|
||||
|
||||
def disconnect_remembered_session(record)
|
||||
return if record.nil? || record.disconnecting?
|
||||
|
||||
if record.remote_signer_pubkey.present? && record.verified_at.present?
|
||||
record.update!(status: "disconnecting")
|
||||
record.clear_remember_token!
|
||||
NostrLogoutJob.perform_later(record.id)
|
||||
else
|
||||
record.destroy!
|
||||
end
|
||||
NostrManager::RemoteSigner::DisconnectSessions.call(sessions: [record])
|
||||
end
|
||||
|
||||
def current_reauth_request
|
||||
|
||||
@@ -25,10 +25,20 @@ module ApplicationHelper
|
||||
return nil unless user_signed_in?
|
||||
return nil unless NostrSignerSession.encryption_available?
|
||||
|
||||
@active_nostr_signer_session ||= current_user.nostr_signer_sessions
|
||||
return @active_nostr_signer_session if defined?(@active_nostr_signer_session)
|
||||
|
||||
session = current_user.nostr_signer_sessions
|
||||
.where(status: %w[verified connected])
|
||||
.order(updated_at: :desc)
|
||||
.first
|
||||
|
||||
@active_nostr_signer_session =
|
||||
if session && (current_user.nostr_pubkey.blank? ||
|
||||
session.user_pubkey != current_user.nostr_pubkey)
|
||||
nil
|
||||
else
|
||||
session
|
||||
end
|
||||
end
|
||||
|
||||
def nostr_remote_signer_available?
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
import { csrfToken } from "lib/csrf"
|
||||
|
||||
const POLL_INTERVAL = 2000
|
||||
const MAX_ATTEMPTS = 160
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = [ "status", "error", "authWrapper", "authLink", "details", "retry", "signerHint", "qr", "qrToggle" ]
|
||||
static values = { statusUrl: String }
|
||||
static targets = [ "status", "error", "authWrapper", "authLink", "details", "retry", "signerHint", "qr", "qrToggle", "replace", "replaceExisting", "replaceSigner" ]
|
||||
static values = { statusUrl: String, replaceUrl: String, destroyUrl: String }
|
||||
|
||||
connect () {
|
||||
this.attempts = 0
|
||||
@@ -40,6 +41,11 @@ export default class extends Controller {
|
||||
return
|
||||
}
|
||||
|
||||
if (data.status === "replace_required") {
|
||||
this.showReplace(data.existing_npub, data.signer_npub)
|
||||
return
|
||||
}
|
||||
|
||||
if (data.status === "failed" || data.status === "expired" || data.status === "disconnected") {
|
||||
this.showTerminalError(data.error || "The connection failed. Please try again.")
|
||||
return
|
||||
@@ -119,6 +125,65 @@ export default class extends Controller {
|
||||
if (this.hasQrToggleTarget) this.qrToggleTarget.classList.add("hidden")
|
||||
}
|
||||
|
||||
showReplace (existingNpub, signerNpub) {
|
||||
this.stopped = true
|
||||
clearTimeout(this.timer)
|
||||
|
||||
if (this.hasReplaceExistingTarget) {
|
||||
this.replaceExistingTarget.textContent = existingNpub || ""
|
||||
}
|
||||
if (this.hasReplaceSignerTarget) {
|
||||
this.replaceSignerTarget.textContent = signerNpub || ""
|
||||
}
|
||||
|
||||
this.statusTarget.classList.add("hidden")
|
||||
if (this.hasDetailsTarget) this.detailsTarget.classList.add("hidden")
|
||||
if (this.hasAuthWrapperTarget) this.authWrapperTarget.classList.add("hidden")
|
||||
if (this.hasReplaceTarget) this.replaceTarget.classList.remove("hidden")
|
||||
}
|
||||
|
||||
async replace () {
|
||||
if (!this.hasReplaceUrlValue || !this.replaceUrlValue) return
|
||||
|
||||
try {
|
||||
const response = await fetch(this.replaceUrlValue, {
|
||||
method: "POST", credentials: "include",
|
||||
headers: { "Accept": "application/json", "X-CSRF-Token": csrfToken() }
|
||||
})
|
||||
const data = await response.json().catch(() => ({}))
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(data.error || `HTTP ${response.status}`)
|
||||
}
|
||||
|
||||
if (data.status === "connected") {
|
||||
this.statusTarget.classList.remove("hidden")
|
||||
this.statusTarget.textContent = "Connected. Redirecting…"
|
||||
window.location.reload()
|
||||
return
|
||||
}
|
||||
|
||||
this.showError(data.error || "Could not replace the key. Please try again.")
|
||||
} catch (error) {
|
||||
this.showError(error.message)
|
||||
}
|
||||
}
|
||||
|
||||
async cancel () {
|
||||
try {
|
||||
if (this.hasDestroyUrlValue && this.destroyUrlValue) {
|
||||
await fetch(this.destroyUrlValue, {
|
||||
method: "DELETE", credentials: "include",
|
||||
headers: { "Accept": "application/json", "X-CSRF-Token": csrfToken() }
|
||||
})
|
||||
}
|
||||
} catch (error) {
|
||||
// Ignore; reload below reflects whatever state the server is in.
|
||||
}
|
||||
|
||||
window.location.reload()
|
||||
}
|
||||
|
||||
showAuthUrl (url) {
|
||||
if (!url) {
|
||||
this.authWrapperTarget.classList.add("hidden")
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
module LdapManager
|
||||
class SetNostrKey < LdapManagerService
|
||||
# Single entry point for changing an account's Nostr key. Every caller
|
||||
# (browser extension, remote signer connect/replace, removal) goes through
|
||||
# here so step-up authentication can be enforced in one place later.
|
||||
def initialize(user:, pubkey:)
|
||||
@user = user
|
||||
@pubkey = pubkey
|
||||
end
|
||||
|
||||
def call
|
||||
authorize!
|
||||
UpdateNostrKey.call(dn: @user.dn, pubkey: @pubkey)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def authorize!
|
||||
# Reserved for step-up authentication (password / 2FA) on key changes.
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,23 @@
|
||||
module NostrManager
|
||||
module RemoteSigner
|
||||
class DisconnectSessions < ApplicationService
|
||||
def initialize(sessions:)
|
||||
@sessions = sessions
|
||||
end
|
||||
|
||||
def call
|
||||
@sessions.each do |session|
|
||||
next if session.nil? || session.disconnecting?
|
||||
|
||||
if session.remote_signer_pubkey.present? && session.verified_at.present?
|
||||
session.update!(status: "disconnecting")
|
||||
session.clear_remember_token!
|
||||
NostrLogoutJob.perform_later(session.id)
|
||||
else
|
||||
session.destroy!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -16,7 +16,8 @@
|
||||
name="nostr_public_key" class="w-full" />
|
||||
<%= link_to nostr_pubkey_settings_path,
|
||||
class: 'btn-md btn-outline relative grow-0 shrink-0 text-red-700',
|
||||
data: { turbo_method: :delete, turbo_confirm: 'Are you sure?' } do %>
|
||||
data: { turbo_method: :delete,
|
||||
turbo_confirm: 'Remove your Nostr public key? Any connected remote signer will be disconnected.' } do %>
|
||||
Remove
|
||||
<% end %>
|
||||
</p>
|
||||
@@ -103,6 +104,7 @@
|
||||
create_url: nostr_signer_settings_path,
|
||||
destroy_url: nostr_signer_settings_path,
|
||||
status_url: nostr_signer_status_settings_path(token: @pending_signer_session.token),
|
||||
replace_url: nostr_signer_replace_settings_path(token: @pending_signer_session.token),
|
||||
title: "Nostr Connect",
|
||||
modal: true
|
||||
) %>
|
||||
|
||||
@@ -79,6 +79,7 @@ Rails.application.routes.draw do
|
||||
post 'nostr_publish', to: 'settings/nostr_events#create', as: :nostr_publish
|
||||
post 'nostr_signer', to: 'settings/nostr_signers#create', as: :nostr_signer
|
||||
get 'nostr_signer/status', to: 'settings/nostr_signers#status', as: :nostr_signer_status
|
||||
post 'nostr_signer/replace', to: 'settings/nostr_signers#replace', as: :nostr_signer_replace
|
||||
delete 'nostr_signer', to: 'settings/nostr_signers#destroy'
|
||||
post 'nostr_sign', to: 'settings/nostr_signatures#create', as: :nostr_sign
|
||||
get 'nostr_sign/:token', to: 'settings/nostr_signatures#show', as: :nostr_sign_status
|
||||
|
||||
@@ -149,4 +149,58 @@ RSpec.describe NostrSignerConnectComponent, type: :component do
|
||||
end
|
||||
expect(connect["class"]).to include("btn-gray")
|
||||
end
|
||||
|
||||
it "renders a hidden replace dialog when a replace URL is given" do
|
||||
pending_session = instance_double(
|
||||
NostrSignerSession,
|
||||
bunker?: false,
|
||||
nostrconnect_uri: "nostrconnect://#{SecureRandom.hex(32)}?relay=wss%3A%2F%2Frelay.example.com&secret=abc"
|
||||
)
|
||||
|
||||
rendered = render_inline(
|
||||
described_class.new(
|
||||
pending_session: pending_session,
|
||||
create_url: "/settings/nostr_signer",
|
||||
destroy_url: "/settings/nostr_signer",
|
||||
status_url: "/settings/nostr_signer/status",
|
||||
replace_url: "/settings/nostr_signer/replace",
|
||||
title: "Nostr Connect",
|
||||
modal: true
|
||||
)
|
||||
)
|
||||
|
||||
dialog = rendered.css("[data-nostr-signer-connect-target='replace']").first
|
||||
expect(dialog["class"]).to include("hidden")
|
||||
expect(dialog.css("[data-nostr-signer-connect-target='replaceExisting']").any?).to be(true)
|
||||
expect(dialog.css("[data-nostr-signer-connect-target='replaceSigner']").any?).to be(true)
|
||||
expect(dialog.text).to include("Replace existing key", "Cancel")
|
||||
|
||||
element = rendered.css("[data-controller='nostr-signer-connect']").first
|
||||
expect(element["data-nostr-signer-connect-replace-url-value"])
|
||||
.to eq("/settings/nostr_signer/replace")
|
||||
expect(element["data-nostr-signer-connect-destroy-url-value"])
|
||||
.to eq("/settings/nostr_signer")
|
||||
|
||||
expect(dialog.css("button[data-action='nostr-signer-connect#replace']").any?).to be(true)
|
||||
expect(dialog.css("button[data-action='nostr-signer-connect#cancel']").any?).to be(true)
|
||||
end
|
||||
|
||||
it "omits the replace dialog when no replace URL is given" do
|
||||
pending_session = instance_double(
|
||||
NostrSignerSession,
|
||||
bunker?: false,
|
||||
nostrconnect_uri: "nostrconnect://#{SecureRandom.hex(32)}?relay=wss%3A%2F%2Frelay.example.com&secret=abc"
|
||||
)
|
||||
|
||||
rendered = render_inline(
|
||||
described_class.new(
|
||||
pending_session: pending_session,
|
||||
create_url: "/users/nostr_connect",
|
||||
status_url: "/users/nostr_connect/status",
|
||||
show_disconnect: false
|
||||
)
|
||||
)
|
||||
|
||||
expect(rendered.css("[data-nostr-signer-connect-target='replace']").any?).to be(false)
|
||||
end
|
||||
end
|
||||
@@ -140,6 +140,90 @@ RSpec.describe "Settings::NostrSigners", type: :request do
|
||||
expect(JSON.parse(response.body)["status"]).to eq("expired")
|
||||
expect(session.reload).to be_expired
|
||||
end
|
||||
|
||||
it "connects without rewriting LDAP when the signer key already matches" do
|
||||
session = create_verified_session
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(user_pubkey)
|
||||
allow(LdapManager::FetchUserByNostrKey).to receive(:call).with(pubkey: user_pubkey).and_return(nil)
|
||||
expect(LdapManager::SetNostrKey).not_to receive(:call)
|
||||
|
||||
get nostr_signer_status_settings_path(token: session.token), as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)["status"]).to eq("connected")
|
||||
expect(session.reload).to be_connected
|
||||
end
|
||||
|
||||
it "asks to replace instead of overwriting a different existing key" do
|
||||
session = create_verified_session
|
||||
existing = SecureRandom.hex(32)
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(existing)
|
||||
allow(LdapManager::FetchUserByNostrKey).to receive(:call).with(pubkey: user_pubkey).and_return(nil)
|
||||
expect(LdapManager::SetNostrKey).not_to receive(:call)
|
||||
|
||||
get nostr_signer_status_settings_path(token: session.token), as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
data = JSON.parse(response.body)
|
||||
expect(data["status"]).to eq("replace_required")
|
||||
expect(data["existing_npub"]).to eq(Nostr::PublicKey.new(existing).to_bech32)
|
||||
expect(data["signer_npub"]).to eq(session.user_pubkey_bech32)
|
||||
expect(session.reload).to be_verified
|
||||
expect(response.headers["Set-Cookie"]).not_to include("nostr_remember")
|
||||
end
|
||||
|
||||
it "keeps returning replace_required while the conflict is unresolved" do
|
||||
session = create_verified_session
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(SecureRandom.hex(32))
|
||||
allow(LdapManager::FetchUserByNostrKey).to receive(:call).and_return(nil)
|
||||
|
||||
2.times do
|
||||
get nostr_signer_status_settings_path(token: session.token), as: :json
|
||||
expect(JSON.parse(response.body)["status"]).to eq("replace_required")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST /settings/nostr_signer/replace" do
|
||||
it "overwrites the existing key when replacement is confirmed" do
|
||||
session = create_verified_session
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(SecureRandom.hex(32))
|
||||
allow(LdapManager::FetchUserByNostrKey).to receive(:call).with(pubkey: user_pubkey).and_return(nil)
|
||||
expect(LdapManager::SetNostrKey).to receive(:call)
|
||||
.with(hash_including(pubkey: user_pubkey)).and_return(0)
|
||||
|
||||
post nostr_signer_replace_settings_path(token: session.token), as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)["status"]).to eq("connected")
|
||||
expect(session.reload).to be_connected
|
||||
end
|
||||
|
||||
it "still rejects a key owned by another account" do
|
||||
session = create_verified_session
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return(SecureRandom.hex(32))
|
||||
allow(LdapManager::FetchUserByNostrKey).to receive(:call).with(pubkey: user_pubkey).and_return(other_user)
|
||||
expect(LdapManager::SetNostrKey).not_to receive(:call)
|
||||
|
||||
post nostr_signer_replace_settings_path(token: session.token), as: :json
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
|
||||
it "rejects a session that is not verified" do
|
||||
session = NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user)
|
||||
expect(LdapManager::SetNostrKey).not_to receive(:call)
|
||||
|
||||
post nostr_signer_replace_settings_path(token: session.token), as: :json
|
||||
|
||||
expect(response).to have_http_status(:unprocessable_entity)
|
||||
end
|
||||
|
||||
it "rejects an unknown token" do
|
||||
post nostr_signer_replace_settings_path(token: "unknown"), as: :json
|
||||
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /settings/nostr_signer" do
|
||||
|
||||
@@ -78,6 +78,47 @@ RSpec.describe "Settings", type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
context "With a different key already on the account" do
|
||||
before do
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return("deadbeef" * 8)
|
||||
expect(LdapManager::SetNostrKey).not_to receive(:call)
|
||||
|
||||
post set_nostr_pubkey_settings_path,
|
||||
params: { signed_event: auth_event }.to_json,
|
||||
headers: {
|
||||
"CONTENT_TYPE" => "application/json",
|
||||
"HTTP_ACCEPT" => "application/json"
|
||||
}
|
||||
end
|
||||
|
||||
it "returns a 422 status" do
|
||||
expect(response).to have_http_status(422)
|
||||
end
|
||||
|
||||
it "asks the user to remove or confirm the replacement" do
|
||||
expect(flash[:alert]).to include("already has a different Nostr public key")
|
||||
end
|
||||
end
|
||||
|
||||
context "With a different key and replacement confirmed" do
|
||||
before do
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return("deadbeef" * 8)
|
||||
expect(LdapManager::SetNostrKey).to receive(:call)
|
||||
.with(hash_including(pubkey: auth_event["pubkey"])).and_return(0)
|
||||
|
||||
post set_nostr_pubkey_settings_path,
|
||||
params: { signed_event: auth_event, replace: "true" }.to_json,
|
||||
headers: {
|
||||
"CONTENT_TYPE" => "application/json",
|
||||
"HTTP_ACCEPT" => "application/json"
|
||||
}
|
||||
end
|
||||
|
||||
it "returns a success status" do
|
||||
expect(response).to have_http_status(200)
|
||||
end
|
||||
end
|
||||
|
||||
context "With wrong site tag" do
|
||||
before do
|
||||
Setting.accounts_domain = "accounts.wikipedia.org"
|
||||
@@ -152,6 +193,44 @@ RSpec.describe "Settings", type: :request do
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE /settings/nostr_pubkey" do
|
||||
let(:user_pubkey) { SecureRandom.hex(32) }
|
||||
|
||||
before do
|
||||
allow(LdapManager::SetNostrKey).to receive(:call).and_return(0)
|
||||
end
|
||||
|
||||
it "removes the key and disconnects an active remote signer" do
|
||||
session = NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user)
|
||||
session.update!(
|
||||
status: "connected",
|
||||
user_pubkey: user_pubkey,
|
||||
remote_signer_pubkey: SecureRandom.hex(32),
|
||||
verified_at: Time.current,
|
||||
consumed_at: Time.current
|
||||
)
|
||||
session.remember!
|
||||
|
||||
expect do
|
||||
delete nostr_pubkey_settings_path
|
||||
end.to have_enqueued_job(NostrLogoutJob).with(session.id)
|
||||
|
||||
expect(LdapManager::SetNostrKey).to have_received(:call)
|
||||
.with(hash_including(pubkey: nil))
|
||||
expect(session.reload.status).to eq("disconnecting")
|
||||
expect(session.remember_token_digest).to be_nil
|
||||
expect(response).to redirect_to(setting_path(:nostr))
|
||||
end
|
||||
|
||||
it "destroys a pending signer session" do
|
||||
NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user)
|
||||
|
||||
expect do
|
||||
delete nostr_pubkey_settings_path
|
||||
end.to change(NostrSignerSession, :count).by(-1)
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /settings/fetch_nostr_metadata" do
|
||||
before do
|
||||
allow_any_instance_of(User).to receive(:nostr_pubkey).and_return("abc123")
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe LdapManager::SetNostrKey, type: :model do
|
||||
let(:user) { create :user, cn: "mark", ou: "kosmos.org" }
|
||||
let(:dn) { "cn=mark,ou=kosmos.org,cn=users,dc=kosmos,dc=org" }
|
||||
|
||||
before do
|
||||
allow(user).to receive(:dn).and_return(dn)
|
||||
end
|
||||
|
||||
it "writes the key through the low-level LDAP writer" do
|
||||
expect(LdapManager::UpdateNostrKey).to receive(:call)
|
||||
.with(dn: dn, pubkey: "abc").and_return(0)
|
||||
|
||||
described_class.call(user: user, pubkey: "abc")
|
||||
end
|
||||
|
||||
it "removes the key when the pubkey is blank" do
|
||||
expect(LdapManager::UpdateNostrKey).to receive(:call)
|
||||
.with(dn: dn, pubkey: nil).and_return(0)
|
||||
|
||||
described_class.call(user: user, pubkey: nil)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,51 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe NostrManager::RemoteSigner::DisconnectSessions, type: :model do
|
||||
let(:user) { create :user, cn: "mark", ou: "kosmos.org" }
|
||||
let(:user_pubkey) { SecureRandom.hex(32) }
|
||||
|
||||
def connected_session
|
||||
NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user).tap do |session|
|
||||
session.update!(
|
||||
status: "connected",
|
||||
remote_signer_pubkey: SecureRandom.hex(32),
|
||||
user_pubkey: user_pubkey,
|
||||
verified_at: Time.current,
|
||||
consumed_at: Time.current
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
it "starts a NIP-46 logout and clears the remember token for a connected session" do
|
||||
session = connected_session
|
||||
session.remember!
|
||||
|
||||
expect do
|
||||
described_class.call(sessions: NostrSignerSession.where(id: session.id))
|
||||
end.to have_enqueued_job(NostrLogoutJob).with(session.id)
|
||||
|
||||
expect(session.reload.status).to eq("disconnecting")
|
||||
expect(session.remember_token_digest).to be_nil
|
||||
end
|
||||
|
||||
it "destroys a pending session without logging out" do
|
||||
session = NostrManager::RemoteSigner::CreateSession.call(flow: "settings", user: user)
|
||||
|
||||
expect do
|
||||
described_class.call(sessions: NostrSignerSession.where(id: session.id))
|
||||
end.not_to have_enqueued_job(NostrLogoutJob)
|
||||
|
||||
expect(NostrSignerSession.find_by(id: session.id)).to be_nil
|
||||
end
|
||||
|
||||
it "ignores already disconnecting and nil sessions" do
|
||||
session = connected_session
|
||||
session.update!(status: "disconnecting")
|
||||
|
||||
expect do
|
||||
described_class.call(sessions: [session, nil])
|
||||
end.not_to have_enqueued_job(NostrLogoutJob)
|
||||
|
||||
expect(session.reload.status).to eq("disconnecting")
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user