webfinger.rb
2.46 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require 'rails/commands/server'
module Api
module Federation
class Webfinger < Grape::API
get 'webfinger' do
result = generate_jrd
present result, with: Grape::Presenters::Presenter
end
end
end
end
def generate_jrd
unless valid_domain?
not_found!
Rails.logger.error 'Domain Not Found'
end
if request_acct?
acct_hash
elsif valid_uri?(params[:resource])
uri_hash
end
end
def domain
if request_acct?
params[:resource].split('@')[1]
else
params[:resource].split('/')[2]
end
end
def valid_domain?
environment.domains.map(&:name).include? domain
end
def request_acct?
params[:resource].include? 'acct:'
end
def acct_hash
rails = Rails::Server.new
acct = Hash.new{|hash, key| hash[key] = Hash.new{|hash, key| hash[key] = Array.new}}
url = rails.options[:Host] + ':' + rails.options[:Port].to_s + '/'
person = Person.find_by_identifier(extract_person_identifier)
if person.nil?
Rails.logger.error 'Person not found'
not_found!
else
acct[:subject] = params[:resource]
acct[:alias] = url + person.identifier
acct[:properties][:identifier] = person.identifier
acct[:properties][:created_at] = person.created_at
for blog in person.blogs do
acct[:links][:rel] << url + 'rel/' + blog.path
acct[:links][:href] << url + person.identifier + '/' + blog.path
end
for galleries in person.articles.galleries do
acct[:links][:rel] << url + 'rel/' + galleries.path
acct[:links][:href] << url + person.identifier + '/' + galleries.path
end
acct[:titles][:name] = person.name
end
acct
end
def extract_person_identifier
params[:resource].split('@')[0].split(':')[1]
end
def valid_uri?(url)
uri = URI.parse(url)
if uri.is_a?(URI::HTTP)
true
else
Rails.logger.error 'Bad URI Error'
not_found!
end
end
def uri_hash
uri = {}
uri[:subject] = params[:resource]
entity = find_entity(params[:resource])
id = params[:resource].split('/').last.to_i
begin
uri[:properties] = entity.classify.constantize.find(id)
rescue ActiveRecord::RecordNotFound
Rails.logger.error "Entity: #{entity} with id: #{id} not found"
not_found!
end
uri
end
def find_entity(uri)
possible_entity = uri.split('/')
possible_entity.map! { |entity| "#{entity}s" }
entity = (ActiveRecord::Base.connection.tables & possible_entity).first
unless entity
Rails.logger.error 'Entity not found on records'
not_found!
end
entity
end