|
|
@ -1,75 +1,108 @@ |
|
|
|
# frozen_string_literal: true |
|
|
|
# frozen_string_literal: true |
|
|
|
|
|
|
|
|
|
|
|
require_relative '../models/account' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResolveAccountService < BaseService |
|
|
|
class ResolveAccountService < BaseService |
|
|
|
include JsonLdHelper |
|
|
|
include JsonLdHelper |
|
|
|
|
|
|
|
include DomainControlHelper |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class WebfingerRedirectError < StandardError; end |
|
|
|
|
|
|
|
|
|
|
|
# Find or create a local account for a remote user. |
|
|
|
# Find or create an account record for a remote user. When creating, |
|
|
|
# When creating, look up the user's webfinger and fetch all |
|
|
|
# look up the user's webfinger and fetch ActivityPub data |
|
|
|
# important information from their feed |
|
|
|
# @param [String, Account] uri URI in the username@domain format or account record |
|
|
|
# @param [String, Account] uri User URI in the form of username@domain |
|
|
|
|
|
|
|
# @param [Hash] options |
|
|
|
# @param [Hash] options |
|
|
|
|
|
|
|
# @option options [Boolean] :redirected Do not follow further Webfinger redirects |
|
|
|
|
|
|
|
# @option options [Boolean] :skip_webfinger Do not attempt to refresh account data |
|
|
|
# @return [Account] |
|
|
|
# @return [Account] |
|
|
|
def call(uri, options = {}) |
|
|
|
def call(uri, options = {}) |
|
|
|
|
|
|
|
return if uri.blank? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process_options!(uri, options) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# First of all we want to check if we've got the account |
|
|
|
|
|
|
|
# record with the URI already, and if so, we can exit early |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return if domain_not_allowed?(@domain) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@account ||= Account.find_remote(@username, @domain) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return @account if @account&.local? || !webfinger_update_due? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# At this point we are in need of a Webfinger query, which may |
|
|
|
|
|
|
|
# yield us a different username/domain through a redirect |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process_webfinger! |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Because the username/domain pair may be different than what |
|
|
|
|
|
|
|
# we already checked, we need to check if we've already got |
|
|
|
|
|
|
|
# the record with that URI, again |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return if domain_not_allowed?(@domain) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@account ||= Account.find_remote(@username, @domain) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return @account if @account&.local? || !webfinger_update_due? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Now it is certain, it is definitely a remote account, and it |
|
|
|
|
|
|
|
# either needs to be created, or updated from fresh data |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
process_account! |
|
|
|
|
|
|
|
rescue Goldfinger::Error, WebfingerRedirectError, Oj::ParseError => e |
|
|
|
|
|
|
|
Rails.logger.debug "Webfinger query for #{@uri} failed: #{e}" |
|
|
|
|
|
|
|
nil |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_options!(uri, options) |
|
|
|
@options = options |
|
|
|
@options = options |
|
|
|
|
|
|
|
|
|
|
|
if uri.is_a?(Account) |
|
|
|
if uri.is_a?(Account) |
|
|
|
@account = uri |
|
|
|
@account = uri |
|
|
|
@username = @account.username |
|
|
|
@username = @account.username |
|
|
|
@domain = @account.domain |
|
|
|
@domain = @account.domain |
|
|
|
uri = "#{@username}@#{@domain}" |
|
|
|
@uri = [@username, @domain].compact.join('@') |
|
|
|
|
|
|
|
|
|
|
|
return @account if @account.local? || !webfinger_update_due? |
|
|
|
|
|
|
|
else |
|
|
|
else |
|
|
|
|
|
|
|
@uri = uri |
|
|
|
@username, @domain = uri.split('@') |
|
|
|
@username, @domain = uri.split('@') |
|
|
|
|
|
|
|
|
|
|
|
return Account.find_local(@username) if TagManager.instance.local_domain?(@domain) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@account = Account.find_remote(@username, @domain) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return @account unless webfinger_update_due? |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
Rails.logger.debug "Looking up webfinger for #{uri}" |
|
|
|
@domain = nil if TagManager.instance.local_domain?(@domain) |
|
|
|
|
|
|
|
end |
|
|
|
@webfinger = Goldfinger.finger("acct:#{uri}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_webfinger! |
|
|
|
|
|
|
|
@webfinger = Goldfinger.finger("acct:#{@uri}") |
|
|
|
confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@') |
|
|
|
confirmed_username, confirmed_domain = @webfinger.subject.gsub(/\Aacct:/, '').split('@') |
|
|
|
|
|
|
|
|
|
|
|
if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero? |
|
|
|
if confirmed_username.casecmp(@username).zero? && confirmed_domain.casecmp(@domain).zero? |
|
|
|
@username = confirmed_username |
|
|
|
@username = confirmed_username |
|
|
|
@domain = confirmed_domain |
|
|
|
@domain = confirmed_domain |
|
|
|
elsif options[:redirected].nil? |
|
|
|
elsif @options[:redirected].nil? |
|
|
|
return call("#{confirmed_username}@#{confirmed_domain}", options.merge(redirected: true)) |
|
|
|
@account = ResolveAccountService.new.call("#{confirmed_username}@#{confirmed_domain}", @options.merge(redirected: true)) |
|
|
|
else |
|
|
|
else |
|
|
|
Rails.logger.debug 'Requested and returned acct URIs do not match' |
|
|
|
raise WebfingerRedirectError, "The URI #{uri} tries to hijack #{@username}@#{@domain}" |
|
|
|
return |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@domain = nil if TagManager.instance.local_domain?(@domain) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
return Account.find_local(@username) if TagManager.instance.local_domain?(@domain) |
|
|
|
def process_account! |
|
|
|
return unless activitypub_ready? |
|
|
|
return unless activitypub_ready? |
|
|
|
|
|
|
|
|
|
|
|
RedisLock.acquire(lock_options) do |lock| |
|
|
|
RedisLock.acquire(lock_options) do |lock| |
|
|
|
if lock.acquired? |
|
|
|
if lock.acquired? |
|
|
|
@account = Account.find_remote(@username, @domain) |
|
|
|
@account = Account.find_remote(@username, @domain) |
|
|
|
|
|
|
|
|
|
|
|
next unless @account.nil? || @account.activitypub? |
|
|
|
next if (@account.present? && !@account.activitypub?) || actor_json.nil? |
|
|
|
|
|
|
|
|
|
|
|
handle_activitypub |
|
|
|
@account = ActivityPub::ProcessAccountService.new.call(@username, @domain, actor_json) |
|
|
|
else |
|
|
|
else |
|
|
|
raise Mastodon::RaceConditionError |
|
|
|
raise Mastodon::RaceConditionError |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
@account |
|
|
|
@account |
|
|
|
rescue Goldfinger::Error => e |
|
|
|
|
|
|
|
Rails.logger.debug "Webfinger query for #{uri} unsuccessful: #{e}" |
|
|
|
|
|
|
|
nil |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def webfinger_update_due? |
|
|
|
def webfinger_update_due? |
|
|
|
@account.nil? || ((!@options[:skip_webfinger] || @account.ostatus?) && @account.possibly_stale?) |
|
|
|
@account.nil? || ((!@options[:skip_webfinger] || @account.ostatus?) && @account.possibly_stale?) |
|
|
|
end |
|
|
|
end |
|
|
@ -78,14 +111,6 @@ class ResolveAccountService < BaseService |
|
|
|
!@webfinger.link('self').nil? && ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@webfinger.link('self').type) |
|
|
|
!@webfinger.link('self').nil? && ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].include?(@webfinger.link('self').type) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def handle_activitypub |
|
|
|
|
|
|
|
return if actor_json.nil? |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@account = ActivityPub::ProcessAccountService.new.call(@username, @domain, actor_json) |
|
|
|
|
|
|
|
rescue Oj::ParseError |
|
|
|
|
|
|
|
nil |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def actor_url |
|
|
|
def actor_url |
|
|
|
@actor_url ||= @webfinger.link('self').href |
|
|
|
@actor_url ||= @webfinger.link('self').href |
|
|
|
end |
|
|
|
end |
|
|
|