Class: Webfinger

Inherits:
Object
  • Object
show all
Defined in:
lib/webfinger.rb

Overview

Copyright © 2010-2012, Diaspora Inc. This file is

licensed under the Affero General Public License version 3 or later.  See
the COPYRIGHT file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account) ⇒ Webfinger


10
11
12
13
# File 'lib/webfinger.rb', line 10

def initialize()
  self. = 
  self.ssl = true
end

Instance Attribute Details

#accountObject

Returns the value of attribute account


6
7
8
# File 'lib/webfinger.rb', line 6

def 
  @account
end

#hcardObject

Returns the value of attribute hcard


6
7
8
# File 'lib/webfinger.rb', line 6

def hcard
  @hcard
end

#hcard_xrdObject

Returns the value of attribute hcard_xrd


6
7
8
# File 'lib/webfinger.rb', line 6

def hcard_xrd
  @hcard_xrd
end

#host_meta_xrdObject

this tries the xrl url with https first, then falls back to http


65
66
67
# File 'lib/webfinger.rb', line 65

def host_meta_xrd
  @host_meta_xrd
end

#personObject

Returns the value of attribute person


6
7
8
# File 'lib/webfinger.rb', line 6

def person
  @person
end

#sslObject

Returns the value of attribute ssl


6
7
8
# File 'lib/webfinger.rb', line 6

def ssl
  @ssl
end

#webfinger_profileObject

Returns the value of attribute webfinger_profile


6
7
8
# File 'lib/webfinger.rb', line 6

def webfinger_profile
  @webfinger_profile
end

#webfinger_profile_xrdObject

Returns the value of attribute webfinger_profile_xrd


6
7
8
# File 'lib/webfinger.rb', line 6

def webfinger_profile_xrd
  @webfinger_profile_xrd
end

Class Method Details

.in_background(account, opts = {}) ⇒ Object


21
22
23
# File 'lib/webfinger.rb', line 21

def self.in_background(, opts={})
  Workers::FetchWebfinger.perform_async()
end

Instance Method Details

#cached_personObject


49
50
51
# File 'lib/webfinger.rb', line 49

def cached_person
  self.person ||= Person.()
end

#create_or_update_person_from_webfinger_profile!Object


53
54
55
56
57
58
59
60
61
62
# File 'lib/webfinger.rb', line 53

def create_or_update_person_from_webfinger_profile!
  FEDERATION_LOGGER.info("webfingering #{account}, it is not known or needs updating")
  if person #update my profile please
    person.assign_new_profile_from_hcard(self.hcard)
  else
    person = make_person_from_webfinger
  end
  FEDERATION_LOGGER.info("successfully webfingered #{@account}") if person
  person
end

#existing_person_with_profile?Boolean


45
46
47
# File 'lib/webfinger.rb', line 45

def existing_person_with_profile?
  cached_person.present? && cached_person.profile.present?
end

#fetchObject


16
17
18
19
# File 'lib/webfinger.rb', line 16

def fetch
  return person if existing_person_with_profile?
  create_or_update_person_from_webfinger_profile!
end

#get(url) ⇒ Object


30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/webfinger.rb', line 30

def get(url)
  Rails.logger.info("Getting: #{url} for #{account}")
  begin
    res = Faraday.get(url)
    return false if res.status == 404
    res.body
  rescue OpenSSL::SSL::SSLError => e
    Rails.logger.info "Failed to fetch #{url}: SSL setup invalid"
    raise e
  rescue => e
    Rails.logger.info("Failed to fetch: #{url} for #{account}; #{e.message}")
    raise e
  end
end

#hcard_urlObject


87
88
89
# File 'lib/webfinger.rb', line 87

def hcard_url
  self.webfinger_profile.hcard
end

#host_meta_urlObject


111
112
113
114
# File 'lib/webfinger.rb', line 111

def host_meta_url
  domain = .split('@')[1]
  "http#{'s' if self.ssl}://#{domain}/.well-known/host-meta"
end

#make_person_from_webfingerObject


107
108
109
# File 'lib/webfinger.rb', line 107

def make_person_from_webfinger
  Person.create_from_webfinger(webfinger_profile, hcard) unless webfinger_profile_xrd == false
end

#swizzle(template) ⇒ Object


116
117
118
# File 'lib/webfinger.rb', line 116

def swizzle(template)
  template.gsub('{uri}', )
end

#webfinger_profile_urlObject


91
92
93
94
95
# File 'lib/webfinger.rb', line 91

def webfinger_profile_url
  doc = Nokogiri::XML(self.host_meta_xrd)
  return nil if doc.namespaces["xmlns"] != "http://docs.oasis-open.org/ns/xri/xrd-1.0"
  swizzle doc.search('Link').find{|x| x['rel']=='lrdd'}['template']
end