Harden the continue-as reauth flow against stale sessions
This commit is contained in:
5 files changed
+141
-11
No files matched your search
@@ -9,6 +9,7 @@ module Users
|
||||
DIFFERENT_LIMIT = 10
|
||||
DIFFERENT_WINDOW = 10.minutes
|
||||
|
||||
before_action :redirect_signed_in_user, only: [:create, :show]
|
||||
before_action :require_nostr_available
|
||||
|
||||
def create
|
||||
@@ -75,6 +76,7 @@ module Users
|
||||
sign_request = current_reauth_request
|
||||
|
||||
if remembered.nil? || sign_request.nil? || sign_request.user_id != remembered.user_id
|
||||
session.delete(:nostr_reauth_token)
|
||||
return render json: { status: "expired", error: "Your login request expired. Please try again." },
|
||||
status: :gone
|
||||
end
|
||||
@@ -85,25 +87,34 @@ module Users
|
||||
end
|
||||
|
||||
if sign_request.consumed?
|
||||
return render json: {
|
||||
status: "connected",
|
||||
redirect_url: after_sign_in_path_for(remembered.user)
|
||||
}
|
||||
if user_signed_in? && current_user.id == sign_request.user_id
|
||||
return render json: {
|
||||
status: "connected",
|
||||
redirect_url: after_sign_in_path_for(current_user)
|
||||
}
|
||||
end
|
||||
|
||||
session.delete(:nostr_reauth_token)
|
||||
return render json: { status: "expired", error: "Your login request expired. Please try again." },
|
||||
status: :gone
|
||||
end
|
||||
|
||||
case sign_request.status
|
||||
when "signed"
|
||||
if sign_request.expires_at < Time.current
|
||||
sign_request.expire!
|
||||
session.delete(:nostr_reauth_token)
|
||||
render json: { status: "expired", error: sign_request.error }, status: :gone
|
||||
else
|
||||
complete_sign_in(remembered, sign_request)
|
||||
end
|
||||
when "failed", "expired"
|
||||
session.delete(:nostr_reauth_token)
|
||||
render json: { status: sign_request.status, error: sign_request.error }, status: :unprocessable_entity
|
||||
else
|
||||
if sign_request.expires_at < Time.current
|
||||
sign_request.expire!
|
||||
session.delete(:nostr_reauth_token)
|
||||
render json: { status: "expired", error: sign_request.error }, status: :gone
|
||||
else
|
||||
render json: { status: "pending" }
|
||||
@@ -129,6 +140,12 @@ module Users
|
||||
|
||||
private
|
||||
|
||||
def redirect_signed_in_user
|
||||
return unless user_signed_in?
|
||||
|
||||
redirect_to after_sign_in_path_for(current_user), notice: "You are already logged in."
|
||||
end
|
||||
|
||||
def complete_sign_in(remembered, sign_request)
|
||||
user = remembered.user
|
||||
failure = nil
|
||||
@@ -234,6 +251,8 @@ module Users
|
||||
end
|
||||
|
||||
def render_error(message, status)
|
||||
session.delete(:nostr_reauth_token)
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to new_user_session_path, alert: message }
|
||||
format.json { render json: { status: "failed", error: message }, status: status }
|
||||
|
||||
@@ -29,9 +29,10 @@ class Users::SessionsController < Devise::SessionsController
|
||||
# end
|
||||
|
||||
# DELETE /resource/sign_out
|
||||
# def destroy
|
||||
# super
|
||||
# end
|
||||
def destroy
|
||||
session.delete(:nostr_reauth_token)
|
||||
super
|
||||
end
|
||||
|
||||
# POST /users/nostr_login
|
||||
def nostr_login
|
||||
|
||||
@@ -6,11 +6,29 @@ class NostrSignEventJob < ApplicationJob
|
||||
return if request.nil? || !request.pending?
|
||||
|
||||
signed = NostrManager::RemoteSigner::SignEvent.call(request: request)
|
||||
request.sign!(signed)
|
||||
|
||||
request.with_lock do
|
||||
if request.pending?
|
||||
request.sign!(signed)
|
||||
elsif request.status == "pending"
|
||||
request.expire!
|
||||
end
|
||||
end
|
||||
rescue NostrManager::RemoteSigner::Error => e
|
||||
NostrSignRequest.find_by(id: request_id)&.fail!(e.message)
|
||||
fail_request(request_id, e.message)
|
||||
rescue StandardError => e
|
||||
Rails.logger.warn("[nostr][signer] signing job failed: #{e.class}: #{e.message}")
|
||||
NostrSignRequest.find_by(id: request_id)&.fail!("Could not sign the event. Please try again.")
|
||||
fail_request(request_id, "Could not sign the event. Please try again.")
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fail_request(request_id, message)
|
||||
request = NostrSignRequest.find_by(id: request_id)
|
||||
return if request.nil?
|
||||
|
||||
request.with_lock do
|
||||
request.fail!(message) if request.pending?
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -50,4 +50,17 @@ RSpec.describe NostrSignEventJob, type: :job do
|
||||
|
||||
described_class.perform_now(request.id)
|
||||
end
|
||||
|
||||
it "expires a request when the signer responds after the TTL" do
|
||||
request = build_request
|
||||
signed = Nostr::User.new.create_event(kind: 0, content: "{}", tags: [])
|
||||
allow(NostrManager::RemoteSigner::SignEvent).to receive(:call) do
|
||||
request.update_columns(expires_at: 1.minute.ago)
|
||||
signed
|
||||
end
|
||||
|
||||
described_class.perform_now(request.id)
|
||||
|
||||
expect(request.reload.status).to eq("expired")
|
||||
end
|
||||
end
|
||||
@@ -209,4 +209,83 @@ RSpec.describe "Users::NostrContinues", type: :request do
|
||||
expect(cookies[:nostr_remember]).to be_blank
|
||||
end
|
||||
end
|
||||
|
||||
describe "when already signed in" do
|
||||
it "redirects instead of starting a sign request" do
|
||||
remember_session
|
||||
login_as user, scope: :user
|
||||
|
||||
expect do
|
||||
post nostr_continue_path
|
||||
end.not_to change(NostrSignRequest, :count)
|
||||
|
||||
expect(response).to redirect_to(root_path)
|
||||
expect(flash[:notice]).to eq("You are already logged in.")
|
||||
end
|
||||
|
||||
it "redirects the waiting page" do
|
||||
login_as user, scope: :user
|
||||
|
||||
get nostr_continue_page_path
|
||||
|
||||
expect(response).to redirect_to(root_path)
|
||||
end
|
||||
end
|
||||
|
||||
describe "a consumed request" do
|
||||
def consume_pending_request
|
||||
post nostr_continue_path
|
||||
request = NostrSignRequest.reauth.last
|
||||
request.update!(status: "signed", consumed_at: Time.current)
|
||||
request
|
||||
end
|
||||
|
||||
it "does not report connected when signed out" do
|
||||
remember_session
|
||||
request = consume_pending_request
|
||||
|
||||
get nostr_continue_status_path, as: :json
|
||||
|
||||
expect(response).to have_http_status(:gone)
|
||||
expect(JSON.parse(response.body)["status"]).to eq("expired")
|
||||
expect(session[:nostr_reauth_token]).to be_nil
|
||||
expect(request.reload).to be_consumed
|
||||
end
|
||||
|
||||
it "reports connected when signed in as the remembered user" do
|
||||
remember_session
|
||||
consume_pending_request
|
||||
login_as user, scope: :user
|
||||
|
||||
get nostr_continue_status_path, as: :json
|
||||
|
||||
expect(response).to have_http_status(:ok)
|
||||
expect(JSON.parse(response.body)["status"]).to eq("connected")
|
||||
end
|
||||
end
|
||||
|
||||
describe "an expired request" do
|
||||
it "clears the reauth token" do
|
||||
remember_session
|
||||
post nostr_continue_path
|
||||
NostrSignRequest.reauth.last.update!(expires_at: 1.minute.ago)
|
||||
|
||||
get nostr_continue_status_path, as: :json
|
||||
|
||||
expect(response).to have_http_status(:gone)
|
||||
expect(session[:nostr_reauth_token]).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
describe "signing out" do
|
||||
it "clears the reauth token" do
|
||||
remember_session
|
||||
post nostr_continue_path
|
||||
login_as user, scope: :user
|
||||
|
||||
get destroy_user_session_path
|
||||
|
||||
expect(session[:nostr_reauth_token]).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user