File Coverage

blib/lib/Ark/Plugin/Authentication/Credential/Password.pm
Criterion Covered Total %
statement 19 20 95.0
branch 3 4 75.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 26 29 89.6


line stmt bran cond sub pod time code
1             package Ark::Plugin::Authentication::Credential::Password;
2 3     3   5328 use strict;
  3         8  
  3         95  
3 3     3   16 use warnings;
  3         5  
  3         77  
4 3     3   16 use Ark::Plugin 'Auth';
  3         5  
  3         17  
5              
6             has cred_password_user_field => (
7             is => 'rw',
8             isa => 'Str',
9             lazy => 1,
10             default => sub {
11             my $self = shift;
12             $self->class_config->{user_field} || 'username';
13             },
14             );
15              
16             has cred_password_password_field => (
17             is => 'rw',
18             isa => 'Str',
19             lazy => 1,
20             default => sub {
21             my $self = shift;
22             $self->class_config->{password_field} || 'password';
23             },
24             );
25              
26             has cred_password_password_type => (
27             is => 'rw',
28             isa => 'Str',
29             lazy => 1,
30             default => sub {
31             my $self = shift;
32             $self->class_config->{password_type} || 'clear';
33             },
34             );
35              
36             has cred_password_password_digest_model => (
37             is => 'rw',
38             isa => 'Object',
39             lazy => 1,
40             default => sub {
41             my $self = shift;
42             my $model = eval {
43             $self->app->model($self->class_config->{digest_model} || 'Digest');
44             };
45             },
46             );
47              
48             has cred_password_password_pre_salt => (
49             is => 'rw',
50             isa => 'Str',
51             lazy => 1,
52             default => sub {
53             my $self = shift;
54             $self->class_config->{password_pre_salt} || '';
55             },
56             );
57              
58             has cred_password_password_post_salt => (
59             is => 'rw',
60             isa => 'Str',
61             lazy => 1,
62             default => sub {
63             my $self = shift;
64             $self->class_config->{password_post_salt} || '';
65             },
66             );
67              
68             around authenticate => sub {
69             my $prev = shift->(@_);
70             return $prev if $prev;
71              
72             my ($self, $info) = @_;
73              
74             my $id = $info->{ $self->cred_password_user_field };
75             if (my $user = $self->find_user($id, $info)) {
76             my $check_password = __PACKAGE__->can('check_password');
77             if ($check_password->($self, $info, $user)) {
78             $self->persist_user($user);
79             return $user;
80             }
81             }
82              
83             return;
84             };
85              
86             sub check_password {
87 3     3 0 7 my ($self, $info, $user) = @_;
88              
89 3         27 my $password = $info->{ $self->cred_password_password_field };
90 3         155 my $password_expected = $user->hash->{ $self->cred_password_password_field };
91              
92 3 100       19 if ($self->cred_password_password_type eq 'clear') {
    50          
93 2         88 return $password eq $password_expected;
94             }
95             elsif ($self->cred_password_password_type eq 'hashed') {
96 1         43 my $digest = $self->cred_password_password_digest_model;
97 1         86 $digest->add( $self->cred_password_password_pre_salt );
98 1         40 $digest->add( $password );
99 1         5 $digest->add( $self->cred_password_password_post_salt );
100              
101 1         43 return $digest->hexdigest eq $password_expected;
102             }
103             else {
104 0           die qq/Unknown password type "$self->{password_type}"/;
105             }
106             }
107              
108             1;