闭社主体 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.

63 lines
1.7 KiB

  1. class XrdController < ApplicationController
  2. before_action :set_default_format_json, only: :webfinger
  3. before_action :set_default_format_xml, only: :host_meta
  4. def host_meta
  5. @webfinger_template = "#{webfinger_url}?resource={uri}"
  6. respond_to do |format|
  7. format.xml { render content_type: 'application/xrd+xml' }
  8. end
  9. end
  10. def webfinger
  11. @account = Account.find_local!(username_from_resource)
  12. @canonical_account_uri = "acct:#{@account.username}@#{Rails.configuration.x.local_domain}"
  13. @magic_key = pem_to_magic_key(@account.keypair.public_key)
  14. respond_to do |format|
  15. format.xml { render content_type: 'application/xrd+xml' }
  16. format.json { render content_type: 'application/jrd+json' }
  17. end
  18. rescue ActiveRecord::RecordNotFound
  19. head 404
  20. end
  21. private
  22. def set_default_format_xml
  23. request.format = 'xml' if request.headers["HTTP_ACCEPT"].nil? && params[:format].nil?
  24. end
  25. def set_default_format_json
  26. request.format = 'json' if request.headers["HTTP_ACCEPT"].nil? && params[:format].nil?
  27. end
  28. def username_from_resource
  29. if resource_param.start_with?('acct:') || resource_param.include?('@')
  30. resource_param.split('@').first.gsub('acct:', '')
  31. else
  32. url = Addressable::URI.parse(resource_param)
  33. url.path.gsub('/users/', '')
  34. end
  35. end
  36. def pem_to_magic_key(public_key)
  37. modulus, exponent = [public_key.n, public_key.e].map do |component|
  38. result = ''
  39. until component.zero?
  40. result << [component % 256].pack('C')
  41. component >>= 8
  42. end
  43. result.reverse!
  44. end
  45. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  46. end
  47. def resource_param
  48. params.require(:resource)
  49. end
  50. end