From 7dda4d7fbffe9ab4d7112d4c041d7b137ed78407 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 23 Feb 2026 16:21:34 +0100 Subject: [PATCH] [WiP] Change keypair generation for new local users to use the `keypairs` table --- app/models/account.rb | 9 ++++----- spec/models/account_spec.rb | 5 ++--- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index 52403475e94..094be7ab6ea 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -459,11 +459,11 @@ class Account < ApplicationRecord end before_validation :prepare_contents, if: :local? - before_create :generate_keys + after_create_commit :generate_keys before_destroy :clean_feed_manager def ensure_keys! - return unless local? && private_key.blank? && public_key.blank? + return unless local? && private_key.blank? && public_key.blank? && keypairs.empty? generate_keys save! @@ -481,11 +481,10 @@ class Account < ApplicationRecord end def generate_keys - return unless local? && private_key.blank? && public_key.blank? + return unless local? && private_key.blank? && public_key.blank? && keypairs.empty? keypair = OpenSSL::PKey::RSA.new(2048) - self.private_key = keypair.to_pem - self.public_key = keypair.public_key.to_pem + keypairs.create!(uri: ActivityPub::TagManager.instance.key_uri_for(self), type: :rsa, public_key: keypair.public_key.to_pem, private_key: keypair.to_pem) end def normalize_domain diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 33f546f717a..665fbc37e68 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -752,9 +752,8 @@ RSpec.describe Account do it 'generates keys' do account = described_class.create!(domain: nil, username: 'user_without_keys') - expect(account) - .to be_private_key - .and be_public_key + expect(account.private_key).to be_nil + expect(account.public_key).to eq '' expect(account.keypair.keypair) .to be_private .and be_public