2018-06-27 12:20:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-09 12:06:55 -05:00
|
|
|
# UrlValidator
|
|
|
|
#
|
|
|
|
# Custom validator for private keys.
|
|
|
|
#
|
|
|
|
# class Project < ActiveRecord::Base
|
|
|
|
# validates :certificate_key, certificate_key: true
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class CertificateKeyValidator < ActiveModel::EachValidator
|
|
|
|
def validate_each(record, attribute, value)
|
|
|
|
unless valid_private_key_pem?(value)
|
|
|
|
record.errors.add(attribute, "must be a valid PEM private key")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid_private_key_pem?(value)
|
2016-02-16 08:40:54 -05:00
|
|
|
return false unless value
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-02-09 12:06:55 -05:00
|
|
|
pkey = OpenSSL::PKey::RSA.new(value)
|
|
|
|
pkey.private?
|
|
|
|
rescue OpenSSL::PKey::PKeyError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|