blib/lib/DBIx/Class/PassphraseColumn.pm

filter: Statementstmt 98.4% 63 / 64 Branchbran 66.6% 8 / 12 Conditioncond 60.0% 6 / 10 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 14 / 14 Podpod 100% 3 / 3 totaltotal 88.0% 96 / 109 CC 12 SCAR 13.5
CC
12
Cov
90%
register_column
8.2
new
4.0
set_column
2.1
CRAP 3.9
↑ Summary
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package DBIx::Class::PassphraseColumn; # git description: v0.04-6-g3f91ab8
5 # ABSTRACT: Automatically hash password/passphrase columns
6  
7 2 our $VERSION = '0.05';
8  
9 2 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Sub::Name 'subname';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use parent 'DBIx::Class';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #pod =head1 SYNOPSIS
17 #pod
18 #pod __PACKAGE__->load_components(qw(PassphraseColumn));
19 #pod
20 #pod __PACKAGE__->add_columns(
21 #pod id => {
22 #pod data_type => 'integer',
23 #pod is_auto_increment => 1,
24 #pod },
25 #pod passphrase => {
26 #pod data_type => 'text',
27 #pod passphrase => 'rfc2307',
28 #pod passphrase_class => 'SaltedDigest',
29 #pod passphrase_args => {
30 #pod algorithm => 'SHA-1',
31 #pod salt_random => 20,
32 #pod },
33 #pod passphrase_check_method => 'check_passphrase',
34 #pod },
35 #pod );
36 #pod
37 #pod __PACKAGE__->set_primary_key('id');
38 #pod
39 #pod
40 #pod In application code:
41 #pod
42 #pod # 'plain' will automatically be hashed using the specified passphrase_class
43 #pod # and passphrase_args. The result of the hashing will stored in the
44 #pod # specified encoding
45 #pod $rs->create({ passphrase => 'plain' });
46 #pod
47 #pod my $row = $rs->find({ id => $id });
48 #pod my $passphrase = $row->passphrase; # an Authen::Passphrase instance
49 #pod
50 #pod if ($row->check_passphrase($input)) { ...
51 #pod
52 #pod $row->passphrase('new passphrase');
53 #pod $row->passphrase( Authen::Passphrase::RejectAll->new );
54 #pod
55 #pod =head1 DESCRIPTION
56 #pod
57 #pod This component can be used to automatically hash password columns using any
58 #pod scheme supported by L<Authen::Passphrase> whenever the value of these columns is
59 #pod changed.
60 #pod
61 #pod =head1 COMPARISON TO SIMILAR MODULES
62 #pod
63 #pod This module is similar to both L<DBIx::Class::EncodedColumn> and
64 #pod L<DBIx::Class::DigestColumns>. Here's a brief comparison that might help you
65 #pod decide which one to choose.
66 #pod
67 #pod =over 4
68 #pod
69 #pod =item * C<DigestColumns> performs the hashing operation on C<insert> and
70 #pod C<update>. C<PassphraseColumn> and C<EncodedColumn> perform the operation when
71 #pod the value is set, or on C<new>.
72 #pod
73 #pod =item * C<DigestColumns> supports only algorithms of the Digest family.
74 #pod
75 #pod =item * C<EncodedColumn> employs a set of thin wrappers around different cipher
76 #pod modules to provide support for any cipher you wish to use and wrappers are very
77 #pod simple to write.
78 #pod
79 #pod =item * C<PassphraseColumn> delegates password hashing and encoding to
80 #pod C<Authen::Passphrase>, which already has support for a huge number of hashing
81 #pod schemes. Writing a new C<Authen::Passphrase> subclass to support other schemes
82 #pod is easy.
83 #pod
84 #pod =item * C<EncodedColumn> and C<DigestColumns> require all values in a hashed column to
85 #pod use the same hashing scheme. C<PassphraseColumn> stores both the hashed
86 #pod passphrase value I<and> the scheme used to hash it. Therefore it's possible to
87 #pod have different rows using different hashing schemes.
88 #pod
89 #pod This is especially useful when, for example, being tasked with importing records
90 #pod (e.g. users) from a legacy application, that used a certain hashing scheme and
91 #pod has no plain-text passwords available, into another application that uses
92 #pod another hashing scheme.
93 #pod
94 #pod =item * C<PassphraseColumn> and C<EncodedColumn> support having more than one hashed
95 #pod column per table and each column can use a different hashing
96 #pod scheme. C<DigestColumns> is limited to one hashed column per table.
97 #pod
98 #pod =item * C<DigestColumns> supports changing certain options at runtime, as well as the
99 #pod option to not automatically hash values on set. Neither C<PassphraseColumn> nor
100 #pod C<EncodedColumn> support this.
101 #pod
102 #pod =back
103 #pod
104 #pod =head1 OPTIONS
105 #pod
106 #pod This module provides the following options for C<add_column>:
107 #pod
108 #pod =begin :list
109 #pod
110 #pod = C<< passphrase => $encoding >>
111 #pod
112 #pod This specifies the encoding that passphrases will be stored in. Possible values are
113 #pod C<rfc2307> and C<crypt>. The value of C<$encoding> is passed on unmodified to the
114 #pod C<inflate_passphrase> option provided by
115 #pod L<DBIx::Class::InflateColumn::Authen::Passphrase>. Please refer to its
116 #pod documentation for details.
117 #pod
118 #pod = C<< passphrase_class => $name >>
119 #pod
120 #pod When receiving a plain string value for a passphrase, that value will be hashed
121 #pod using the C<Authen::Passphrase> subclass specified by C<$name>. A value of
122 #pod C<SaltedDigest>, for example, will cause passphrases to be hashed using
123 #pod C<Authen::Passphrase::SaltedDigest>.
124 #pod
125 #pod = C<< passphrase_args => \%args >>
126 #pod
127 #pod When attempting to hash a given passphrase, the C<%args> specified in this
128 #pod options will be passed to the constructor of the C<Authen::Passphrase> class
129 #pod specified using C<passphrase_class>, in addition to the actual password to hash.
130 #pod
131 #pod = C<< passphrase_check_method => $method_name >>
132 #pod
133 #pod If this option is specified, a method with the name C<$method_name> will be
134 #pod created in the result class. This method takes one argument, a plain text
135 #pod passphrase, and returns a true value if the provided passphrase matches the
136 #pod encoded passphrase stored in the row it's being called on.
137 #pod
138 #pod =end :list
139 #pod
140 #pod =cut
141  
142 2 __PACKAGE__->load_components(qw(InflateColumn::Authen::Passphrase));
143  
144 2 __PACKAGE__->mk_classdata('_passphrase_columns');
145  
146 #pod =method register_column
147 #pod
148 #pod Chains with the C<register_column> method in C<DBIx::Class::Row>, and sets up
149 #pod passphrase columns according to the options documented above. This would not
150 #pod normally be directly called by end users.
151 #pod
152 #pod =cut
153  
154 6 ▶sub register_column {
Subroutine register_column: called CC=8 SCAR=21.0
Pod documented
155 6 my ($self, $column, $info, @rest) = @_;
156  
157 6 ▶ if (my $encoding = $info->{passphrase}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = $info->{"passphrase"}) 4 2 6
158 4 $info->{inflate_passphrase} = $encoding;
159  
160 $self->throw_exception(q['passphrase_class' is a required argument])
161 unless exists $info->{passphrase_class}
162 4 ▶ && defined $info->{passphrase_class};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $info->{"passphrase_class"} and defined $info->{"passphrase_class"} 0 4 4
Condition33%1/3
exists $info->{"passphrase_class"} and defined $info->{"passphrase_class"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $info->{"passphrase_class"} and defined $info->{"passphrase_class"}
exists $info->{"passphrase_class"} defined $info->{"passphrase_class"}
Truth table33%1/3
A and B
A= exists $info->{"passphrase_class"}B= defined $info->{"passphrase_class"}
ABresult
0X0
100
111
163  
164 4 my $class = 'Authen::Passphrase::' . $info->{passphrase_class};
165 4 require_module($class);
166  
167 4 ▶ my $args = $info->{passphrase_args} || {};
Condition50%1/2
$info->{'passphrase_args'} || {}
l!l
40
MC/DC0%0/1
$info->{'passphrase_args'} || {}
$info->{'passphrase_args'}
Truth table50%1/2
A
A= $info->{'passphrase_args'}
Aresult
11
00
168 4 ▶ $self->throw_exception(q['passphrase_args' must be a hash reference])
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 4 4
169 unless ref $args eq 'HASH';
170  
171 16 ▶ my $encoder = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
172 16 my ($val) = @_;
173 16 $class->new(%{ $args }, passphrase => Encode::encode('UTF-8', $val))->${\"as_${encoding}"};
174 4 };
175  
176 $self->_passphrase_columns({
177 4 ▶ %{ $self->_passphrase_columns || {} },
Condition100%2/2
$self->_passphrase_columns || {}
l!l
22
MC/DC100%1/1
$self->_passphrase_columns || {}
$self->_passphrase_columns
Truth table100%2/2
A
A= $self->_passphrase_columns
Aresult
11
00
178 $column => $encoder,
179 });
180  
181 4 ▶ if (defined(my $meth = $info->{passphrase_check_method})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $meth = $info->{"passphrase_check_method"})) 4 0 4
182 20 ▶ my $checker = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine check_passphrase_crypt: called CC=1 SCAR=0.0
183 20 my ($row, $val) = @_;
184 20 return $row->get_inflated_column($column)->match(Encode::encode('UTF-8', $val));
185 4 };
186  
187 4 my $name = join q[::] => $self->result_class, $meth;
188  
189 {
190 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
191 4 *$name = subname $name => $checker;
192 }
193 }
194 }
195  
196 6 $self->next::method($column, $info, @rest);
197 }
198  
199 #pod =method set_column
200 #pod
201 #pod Hash a passphrase column whenever it is set.
202 #pod
203 #pod =cut
204  
205 12 ▶sub set_column {
Subroutine set_column: called CC=2 SCAR=7.6
Pod documented
206 12 my ($self, $col, $val, @rest) = @_;
207  
208 12 my $ppr_cols = $self->_passphrase_columns;
209 return $self->next::method($col, $ppr_cols->{$col}->($val), @rest)
210 12 ▶ if exists $ppr_cols->{$col};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ppr_cols->{$col} 12 0 12
211  
212 0 return $self->next::method($col, $val, @rest);
213 }
214  
215 #pod =method new
216 #pod
217 #pod Hash all passphrase columns on C<new()> so that C<copy()>, C<create()>, and
218 #pod others B<DWIM>.
219 #pod
220 #pod =cut
221  
222 4 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
223 4 my ($self, $attr, @rest) = @_;
224  
225 4 my $ppr_cols = $self->_passphrase_columns;
226 4 for my $col (keys %{ $ppr_cols }) {
227 8 ▶ next unless exists $attr->{$col} && !ref $attr->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $attr->{$col} and not ref $attr->{$col} 4 4 8
Condition66%2/3
exists $attr->{$col} and not ref $attr->{$col}
!ll&&!rl&&r
044
MC/DC50%1/2
exists $attr->{$col} and not ref $attr->{$col}
exists $attr->{$col} not ref $attr->{$col}
Truth table66%2/3
A and not(B)
A= exists $attr->{$col}B= not ref $attr->{$col}
ABresult
0X0
100
111
228 4 $attr->{$col} = $ppr_cols->{$col}->( $attr->{$col} );
229 }
230  
231 4 return $self->next::method($attr, @rest);
232 }
233  
234 #pod =head1 SEE ALSO
235 #pod
236 #pod L<DBIx::Class::InflateColumn::Authen::Passphrase>
237 #pod
238 #pod L<DBIx::Class::EncodedColumn>
239 #pod
240 #pod L<DBIx::Class::DigestColumns>
241 #pod
242 #pod =cut
243  
244 2 1;
245  
246 __END__
↑ Summary