File Coverage

blib/lib/Auth/Kokolores/Plugin/SqlRetrieve.pm
Criterion Covered Total %
statement 20 20 100.0
branch 4 4 100.0
condition n/a
subroutine 3 3 100.0
pod 0 1 0.0
total 27 28 96.4


line stmt bran cond sub pod time code
1             package Auth::Kokolores::Plugin::SqlRetrieve;
2              
3 2     2   1805 use Moose;
  2         3  
  2         13  
4 2     2   12317 use DBI;
  2         25447  
  2         663  
5              
6             # ABSTRACT: kokolores plugin for retrieving users from SQL databases
7             our $VERSION = '1.00'; # VERSION
8              
9             extends 'Auth::Kokolores::Plugin';
10              
11              
12             has 'select' => (
13             is => 'ro', isa => 'Str',
14             default => 'SELECT username, method, salt, cost, hash FROM users WHERE username = ?',
15             );
16              
17             has 'user' => ( is => 'ro', isa => 'Str', default => '' );
18             has 'password' => ( is => 'ro', isa => 'Str', default => '' );
19              
20             has 'dsn' => (
21             is => 'ro', isa => 'Str',
22             default => 'dbi:SQLite:dbname=/var/lib/saslauthd/saslauth.sqlite',
23             );
24              
25             has 'dbh' => (
26             is => 'ro', isa => 'DBI::db',
27             lazy => 1,
28             default => sub {
29             my $self = shift;
30             $self->log(1, 'connecting to '.$self->dsn.'...');
31             my $dbh = DBI->connect(
32             $self->dsn, $self->user, $self->password,
33             { RaiseError => 1 },
34             );
35             return $dbh;
36             },
37             );
38              
39             has 'sth' => (
40             is => 'ro', isa => 'DBI::st', lazy => 1,
41             default => sub {
42             my $self = shift;
43             my $dbh = $self->dbh;
44             $self->log(1, 'preparing statement '.$self->select.'...');
45             return $dbh->prepare($self->select);
46             },
47             );
48              
49             sub authenticate {
50 2     2 0 568 my ( $self, $r ) = @_;
51 2         67 my $sth = $self->sth;
52            
53 2         51 $sth->execute( $r->username );
54 2         33 my $fields = $sth->fetchrow_hashref;
55 2         8 $sth->finish;
56              
57 2 100       6 if( ! defined $fields ) {
58 1         28 $r->log(1, 'could not find user '.$r->username);
59 1         84 return 0;
60             }
61              
62 1         4 foreach my $field ( keys %$fields ) {
63 5 100       9 if( ! defined $fields->{$field} ) {
64 2         12 next;
65             }
66 3         15 $r->log(1, 'retrieved userinfo '.$field.'='.$fields->{$field});
67 3         300 $r->set_info( $field, $fields->{$field} );
68             }
69              
70 1         6 return 1;
71             }
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Auth::Kokolores::Plugin::SqlRetrieve - kokolores plugin for retrieving users from SQL databases
84              
85             =head1 VERSION
86              
87             version 1.00
88              
89             =head1 DESCRIPTION
90              
91             Retrieve a user from a SQL database.
92              
93             Will fail if no user is found.
94              
95             If a user is found all fields of the record will be stored in the
96             current requests userinfo field.
97              
98             =head1 EXAMPLE
99              
100             <Plugin retrieve-user>
101             module = "SqlRetrieve"
102             dsn = "dbi:SQLite:dbname=/var/lib/kokolores/users.sqlite"
103             select = "SELECT * FROM passwd WHERE username = ?"
104             </Plugin>
105              
106             =head1 MODULE PARAMETERS
107              
108             =head2 select (default: SELECT username, method, salt, cost, hash FROM users WHERE username = ?)
109              
110             Define a SQL SELECT query to retrieve the user from the SQL database.
111              
112             =head2 user (default: '')
113              
114             User used to connect to SQL database.
115              
116             =head2 password (default: '')
117              
118             Password used to connect to SQL database.
119              
120             =head2 dsn (default: 'dbi:SQLite:dbname=/var/lib/saslauthd/saslauth.sqlite')
121              
122             A connection string for the database in perl-DBI format.
123              
124             =head1 AUTHOR
125              
126             Markus Benning <ich@markusbenning.de>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is Copyright (c) 2016 by Markus Benning <ich@markusbenning.de>.
131              
132             This is free software, licensed under:
133              
134             The GNU General Public License, Version 2, June 1991
135              
136             =cut