Welcome to mirror list, hosted at ThFree Co, Russian Federation.

webfinger_profile.rb « lib - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5fe33d705755d3672af33c41b141fdb1a2971123 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class WebfingerProfile
  attr_accessor :webfinger_profile, :account, :links, :hcard, :guid, :public_key, :seed_location

  def initialize(account, webfinger_profile)
    @account = account
    @webfinger_profile = webfinger_profile
    @links = {}
    set_fields
  end

  def valid_diaspora_profile?
    !(@webfinger_profile.nil? || @account.nil? || @links.nil? || @hcard.nil? ||
        @guid.nil? || @public_key.nil? || @seed_location.nil? )
  end

  private

  def set_fields
    doc = Nokogiri::XML.parse(webfinger_profile)
    doc.remove_namespaces!

    account_string = doc.css('Subject').text.gsub('acct:', '').strip
    
    raise "account in profile(#{account_string}) and account requested (#{@account}) do not match" if account_string != @account

    doc.css('Link').each do |l|
      rel = text_of_attribute(l, 'rel')
      href = text_of_attribute(l, 'href')
      @links[rel] = href
      case rel
        when "http://microformats.org/profile/hcard"
          @hcard = href
        when "http://joindiaspora.com/guid"
          @guid = href
        when "http://joindiaspora.com/seed_location"
          @seed_location = href
      end
    end

    begin
      pubkey = text_of_attribute( doc.at('Link[rel=diaspora-public-key]'), 'href')
      @public_key = Base64.decode64 pubkey
    rescue => e
      Rails.logger.info("event => :invalid_profile, :identifier => #{@account}")
    end
  end

  def text_of_attribute(doc, attr)
    doc.attribute(attr) ? doc.attribute(attr).text : nil
  end
end