diff --git a/app/controllers/well_known_controller.rb b/app/controllers/well_known_controller.rb new file mode 100644 index 0000000..02eb02c --- /dev/null +++ b/app/controllers/well_known_controller.rb @@ -0,0 +1,16 @@ +class WellKnownController < ApplicationController + 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? + + respond_to do |format| + format.json do + render json: { + names: { "#{@user.cn}": @user.nostr_pubkey } + }.to_json + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 8668111..b454463 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,6 +44,8 @@ Rails.application.routes.draw do get 'keysend/:address', to: 'lnurlpay#keysend', as: 'lightning_address_keysend', constraints: { address: /[^\/]+/} + get '.well-known/nostr', to: 'well_known#nostr' + post 'webhooks/lndhub', to: 'webhooks#lndhub' namespace :api do diff --git a/spec/requests/well_known_spec.rb b/spec/requests/well_known_spec.rb new file mode 100644 index 0000000..1a24a12 --- /dev/null +++ b/spec/requests/well_known_spec.rb @@ -0,0 +1,41 @@ +require 'rails_helper' + +RSpec.describe "Well-known URLs", type: :request do + describe "GET /nostr" do + context "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 + 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 + let(:user) { create :user, cn: 'spongebob', ou: 'kosmos.org' } + + it "returns a 404 status" do + get "/.well-known/nostr.json?name=spongebob" + expect(response).to have_http_status(:not_found) + end + end + + context "user with nostr pubkey" do + let(:user) { create :user, cn: 'bobdylan', ou: 'kosmos.org', nostr_pubkey: '438d35a6750d0dd6b75d032af8a768aad76b62f0c70ecb45f9c4d9e63540f7f4' } + before { user.save! } + + it "returns a NIP-05 response" do + get "/.well-known/nostr.json?name=bobdylan" + expect(response).to have_http_status(:ok) + res = JSON.parse(response.body) + expect(res["names"].keys.size).to eq(1) + expect(res["names"]["bobdylan"]).to eq(user.nostr_pubkey) + end + end + end +end