app/models/federails/actor.rb in federails-0.1.0 vs app/models/federails/actor.rb in federails-0.2.0

- old
+ new

@@ -1,10 +1,12 @@ require 'federails/utils/host' require 'fediverse/webfinger' module Federails class Actor < ApplicationRecord # rubocop:disable Metrics/ClassLength + include Federails::HasUuid + validates :federated_url, presence: { unless: :entity }, uniqueness: { unless: :entity } validates :username, presence: { unless: :entity } validates :server, presence: { unless: :entity } validates :inbox_url, presence: { unless: :entity } validates :outbox_url, presence: { unless: :entity } @@ -110,11 +112,11 @@ actor end def find_by_federation_url(federated_url) local_route = Utils::Host.local_route federated_url - return find local_route[:id] if local_route && local_route[:controller] == 'federails/server/actors' && local_route[:action] == 'show' + return find_param(local_route[:id]) if local_route && local_route[:controller] == 'federails/server/actors' && local_route[:action] == 'show' actor = find_by federated_url: federated_url return actor if actor Fediverse::Webfinger.fetch_actor_url(federated_url) @@ -145,8 +147,43 @@ find_or_create_by_federation_url object['id'] else raise "Unsupported object type for actor (#{object.class})" end end + end + + def public_key + ensure_key_pair_exists! + self[:public_key] + end + + def private_key + ensure_key_pair_exists! + self[:private_key] + end + + def key_id + "#{federated_url}#main-key" + end + + private + + def ensure_key_pair_exists! + return if self[:private_key].present? || !local? + + update!(generate_key_pair) + end + + def generate_key_pair + rsa_key = OpenSSL::PKey::RSA.new 2048 + cipher = OpenSSL::Cipher.new('AES-128-CBC') + { + private_key: if Rails.application.credentials.secret_key_base + rsa_key.to_pem(cipher, Rails.application.credentials.secret_key_base) + else + rsa_key.to_pem + end, + public_key: rsa_key.public_key.to_pem, + } end end end