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

65 lines
1.8 KiB

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