From e544c281051deacca85d36ea7c19eabe447bf313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 11 Sep 2024 16:28:12 +0200 Subject: [PATCH 1/2] Config for separate primary domain Nostr pubkey Allow to configure a separate key for the NIP-05 address of the primary domain vs the accounts domain. --- app/controllers/well_known_controller.rb | 10 ++++-- .../concerns/settings/nostr_settings.rb | 3 ++ .../admin/settings/services/_nostr.html.erb | 5 +++ spec/requests/well_known_spec.rb | 34 ++++++++++++++++--- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/controllers/well_known_controller.rb b/app/controllers/well_known_controller.rb index fd3c31d..cb0c018 100644 --- a/app/controllers/well_known_controller.rb +++ b/app/controllers/well_known_controller.rb @@ -10,8 +10,14 @@ class WellKnownController < ApplicationController relay_url = Setting.nostr_relay_url.presence if params[:name] == "_" - # pubkey for the primary domain without a username (e.g. kosmos.org) - res = { names: { "_": Setting.nostr_public_key } } + if domain == Setting.primary_domain + # pubkey for the primary domain without a username (e.g. kosmos.org) + res = { names: { "_": Setting.nostr_public_key_primary_domain || Setting.nostr_public_key } } + else + # pubkey for the akkounts domain without a username (e.g. accounts.kosmos.org) + res = { names: { "_": Setting.nostr_public_key } } + end + res[:relays] = { "_" => [ relay_url ] } if relay_url else @user = User.where(cn: params[:name], ou: domain).first diff --git a/app/models/concerns/settings/nostr_settings.rb b/app/models/concerns/settings/nostr_settings.rb index 194b3f0..dbe49b5 100644 --- a/app/models/concerns/settings/nostr_settings.rb +++ b/app/models/concerns/settings/nostr_settings.rb @@ -12,6 +12,9 @@ module Settings field :nostr_public_key, type: :string, default: ENV["NOSTR_PUBLIC_KEY"].presence + field :nostr_public_key_primary_domain, type: :string, + default: nil + field :nostr_relay_url, type: :string, default: ENV["NOSTR_RELAY_URL"].presence diff --git a/app/views/admin/settings/services/_nostr.html.erb b/app/views/admin/settings/services/_nostr.html.erb index c5db5e3..f714af6 100644 --- a/app/views/admin/settings/services/_nostr.html.erb +++ b/app/views/admin/settings/services/_nostr.html.erb @@ -19,6 +19,11 @@ title: "Public key", description: "The corresponding public key of the accounts service" ) %> + <%= render FormElements::FieldsetResettableSettingComponent.new( + key: :nostr_public_key_primary_domain, + title: "Public key for primary domain (NIP-05)", + description: "(optional) A different pubkey to announce for the _@#{Setting.primary_domain} Nostr address" + ) %> <%= render FormElements::FieldsetResettableSettingComponent.new( key: :nostr_relay_url, title: "Relay URL", diff --git a/spec/requests/well_known_spec.rb b/spec/requests/well_known_spec.rb index a27f14c..0701c2f 100644 --- a/spec/requests/well_known_spec.rb +++ b/spec/requests/well_known_spec.rb @@ -79,10 +79,36 @@ RSpec.describe "Well-known URLs", type: :request do end describe "placeholder username for domain's own pubkey" do - it "returns the configured nostr pubkey" do - get "/.well-known/nostr.json?name=_" - res = JSON.parse(response.body) - expect(res["names"]["_"]).to eq(Setting.nostr_public_key) + describe "for primary domain" do + context "no different pubkey configured for primary domain" do + it "returns the akkounts nostr pubkey" do + get "/.well-known/nostr.json?name=_" + res = JSON.parse(response.body) + expect(res["names"]["_"]).to eq("bdd76ce2934b2f591f9fad2ebe9da18f20d2921de527494ba00eeaa0a0efadcf") + end + end + + context "different pubkey configured for primary domain" do + before do + Setting.nostr_public_key_primary_domain = "b3e8f62fbe41217ffc0aa1e178d297339932d8ba4f46d9c7df3b61575e78fecc" + end + + it "returns the primary domain's nostr pubkey" do + get "/.well-known/nostr.json?name=_" + res = JSON.parse(response.body) + expect(res["names"]["_"]).to eq("b3e8f62fbe41217ffc0aa1e178d297339932d8ba4f46d9c7df3b61575e78fecc") + end + end + end + + describe "for akkounts domain" do + it "returns the configured nostr pubkey" do + headers = { "X-Forwarded-Host" => "accounts.kosmos.org" } + get "/.well-known/nostr.json?name=_" + + res = JSON.parse(response.body) + expect(res["names"]["_"]).to eq("bdd76ce2934b2f591f9fad2ebe9da18f20d2921de527494ba00eeaa0a0efadcf") + end end context "with relay configured" do From ac77e5b7c15b1d0dcf146b751f78226a6bb0ee59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 11 Sep 2024 16:31:04 +0200 Subject: [PATCH 2/2] Allow ENV var for new setting --- app/models/concerns/settings/nostr_settings.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/settings/nostr_settings.rb b/app/models/concerns/settings/nostr_settings.rb index dbe49b5..84dba49 100644 --- a/app/models/concerns/settings/nostr_settings.rb +++ b/app/models/concerns/settings/nostr_settings.rb @@ -13,7 +13,7 @@ module Settings default: ENV["NOSTR_PUBLIC_KEY"].presence field :nostr_public_key_primary_domain, type: :string, - default: nil + default: ENV["NOSTR_PUBLIC_KEY_PRIMARY_DOMAIN"].presence field :nostr_relay_url, type: :string, default: ENV["NOSTR_RELAY_URL"].presence