Select correct self link when parsing Webfinger response (#31110)

This commit is contained in:
Adam Niedzielski 2024-07-23 16:42:31 +02:00 committed by KMY
parent fe85d7400a
commit ad7d1fff25
10 changed files with 73 additions and 26 deletions

View file

@ -6,6 +6,8 @@ class Webfinger
class RedirectError < Error; end
class Response
ACTIVITYPUB_READY_TYPE = ['application/activity+json', 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"'].freeze
attr_reader :uri
def initialize(uri, body)
@ -20,17 +22,28 @@ class Webfinger
end
def link(rel, attribute)
links.dig(rel, attribute)
links.dig(rel, 0, attribute)
end
def self_link_href
self_link.fetch('href')
end
private
def links
@links ||= @json['links'].index_by { |link| link['rel'] }
@links ||= @json.fetch('links', []).group_by { |link| link['rel'] }
end
def self_link
links.fetch('self', []).find do |link|
ACTIVITYPUB_READY_TYPE.include?(link['type'])
end
end
def validate_response!
raise Webfinger::Error, "Missing subject in response for #{@uri}" if subject.blank?
raise Webfinger::Error, "Missing self link in response for #{@uri}" if self_link.blank?
end
end