Stop abandoned listeners after 60s without polling, re-enqueue from status polls with a throttle, and add a Start over action for stuck sessions.
191 lines
6.1 KiB
Ruby
191 lines
6.1 KiB
Ruby
module Users
|
|
class NostrConnectsController < ApplicationController
|
|
RATE_LIMIT = 10
|
|
RATE_WINDOW = 10.minutes
|
|
STATUS_RATE_LIMIT = 240
|
|
STATUS_RATE_WINDOW = 5.minutes
|
|
MAX_PENDING = 20
|
|
LISTENER_ENQUEUE_THROTTLE = 5.seconds
|
|
|
|
def show
|
|
unless Setting.nostr_enabled? && NostrSignerSession.encryption_available?
|
|
redirect_to new_user_session_path, alert: "Nostr login is not available right now."
|
|
return
|
|
end
|
|
|
|
@signer_session = find_or_create_session
|
|
|
|
if @signer_session.nil?
|
|
redirect_to new_user_session_path,
|
|
alert: "Too many connection attempts. Please try again later."
|
|
else
|
|
session[:nostr_connect_token] = @signer_session.token
|
|
end
|
|
end
|
|
|
|
def create
|
|
unless Setting.nostr_enabled? && NostrSignerSession.encryption_available?
|
|
redirect_to new_user_session_path, alert: "Nostr login is not available right now."
|
|
return
|
|
end
|
|
|
|
if rate_limited?
|
|
redirect_to new_user_session_path,
|
|
alert: "Too many connection attempts. Please try again later."
|
|
return
|
|
end
|
|
|
|
if pending_connections_exceeded?
|
|
redirect_to new_user_session_path,
|
|
alert: "Too many pending connections. Please try again later."
|
|
return
|
|
end
|
|
|
|
parsed = NostrManager::RemoteSigner::ParseBunkerUri.call(uri: params[:bunker_uri])
|
|
signer_session = NostrManager::RemoteSigner::CreateSession.call(
|
|
flow: "login",
|
|
relays: parsed[:relays],
|
|
connection_method: "bunker",
|
|
remote_signer_pubkey: parsed[:remote_signer_pubkey],
|
|
secret: parsed[:secret]
|
|
)
|
|
|
|
session[:nostr_connect_token] = signer_session.token
|
|
NostrConnectListenerJob.perform_later(signer_session.id)
|
|
|
|
redirect_to nostr_connect_path
|
|
rescue NostrManager::RemoteSigner::Error => e
|
|
redirect_to nostr_connect_path, alert: e.message
|
|
end
|
|
|
|
def status
|
|
if status_rate_limited?
|
|
return render json: { error: "Too many status requests. Please try again later." },
|
|
status: :too_many_requests
|
|
end
|
|
|
|
signer_session = NostrSignerSession.find_by_token(session[:nostr_connect_token])
|
|
|
|
unless signer_session&.flow == "login"
|
|
return render json: { status: "failed", error: "Unknown connection" }, status: :not_found
|
|
end
|
|
|
|
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
|
|
|
|
ensure_listener(signer_session) if signer_session.pending?
|
|
|
|
if signer_session.verified?
|
|
sign_in_user(signer_session)
|
|
elsif signer_session.connected? && signer_session.user.present?
|
|
sign_in(:user, signer_session.user)
|
|
session.delete(:nostr_connect_token)
|
|
render json: {
|
|
status: "connected",
|
|
redirect_url: after_sign_in_path_for(signer_session.user)
|
|
}
|
|
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 restart
|
|
existing = NostrSignerSession.find_by_token(session[:nostr_connect_token])
|
|
existing.expire! if existing && (existing.pending? || existing.verified?)
|
|
session.delete(:nostr_connect_token)
|
|
|
|
redirect_to nostr_connect_path
|
|
end
|
|
|
|
private
|
|
|
|
def ensure_listener(signer_session)
|
|
started_at = signer_session.listener_started_at
|
|
return if started_at.present? && started_at > NostrConnectListenerJob::LISTENER_STALE_AFTER.ago
|
|
|
|
key = "nostr_listener_enqueued:#{signer_session.id}"
|
|
return if Rails.cache.read(key)
|
|
|
|
Rails.cache.write(key, true, expires_in: LISTENER_ENQUEUE_THROTTLE)
|
|
NostrConnectListenerJob.perform_later(signer_session.id)
|
|
end
|
|
|
|
def find_or_create_session
|
|
existing = NostrSignerSession.find_by_token(session[:nostr_connect_token])
|
|
|
|
if existing && existing.flow == "login" && (existing.pending? || existing.verified?)
|
|
return existing
|
|
end
|
|
|
|
return nil if pending_connections_exceeded?
|
|
|
|
NostrManager::RemoteSigner::CreateSession.call(flow: "login").tap do |signer_session|
|
|
NostrConnectListenerJob.perform_later(signer_session.id)
|
|
end
|
|
end
|
|
|
|
def sign_in_user(signer_session)
|
|
user = LdapManager::FetchUserByNostrKey.call(pubkey: signer_session.user_pubkey)
|
|
|
|
if user.nil?
|
|
signer_session.fail!("No account is linked to this public key. Add it in your settings first.")
|
|
return render json: { status: "failed", error: signer_session.error }, status: :unprocessable_entity
|
|
end
|
|
|
|
signer_session.with_lock do
|
|
next if signer_session.consumed_at.present?
|
|
|
|
NostrSignerSession
|
|
.where(user: user, status: %w[pending verified connected])
|
|
.where.not(id: signer_session.id)
|
|
.delete_all
|
|
|
|
signer_session.update!(
|
|
user: user,
|
|
status: "connected",
|
|
consumed_at: Time.current
|
|
)
|
|
remember_nostr_account(user, session_record: signer_session)
|
|
end
|
|
|
|
sign_in(:user, user)
|
|
session.delete(:nostr_connect_token)
|
|
|
|
render json: {
|
|
status: "connected",
|
|
redirect_url: after_sign_in_path_for(user)
|
|
}
|
|
end
|
|
|
|
def rate_limited?
|
|
key = "nostr_connect_ip:#{request.remote_ip}"
|
|
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_connect_status_ip:#{request.remote_ip}"
|
|
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
|
|
end
|
|
end |