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.

57 lines
1.4 KiB

  1. # frozen_string_literal: true
  2. module WellKnown
  3. class WebfingerController < ApplicationController
  4. include RoutingHelper
  5. def show
  6. @account = Account.find_local!(username_from_resource)
  7. @canonical_account_uri = @account.to_webfinger_s
  8. @magic_key = pem_to_magic_key(@account.keypair.public_key)
  9. respond_to do |format|
  10. format.any(:json, :html) do
  11. render formats: :json, content_type: 'application/jrd+json'
  12. end
  13. format.xml do
  14. render content_type: 'application/xrd+xml'
  15. end
  16. end
  17. rescue ActiveRecord::RecordNotFound
  18. head 404
  19. end
  20. private
  21. def username_from_resource
  22. resource_user = resource_param
  23. username, domain = resource_user.split('@')
  24. if Rails.configuration.x.alternate_domains.include?(domain)
  25. resource_user = "#{username}@#{Rails.configuration.x.local_domain}"
  26. end
  27. WebfingerResource.new(resource_user).username
  28. end
  29. def pem_to_magic_key(public_key)
  30. modulus, exponent = [public_key.n, public_key.e].map do |component|
  31. result = []
  32. until component.zero?
  33. result << [component % 256].pack('C')
  34. component >>= 8
  35. end
  36. result.reverse.join
  37. end
  38. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  39. end
  40. def resource_param
  41. params.require(:resource)
  42. end
  43. end
  44. end