闭社主体 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.

39 lines
973 B

  1. class XrdController < ApplicationController
  2. before_filter :set_format
  3. def host_meta
  4. @webfinger_template = "#{webfinger_url}?resource={uri}"
  5. end
  6. def webfinger
  7. @account = Account.find_by!(username: username_from_resource, domain: nil)
  8. @canonical_account_uri = "acct:#{@account.username}@#{LOCAL_DOMAIN}"
  9. @magic_key = pem_to_magic_key(@account.keypair.public_key)
  10. end
  11. private
  12. def set_format
  13. request.format = 'xml'
  14. response.headers['Content-Type'] = 'application/xrd+xml'
  15. end
  16. def username_from_resource
  17. params[:resource].split('@').first.gsub('acct:', '')
  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 == 0 do
  23. result << [component % 256].pack('C')
  24. component >>= 8
  25. end
  26. result.reverse!
  27. end
  28. (["RSA"] + [modulus, exponent].map { |n| Base64.urlsafe_encode64(n) }).join('.')
  29. end
  30. end