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.

58 lines
1.5 KiB

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