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 Sep 25, 2023
1 parent 5ca1261 commit e4e18e4
Show file tree
Hide file tree
Showing 3 changed files with 52 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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
get '.well-known/webfinger', to: 'well_known/webfinger#show', as: :webfinger
get '.well-known/change-password', to: redirect('/auth/edit')
get '.well-known/proxy', to: redirect { |_, request| "/authorize_interaction?#{request.params.to_query}" }
get '.well-known/protocol-handler', to: 'well_known/protocol_handler#show', as: :protocol_handler


get '/nodeinfo/2.0', to: 'well_known/nodeinfo#show', as: :nodeinfo_schema

Expand Down

0 comments on commit e4e18e4

Please sign in to comment.