webfinger.rb
1.73 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
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
  acct = {}
  acct[:subject] = params[:resource]
  acct[:properties] = Person.find_by_identifier(extract_person_identifier)
  if acct[:properties].nil?
    Rails.logger.error 'Person not found'
    not_found!
  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