Skip to content

Commit

Permalink
fedilinks: Add well-known protocol handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SoniEx2 authored and arachnist committed Jul 28, 2024
1 parent 4163a61 commit c1e3c6e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/controllers/authorize_interactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class AuthorizeInteractionsController < ApplicationController
include Authorization

before_action :authenticate_user!
before_action :authenticate_user_if_remote!
before_action :set_resource

def show
Expand All @@ -18,6 +18,11 @@ def show

private

def authenticate_user_if_remote!
return if uri_param_is_url? && Rails.configuration.x.local_domain == parsed_uri.host
authenticate_user!
end

def set_resource
@resource = located_resource
authorize(@resource, :show?) if @resource.is_a?(Status)
Expand Down
44 changes: 44 additions & 0 deletions app/controllers/well_known/protocol_handler_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module WellKnown
class ProtocolHandlerController < ActionController::Base # rubocop:disable Rails/ApplicationController

before_action :set_target
before_action :target_acceptable?

rescue_from ActionController::ParameterMissing, with: :bad_request

def show
redirect_to authorize_interaction_path(uri: @target.sub("web+ap", "https"))
end

private

def set_target
@target = target_param
end

# NOTE: supports "probing"
def target_acceptable?
# FIXME: this should be web+ap: but doing it this way avoids issues in
# the short term... revisit this once web+ap:foo... is being used in
# addition to web+ap://example/...
return if @target.starts_with? "web+ap://"
not_found
end

def target_param
params.require(:target)
end

def bad_request
expires_in(3.minutes, public: true)
head 400
end

def not_found
expires_in(3.minutes, public: true)
head 404
end
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def redirect_with_vary(path)
get 'host-meta', to: 'host_meta#show', as: :host_meta, defaults: { format: 'xml' }
get 'nodeinfo', to: 'node_info#index', as: :nodeinfo, defaults: { format: 'json' }
get 'webfinger', to: 'webfinger#show', as: :webfinger
get 'protocol-handler', to: 'protocol_handler#show', as: :protocol_handler
end
get 'change-password', to: redirect('/auth/edit'), as: nil
get 'proxy', to: redirect { |_, request| "/authorize_interaction?#{request.params.to_query}" }, as: nil
Expand Down

0 comments on commit c1e3c6e

Please sign in to comment.