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.

52 lines
1.4 KiB

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