闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. module WellKnown
  3. class WebfingerController < ApplicationController
  4. def show
  5. @account = Account.find_local!(username_from_resource)
  6. @canonical_account_uri = @account.to_webfinger_s
  7. @magic_key = pem_to_magic_key(@account.keypair.public_key)
  8. respond_to do |format|
  9. format.any(:json, :html) do
  10. render formats: :json, content_type: 'application/jrd+json'
  11. end
  12. format.xml do
  13. render content_type: 'application/xrd+xml'
  14. end
  15. end
  16. rescue ActiveRecord::RecordNotFound
  17. head 404
  18. end
  19. private
  20. def username_from_resource
  21. WebfingerResource.new(resource_param).username
  22. end
  23. def pem_to_magic_key(public_key)
  24. modulus, exponent = [public_key.n, public_key.e].map do |component|
  25. result = []
  26. until component.zero?
  27. result << [component % 256].pack('C')
  28. component >>= 8
  29. end
  30. result.reverse.join
  31. end
  32. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  33. end
  34. def resource_param
  35. params.require(:resource)
  36. end
  37. end
  38. end