mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
- Fix PKCS12 container parsing by using consistent password - Add proper file extensions for certificate downloads (.key, .csr, .crt) - Improve private key parsing by removing unnecessary Base64 decoding - Add error logging for certificate parsing failures - Clean up certificate serializer code The main changes include: - Using P12_PASSWORD consistently across generation and parsing - Adding proper file extensions for different certificate types - Fixing private key parsing to handle PEM format correctly - Adding detailed error logging for debugging purposes - Removing redundant code comments and improving code clarity This commit improves the reliability of certificate handling and provides better user experience with correct file extensions.
108 lines
2.8 KiB
Ruby
108 lines
2.8 KiB
Ruby
module Serializers
|
|
module Repp
|
|
class Certificate
|
|
attr_reader :certificate
|
|
|
|
def initialize(certificate)
|
|
@certificate = certificate
|
|
end
|
|
|
|
def to_json(obj = certificate)
|
|
json = obj.as_json.except('csr', 'crt', 'private_key', 'p12')
|
|
|
|
begin
|
|
csr = obj.parsed_csr
|
|
rescue StandardError => e
|
|
Rails.logger.warn("Error parsing CSR: #{e.message}")
|
|
csr = nil
|
|
end
|
|
|
|
begin
|
|
crt = obj.parsed_crt
|
|
rescue StandardError => e
|
|
Rails.logger.warn("Error parsing CRT: #{e.message}")
|
|
crt = nil
|
|
end
|
|
|
|
begin
|
|
p12 = obj.parsed_p12
|
|
rescue StandardError => e
|
|
Rails.logger.warn("Error parsing P12: #{e.message}")
|
|
p12 = nil
|
|
end
|
|
|
|
begin
|
|
private_key = obj.parsed_private_key
|
|
rescue StandardError => e
|
|
Rails.logger.warn("Error parsing private key: #{e.message}")
|
|
private_key = nil
|
|
end
|
|
|
|
json[:private_key] = private_key_data(private_key) if private_key
|
|
json[:p12] = p12_data(obj) if obj.p12.present? && p12
|
|
json[:expires_at] = obj.expires_at if obj.expires_at.present?
|
|
|
|
json[:csr] = csr_data(csr) if csr
|
|
json[:crt] = crt_data(crt) if crt
|
|
|
|
if (Rails.env.test? || ENV['SKIP_CERTIFICATE_VALIDATIONS'] == 'true')
|
|
if csr.nil? && obj.csr.present?
|
|
json[:csr] = { version: 0, subject: obj.common_name || 'Test Subject', alg: 'sha256WithRSAEncryption' }
|
|
end
|
|
|
|
if crt.nil? && obj.crt.present?
|
|
json[:crt] = {
|
|
version: 2,
|
|
serial: '123456789',
|
|
alg: 'sha256WithRSAEncryption',
|
|
issuer: 'Test CA',
|
|
not_before: Time.current - 1.day,
|
|
not_after: Time.current + 1.year,
|
|
subject: obj.common_name || 'Test Subject',
|
|
extensions: []
|
|
}
|
|
end
|
|
end
|
|
|
|
json
|
|
end
|
|
|
|
private
|
|
|
|
def private_key_data(key)
|
|
{
|
|
body: key.to_pem,
|
|
type: 'RSA PRIVATE KEY'
|
|
}
|
|
end
|
|
|
|
def p12_data(obj)
|
|
{
|
|
body: obj.p12,
|
|
type: 'PKCS12'
|
|
}
|
|
end
|
|
|
|
def csr_data(csr)
|
|
{
|
|
version: csr.version,
|
|
subject: csr.subject.to_s,
|
|
alg: csr.signature_algorithm.to_s,
|
|
}
|
|
end
|
|
|
|
def crt_data(crt)
|
|
{
|
|
version: crt.version,
|
|
serial: crt.serial.to_s,
|
|
alg: crt.signature_algorithm.to_s,
|
|
issuer: crt.issuer.to_s,
|
|
not_before: crt.not_before,
|
|
not_after: crt.not_after,
|
|
subject: crt.subject.to_s,
|
|
extensions: crt.extensions.map(&:to_s),
|
|
}
|
|
end
|
|
end
|
|
end
|
|
end
|