2016-02-09 12:06:55 -05:00
|
|
|
# UrlValidator
|
|
|
|
#
|
|
|
|
# Custom validator for private keys.
|
|
|
|
#
|
|
|
|
# class Project < ActiveRecord::Base
|
2016-02-09 13:04:39 -05:00
|
|
|
# validates :certificate_key, certificate: true
|
2016-02-09 12:06:55 -05:00
|
|
|
# end
|
|
|
|
#
|
|
|
|
class CertificateValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
2016-02-09 13:04:39 -05:00
|
|
|
unless valid_certificate_pem?(value)
|
2016-02-09 12:06:55 -05:00
|
|
|
record.errors.add(attribute, "must be a valid PEM certificate")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-02-09 13:04:39 -05:00
|
|
|
def valid_certificate_pem?(value)
|
2016-02-16 08:40:54 -05:00
|
|
|
return false unless value
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-02-16 08:40:54 -05:00
|
|
|
OpenSSL::X509::Certificate.new(value).present?
|
2016-02-09 12:06:55 -05:00
|
|
|
rescue OpenSSL::X509::CertificateError
|
2016-02-16 08:40:54 -05:00
|
|
|
false
|
2016-02-09 12:06:55 -05:00
|
|
|
end
|
|
|
|
end
|