lib/io_streams/pgp.rb in iostreams-0.10.0 vs lib/io_streams/pgp.rb in iostreams-0.10.1

- old
+ new

@@ -164,20 +164,39 @@ def self.has_key?(email:) Open3.popen2e("gpg --list-keys --with-colons #{email}") do |stdin, out, waith_thr| output = out.read.chomp if waith_thr.value.success? output.each_line do |line| - return true if line.match(/\Auid.*::([^\:]*):\Z/) + return true if line.include?(email) end false else return false if output =~ /(public key not found|No public key)/i raise(Pgp::Failure, "GPG Failed calling gpg to list keys for #{email}: #{output}") end end end + # Returns [String] the first fingerprint for the supplied email + # Returns nil if no fingerprint was found + def self.fingerprint(email:) + Open3.popen2e("gpg --list-keys --fingerprint --with-colons #{email}") do |stdin, out, waith_thr| + output = out.read.chomp + if waith_thr.value.success? + output.each_line do |line| + if match = line.match(/\Afpr.*::([^\:]*):\Z/) + return match[1] + end + end + nil + else + return if output =~ /(public key not found|No public key)/i + raise(Pgp::Failure, "GPG Failed calling gpg to list keys for #{email}: #{output}") + end + end + end + # Returns [String] the key for the supplied email address # # email: [String] Email address for requested key # # ascii: [true|false] @@ -204,9 +223,29 @@ out, err, status = Open3.capture3('gpg --import', binmode: true, stdin_data: key) if status.success? && out.length > 0 out else raise(Pgp::Failure, "GPG Failed importing key: #{err} #{out}") + end + end + + # Set the trust level for an existing key. + # + # Returns [String] output if the trust was successfully updated + # Returns nil if the email was not found + # + # After importing keys, they are not trusted and the relevant trust level must be set. + # Default: 5 : Ultimate + def self.set_trust(email:, level: 5) + fingerprint = fingerprint(email: email) + return unless fingerprint + + trust = "#{fingerprint}:#{level + 1}:\n" + out, err, status = Open3.capture3('gpg --import-ownertrust', stdin_data: trust) + if status.success? + err + else + raise(Pgp::Failure, "GPG Failed trusting key: #{err} #{out}") end end end end