Support "_" placeholder username for domain's own NIP-05
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

This commit is contained in:
Râu Cao 2024-06-19 20:57:22 +02:00
parent 7ac3130c18
commit 48ab96dda9
Signed by: raucao
GPG Key ID: 37036C356E56CC51
2 changed files with 43 additions and 13 deletions

View File

@ -1,18 +1,23 @@
class WellKnownController < ApplicationController
before_action :require_nostr_enabled, only: [ :nostr ]
def nostr
http_status :unprocessable_entity and return if params[:name].blank?
domain = request.headers["X-Forwarded-Host"].presence || Setting.primary_domain
@user = User.where(cn: params[:name], ou: domain).first
http_status :not_found and return if @user.nil? || @user.nostr_pubkey.blank?
relay_url = Setting.nostr_relay_url
res = {
names: { @user.cn => @user.nostr_pubkey }
}
if params[:name] == "_"
# pubkey for the primary domain without a username (e.g. kosmos.org)
res = { names: { "_": Setting.nostr_public_key } }
else
@user = User.where(cn: params[:name], ou: domain).first
http_status :not_found and return if @user.nil? || @user.nostr_pubkey.blank?
if Setting.nostr_relay_url
res[:relays] = {
@user.nostr_pubkey => [ Setting.nostr_relay_url ]
}
res = { names: { @user.cn => @user.nostr_pubkey } }
end
if relay_url
res[:relays] = { @user.nostr_pubkey => [ relay_url ] }
end
respond_to do |format|
@ -21,4 +26,10 @@ class WellKnownController < ApplicationController
end
end
end
private
def require_nostr_enabled
http_status :not_found unless Setting.nostr_enabled?
end
end

View File

@ -2,21 +2,21 @@ require 'rails_helper'
RSpec.describe "Well-known URLs", type: :request do
describe "GET /nostr" do
context "without username param" do
describe "without username param" do
it "returns a 422 status" do
get "/.well-known/nostr.json"
expect(response).to have_http_status(:unprocessable_entity)
end
end
context "non-existent user" do
describe "non-existent user" do
it "returns a 404 status" do
get "/.well-known/nostr.json?name=bob"
expect(response).to have_http_status(:not_found)
end
end
context "user does not have a nostr pubkey configured" do
describe "user does not have a nostr pubkey configured" do
let(:user) { create :user, cn: 'spongebob', ou: 'kosmos.org' }
before do
@ -30,7 +30,7 @@ RSpec.describe "Well-known URLs", type: :request do
end
end
context "user with nostr pubkey" do
describe "user with nostr pubkey" do
let(:user) { create :user, cn: 'bobdylan', ou: 'kosmos.org' }
before do
user.save!
@ -67,5 +67,24 @@ RSpec.describe "Well-known URLs", type: :request do
end
end
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)
end
context "nostr service integration not enabled" do
before do
Setting.nostr_enabled = false
end
it "returns a 404 status" do
get "/.well-known/nostr.json?name=_"
expect(response).to have_http_status(:not_found)
end
end
end
end
end