| 1 | package DBIx::Class::EncodedColumn::Crypt::PBKDF2; | ||||||||||
| 2 | # ABSTRACT: PBKDF2 support for DBIx::Class::EncodedColumn | ||||||||||
| 3 | |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Crypt::PBKDF2; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | our $VERSION = '0.000001'; # VERSION: generated by DZP::OurPkgVersion | |||||||||
| 11 | |||||||||||
| 12 | sub make_encode_sub { | ||||||||||
| 13 | 0 | ▶ | my ( $class, $col, $args ) = @_; | ||||||||
|
Subroutine
make_encode_sub: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 14 | |||||||||||
| 15 | 0 | my $pbkdf2 = _get_pbkdf2_obj_from_args($args); | |||||||||
| 16 | |||||||||||
| 17 | return sub { | ||||||||||
| 18 | 0 | ▶ | my ( $plain_text ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 19 | 0 | return $pbkdf2->generate($plain_text); | |||||||||
| 20 | 0 | }; | |||||||||
| 21 | } ## end sub make_encode_sub | ||||||||||
| 22 | |||||||||||
| 23 | |||||||||||
| 24 | sub make_check_sub { | ||||||||||
| 25 | 0 | ▶ | my ( $class, $col, $args ) = @_; | ||||||||
|
Subroutine
make_check_sub: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 26 | |||||||||||
| 27 | 0 | my $pbkdf2 = _get_pbkdf2_obj_from_args($args); | |||||||||
| 28 | |||||||||||
| 29 | return sub { | ||||||||||
| 30 | 0 | ▶ | my ( $self, $password ) = @_; | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 31 | 0 | ▶ | croak 'Must supply a password to check' unless $password; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 32 | 0 | return $pbkdf2->validate( $self->get_column($col), $password ); | |||||||||
| 33 | 0 | }; | |||||||||
| 34 | } ## end sub make_check_sub | ||||||||||
| 35 | |||||||||||
| 36 | sub _get_pbkdf2_obj_from_args { | ||||||||||
| 37 | 0 | ▶ | my ( $args ) = @_; | ||||||||
|
Subroutine
_get_pbkdf2_obj_from_args: not called CC=2 SCAR=17.9
| |||||||||||
| 38 | |||||||||||
| 39 | 0 | my %new_args = %$args; | |||||||||
| 40 | # my %new_args; ## We should probably do some validation of individual args | ||||||||||
| 41 | # $new_args{hash_class} = $args->{hash_class} if exists $args->{hash_class}; ## 'HMACSHA1', 'HMACSHA3' | ||||||||||
| 42 | # $new_args{hash_args} = $args->{hash_args} if exists $args->{hash_args}; ## { sha_size => 512 } | ||||||||||
| 43 | # $new_args{iterations} = $args->{iterations} if exists $args->{iterations}; ## 1000 | ||||||||||
| 44 | # $new_args{output_len} = $args->{output_len} if exists $args->{output_len}; ## 20 | ||||||||||
| 45 | # $new_args{salt_len} = $args->{salt_len} if exists $args->{salt_len}; ## 4 | ||||||||||
| 46 | # $new_args{encoding} = $args->{encoding} if exists $args->{encoding}; ## 'ldap' | ||||||||||
| 47 | # $new_args{length_limit} = $args->{length_limit} if exists $args->{length_limit}; | ||||||||||
| 48 | |||||||||||
| 49 | 0 | my $pbkdf2 = Crypt::PBKDF2->new(%new_args); | |||||||||
| 50 | 0 | ▶ | croak sprintf( "failed to create pbkdf2: %s\n", $_ ) unless $pbkdf2; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | 0 | return $pbkdf2; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | |||||||||||
| 56 | 1 | 1; | |||||||||
| 57 | |||||||||||
| 58 | =pod | ||||||||||
| 59 | |||||||||||
| 60 | =encoding UTF-8 | ||||||||||
| 61 | |||||||||||
| 62 | =head1 NAME | ||||||||||
| 63 | |||||||||||
| 64 | DBIx::Class::EncodedColumn::Crypt::PBKDF2 - PBKDF2 support for DBIx::Class::EncodedColumn | ||||||||||
| 65 | |||||||||||
| 66 | =head1 VERSION | ||||||||||
| 67 | |||||||||||
| 68 | version 0.000001 | ||||||||||
| 69 | |||||||||||
| 70 | =head1 SYNOPSIS | ||||||||||
| 71 | |||||||||||
| 72 | __PACKAGE__->add_columns( | ||||||||||
| 73 | 'password' => { | ||||||||||
| 74 | data_type => 'text', | ||||||||||
| 75 | encode_column => 1, | ||||||||||
| 76 | encode_class => 'Crypt::PBKDF2', | ||||||||||
| 77 | encode_args => { | ||||||||||
| 78 | hash_class => 'HMACSHA1', | ||||||||||
| 79 | iterations => 1000 | ||||||||||
| 80 | }, | ||||||||||
| 81 | encode_check_method => 'check_password', | ||||||||||
| 82 | } | ||||||||||
| 83 | ) | ||||||||||
| 84 | |||||||||||
| 85 | =head1 DESCRIPTION | ||||||||||
| 86 | |||||||||||
| 87 | =head1 NAME | ||||||||||
| 88 | |||||||||||
| 89 | DBIx::Class::EncodedColumn::Crypt::PBKDF2 - PBKDF2 support for DBIx::Class::EncodedColumn | ||||||||||
| 90 | |||||||||||
| 91 | =head1 NAME | ||||||||||
| 92 | |||||||||||
| 93 | DBIx::Class::EncodedColumn::Crypt::PBKDF2 | ||||||||||
| 94 | |||||||||||
| 95 | =head1 ARGUMENTS for C<encode_args> - same as args for C<Crypt::PBKDF2->new> | ||||||||||
| 96 | |||||||||||
| 97 | =head2 hash_class | ||||||||||
| 98 | |||||||||||
| 99 | B<Type:> String, B<Default:> HMACSHA1 | ||||||||||
| 100 | |||||||||||
| 101 | The name of the default class that will provide PBKDF2's Pseudo-Random | ||||||||||
| 102 | Function (the backend hash). If the value starts with a C<+>, the C<+> will | ||||||||||
| 103 | be removed and the remainder will be taken as a fully-qualified package | ||||||||||
| 104 | name. Otherwise, the value will be appended to C<Crypt::PBKDF2::Hash::>. | ||||||||||
| 105 | |||||||||||
| 106 | =head2 hash_args | ||||||||||
| 107 | |||||||||||
| 108 | B<Type:> HashRef, B<Default:> {} | ||||||||||
| 109 | |||||||||||
| 110 | Arguments to be passed to the C<hash_class> constructor. | ||||||||||
| 111 | |||||||||||
| 112 | =head2 hasher | ||||||||||
| 113 | |||||||||||
| 114 | B<Type:> Object (must fulfill role L<Crypt::PBKDF2::Hash>), B<Default:> None. | ||||||||||
| 115 | |||||||||||
| 116 | It is also possible to provide a hash object directly; in this case the | ||||||||||
| 117 | C<hash_class> and C<hash_args> are ignored. | ||||||||||
| 118 | |||||||||||
| 119 | =head2 iterations | ||||||||||
| 120 | |||||||||||
| 121 | B<Type:> Integer, B<Default:> 1000. | ||||||||||
| 122 | |||||||||||
| 123 | The default number of iterations of the hashing function to use for the | ||||||||||
| 124 | C<generate> and C<PBKDF2> methods. | ||||||||||
| 125 | |||||||||||
| 126 | =head2 output_len | ||||||||||
| 127 | |||||||||||
| 128 | B<Type:> Integer. | ||||||||||
| 129 | |||||||||||
| 130 | The default size (in bytes, not bits) of the output hash. If a value isn't | ||||||||||
| 131 | provided, the output size depends on the C<hash_class>S< / >C<hasher> | ||||||||||
| 132 | selected, and will equal the output size of the backend hash (e.g. 20 bytes | ||||||||||
| 133 | for HMACSHA1). | ||||||||||
| 134 | |||||||||||
| 135 | =head2 salt_len | ||||||||||
| 136 | |||||||||||
| 137 | B<Type:> Integer, B<Default:> 4 | ||||||||||
| 138 | |||||||||||
| 139 | The default salt length (in bytes) for the C<generate> method. | ||||||||||
| 140 | |||||||||||
| 141 | =head2 encoding | ||||||||||
| 142 | |||||||||||
| 143 | B<Type:> String (either "crypt" or "ldap"), B<Default:> "ldap" | ||||||||||
| 144 | |||||||||||
| 145 | The hash format to generate. The "ldap" format is intended to be compatible | ||||||||||
| 146 | with RFC2307, and looks like: | ||||||||||
| 147 | |||||||||||
| 148 | {X-PBKDF2}HMACSHA1:AAAD6A:8ODUPA==:1HSdSVVwlWSZhbPGO7GIZ4iUbrk= | ||||||||||
| 149 | |||||||||||
| 150 | While the "crypt" format is similar to the format used by the C<crypt()> | ||||||||||
| 151 | function, except with more structured information in the second (salt) field. | ||||||||||
| 152 | It looks like: | ||||||||||
| 153 | |||||||||||
| 154 | $PBKDF2$HMACSHA1:1000:4q9OTg==$9Pb6bCRgnct/dga+4v4Lyv8x31s= | ||||||||||
| 155 | |||||||||||
| 156 | Versions of this module up to 0.110461 generated the "crypt" format, so set | ||||||||||
| 157 | that if you want it. Current versions of this module will read either format, | ||||||||||
| 158 | but the "ldap" format is preferred. | ||||||||||
| 159 | |||||||||||
| 160 | =head2 length_limit | ||||||||||
| 161 | |||||||||||
| 162 | B<Type:> Integer | ||||||||||
| 163 | |||||||||||
| 164 | The maximum password length to allow, for generate and verify functions. | ||||||||||
| 165 | Allowing passwords of unlimited length can allow a denial-of-service attack | ||||||||||
| 166 | in which an attacker asks the server to validate very large passwords. | ||||||||||
| 167 | |||||||||||
| 168 | For compatibility this attribute is unset by default, but it is recommended | ||||||||||
| 169 | to set it to a reasonably small value like 100 -- large enough that users | ||||||||||
| 170 | aren't discouraged from having secure passwords, but small enough to limit | ||||||||||
| 171 | the computation needed to validate any one password. | ||||||||||
| 172 | |||||||||||
| 173 | =head1 METHODS | ||||||||||
| 174 | |||||||||||
| 175 | =head2 make_encode_sub($column_name, \%encode_args) | ||||||||||
| 176 | |||||||||||
| 177 | Returns a coderef that accepts a plaintext value and returns an | ||||||||||
| 178 | encoded value. | ||||||||||
| 179 | |||||||||||
| 180 | =head2 make_check_sub($column_name, \%encode_args) | ||||||||||
| 181 | |||||||||||
| 182 | Returns a coderef that when given the row object and a plaintext value | ||||||||||
| 183 | will return a boolean if the plaintext matches the encoded value. This | ||||||||||
| 184 | is typically used for password authentication. | ||||||||||
| 185 | |||||||||||
| 186 | =head1 SEE ALSO | ||||||||||
| 187 | |||||||||||
| 188 | =over 4 | ||||||||||
| 189 | |||||||||||
| 190 | =item * | ||||||||||
| 191 | |||||||||||
| 192 | L<DBIx::Class::EncodedColumn> | ||||||||||
| 193 | |||||||||||
| 194 | =item * | ||||||||||
| 195 | |||||||||||
| 196 | L<Crypt::PBKDF2> | ||||||||||
| 197 | |||||||||||
| 198 | =back | ||||||||||
| 199 | |||||||||||
| 200 | =head1 AUTHOR | ||||||||||
| 201 | |||||||||||
| 202 | Charlie Garrison <garrison@zeta.org.au> | ||||||||||
| 203 | |||||||||||
| 204 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 205 | |||||||||||
| 206 | This software is copyright (c) 2016 by Charlie Garrison. | ||||||||||
| 207 | |||||||||||
| 208 | This is free software; you can redistribute it and/or modify it under | ||||||||||
| 209 | the same terms as the Perl 5 programming language system itself. | ||||||||||
| 210 | |||||||||||
| 211 | =cut | ||||||||||
| 212 | |||||||||||
| 213 | __END__; | ||||||||||