summaryrefslogtreecommitdiff
path: root/app/validators/certificate_key_validator.rb
blob: 098b16017d27fbe05ff0a61b406a0c2ec9e7bdfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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)
    return false unless value
    pkey = OpenSSL::PKey::RSA.new(value)
    pkey.private?
  rescue OpenSSL::PKey::PKeyError
    false
  end
end