Move code from RSAKey to AsymmetricKey
This commit is contained in:
parent
2979c7a38c
commit
2172d55317
3 changed files with 75 additions and 58 deletions
|
@ -2,4 +2,72 @@
|
||||||
|
|
||||||
class AsymmetricKey < ApplicationRecord
|
class AsymmetricKey < ApplicationRecord
|
||||||
PRIVATE_KEY_CLEAR_DELAY = 1.hour.freeze
|
PRIVATE_KEY_CLEAR_DELAY = 1.hour.freeze
|
||||||
|
|
||||||
|
attr_accessor :private_key_pem, :private_key_pem_secret
|
||||||
|
|
||||||
|
################
|
||||||
|
# Associations #
|
||||||
|
################
|
||||||
|
|
||||||
|
belongs_to :account, optional: true
|
||||||
|
|
||||||
|
###############
|
||||||
|
# Validations #
|
||||||
|
###############
|
||||||
|
|
||||||
|
validates :public_key_pem,
|
||||||
|
presence: true,
|
||||||
|
uniqueness: true
|
||||||
|
|
||||||
|
validates :public_key_der,
|
||||||
|
presence: true,
|
||||||
|
uniqueness: true
|
||||||
|
|
||||||
|
validates :has_password, exclusion: { in: [nil] }
|
||||||
|
|
||||||
|
validates :bits,
|
||||||
|
numericality: {
|
||||||
|
only_integer: true,
|
||||||
|
greater_than: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
validates :sha1,
|
||||||
|
presence: true,
|
||||||
|
uniqueness: { case_sensitive: false }
|
||||||
|
|
||||||
|
validates :sha256,
|
||||||
|
presence: true,
|
||||||
|
uniqueness: { case_sensitive: false }
|
||||||
|
|
||||||
|
###########
|
||||||
|
# Methods #
|
||||||
|
###########
|
||||||
|
|
||||||
|
def encrypt_private_key_pem
|
||||||
|
cipher = OpenSSL::Cipher::AES256.new
|
||||||
|
cipher.encrypt
|
||||||
|
|
||||||
|
self.private_key_pem_iv = cipher.random_iv.freeze
|
||||||
|
self.private_key_pem_secret = cipher.random_key.freeze
|
||||||
|
|
||||||
|
self.private_key_pem_ciphertext = [
|
||||||
|
cipher.update(private_key_pem),
|
||||||
|
cipher.final,
|
||||||
|
].join.freeze
|
||||||
|
|
||||||
|
private_key_pem_secret
|
||||||
|
end
|
||||||
|
|
||||||
|
def decrypt_private_key_pem
|
||||||
|
cipher = OpenSSL::Cipher::AES256.new
|
||||||
|
cipher.decrypt
|
||||||
|
|
||||||
|
cipher.iv = private_key_pem_iv
|
||||||
|
cipher.key = private_key_pem_secret
|
||||||
|
|
||||||
|
self.private_key_pem = [
|
||||||
|
cipher.update(private_key_pem_ciphertext),
|
||||||
|
cipher.final,
|
||||||
|
].join.freeze
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,67 +1,9 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class RSAKey < AsymmetricKey
|
class RSAKey < AsymmetricKey
|
||||||
attr_accessor :private_key_pem, :private_key_pem_secret
|
|
||||||
|
|
||||||
################
|
|
||||||
# Associations #
|
|
||||||
################
|
|
||||||
|
|
||||||
belongs_to :account, optional: true
|
|
||||||
|
|
||||||
###############
|
###############
|
||||||
# Validations #
|
# Validations #
|
||||||
###############
|
###############
|
||||||
|
|
||||||
validates :public_key_pem,
|
|
||||||
presence: true,
|
|
||||||
uniqueness: true
|
|
||||||
|
|
||||||
validates :public_key_der,
|
|
||||||
presence: true,
|
|
||||||
uniqueness: true
|
|
||||||
|
|
||||||
validates :has_password, exclusion: { in: [nil] }
|
|
||||||
|
|
||||||
validates :bits, inclusion: { in: [2048, 4096] }
|
validates :bits, inclusion: { in: [2048, 4096] }
|
||||||
|
|
||||||
validates :sha1,
|
|
||||||
presence: true,
|
|
||||||
uniqueness: { case_sensitive: false }
|
|
||||||
|
|
||||||
validates :sha256,
|
|
||||||
presence: true,
|
|
||||||
uniqueness: { case_sensitive: false }
|
|
||||||
|
|
||||||
###########
|
|
||||||
# Methods #
|
|
||||||
###########
|
|
||||||
|
|
||||||
def encrypt_private_key_pem
|
|
||||||
cipher = OpenSSL::Cipher::AES256.new
|
|
||||||
cipher.encrypt
|
|
||||||
|
|
||||||
self.private_key_pem_iv = cipher.random_iv.freeze
|
|
||||||
self.private_key_pem_secret = cipher.random_key.freeze
|
|
||||||
|
|
||||||
self.private_key_pem_ciphertext = [
|
|
||||||
cipher.update(private_key_pem),
|
|
||||||
cipher.final,
|
|
||||||
].join.freeze
|
|
||||||
|
|
||||||
private_key_pem_secret
|
|
||||||
end
|
|
||||||
|
|
||||||
def decrypt_private_key_pem
|
|
||||||
cipher = OpenSSL::Cipher::AES256.new
|
|
||||||
cipher.decrypt
|
|
||||||
|
|
||||||
cipher.iv = private_key_pem_iv
|
|
||||||
cipher.key = private_key_pem_secret
|
|
||||||
|
|
||||||
self.private_key_pem = [
|
|
||||||
cipher.update(private_key_pem_ciphertext),
|
|
||||||
cipher.final,
|
|
||||||
].join.freeze
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -27,6 +27,13 @@ RSpec.describe RSAKey do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#bits' do
|
describe '#bits' do
|
||||||
|
it do
|
||||||
|
is_expected.to \
|
||||||
|
validate_numericality_of(:bits)
|
||||||
|
.only_integer
|
||||||
|
.is_greater_than(0)
|
||||||
|
end
|
||||||
|
|
||||||
it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) }
|
it { is_expected.to validate_inclusion_of(:bits).in_array([2048, 4096]) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Reference in a new issue