1. Sergey Generalov
  2. redmine-mercurial-server

Source

redmine-mercurial-server / lib / custom_value_path.rb

require_dependency "custom_value"

module CustomValuePath
  def self.included(base)
    base.send(:include, InstanceMethods)
    base.class_eval do
      unloadable
      validate :validate_ssh
    end
  end
  
  module InstanceMethods
    def validate_ssh
      begin
        return true if value == ""
        keys = value.split("\n\n")
				for key in keys do
					type, blob, host = key.split(/ /)
					raise Net::SSH::Exception, "public key is not valid (hostname: #{host.to_s})" if blob.nil?
          blob = blob.unpack("m*").first
				  reader = Net::SSH::Buffer.new(blob)
					reader.read_key or raise OpenSSL::PKey::PKeyError, "not a public key (hostname: #{host.to_s})"
				end
      rescue
        errors.add(:value, :invalid)
      end
    end
  end
end