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.

55 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. module WellKnown
  3. class WebfingerController < ApplicationController
  4. def show
  5. @account = Account.find_local!(username_from_resource)
  6. @canonical_account_uri = @account.to_webfinger_s
  7. @magic_key = pem_to_magic_key(@account.keypair.public_key)
  8. respond_to do |format|
  9. format.any(:json, :html) do
  10. render formats: :json, content_type: 'application/jrd+json'
  11. end
  12. format.xml do
  13. render content_type: 'application/xrd+xml'
  14. end
  15. end
  16. rescue ActiveRecord::RecordNotFound
  17. head 404
  18. end
  19. private
  20. def username_from_resource
  21. resource_user = resource_param
  22. username, domain = resource_user.split('@')
  23. if Rails.configuration.x.alternate_domains.include?(domain)
  24. resource_user = "#{username}@#{Rails.configuration.x.local_domain}"
  25. end
  26. WebfingerResource.new(resource_user).username
  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.join
  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
  43. end