闭社主体 forked from https://github.com/tootsuite/mastodon
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.

43 lines
1.1 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.xml { render content_type: 'application/xrd+xml' }
  10. format.json { render content_type: 'application/jrd+json' }
  11. end
  12. rescue ActiveRecord::RecordNotFound
  13. head 404
  14. end
  15. private
  16. def username_from_resource
  17. WebfingerResource.new(resource_param).username
  18. end
  19. def pem_to_magic_key(public_key)
  20. modulus, exponent = [public_key.n, public_key.e].map do |component|
  21. result = []
  22. until component.zero?
  23. result << [component % 256].pack('C')
  24. component >>= 8
  25. end
  26. result.reverse.join
  27. end
  28. (['RSA'] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  29. end
  30. def resource_param
  31. params.require(:resource)
  32. end
  33. end
  34. end