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 4, 2024
1 parent 87806dd commit 0923ce7
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

Check failure on line 22 in app/controllers/authorize_interactions_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
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

Check failure on line 5 in app/controllers/well_known/protocol_handler_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLinesAroundClassBody: Extra empty line detected at class body beginning. (https://rubystyle.guide#empty-lines-around-bodies)
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"))

Check failure on line 12 in app/controllers/well_known/protocol_handler_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)

Check failure on line 12 in app/controllers/well_known/protocol_handler_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
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://"

Check failure on line 26 in app/controllers/well_known/protocol_handler_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.

Check failure on line 26 in app/controllers/well_known/protocol_handler_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/ReturnNilInPredicateMethodDefinition: Return false instead of nil in predicate methods. (https://rubystyle.guide#bool-methods-qmark)

Check failure on line 26 in app/controllers/well_known/protocol_handler_controller.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
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 @@ -70,6 +70,8 @@ def redirect_with_vary(path)
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


Check failure on line 75 in config/routes.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/EmptyLines: Extra blank line detected. (https://rubystyle.guide#two-or-more-empty-lines)
get '/nodeinfo/2.0', to: 'well_known/node_info#show', as: :nodeinfo_schema

Expand Down

0 comments on commit 0923ce7

Please sign in to comment.