BigW Consortium Gitlab

certificate_validator.rb 574 Bytes
Newer Older
1 2 3 4 5
# UrlValidator
#
# Custom validator for private keys.
#
#   class Project < ActiveRecord::Base
6
#     validates :certificate_key, certificate: true
7 8 9 10
#   end
#
class CertificateValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
11
    unless valid_certificate_pem?(value)
12 13 14 15 16 17
      record.errors.add(attribute, "must be a valid PEM certificate")
    end
  end

  private

18
  def valid_certificate_pem?(value)
19 20
    return false unless value
    OpenSSL::X509::Certificate.new(value).present?
21
  rescue OpenSSL::X509::CertificateError
22
    false
23 24
  end
end