added Crypt::Monkeysphere::Validator->findall()
authorDaniel Kahn Gillmor <dkg@fifthhorseman.net>
Fri, 22 Apr 2011 02:54:19 +0000 (22:54 -0400)
committerDaniel Kahn Gillmor <dkg@fifthhorseman.net>
Fri, 22 Apr 2011 02:54:19 +0000 (22:54 -0400)
Crypt/Monkeysphere/Validator.pm
unit-tests/20.validator/10.findall.t [new file with mode: 0644]

index 66fc638126a66cb4a0b3204371424cfd660569d2..3247f0c0a48dacb2839c2a0789eb0cbf73a0a58f 100644 (file)
@@ -147,6 +147,67 @@ sub lookup {
   return $self->_tryquery(uid => $opts{uid}, fpr => $opts{fpr}, key => $opts{key});
 }
 
+sub valid_binding {
+  my $self = shift;
+  my $uid  = shift;
+  my $gpgkey = shift;
+
+  my $validity = '-';
+  foreach my $tryuid ($gpgkey->user_ids) {
+    if ($tryuid->as_string eq $uid) {
+      return 1
+        if $tryuid->validity =~ /^[fu]$/;
+    }
+  }
+  return 0;
+}
+
+=pod
+
+=head2 findall
+
+Find all keys with appropriate capabilities and valid bindings to the given uid.
+
+=cut
+
+sub findall{
+  my $self=shift;
+  my $uid=shift;
+
+  $self->fetch_uid($uid) if ($self->{kspolicy} eq 'always');
+
+  my @keys = $self->_findall($uid);
+
+  if (scalar(@keys) == 0 and $self->{kspolicy} eq 'unlessvalid'){
+    $self->fetch_uid($uid);
+    @keys=$self->_findall($uid);
+  }
+
+  return @keys;
+}
+
+sub _findall {
+  my $self=shift;
+  my $uid=shift;
+
+  my @keys;
+  my $x = 0;
+
+  foreach my $gpgkey ($self->{gnupg}->get_public_keys('='.$uid)) {
+    if ($self->valid_binding($uid, $gpgkey)) {
+      foreach my $subkey ($gpgkey, @{$gpgkey->subkeys()}) {
+       if ($self->test_capable($subkey) ) {
+         $self->log('verbose', "key 0x%s is capable...\n",$subkey->hex_id);
+
+         push(@keys, $subkey);
+       }
+      }
+    }
+  }
+  return @keys;
+}
+
+
 sub keycomp {
   my $self=shift;
   my $rsakey = shift;
diff --git a/unit-tests/20.validator/10.findall.t b/unit-tests/20.validator/10.findall.t
new file mode 100644 (file)
index 0000000..fc316d5
--- /dev/null
@@ -0,0 +1,41 @@
+# -*- perl -*-
+use Test::More;
+
+use Crypt::Monkeysphere::Validator;
+use GnuPG::Interface;
+use File::Temp qw(tempdir);
+use Data::Dumper;
+
+use strict;
+
+
+my $gpgdir = $ENV{MSTEST_GNUPGHOME};
+
+unless (defined $gpgdir && -d $gpgdir){
+  plan skip_all => "Preseeded GPGHOME not found";
+  goto end;
+}
+
+
+my $gnupg = new GnuPG::Interface();
+$gnupg->options->hash_init(homedir=>$gpgdir);
+
+my $validator=new Crypt::Monkeysphere::Validator(gnupg=>$gnupg,
+                                                kspolicy=>'never',
+                                                loglevel=>'debug');
+
+
+plan tests =>2;
+
+isa_ok($validator,'Crypt::Monkeysphere::Validator');
+
+my $uid='Joe Tester <joe@example.net>';
+
+my @keys=$validator->findall($uid);
+
+
+
+ok(scalar @keys >= 3);
+
+
+end: