diff options
-rw-r--r-- | users/app/controllers/webfinger_controller.rb | 4 | ||||
-rw-r--r-- | users/app/views/webfinger/search.xml.erb | 10 | ||||
-rw-r--r-- | users/lib/webfinger/host_meta_presenter.rb | 2 | ||||
-rw-r--r-- | users/lib/webfinger/user_presenter.rb | 36 | ||||
-rw-r--r-- | users/test/unit/webfinger/.user_presenter_test.rb.swp | bin | 0 -> 12288 bytes | |||
-rw-r--r-- | users/test/unit/webfinger/user_presenter_test.rb | 49 |
6 files changed, 79 insertions, 22 deletions
diff --git a/users/app/controllers/webfinger_controller.rb b/users/app/controllers/webfinger_controller.rb index f28dd3e..8872802 100644 --- a/users/app/controllers/webfinger_controller.rb +++ b/users/app/controllers/webfinger_controller.rb @@ -12,8 +12,8 @@ class WebfingerController < ApplicationController username = params[:q].split('@')[0].to_s.downcase user = User.find_by_login(username) raise RECORD_NOT_FOUND, 'User not found' unless user.present? - @subject = Webfinger::UserPresenter.new(user, request) - respond_with @subject + @presenter = Webfinger::UserPresenter.new(user, request) + respond_with @presenter end end diff --git a/users/app/views/webfinger/search.xml.erb b/users/app/views/webfinger/search.xml.erb index 270383e..27f26d1 100644 --- a/users/app/views/webfinger/search.xml.erb +++ b/users/app/views/webfinger/search.xml.erb @@ -1,8 +1,10 @@ <?xml version="1.0" encoding="UTF-8"?> <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"> - <Subject>acct:<%= @subject.email_identifier %></Subject> - <Alias>"<%= user_url(@subject.subject) %>"</Alias> - <%- if @subject.key.present? %> - <Link rel="public-key" type = 'PGP' href="<%= @subject.key %>"/> + <Subject><%= @presenter.subject %></Subject> + <%- @presenter.aliases.each do |user_alias| %> + <Alias>"<%= user_alias %>"</Alias> + <% end %> + <%- @presenter.links.each do |rel, link| %> + <Link rel=<%=rel%> type=<%=link[:type]%> href="<%= link[:key] %>"/> <% end %> </XRD> diff --git a/users/lib/webfinger/host_meta_presenter.rb b/users/lib/webfinger/host_meta_presenter.rb index b48729c..6a6235f 100644 --- a/users/lib/webfinger/host_meta_presenter.rb +++ b/users/lib/webfinger/host_meta_presenter.rb @@ -15,7 +15,7 @@ class Webfinger::HostMetaPresenter "#{subject}/#{path}?#{query_param}={uri}" end - def to_json(options) + def to_json(options = {}) { subject: subject, links: { diff --git a/users/lib/webfinger/user_presenter.rb b/users/lib/webfinger/user_presenter.rb index 8288bff..41153a8 100644 --- a/users/lib/webfinger/user_presenter.rb +++ b/users/lib/webfinger/user_presenter.rb @@ -1,20 +1,26 @@ class Webfinger::UserPresenter include Rails.application.routes.url_helpers - attr_accessor :subject + attr_accessor :user - def initialize(subject, request) - @subject = subject + def initialize(user, request) + @user = user @request = request end - def email_identifier - "#{@subject.username}@#{@request.host}" + def to_json(options = {}) + { + subject: subject, + aliases: aliases, + links: links + }.to_json(options) end - def key - if @subject.public_key.present? - Base64.encode64(@subject.public_key.to_s) - end + def subject + "acct:#{@user.email_address}" + end + + def aliases + [ user_url(@user, :host => @request.host) ] end def links @@ -23,12 +29,12 @@ class Webfinger::UserPresenter return links end - def to_json(options) - { - subject: "acct:#{email_identifier}", - aliases: [ user_url(@subject, :host => @request.host) ], - links: links - }.to_json(options) + protected + + def key + if @user.public_key.present? + Base64.encode64(@user.public_key.to_s) + end end end diff --git a/users/test/unit/webfinger/.user_presenter_test.rb.swp b/users/test/unit/webfinger/.user_presenter_test.rb.swp Binary files differnew file mode 100644 index 0000000..5b92867 --- /dev/null +++ b/users/test/unit/webfinger/.user_presenter_test.rb.swp diff --git a/users/test/unit/webfinger/user_presenter_test.rb b/users/test/unit/webfinger/user_presenter_test.rb new file mode 100644 index 0000000..7f7a0d4 --- /dev/null +++ b/users/test/unit/webfinger/user_presenter_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' +require 'webfinger' +require 'json' + +class Webfinger::UserPresenterTest < ActiveSupport::TestCase + + + setup do + @user = stub( + username: 'testuser', + email_address: "testuser@#{APP_CONFIG[:domain]}" + ) + @request = stub( + host: APP_CONFIG[:domain] + ) + end + + test "user without key has no links" do + @user.stubs :public_key => nil + presenter = Webfinger::UserPresenter.new(@user, @request) + assert_equal Hash.new, presenter.links + end + + test "user with key has corresponding link" do + @user.stubs :public_key => "here's a key" + presenter = Webfinger::UserPresenter.new(@user, @request) + assert_equal [:public_key], presenter.links.keys + assert_equal "PGP", presenter.links[:public_key][:type] + assert_equal presenter.send(:key), presenter.links[:public_key][:href] + end + + test "key is base64 encoded" do + @user.stubs :public_key => "here's a key" + presenter = Webfinger::UserPresenter.new(@user, @request) + assert_equal Base64.encode64(@user.public_key), presenter.send(:key) + end + + test "creates proper json representation" do + @user.stubs :public_key => "here's a key" + presenter = Webfinger::UserPresenter.new(@user, @request) + hash = JSON.parse presenter.to_json + assert_equal ["subject", "links", "aliases"].sort, hash.keys.sort + hash.each do |key, value| + assert_equal presenter.send(key.to_sym).to_json, value.to_json + end + end + + +end |