From 42bcbd36b72c7ddc5ae1ecced09614863ca382bd Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Wed, 6 Dec 2017 07:04:49 +0100 Subject: [PATCH] Remove rabl dependency (#5894) * Remove rabl dependency * Replicate old Oj configuration --- Gemfile | 1 - Gemfile.lock | 3 --- .../well_known/webfinger_controller.rb | 19 +------------- app/models/account.rb | 15 +++++++++++ app/serializers/webfinger_serializer.rb | 26 +++++++++++++++++++ app/views/well_known/webfinger/show.json.rabl | 18 ------------- app/views/well_known/webfinger/show.xml.ruby | 4 +-- config/initializers/oj.rb | 1 + config/initializers/rabl_init.rb | 7 ----- 9 files changed, 45 insertions(+), 49 deletions(-) create mode 100644 app/serializers/webfinger_serializer.rb delete mode 100644 app/views/well_known/webfinger/show.json.rabl create mode 100644 config/initializers/oj.rb delete mode 100644 config/initializers/rabl_init.rb diff --git a/Gemfile b/Gemfile index d0b7aaef11..e43b1a2566 100644 --- a/Gemfile +++ b/Gemfile @@ -49,7 +49,6 @@ gem 'oj', '~> 3.3' gem 'ostatus2', '~> 2.0' gem 'ox', '~> 2.8' gem 'pundit', '~> 1.1' -gem 'rabl', '~> 0.13' gem 'rack-attack', '~> 5.0' gem 'rack-cors', '~> 0.4', require: 'rack/cors' gem 'rack-timeout', '~> 0.4' diff --git a/Gemfile.lock b/Gemfile.lock index 37fc77fdf7..5f050d031b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -334,8 +334,6 @@ GEM puma (3.11.0) pundit (1.1.0) activesupport (>= 3.0.0) - rabl (0.13.1) - activesupport (>= 2.3.14) rack (2.0.3) rack-attack (5.0.1) rack @@ -606,7 +604,6 @@ DEPENDENCIES pry-rails (~> 0.3) puma (~> 3.10) pundit (~> 1.1) - rabl (~> 0.13) rack-attack (~> 5.0) rack-cors (~> 0.4) rack-timeout (~> 0.4) diff --git a/app/controllers/well_known/webfinger_controller.rb b/app/controllers/well_known/webfinger_controller.rb index 1c27b2b184..5cc606808b 100644 --- a/app/controllers/well_known/webfinger_controller.rb +++ b/app/controllers/well_known/webfinger_controller.rb @@ -6,12 +6,10 @@ module WellKnown def show @account = Account.find_local!(username_from_resource) - @canonical_account_uri = @account.to_webfinger_s - @magic_key = pem_to_magic_key(@account.keypair.public_key) respond_to do |format| format.any(:json, :html) do - render formats: :json, content_type: 'application/jrd+json' + render json: @account, serializer: WebfingerSerializer, content_type: 'application/jrd+json' end format.xml do @@ -35,21 +33,6 @@ module WellKnown WebfingerResource.new(resource_user).username end - def pem_to_magic_key(public_key) - modulus, exponent = [public_key.n, public_key.e].map do |component| - result = [] - - until component.zero? - result << [component % 256].pack('C') - component >>= 8 - end - - result.reverse.join - end - - (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.') - end - def resource_param params.require(:resource) end diff --git a/app/models/account.rb b/app/models/account.rb index 77b78ffefe..e8a8c65608 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -184,6 +184,21 @@ class Account < ApplicationRecord @keypair ||= OpenSSL::PKey::RSA.new(private_key || public_key) end + def magic_key + modulus, exponent = [keypair.public_key.n, keypair.public_key.e].map do |component| + result = [] + + until component.zero? + result << [component % 256].pack('C') + component >>= 8 + end + + result.reverse.join + end + + (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.') + end + def subscription(webhook_url) @subscription ||= OStatus2::Subscription.new(remote_url, secret: secret, webhook: webhook_url, hub: hub_url) end diff --git a/app/serializers/webfinger_serializer.rb b/app/serializers/webfinger_serializer.rb new file mode 100644 index 0000000000..f80d12c024 --- /dev/null +++ b/app/serializers/webfinger_serializer.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +class WebfingerSerializer < ActiveModel::Serializer + include RoutingHelper + + attributes :subject, :aliases, :links + + def subject + object.to_webfinger_s + end + + def aliases + [short_account_url(object), account_url(object)] + end + + def links + [ + { rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: short_account_url(object) }, + { rel: 'http://schemas.google.com/g/2010#updates-from', type: 'application/atom+xml', href: account_url(object, format: 'atom') }, + { rel: 'self', type: 'application/activity+json', href: account_url(object) }, + { rel: 'salmon', href: api_salmon_url(object.id) }, + { rel: 'magic-public-key', href: "data:application/magic-public-key,#{object.magic_key}" }, + { rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_follow_url}?acct={uri}" }, + ] + end +end diff --git a/app/views/well_known/webfinger/show.json.rabl b/app/views/well_known/webfinger/show.json.rabl deleted file mode 100644 index 762d1860d7..0000000000 --- a/app/views/well_known/webfinger/show.json.rabl +++ /dev/null @@ -1,18 +0,0 @@ -object @account - -node(:subject) { @canonical_account_uri } - -node(:aliases) do - [short_account_url(@account), account_url(@account)] -end - -node(:links) do - [ - { rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: short_account_url(@account) }, - { rel: 'http://schemas.google.com/g/2010#updates-from', type: 'application/atom+xml', href: account_url(@account, format: 'atom') }, - { rel: 'self', type: 'application/activity+json', href: account_url(@account) }, - { rel: 'salmon', href: api_salmon_url(@account.id) }, - { rel: 'magic-public-key', href: "data:application/magic-public-key,#{@magic_key}" }, - { rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_follow_url}?acct={uri}" }, - ] -end diff --git a/app/views/well_known/webfinger/show.xml.ruby b/app/views/well_known/webfinger/show.xml.ruby index b0158b8bd1..0c7289d6a5 100644 --- a/app/views/well_known/webfinger/show.xml.ruby +++ b/app/views/well_known/webfinger/show.xml.ruby @@ -1,13 +1,13 @@ Nokogiri::XML::Builder.new do |xml| xml.XRD(xmlns: 'http://docs.oasis-open.org/ns/xri/xrd-1.0') do - xml.Subject @canonical_account_uri + xml.Subject @account.to_webfinger_s xml.Alias short_account_url(@account) xml.Alias account_url(@account) xml.Link(rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: short_account_url(@account)) xml.Link(rel: 'http://schemas.google.com/g/2010#updates-from', type: 'application/atom+xml', href: account_url(@account, format: 'atom')) xml.Link(rel: 'self', type: 'application/activity+json', href: account_url(@account)) xml.Link(rel: 'salmon', href: api_salmon_url(@account.id)) - xml.Link(rel: 'magic-public-key', href: "data:application/magic-public-key,#{@magic_key}") + xml.Link(rel: 'magic-public-key', href: "data:application/magic-public-key,#{@account.magic_key}") xml.Link(rel: 'http://ostatus.org/schema/1.0/subscribe', template: "#{authorize_follow_url}?acct={uri}") end end.to_xml diff --git a/config/initializers/oj.rb b/config/initializers/oj.rb new file mode 100644 index 0000000000..de3e17f2eb --- /dev/null +++ b/config/initializers/oj.rb @@ -0,0 +1 @@ +Oj.default_options = { mode: :compat, time_format: :ruby, use_to_json: true } diff --git a/config/initializers/rabl_init.rb b/config/initializers/rabl_init.rb deleted file mode 100644 index 132a421442..0000000000 --- a/config/initializers/rabl_init.rb +++ /dev/null @@ -1,7 +0,0 @@ -Rabl.configure do |config| - config.json_engine = Oj - config.cache_all_output = false - config.cache_sources = Rails.env.production? - config.include_json_root = false - config.view_paths = [Rails.root.join('app/views')] -end