File Coverage

blib/lib/Auth/Kokolores/Plugin/SqlRetrieve.pm
Criterion Covered Total %
statement 23 23 100.0
branch 4 4 100.0
condition n/a
subroutine 4 4 100.0
pod 0 1 0.0
total 31 32 96.8


line stmt bran cond sub pod time code
1             package Auth::Kokolores::Plugin::SqlRetrieve;
2              
3 2     2   1533 use Moose;
  2         3  
  2         11  
4 2     2   7815 use DBI;
  2         3  
  2         110  
5              
6             # ABSTRACT: kokolores plugin for retrieving users from SQL databases
7             our $VERSION = '1.01'; # VERSION
8              
9             extends 'Auth::Kokolores::Plugin';
10              
11 2     2   7 use Auth::Kokolores::ConnectionPool;
  2         2  
  2         444  
12              
13              
14             has 'handle' => ( is => 'rw', isa => 'Str', default => 'sql' );
15              
16             has 'select' => (
17             is => 'ro', isa => 'Str',
18             default => 'SELECT username, method, salt, cost, hash FROM users WHERE username = ?',
19             );
20              
21             has 'sth' => (
22             is => 'ro', isa => 'DBI::st', lazy => 1,
23             default => sub {
24             my $self = shift;
25             my $dbh = Auth::Kokolores::ConnectionPool->get_handle(
26             $self->handle,
27             );
28             if( ! defined $dbh || ref($dbh) ne 'DBI::db' ) {
29             die('There is no SQL connection configured with name \''.$self->handle.'\'!');
30             }
31             $self->log(4, 'preparing statement '.$self->select.'...');
32             return $dbh->prepare($self->select);
33             },
34             );
35              
36             sub authenticate {
37 2     2 0 2183 my ( $self, $r ) = @_;
38 2         60 my $sth = $self->sth;
39            
40 2         50 $sth->execute( $r->username );
41 2         36 my $fields = $sth->fetchrow_hashref;
42 2         8 $sth->finish;
43              
44 2 100       36 if( ! defined $fields ) {
45 1         27 $r->log(3, 'could not find user '.$r->username);
46 1         149 return 0;
47             }
48              
49 1         4 foreach my $field ( keys %$fields ) {
50 5 100       11 if( ! defined $fields->{$field} ) {
51 2         2 next;
52             }
53 3         12 $r->log(4, 'retrieved userinfo '.$field.'='.$fields->{$field});
54 3         527 $r->set_info( $field, $fields->{$field} );
55             }
56              
57 1         6 return 1;
58             }
59              
60             1;
61              
62             __END__
63              
64             =pod
65              
66             =encoding UTF-8
67              
68             =head1 NAME
69              
70             Auth::Kokolores::Plugin::SqlRetrieve - kokolores plugin for retrieving users from SQL databases
71              
72             =head1 VERSION
73              
74             version 1.01
75              
76             =head1 DESCRIPTION
77              
78             Retrieve a user from a SQL database.
79              
80             Will fail if no user is found.
81              
82             If a user is found all fields of the record will be stored in the
83             current requests userinfo field.
84              
85             =head1 EXAMPLE
86              
87             <Plugin retrieve-user>
88             module = "SqlRetrieve"
89             select = "SELECT * FROM passwd WHERE username = ?"
90             # handle = 'sql'
91             </Plugin>
92              
93             =head1 MODULE PARAMETERS
94              
95             =head2 select (default: SELECT username, method, salt, cost, hash FROM users WHERE username = ?)
96              
97             Define a SQL SELECT query to retrieve the user from the SQL database.
98              
99             =head2 handle (default: 'sql')
100              
101             Name of the SQL connection to retrieve from the connection pool.
102              
103             You must configure a SQL connection with the SqlConnection plugin.
104             It'll register a connection within the connection pool. The default
105             name will be 'sql'.
106              
107             =head1 AUTHOR
108              
109             Markus Benning <ich@markusbenning.de>
110              
111             =head1 COPYRIGHT AND LICENSE
112              
113             This software is Copyright (c) 2016 by Markus Benning <ich@markusbenning.de>.
114              
115             This is free software, licensed under:
116              
117             The GNU General Public License, Version 2, June 1991
118              
119             =cut