File Coverage

blib/lib/PlugAuth/Plugin/DBIAuth.pm
Criterion Covered Total %
statement 20 63 31.7
branch 0 28 0.0
condition 0 9 0.0
subroutine 7 14 50.0
pod 7 7 100.0
total 34 121 28.1


line stmt bran cond sub pod time code
1             package PlugAuth::Plugin::DBIAuth;
2              
3 1     1   816 use strict;
  1         2  
  1         29  
4 1     1   6 use warnings;
  1         1  
  1         31  
5 1     1   42 use 5.010;
  1         3  
6 1     1   2338 use DBI;
  1         18262  
  1         69  
7 1     1   1406 use Log::Log4perl qw/:easy/;
  1         53962  
  1         5  
8 1     1   1316800 use Role::Tiny::With;
  1         9124  
  1         87  
9 1     1   847 use Crypt::PasswdMD5 qw( unix_md5_crypt apache_md5_crypt );
  1         1116  
  1         990  
10              
11             with 'PlugAuth::Role::Plugin';
12             with 'PlugAuth::Role::Auth';
13              
14             # ABSTRACT: DBI Authentication back end for PlugAuth
15             our $VERSION = '0.05'; # VERSION
16              
17              
18             sub init
19             {
20 0     0 1   my($self) = @_;
21 0           my %db = $self->plugin_config->db;
22 0           my %sql = $self->plugin_config->sql;
23              
24             $self->{dbh} = DBI->connect($db{dsn}, $db{user}, $db{pass},
25 0           { RaiseError => 1, AutoCommit => 1 }
26             );
27            
28             $self->{dbh}->do($sql{init})
29 0 0         if defined $sql{init};
30            
31 0           foreach my $name (qw( check_credentials all_users create_user change_password delete_user ))
32             {
33             $self->{$name} = $self->{dbh}->prepare($sql{$name})
34 0 0         if defined $sql{$name};
35             }
36            
37 0           $self->{encryption} = $self->plugin_config->encryption(default => 'apache_md5');
38             }
39              
40              
41             sub check_credentials
42             {
43 0     0 1   my($self, $user, $pass) = @_;
44              
45 0 0         if(defined $self->{check_credentials})
46             {
47 0           $self->{check_credentials}->execute($user);
48 0           my($encrypted) = $self->{check_credentials}->fetchrow_array;
49 0           $self->{check_credentials}->finish;
50 0 0         if($encrypted)
51             {
52 0           my $tmp = crypt($pass, $encrypted);
53 0 0 0       return 1 if (defined $tmp) && ($tmp eq $encrypted);
54            
55 0 0         if($encrypted =~ /^\$(\w+)\$/)
56             {
57 0 0 0       return 1 if $1 eq 'apr1' && apache_md5_crypt( $pass, $encrypted ) eq $encrypted;
58 0 0 0       return 1 if $1 eq '1' && unix_md5_crypt ( $pass, $encrypted ) eq $encrypted;
59             }
60             }
61             }
62              
63 0           $self->deligate_check_credentials($user, $pass);
64             }
65              
66              
67             sub all_users
68             {
69             my($self) = @_;
70            
71             my @list;
72            
73             if(defined $self->{all_users})
74             {
75             $self->{all_users}->execute;
76             while(my $row = $self->{all_users}->fetchrow_arrayref)
77             {
78             push @list, $row->[0];
79             }
80             }
81            
82             @list;
83             }
84              
85              
86             sub create_user {
87 0     0 1   my($self, $user, $pass) = @_;
88            
89 0 0         if(defined $self->{create_user})
90             {
91 0           $self->{create_user}->execute($user, $self->created_encrypted_password($pass));
92 0           return 1;
93             }
94             else
95             {
96 0           return 0;
97             }
98             }
99              
100              
101             sub change_password {
102 0     0 1   my($self, $user, $pass) = @_;
103            
104 0 0         if(defined $self->{change_password})
105             {
106 0           $self->{change_password}->execute($self->created_encrypted_password($pass), $user);
107 0           return 1;
108             }
109             else
110             {
111 0           return 0;
112             }
113             }
114              
115              
116             sub delete_user {
117 0     0 1   my($self, $user) = @_;
118            
119 0 0         if(defined $self->{delete_user})
120             {
121 0           $self->{delete_user}->execute($user);
122 0           return 1;
123             }
124             else
125             {
126 0           return 0;
127             }
128             }
129              
130              
131 0     0 1   sub dbh { shift->{dbh} }
132              
133              
134             sub created_encrypted_password
135             {
136 0     0 1   my($self, $plain) = @_;
137 0           my $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
138 0 0         if($self->{encryption} eq 'apache_md5')
    0          
    0          
139             {
140 0           return apache_md5_crypt($plain, $salt);
141             }
142             elsif($self->{encryption} eq 'unix_md5')
143             {
144 0           return unix_md5_crypt($plain, $salt);
145             }
146             elsif($self->{encryption} eq 'unix')
147             {
148 0           return crypt($plain, $salt);
149             }
150             else
151             {
152 0           die "unknown encryption " . $self->{encryption};
153             }
154             }
155              
156             1;
157              
158             __END__