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.

68 lines
2.0 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 =~ /\Ahttps?:\/\//
  31. path_params = Rails.application.routes.recognize_path(resource_param)
  32. raise ActiveRecord::RecordNotFound unless path_params[:controller] == 'users' && path_params[:action] == 'show'
  33. path_params[:username]
  34. else
  35. username, domain = resource_param.gsub(/\Aacct:/, '').split('@')
  36. raise ActiveRecord::RecordNotFound unless TagManager.instance.local_domain?(domain)
  37. username
  38. end
  39. end
  40. def pem_to_magic_key(public_key)
  41. modulus, exponent = [public_key.n, public_key.e].map do |component|
  42. result = []
  43. until component.zero?
  44. result << [component % 256].pack('C')
  45. component >>= 8
  46. end
  47. result.reverse.join
  48. end
  49. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  50. end
  51. def resource_param
  52. params.require(:resource)
  53. end
  54. end