lib/Web/Authenticate/User/Storage/Handler/SQL.pm

filter: Statementstmt 50.0% 44 / 88 Branchbran 8.3% 2 / 24 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 76.9% 10 / 13 Podpod 100% 3 / 3 totaltotal 42.8% 60 / 140 CC 16 SCAR 27.6
CC
16
Cov
39%
load_user
42.0
load_user_by_id
20.0
_get_selection
12.0
CRAP 15.8
« blib/lib/Web/Authenticate.pm ↑ Summary blib/lib/Web/Authenticate/User/Storage/Handler/SQL.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Web::Authenticate::User::Storage::Handler::SQL;
3 1 $Web::Authenticate::User::Storage::Handler::SQL::VERSION = '0.003';
4 1 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use DBIx::Raw;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Web::Authenticate::Digest;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Web::Authenticate::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Ref::Util qw/is_hashref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 #ABSTRACT: Implementation of Web::Authenticate::User::Storage::Handler::Role that can be used with MySQL or SQLite.
11  
12 1 with 'Web::Authenticate::User::Storage::Handler::Role';
13  
14  
15  
16 1 has users_table => (
17 isa => 'Str',
18 is => 'ro',
19 required => 1,
20 default => 'users',
21 );
22  
23  
24 1 has id_field => (
25 isa => 'Str',
26 is => 'ro',
27 required => 1,
28 default => 'id',
29 );
30  
31  
32 1 has username_field => (
33 isa => 'Str',
34 is => 'ro',
35 required => 1,
36 default => 'username',
37 );
38  
39  
40 1 has password_field => (
41 isa => 'Str',
42 is => 'ro',
43 required => 1,
44 default => 'password',
45 );
46  
47  
48 has digest => (
49 does => 'Web::Authenticate::Digest::Role',
50 is => 'ro',
51 required => 1,
52 1 ▶ default => sub { Web::Authenticate::Digest->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
53 1 );
54  
55  
56 1 has dbix_raw => (
57 isa => 'DBIx::Raw',
58 is => 'ro',
59 required => 1,
60 );
61  
62  
63 has columns => (
64 isa => 'ArrayRef',
65 is => 'ro',
66 required => 1,
67 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 1 );
69  
70  
71 sub load_user {
72 0 ▶ my ($self, $username, $password) = @_;
Subroutine load_user: not called CC=6 SCAR=37.4
Pod documented
73 0 ▶ croak "must provide username" unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 0 0
74 0 ▶ croak "must provide password" unless $password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 0 0 0
75  
76 0 my $users_table = $self->users_table;
77 0 my $id_field = $self->id_field;
78 0 my $username_field = $self->username_field;
79 0 my $password_field = $self->password_field;
80  
81 0 my $selection = $self->_get_selection($id_field, $password_field);
82 0 my $user = $self->dbix_raw->raw("SELECT $selection FROM $users_table WHERE $username_field = ?", $username);
83  
84 0 ▶ unless ($user->{$id_field} and $user->{$password_field} and
Branch: 1 branch
BranchTrueFalseTotal
unless ($user->{$id_field} and $user->{$password_field} and $self->digest->validate($user->{$password_field}, $password)) 0 0 0
Condition0%0/6
$user->{$id_field} and $user->{$password_field} and $self->digest->validate($user->{$password_field}, $password)
!ll&&!rl&&r
000
$user->{$id_field} and $user->{$password_field}
!ll&&!rl&&r
000
MC/DC0%0/3
$user->{$id_field} and $user->{$password_field} and $self->digest->validate($user->{$password_field}, $password)
$user->{$id_field} $user->{$password_field} $self->digest->validate($user->{$password_field}, $password)
Truth table0%0/4
A and B and C
A= $user->{$id_field}B= $user->{$password_field}C= $self->digest->validate($user->{$password_field}, $password)
ABCresult
0XX0
10X0
1100
1111
85 $self->digest->validate($user->{$password_field}, $password)) {
86 0 carp "unable to load user";
87 0 return;
88 }
89  
90 0 delete $user->{$password_field};
91  
92 0 return Web::Authenticate::User->new(id => $user->{$id_field}, row => $user);
93 }
94  
95  
96 sub load_user_by_id {
97 0 ▶ my ($self, $user_id) = @_;
Subroutine load_user_by_id: not called CC=4 SCAR=30.0
Pod documented
98 0 ▶ croak "must provide user_id" unless $user_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $user_id 0 0 0
99  
100 0 my $users_table = $self->users_table;
101 0 my $id_field = $self->id_field;
102 0 my $username_field = $self->username_field;
103 0 my $password_field = $self->password_field;
104  
105 0 my $selection = $self->_get_selection($id_field);
106 0 my $user = $self->dbix_raw->raw("SELECT $selection FROM $users_table WHERE $id_field = ?", $user_id);
107  
108 0 ▶ unless ($user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
109 0 carp "unable to load user";
110 0 return;
111 }
112  
113 # if the only thing we selected was id, then we just got a scalar back
114 0 ▶ unless (is_hashref($user)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_hashref($user)) 0 0 0
115 0 $user = {$id_field => $user};
116 }
117  
118 0 return Web::Authenticate::User->new(id => $user->{$id_field}, row => $user);
119 }
120  
121  
122 sub store_user {
123 1 ▶ my ($self, $username, $password, $user_values) = @_;
Subroutine store_user: called CC=6 SCAR=24.0
Pod documented
124 1 ▶ croak "must provide username" unless $username;
Branch: 1 branch
BranchTrueFalseTotal
unless $username 0 1 1
125 1 ▶ croak "must provide password" unless $password;
Branch: 1 branch
BranchTrueFalseTotal
unless $password 0 1 1
126  
127 1 my $users_table = $self->users_table;
128 1 my $id_field = $self->id_field;;
129 1 my $username_field = $self->username_field;
130 1 my $password_field = $self->password_field;
131  
132 1 ▶ $user_values //= {};
Condition50%1/2
$user_values //= {}
l!l
01
MC/DC0%0/1
$user_values //= {}
$user_values
Truth table50%1/2
A
A= $user_values
Aresult
11
00
133 1 $user_values->{$username_field} = $username;
134 1 $user_values->{$password_field} = $self->digest->generate($password);;
135  
136 1 $self->dbix_raw->insert(href => $user_values, table => $users_table);
137  
138 # select columns user wants here instead except password_hash. Then pass into extra field
139 0 my $selection = $self->_get_selection($id_field);
140 0 my $user = $self->dbix_raw->raw("SELECT $selection FROM $users_table WHERE $username_field = ?", $username);
141  
142 0 ▶ unless ($user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
143 0 carp "unable to load user";
144 0 return;
145 }
146  
147 # if the only thing we selected was id, then we just got a scalar back
148 0 ▶ unless (is_hashref($user)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (is_hashref($user)) 0 0 0
149 0 $user = {$id_field => $user};
150 }
151  
152 0 return Web::Authenticate::User->new(id => $user->{$id_field}, row => $user);
153 }
154  
155 sub _get_selection {
156 0 ▶ my ($self, @columns) = @_;
Subroutine _get_selection: not called CC=3 SCAR=24.8
157  
158 0 ▶ die "columns required" unless @columns;
Branch: 1 branch
BranchTrueFalseTotal
unless @columns 0 0 0
159  
160 0 my $selection = join ',', @columns;
161  
162 0 ▶ if (@{$self->columns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->columns;}) 0 0 0
163 0 $selection .= ',' . join ',', @{$self->columns};
164 }
165  
166 0 return $selection;
167 }
168  
169 1 1;
170  
171 __END__
172  
173 =pod
174  
175 =encoding UTF-8
176  
177 =head1 NAME
178  
179 Web::Authenticate::User::Storage::Handler::SQL - Implementation of Web::Authenticate::User::Storage::Handler::Role that can be used with MySQL or SQLite.
180  
181 =head1 VERSION
182  
183 version 0.003
184  
185 =head1 DESCRIPTION
186  
187 This L<Web::Authenticate::User::Storage::Handler::Role> is meant to be used with a very specific table structure:
188  
189 CREATE TABLE users (
190 id INTEGER PRIMARY KEY AUTO_INCREMENT,
191 username VARCHAR(255) NOT NULL UNIQUE,
192 password TEXT NOT NULL
193 );
194  
195 Other columns can exists, and the names of the table and column can change. But at least these columns must exist
196 in order for this storage handler to work properly. Also, the primary key does not have to be an integer, and could
197 even be the username.
198  
199 =head1 METHODS
200  
201 =head2 users_table
202  
203 Sets the name of the users table that will be used when querying the database. Default is 'users'.
204  
205 =head2 id_field
206  
207 Sets the name of the id field that will be used when querying the database. Default is 'id'.
208  
209 =head2 username_field
210  
211 Sets the name of the username field that will be used when querying the database. Default is 'username'.
212  
213 =head2 password_field
214  
215 Sets the name of the password field that will be used when querying the database. Default is 'password'.
216  
217 =head2 digest
218  
219 Sets the L<Web::Authenticate::Digest::Role> that is used. Default is L<Web::Authenticate::Digest>.
220  
221 =head2 dbix_raw
222  
223 Sets the L<DBIx::Raw> object that will be used to query the database. This is required with no default.
224  
225 =head2 columns
226  
227 The columns to select. At a minimum, L</id_field> and L</password_field> will always be selected.
228  
229 $user_storage_handler->columns([qw/name age/]);
230  
231 Default is an empty array ref.
232  
233 =head2 load_user
234  
235 Accepts the username and password for a user, and returns a L<Web::Authenticate::User> if the password is correct and the user exists.
236 Otherwise, undef is returned. Any additional L</columns> will be stored in L<Web::Authenticate::User/row>. However, the password will be
237 deleted from the row before it is stored.
238  
239 my $user = $user_storage_handler->load_user($username, $password);
240  
241 =head2 load_user_by_id
242  
243 Loads a user by id.
244  
245 my $user = $user_storage_handler->load_user_by_id($user_id);
246  
247 =head2 store_user
248  
249 Takes in username, password, and any additional values for columns in a hash and we create that user with their hashed password.
250 Returns a L<Web::Authenticate::User> with any values from L</columns> stored in L<Web::Authenticate::User/row>.
251  
252 my $user_values => {
253 name => 'Fred',
254 age => 34,
255 insert_time => \'NOW()', # scalar ref for literal values. See DBIx::Raw
256 };
257 my $user = $user_storage_handler->store_user($username, $password, $user_vaules);
258  
259 # if you need no extra user values in the row
260 my $user = $user_storage_handler->store_user($username, $password);
261  
262 =head1 AUTHOR
263  
264 Adam Hopkins <srchulo@cpan.org>
265  
266 =head1 COPYRIGHT AND LICENSE
267  
268 This software is copyright (c) 2017 by Adam Hopkins.
269  
270 This is free software; you can redistribute it and/or modify it under
271 the same terms as the Perl 5 programming language system itself.
272  
273 =cut
« blib/lib/Web/Authenticate.pm ↑ Summary blib/lib/Web/Authenticate/User/Storage/Handler/SQL.pm »