File Coverage

blib/lib/DBIx/Class/CryptColumn.pm
Criterion Covered Total %
statement 58 58 100.0
branch 10 16 62.5
condition 1 3 33.3
subroutine 17 17 100.0
pod 4 4 100.0
total 90 98 91.8


line stmt bran cond sub pod time code
1             package DBIx::Class::CryptColumn;
2             $DBIx::Class::CryptColumn::VERSION = '0.007';
3 1     1   227407 use strict;
  1         8  
  1         28  
4 1     1   6 use warnings;
  1         3  
  1         30  
5              
6 1     1   5 use Sub::Util 1.40 'set_subname';
  1         23  
  1         105  
7 1     1   7 use namespace::clean;
  1         2  
  1         6  
8              
9 1     1   220 use parent 'DBIx::Class';
  1         4  
  1         7  
10              
11             __PACKAGE__->load_components(qw(InflateColumn::Crypt::Passphrase));
12              
13             sub new {
14 1     1 1 123994 my ($self, $attr, @rest) = @_;
15              
16 1         12 for my $col (grep { !/^-/ } keys %{ $attr }) {
  2         12  
  1         6  
17 1 50       69 next unless my $inflate = $self->column_info($col)->{inflate_passphrase};
18 1         101 $attr->{$col} = $inflate->hash_password($attr->{$col});
19             }
20              
21 1         288 return $self->next::method($attr, @rest);
22             }
23              
24             sub _export_sub {
25 3     3   10 my ($self, $name, $sub) = @_;
26 3         90 my $full_name = $self->result_class . "::$name";
27 1     1   266 no strict 'refs';
  1         6  
  1         553  
28 3         965 *$full_name = set_subname $full_name => $sub;
29             }
30              
31             sub register_column {
32 2     2 1 2038 my ($self, $column, $info, @rest) = @_;
33              
34 2 100       14 if (my $args = $info->{inflate_passphrase}) {
35 1 50       96 $self->throw_exception(q['inflate_passphrase' must be a hash reference]) unless ref $args eq 'HASH';
36              
37 1         4 my $crypt_passphrase = Crypt::Passphrase->new(%{$args});
  1         6  
38              
39 1 50       15716 if (defined(my $name = $args->{verify_method})) {
40             $self->_export_sub($name, sub {
41 7     7   9072 my ($row, $password) = @_;
        7      
42 7         25 return $crypt_passphrase->verify_password($password, $row->get_column($column));
43 1         19 });
44             }
45              
46 1 50       5 if (defined(my $name = $args->{rehash_method})) {
47             $self->_export_sub($name, sub {
48 4     4   7609 my $row = shift;
        4      
49 4         15 return $crypt_passphrase->needs_rehash($row->get_column($column));
50 1         10 });
51             }
52              
53 1 50       5 if (defined(my $name = $args->{verify_and_rehash_method})) {
54             $self->_export_sub($name, sub {
55 1     1   338 my ($row, $password) = @_;
        1      
56              
57 1         5 my $hash = $row->get_column($column);
58 1         14 my $result = $crypt_passphrase->verify_password($password, $hash);
59 1 50 33     161 if ($result && $crypt_passphrase->needs_rehash($hash)) {
60 1         33 $row->update({ $column => $password })->discard_changes;
61             }
62              
63 1         4390 return $result;
64 1         7 });
65             }
66              
67 1         4 $info->{inflate_passphrase} = $crypt_passphrase;
68             }
69              
70 2         11 $self->next::method($column, $info, @rest);
71             }
72              
73             sub set_column {
74 3     3 1 556 my ($self, $col, $val, @rest) = @_;
75              
76 3         70 my $inflate = $self->column_info($col)->{inflate_passphrase};
77 3 100       233 $val = $inflate->hash_password($val) if $inflate;
78              
79 3         273 return $self->next::method($col, $val, @rest);
80             }
81              
82             sub set_inflated_column {
83 1     1 1 2408 my ($self, $col, $val, @rest) = @_;
84 1         26 local $self->column_info($col)->{inflate_passphrase};
85 1         59 $self->next::method($col, $val, @rest);
86             }
87              
88             1;
89              
90             # ABSTRACT: Automatically hash password/passphrase columns
91              
92             __END__