зеркало из https://github.com/github/ruby-gpgme.git
GPGME.resolve_keys => GPGME::Key.find, tests, and extracted common code for Key and SubKey
This commit is contained in:
Родитель
dee891d20e
Коммит
0c5c28171b
|
@ -89,6 +89,25 @@ module GPGME
|
|||
end
|
||||
end
|
||||
|
||||
##
|
||||
# DEPRECATED use GPGME::Key.find
|
||||
def resolve_keys(keys_or_names, secret_only, purposes = Array.new)
|
||||
keys = Array.new
|
||||
keys_or_names.each do |key_or_name|
|
||||
if key_or_name.kind_of? Key
|
||||
keys << key_or_name
|
||||
elsif key_or_name.kind_of? String
|
||||
GPGME::Ctx.new do |ctx|
|
||||
key = ctx.keys(key_or_name, secret_only).find {|k|
|
||||
k.usable_for?(purposes)
|
||||
}
|
||||
keys << key if key
|
||||
end
|
||||
end
|
||||
end
|
||||
keys
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def split_args(args_options)
|
||||
|
@ -114,23 +133,6 @@ module GPGME
|
|||
end
|
||||
end
|
||||
|
||||
def resolve_keys(keys_or_names, secret_only, purposes = Array.new)
|
||||
keys = Array.new
|
||||
keys_or_names.each do |key_or_name|
|
||||
if key_or_name.kind_of? Key
|
||||
keys << key_or_name
|
||||
elsif key_or_name.kind_of? String
|
||||
GPGME::Ctx.new do |ctx|
|
||||
key = ctx.keys(key_or_name, secret_only).find {|k|
|
||||
k.usable_for?(purposes)
|
||||
}
|
||||
keys << key if key
|
||||
end
|
||||
end
|
||||
end
|
||||
keys
|
||||
end
|
||||
|
||||
def input_data(input)
|
||||
if input.kind_of? GPGME::Data
|
||||
input
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
module GPGME
|
||||
# A public or secret key.
|
||||
|
||||
##
|
||||
# A ruby representation of a public or a secret key.
|
||||
class Key
|
||||
private_class_method :new
|
||||
|
||||
|
@ -7,32 +9,48 @@ module GPGME
|
|||
attr_reader :issuer_serial, :issuer_name, :chain_id
|
||||
attr_reader :subkeys, :uids
|
||||
|
||||
def trust
|
||||
return :revoked if @revoked == 1
|
||||
return :expired if @expired == 1
|
||||
return :disabled if @disabled == 1
|
||||
return :invalid if @invalid == 1
|
||||
end
|
||||
include KeyCommon
|
||||
|
||||
def capability
|
||||
caps = Array.new
|
||||
caps << :encrypt if @can_encrypt
|
||||
caps << :sign if @can_sign
|
||||
caps << :certify if @can_certify
|
||||
caps << :authenticate if @can_authenticate
|
||||
caps
|
||||
end
|
||||
##
|
||||
# Returns an array of {GPGME::Key} objects that match the parameters.
|
||||
# * +secret+ set to +:secret+ to get only secret keys, or to +:public+ to
|
||||
# get only public keys.
|
||||
# * +keys_or_names+ an array or an item that can be either {GPGME::Key}
|
||||
# elements, or string identifiers like the email or the sha. Leave
|
||||
# blank to get all.
|
||||
# * +purposes+ get only keys that are usable for any of these purposes.
|
||||
# See {GPGME::Key} for a list of possible key capabilities.
|
||||
#
|
||||
# @example
|
||||
# GPGME::Key.find :secret # => first secret key found
|
||||
#
|
||||
# @example
|
||||
# GPGME::Key.find(:public, "mrsimo@example.com")
|
||||
# # => return only public keys that match mrsimo@example.com
|
||||
#
|
||||
# @example
|
||||
# GPGME::Key.find(:public, "mrsimo@example.com", :sign)
|
||||
# # => return the public keys that match mrsimo@exampl.com and are
|
||||
# # capable of signing
|
||||
def self.find(secret, keys_or_names = nil, purposes = [])
|
||||
secret = (secret == :secret)
|
||||
keys_or_names = [""] if keys_or_names.nil? || keys_or_names.empty?
|
||||
keys_or_names = [keys_or_names].flatten
|
||||
purposes = [purposes].flatten.compact.uniq
|
||||
|
||||
def usable_for?(purposes)
|
||||
unless purposes.kind_of? Array
|
||||
purposes = [purposes]
|
||||
keys = []
|
||||
keys_or_names.each do |key_or_name|
|
||||
case key_or_name
|
||||
when Key then keys << key_or_name
|
||||
when String
|
||||
GPGME::Ctx.new do |ctx|
|
||||
keys += ctx.keys(key_or_name, secret).select do |k|
|
||||
k.usable_for?(purposes)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return false if [:revoked, :expired, :disabled, :invalid].include? trust
|
||||
return (purposes - capability).empty?
|
||||
end
|
||||
|
||||
def secret?
|
||||
@secret == 1
|
||||
keys
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
module GPGME
|
||||
module KeyCommon
|
||||
|
||||
##
|
||||
# Returns nil if the trust is valid.
|
||||
# Returns one of +:revoked+, +:expired+, +:disabled+, +:invalid+
|
||||
def trust
|
||||
return :revoked if @revoked == 1
|
||||
return :expired if @expired == 1
|
||||
return :disabled if @disabled == 1
|
||||
return :invalid if @invalid == 1
|
||||
end
|
||||
|
||||
##
|
||||
# Array of capabilities for this key. It can contain any combination of
|
||||
# +:encrypt+, +:sign+, +:certify+ or +:authenticate+
|
||||
def capability
|
||||
caps = []
|
||||
caps << :encrypt if @can_encrypt
|
||||
caps << :sign if @can_sign
|
||||
caps << :certify if @can_certify
|
||||
caps << :authenticate if @can_authenticate
|
||||
caps
|
||||
end
|
||||
|
||||
##
|
||||
# Checks if the key is capable of all of these actions. If empty array
|
||||
# is passed then will return true.
|
||||
#
|
||||
# Returns false if the keys trust has been invalidated.
|
||||
def usable_for?(purposes)
|
||||
unless purposes.kind_of? Array
|
||||
purposes = [purposes]
|
||||
end
|
||||
return false if [:revoked, :expired, :disabled, :invalid].include? trust
|
||||
return (purposes - capability).empty?
|
||||
end
|
||||
|
||||
def secret?
|
||||
@secret == 1
|
||||
end
|
||||
end
|
||||
end
|
|
@ -5,33 +5,7 @@ module GPGME
|
|||
attr_reader :pubkey_algo, :length, :keyid, :fpr
|
||||
alias fingerprint fpr
|
||||
|
||||
def trust
|
||||
return :revoked if @revoked == 1
|
||||
return :expired if @expired == 1
|
||||
return :disabled if @disabled == 1
|
||||
return :invalid if @invalid == 1
|
||||
end
|
||||
|
||||
def capability
|
||||
caps = Array.new
|
||||
caps << :encrypt if @can_encrypt
|
||||
caps << :sign if @can_sign
|
||||
caps << :certify if @can_certify
|
||||
caps << :authenticate if @can_authenticate
|
||||
caps
|
||||
end
|
||||
|
||||
def usable_for?(purposes)
|
||||
unless purposes.kind_of? Array
|
||||
purposes = [purposes]
|
||||
end
|
||||
return false if [:revoked, :expired, :disabled, :invalid].include? trust
|
||||
return (purposes - capability).empty?
|
||||
end
|
||||
|
||||
def secret?
|
||||
@secret == 1
|
||||
end
|
||||
include KeyCommon
|
||||
|
||||
def timestamp
|
||||
Time.at(@timestamp)
|
||||
|
@ -42,14 +16,14 @@ module GPGME
|
|||
end
|
||||
|
||||
PUBKEY_ALGO_LETTERS = {
|
||||
PK_RSA => ?R,
|
||||
PK_ELG_E => ?g,
|
||||
PK_ELG => ?G,
|
||||
PK_DSA => ?D
|
||||
PK_RSA => "R",
|
||||
PK_ELG_E => "g",
|
||||
PK_ELG => "G",
|
||||
PK_DSA => "D"
|
||||
}
|
||||
|
||||
def pubkey_algo_letter
|
||||
PUBKEY_ALGO_LETTERS[@pubkey_algo] || ??
|
||||
PUBKEY_ALGO_LETTERS[@pubkey_algo] || "?"
|
||||
end
|
||||
|
||||
def inspect
|
||||
|
|
|
@ -0,0 +1,89 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
require 'test_helper'
|
||||
|
||||
describe GPGME::Key do
|
||||
|
||||
it "has certain attributes" do
|
||||
key = GPGME::Key.find(:secret).first
|
||||
[:keylist_mode, :protocol, :owner_trust, :issuer_serial,
|
||||
:issuer_name, :chain_id, :subkeys, :uids].each do |attrib|
|
||||
assert key.respond_to?(attrib), "Key doesn't respond to #{attrib}"
|
||||
end
|
||||
end
|
||||
|
||||
it "won't allow the creation of GPGME::Key's without the C API" do
|
||||
assert_raises NoMethodError do
|
||||
GPGME::Key.new
|
||||
end
|
||||
end
|
||||
|
||||
describe :find do
|
||||
it "should return all by default" do
|
||||
keys = GPGME::Key.find :secret
|
||||
assert_instance_of GPGME::Key, keys.first
|
||||
assert 0 < keys.size
|
||||
end
|
||||
|
||||
it "returns an array even if you pass only one descriptor" do
|
||||
keys_one = GPGME::Key.find(:secret, KEY[:sha]).map{|key| key.subkeys.map(&:keyid)}
|
||||
keys_array = GPGME::Key.find(:secret, [KEY[:sha]]).map{|key| key.subkeys.map(&:keyid)}
|
||||
assert_equal keys_one, keys_array
|
||||
end
|
||||
|
||||
it "returns only secret keys if told to do so" do
|
||||
keys = GPGME::Key.find :secret
|
||||
assert keys.all?(&:secret?)
|
||||
end
|
||||
|
||||
it "returns only public keys if told to do so" do
|
||||
keys = GPGME::Key.find :public
|
||||
assert keys.none?(&:secret?)
|
||||
end
|
||||
|
||||
it "filters by capabilities" do
|
||||
GPGME::Key.any_instance.stubs(:usable_for?).returns(false)
|
||||
keys = GPGME::Key.find :public, "", :wadusing
|
||||
assert keys.empty?
|
||||
end
|
||||
end
|
||||
|
||||
# describe :trust do
|
||||
# it "returns :revoked if it is so"
|
||||
# it "returns :expired if it is expired"
|
||||
# it "returns :disabled if it is so"
|
||||
# it "returns :invalid if it is so"
|
||||
# it "returns nil otherwise"
|
||||
# end
|
||||
|
||||
# describe :capability do
|
||||
# it "returns an array of possible capabilities"
|
||||
# end
|
||||
|
||||
# describe :secret? do
|
||||
# "returns true/false depending on the instance variable"
|
||||
# end
|
||||
|
||||
describe :usable_for? do
|
||||
it "checks for the capabilities of the key and returns true if it matches all" do
|
||||
key = GPGME::Key.find(:secret).first
|
||||
|
||||
key.stubs(:capability).returns([:encrypt, :sign])
|
||||
assert key.usable_for?([])
|
||||
|
||||
key.stubs(:capability).returns([:encrypt, :sign])
|
||||
assert key.usable_for?([:encrypt])
|
||||
|
||||
key.stubs(:capability).returns([:encrypt, :sign])
|
||||
refute key.usable_for?([:certify])
|
||||
end
|
||||
|
||||
it "returns false if the key is expired or revoked or disabled or disabled" do
|
||||
key = GPGME::Key.find(:secret).first
|
||||
key.stubs(:trust).returns(:revoked)
|
||||
key.stubs(:capability).returns([:encrypt, :sign])
|
||||
refute key.usable_for?([:encrypt])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
# -*- encoding: utf-8 -*-
|
||||
require 'test_helper'
|
||||
|
||||
describe GPGME::SubKey do
|
||||
|
||||
# We trust Key for common methods that come from KeyCommon
|
||||
|
||||
it "has certain attributes" do
|
||||
subkey = GPGME::Key.find(:secret).first.subkeys.first
|
||||
[:pubkey_algo, :length, :keyid, :fpr, :fingerprint].each do |attrib|
|
||||
assert subkey.respond_to?(attrib), "Key doesn't respond to #{attrib}"
|
||||
end
|
||||
end
|
||||
|
||||
it "won't allow the creation of GPGME::SubKey's without the C API" do
|
||||
assert_raises NoMethodError do
|
||||
GPGME::SubKey.new
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Загрузка…
Ссылка в новой задаче