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.

50 lines
1.2 KiB

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