diff --git a/app/interactors/create_rsa_keys.rb b/app/interactors/create_rsa_keys.rb index 08e58f6..a85ccf2 100644 --- a/app/interactors/create_rsa_keys.rb +++ b/app/interactors/create_rsa_keys.rb @@ -9,11 +9,10 @@ class CreateRSAKeys before :set_ciphertext def call - context.private_key_pem = @pkey.to_pem.freeze - context.public_key = RSAPublicKey.create!( bits: BITS, public_key_pem: @pkey.public_key.to_pem.freeze, + private_key_pem: @pkey.to_pem.freeze, private_key_pem_iv: @iv, private_key_pem_secret: @key, private_key_pem_ciphertext: @ciphertext, diff --git a/app/interactors/create_x509_certificate_request.rb b/app/interactors/create_x509_certificate_request.rb index 3efd355..5426fae 100644 --- a/app/interactors/create_x509_certificate_request.rb +++ b/app/interactors/create_x509_certificate_request.rb @@ -14,7 +14,8 @@ class CreateX509CertificateRequest private def private_key_pkey - @private_key_pkey ||= OpenSSL::PKey::RSA.new context.private_key_pem + @private_key_pkey ||= + OpenSSL::PKey::RSA.new context.public_key.private_key_pem end def public_key_pkey diff --git a/app/interactors/create_x509_self_signed_certificate.rb b/app/interactors/create_x509_self_signed_certificate.rb index c33f742..d243100 100644 --- a/app/interactors/create_x509_self_signed_certificate.rb +++ b/app/interactors/create_x509_self_signed_certificate.rb @@ -22,7 +22,8 @@ class CreateX509SelfSignedCertificate private def private_key_pkey - @private_key_pkey ||= OpenSSL::PKey::RSA.new context.private_key_pem + @private_key_pkey ||= + OpenSSL::PKey::RSA.new context.public_key.private_key_pem end def public_key_pkey diff --git a/app/interactors/decrypt_rsa_private_key.rb b/app/interactors/decrypt_rsa_private_key.rb index 45a733b..70c4a07 100644 --- a/app/interactors/decrypt_rsa_private_key.rb +++ b/app/interactors/decrypt_rsa_private_key.rb @@ -6,7 +6,7 @@ class DecryptRSAPrivateKey before :set_cipher def call - context.private_key_pem_cleartext = [ + context.public_key.private_key_pem = [ @cipher.update(context.public_key.private_key_pem_ciphertext), @cipher.final, ].join.freeze diff --git a/app/models/rsa_public_key.rb b/app/models/rsa_public_key.rb index 42f29ce..c1d31af 100644 --- a/app/models/rsa_public_key.rb +++ b/app/models/rsa_public_key.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class RSAPublicKey < ApplicationRecord - attr_accessor :private_key_pem_secret + attr_accessor :private_key_pem, :private_key_pem_secret ############### # Validations # diff --git a/spec/interactors/create_rsa_keys_and_x509_self_signed_certificate_spec.rb b/spec/interactors/create_rsa_keys_and_x509_self_signed_certificate_spec.rb index 3025906..87e71be 100644 --- a/spec/interactors/create_rsa_keys_and_x509_self_signed_certificate_spec.rb +++ b/spec/interactors/create_rsa_keys_and_x509_self_signed_certificate_spec.rb @@ -23,10 +23,6 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do expect { subject }.to change(X509Certificate, :count).by(1) end - specify do - expect(subject.private_key_pem).to be_instance_of String - end - specify do expect(subject.public_key).to be_instance_of RSAPublicKey end @@ -36,7 +32,7 @@ RSpec.describe CreateRSAKeysAndX509SelfSignedCertificate do end specify do - expect(subject.private_key_pem).not_to be_blank + expect(subject.public_key.private_key_pem).not_to be_blank end specify do diff --git a/spec/interactors/create_rsa_keys_spec.rb b/spec/interactors/create_rsa_keys_spec.rb index 3d44354..d0a1ca3 100644 --- a/spec/interactors/create_rsa_keys_spec.rb +++ b/spec/interactors/create_rsa_keys_spec.rb @@ -9,20 +9,12 @@ RSpec.describe CreateRSAKeys do expect { subject }.to change(RSAPublicKey, :count).by(1) end - specify do - expect(subject.private_key_pem).to be_instance_of String - end - specify do expect(subject.public_key).to be_instance_of RSAPublicKey end specify do - expect(subject.private_key_pem).to be_frozen - end - - specify do - expect(subject.private_key_pem).not_to be_blank + expect(subject.public_key.private_key_pem).not_to be_blank end specify do @@ -30,7 +22,9 @@ RSpec.describe CreateRSAKeys do end specify do - expect { OpenSSL::PKey::RSA.new subject.private_key_pem }.not_to raise_error + expect do + OpenSSL::PKey::RSA.new subject.public_key.private_key_pem + end.not_to raise_error end specify do @@ -39,8 +33,9 @@ RSpec.describe CreateRSAKeys do end specify do - expect(subject.public_key.public_key_pem).to \ - eq OpenSSL::PKey::RSA.new(subject.private_key_pem).public_key.to_pem + expect(subject.public_key.public_key_pem).to eq( + OpenSSL::PKey::RSA.new(subject.public_key.private_key_pem).public_key.to_pem, + ) end specify do @@ -62,6 +57,6 @@ RSpec.describe CreateRSAKeys do cipher.final, ].join.freeze - expect(cleartext).to eq subject.private_key_pem + expect(cleartext).to eq subject.public_key.private_key_pem end end diff --git a/spec/interactors/create_x509_certificate_request_spec.rb b/spec/interactors/create_x509_certificate_request_spec.rb index a251ae2..ef832dc 100644 --- a/spec/interactors/create_x509_certificate_request_spec.rb +++ b/spec/interactors/create_x509_certificate_request_spec.rb @@ -5,14 +5,12 @@ require 'rails_helper' RSpec.describe CreateX509CertificateRequest do subject do described_class.call( - private_key_pem: private_key_pem, public_key: public_key, distinguished_name: distinguished_name, ) end let(:rsa_keys) { CreateRSAKeys.call } - let(:private_key_pem) { rsa_keys.private_key_pem } let(:public_key) { rsa_keys.public_key } let(:distinguished_name) { "CN=#{Faker::Internet.domain_name}" }