Read about our upcoming Code of Conduct on this issue

This instance will be upgraded to Heptapod 0.28.1 on 2022-01-26 at 16:00 UTC+1 (a few minutes of down time)

certificate_validator.rb 544 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)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
19
    OpenSSL::X509::Certificate.new(value).present?
20
  rescue OpenSSL::X509::CertificateError
Kamil Trzcinski's avatar
Kamil Trzcinski committed
21
    false
22
23
  end
end