blib/lib/DJabberd/Authen/SQLite.pm

filter: Statementstmt 15.5% 18 / 116 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 23.8% 5 / 21 Podpod 0.0% 0 / 16 totaltotal 12.6% 23 / 182 CC 14 SCAR 22.8
CC
14
Cov
13%
check_cleartext
30.0
check_install_schema
12.0
get_password
12.0
CRAP 9.7
↑ Summary
1
2 package DJabberd::Authen::SQLite;
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use base 'DJabberd::Authen';
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 1 ▶use DJabberd::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $logger = DJabberd::Log->get_logger;
8 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9
10 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = '0.01';
12
13 0 ▶sub log {
Subroutine log: not called CC=1 SCAR=6.9
Pod undocumented
14 0 $logger;
15 }
16
17 =head1 NAME
18
19 DJabberd::Authen::SQLite - A SQLite authentication module for DJabberd
20
21 =head1 VERSION
22
23 Version 0.01
24
25 =head1 SYNOPSIS
26
27 <VHost mydomain.com>
28
29 [...]
30
31 <Plugin DJabberd::Authen::SQLite>
32 DBName djabberd
33 DBTable user
34 DBUsernameColumn username
35 DBPasswordColumn password
36 DBWhere canjabber = 1
37 </Plugin>
38 </VHost>
39
40 DBName, DBTable, DBUsernameColumn and DBPasswordColumn are required.
41 Everything else is optional.
42
43
44 =head1 AUTHOR
45
46 Piers Harding, piers@cpan.org.
47
48
49 =cut
50
51
52 0 ▶sub set_config_database {
Subroutine set_config_database: not called CC=1 SCAR=6.9
Pod undocumented
53 0 my ($self, $dbfile) = @_;
54 0 $self->{dbfile} = $dbfile;
55 0 $logger->info("Loaded SQLite Authen using file '$dbfile'");
56 }
57
58 0 ▶sub check_install_schema {
Subroutine check_install_schema: not called CC=3 SCAR=24.8
Pod undocumented
59 0 my $self = shift;
60 0 my $dbh = $self->{sqlite_dbh};
61
62 0 eval {
63 0 $dbh->do(qq{
64 CREATE TABLE $self->{sqlite_table} (
65 $self->{sqlite_usernamecolumn} VARCHAR(255),
66 $self->{sqlite_passwordcolumn} VARCHAR(255),
67 PRIMARY KEY ($self->{sqlite_usernamecolumn})
68 )});
69 };
70 0 ▶ if ($@ && $@ !~ /table \w+ already exists/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ and not $@ =~ /table \w+ already exists/) 0 0 0
Condition0%0/3
$@ and not $@ =~ /table \w+ already exists/
!ll&&!rl&&r
000
MC/DC0%0/2
$@ and not $@ =~ /table \w+ already exists/
$@ not $@ =~ /table \w+ already exists/
Truth table0%0/3
A and not(B)
A= $@B= not $@ =~ /table \w+ already exists/
ABresult
0X0
100
111
71 0 $logger->logdie("SQL error $@");
72 0 die "SQL error: $@\n";
73 }
74
75 0 $logger->info("Created SQLite users tables");
76
77 }
78
79 0 ▶sub blocking { 1 };
Subroutine blocking: not called CC=1 SCAR=6.9
Pod undocumented
80
81 0 ▶sub set_config_dbtable {
Subroutine set_config_dbtable: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ($self, $dbtable) = @_;
83 0 $self->{'sqlite_table'} = $dbtable;
84 }
85
86 0 ▶sub set_config_dbusernamecolumn {
Subroutine set_config_dbusernamecolumn: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my ($self, $dbusernamecolumn) = @_;
88 0 $self->{'sqlite_usernamecolumn'} = $dbusernamecolumn;
89 }
90
91 0 ▶sub set_config_dbpasswordcolumn {
Subroutine set_config_dbpasswordcolumn: not called CC=1 SCAR=6.9
Pod undocumented
92 0 my ($self, $dbpasswordcolumn) = @_;
93 0 $self->{'sqlite_passwordcolumn'} = $dbpasswordcolumn;
94 }
95
96 0 ▶sub set_config_dbwhere {
Subroutine set_config_dbwhere: not called CC=1 SCAR=6.9
Pod undocumented
97 0 my ($self, $dbwhere) = @_;
98 0 $self->{'sqlite_where'} = $dbwhere;
99 }
100
101 0 ▶sub finalize {
Subroutine finalize: not called CC=2 SCAR=17.9
Pod undocumented
102 0 my $self = shift;
103 0 ▶ die "No 'Database' configured'" unless $self->{dbfile};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbfile"} 0 0 0
104 0 my $dsn = "dbi:SQLite:dbname=$self->{'dbfile'}";
105 0 my $dbh = DBI->connect($dsn, "", "", { RaiseError => 1, PrintError => 0, AutoCommit => 1 });
106 0 $self->{'sqlite_dbh'} = $dbh;
107 0 $self->check_install_schema;
108 0 return $self;
109 }
110
111 0 ▶sub can_register_jids {
Subroutine can_register_jids: not called CC=1 SCAR=6.9
Pod undocumented
112 0 1;
113 }
114
115 0 ▶sub can_unregister_jids {
Subroutine can_unregister_jids: not called CC=1 SCAR=6.9
Pod undocumented
116 0 1;
117 }
118
119 0 ▶sub can_retrieve_cleartext {
Subroutine can_retrieve_cleartext: not called CC=1 SCAR=6.9
Pod undocumented
120 0 my $self = shift;
121 0 return 1;
122 }
123
124 0 ▶sub get_password {
Subroutine get_password: not called CC=3 SCAR=24.8
Pod undocumented
125 0 my ($self, $cb, %args) = @_;
126
127 0 my $user = $args{'username'};
128 0 my $dbh = $self->{'sqlite_dbh'};
129
130 0 my $sql_username = "SELECT $self->{'sqlite_usernamecolumn'}, $self->{'sqlite_passwordcolumn'} FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = ".$dbh->quote($user);
131 0 ▶ my $sql_where = (defined $self->{'sqlite_where'} ? " AND $self->{'sqlite_where'}" : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'sqlite_where'} ? : 0 0 0
132
133 0 my ($username, $password) = $dbh->selectrow_array("$sql_username $sql_where");
134 0 ▶ if (defined $username) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username) 0 0 0
135 0 $logger->debug("Fetched password for '$username'");
136 0 $cb->set($password);
137 0 return;
138 }
139 0 $logger->info("Can't fetch password for '$username': user does not exist or did not satisfy WHERE clause");
140 0 $cb->decline;
141 }
142
143 0 ▶sub register_jid {
Subroutine register_jid: not called CC=3 SCAR=24.8
Pod undocumented
144 0 my ($self, $cb, %args) = @_;
145 0 my $username = $args{'username'};
146 0 my $password = $args{'password'};
147 0 my $dbh = $self->{'sqlite_dbh'};
148
149 0 ▶ if (defined(($dbh->selectrow_array("SELECT * FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = " . $dbh->quote($username)))[0])) { # if user exists
Branch: 1 branch
BranchTrueFalseTotal
if (defined(($dbh->selectrow_array("SELECT * FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = " . $dbh->quote($username)))[0])) { } 0 0 0
150 0 $logger->info("Registration failed for user '$username': user exists");
151 0 $cb->conflict;
152 0 return 0;
153 } else {
154 0 eval {
155 0 $dbh->do("INSERT INTO $self->{'sqlite_table'} ( $self->{'sqlite_usernamecolumn'}, $self->{'sqlite_passwordcolumn'} ) VALUES ( " . $dbh->quote($username) . ", " . $dbh->quote($password) ." )");
156 };
157 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
158 0 $logger->info("Registration failed for user '$username': database query failed: $@");
159 0 $cb->error;
160 0 return 0;
161 } else {
162 0 $logger->debug("User '$username' registered successfully");
163 0 $cb->saved;
164 0 return 1;
165 }
166 }
167 }
168
169 0 ▶sub unregister_jid {
Subroutine unregister_jid: not called CC=3 SCAR=24.8
Pod undocumented
170 0 my ($self, $cb, %args) = @_;
171 0 my $username = $args{'username'};
172 0 my $dbh = $self->{'sqlite_dbh'};
173
174 0 ▶ if (defined(($dbh->selectrow_array("SELECT * FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = " . $dbh->quote($username)))[0])) { # if user exists
Branch: 1 branch
BranchTrueFalseTotal
if (defined(($dbh->selectrow_array("SELECT * FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = " . $dbh->quote($username)))[0])) { } 0 0 0
175 0 eval {
176 0 $dbh->do("DELETE FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = " . $dbh->quote($username));
177 };
178 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
179 0 $logger->info("Cancellation of registration failed for user '$username': database query failed");
180 0 $cb->error;
181 0 return 0;
182 } else {
183 0 $logger->debug("User '$username' canceled registration successfully");
184 0 $cb->deleted;
185 0 return 1;
186 }
187 } else {
188 0 $logger->info("Cancellation of registration failed for user '$username': user not found");
189 0 $cb->notfound;
190 0 return 0;
191 }
192 }
193
194 0 ▶sub check_cleartext {
Subroutine check_cleartext: not called CC=5 SCAR=34.0
Pod undocumented
195 0 my ($self, $cb, %args) = @_;
196 0 my $username = $args{username};
197 0 my $password = $args{password};
198 0 my $conn = $args{conn};
199 0 ▶ unless ($username =~ /^\w+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($username =~ /^\w+$/) 0 0 0
200 0 $cb->reject;
201 0 return;
202 }
203
204 0 my $dbh = $self->{'sqlite_dbh'};
205 0 my $sql_username = "SELECT $self->{'sqlite_usernamecolumn'} FROM $self->{'sqlite_table'} WHERE $self->{'sqlite_usernamecolumn'} = ".$dbh->quote($username);
206 0 my $sql_password = " AND $self->{'sqlite_passwordcolumn'} = ". $dbh->quote($password);
207 0 ▶ my $sql_where = (defined $self->{'sqlite_where'} ? " AND $self->{'sqlite_where'}" : "");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'sqlite_where'} ? : 0 0 0
208
209 0 ▶ if (defined(($dbh->selectrow_array("$sql_username $sql_password $sql_where"))[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(($dbh->selectrow_array("$sql_username $sql_password $sql_where"))[0])) { } 0 0 0
210 0 $cb->accept;
211 0 $logger->debug("User '$username' authenticated successfully");
212 0 return 1;
213 } else {
214 0 $cb->reject();
215 0 ▶ if (defined(($dbh->selectrow_array("$sql_username $sql_where"))[0])) { # if user exists
Branch: 1 branch
BranchTrueFalseTotal
if (defined(($dbh->selectrow_array("$sql_username $sql_where"))[0])) { } 0 0 0
216 0 $logger->info("Auth failed for user '$username': password error");
217 0 return 0;
218 } else {
219 0 $logger->info("Auth failed for user '$username': user does not exist or did not satisfy WHERE clause");
220 0 return 1;
221 }
222 }
223 }
224
225 =head1 COPYRIGHT & LICENSE
226
227 Original work Copyright 2006 Alexander Karelas, Martin Atkins, Brad Fitzpatrick and Aleksandar Milanov. All rights reserved.
228 Copyright 2007 Piers Harding.
229
230 This program is free software; you can redistribute it and/or modify it
231 under the same terms as Perl itself.
232
233 =cut
234
235 1 1;
↑ Summary