blib/lib/MYDan/Agent/Auth.pm untested

filter: Statementstmt 0.0% 0 / 45 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 95 CC 12 SCAR 30.7
CC
12
Cov
0%
verify
42.0
new
30.0
sign
12.0
CRAP 21.5
1 package MYDan::Agent::Auth;
2  
3 =head1 NAME
4  
5 MYDan::Agent::Auth
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Agent::Auth;
10  
11 my $sig = MYDan::Agent::Auth->new( key => /foo/ca/ )->sign( $mesg );
12  
13 MYDan::Agent::Auth->new( pub => /foo/ca/ )->verify( $sig, $mesg );
14  
15  
16 =cut
17 use strict;
18 use warnings;
19  
20 use Carp;
21 use YAML::XS;
22 use File::Basename;
23 use Crypt::PK::RSA;
24 use FindBin qw( $RealBin );
25  
26 sub new
27 {
28 my ( $class, $type, $auth, %self ) = @_;
29 confess "invalid auth dir" unless $auth && -d $auth;
30  
31 $self{data} = +{};
32 for my $file ( ( $self{user} && $type eq 'pub' ) ? ( "$auth/$self{user}.pub" ) : glob "$auth/*" )
33 {
34 next unless -f $file;
35 $self{data}{ basename $file} = "$file.$type" if $file =~ s/\.$type$//;
36 }
37  
38 bless \%self, ref $class || $class;
39 }
40  
41 sub sign
42 {
43 my ( $this, $mesg, %sig ) = splice @_, 0, 2;
44 confess "no mesg" unless $mesg;
45  
46 map
47 {
48 $sig{$_} = Crypt::PK::RSA->new( $this->{data}{$_} )->sign_message( $mesg );
49 }keys %{$this->{data}};
50 return wantarray ? %sig : \%sig;
51 }
52  
53 sub verify
54 {
55 my ( $this, $sig, $mesg ) = @_;
56 if( my $user = $this->{user} )
57 {
58 return 0 unless $this->{data}{$user};
59 map{
60 return 1 if Crypt::PK::RSA->new( $this->{data}{$user} )->verify_message( $sig->{$_}, $mesg );
61 }keys %$sig;
62 }
63 else
64 {
65 for( keys %{$this->{data}} )
66 {
67 next unless $sig->{$_};
68 return 1 if Crypt::PK::RSA->new( $this->{data}{$_} )->verify_message( $sig->{$_}, $mesg );
69 }
70 }
71 return 0;
72 }
73  
74 1;