Jifty/Plugin/Authentication/Password/Mixin/Model/User.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 104 CC 7 SCAR 18.7
CC
7
Cov
0%
password_is
12.0
validate_password
12.0
after_create
12.0
CRAP 6.5
1 use strict;
2 use warnings;
3  
4 package Jifty::Plugin::Authentication::Password::Mixin::Model::User;
5 use Jifty::DBI::Schema;
6 use base 'Jifty::DBI::Record::Plugin';
7  
8 use Digest::MD5 qw'';
9  
10 our @EXPORT = qw(password_is hashed_password_is regenerate_auth_token has_alternative_auth);
11  
12 =head1 NAME
13  
14 Jifty::Plugin::Authentication::Password::Mixin::Model::User - password plugin user mixin model
15  
16 =head1 SYNOPSIS
17  
18 package MyApp::Model::User;
19 use Jifty::DBI::Schema;
20 use MyApp::Record schema {
21 # custom column definitions
22 };
23  
24 use Jifty::Plugin::User::Mixin::Model::User; # name, email, email_confirmed
25 use Jifty::Plugin::Authentication::Password::Mixin::Model::User;
26 # ^^ password, auth_token
27  
28 =head1 DESCRIPTION
29  
30 This mixin model is added to the application's account model for use with the password authentication plugin. This mixin should be used in combination with L<Jifty::Plugin::User::Mixin::Model::User>.
31  
32 =head1 SCHEMA
33  
34 This mixin adds the following columns to the model schema:
35  
36 =head2 auth_token
37  
38 This is a unique identifier used when confirming a user's email account and recovering a lost password.
39  
40 =head2 password
41  
42 This is the user's password. It will be stored in the database after being processed through L<Digest::MD5>, so the password cannot be directly recovered from the database.
43  
44 =cut
45  
46 use Jifty::Plugin::Authentication::Password::Record schema {
47  
48  
49 column auth_token =>
50 render_as 'unrendered',
51 type is 'varchar(255)',
52 max_length is 255,
53 default is '',
54 label is _('Authentication token');
55
56  
57  
58 column password =>
59 is unreadable,
60 label is _('Password'),
61 type is 'varchar(64)',
62 max_length is 64,
63 hints is _('Your password should be at least six characters'),
64 render_as 'password',
65 filters are 'Jifty::DBI::Filter::SaltHash';
66  
67  
68 };
69  
70 =head1 METHODS
71  
72 =head2 register_triggers
73  
74 Adds the triggers to the model this mixin is added to.
75  
76 =cut
77  
78 sub register_triggers {
79 my $self = shift;
80 $self->add_trigger(name => 'after_create', callback => \&after_create);
81 $self->add_trigger(name => 'after_set_password', callback => \&after_set_password);
82 $self->add_trigger(name => 'validate_password', callback => \&validate_password, abortable =>1);
83 }
84  
85  
86 =head2 password_is PASSWORD
87  
88 Checks if the user's password matches the provided I<PASSWORD>.
89  
90 =cut
91  
92 sub password_is {
93 my $self = shift;
94 my $pass = shift;
95  
96 return undef unless $self->__value('password');
97 my ($hash, $salt) = @{$self->__value('password')};
98  
99 return 1 if ( $hash eq Digest::MD5::md5_hex($pass . $salt) );
100 return undef;
101  
102 }
103  
104 =head2 hashed_password_is HASH TOKEN
105  
106 Check if the given I<HASH> is the result of hashing our (already
107 salted and hashed) password with I<TOKEN>.
108  
109 This can be used in cases where the pre-hashed password is sent during login as an additional security precaution (such as could be done via Javascript).
110  
111 =cut
112  
113 sub hashed_password_is {
114 my $self = shift;
115 my $hash = shift;
116 my $token = shift;
117  
118 my $password = $self->__value('password');
119 return $password && Digest::MD5::md5_hex("$token " . $password->[0]) eq $hash;
120 }
121  
122  
123 =head2 validate_password
124  
125 Makes sure that the password is six characters long or longer, unless
126 we have alternative means to authenticate.
127  
128 =cut
129  
130 sub validate_password {
131 my $self = shift;
132 my $new_value = shift;
133  
134 return 1 if $self->has_alternative_auth();
135  
136 return ( 0, _('Passwords need to be at least six characters long') )
137 if length($new_value) < 6;
138  
139 return 1;
140 }
141  
142 =head2 after_create
143  
144 This trigger is added to the account model. It automatically sends a notification email to the user for password confirmation.
145  
146 See L<Jifty::Plugin::Authentication::Password::Notification::ConfirmEmail>.
147  
148 =cut
149  
150  
151 sub after_create {
152 my $self = shift;
153 # We get a reference to the return value of insert
154 my $value = shift; return unless $$value; $self->load($$value);
155 $self->regenerate_auth_token;
156 if ( $self->id and $self->email and not $self->email_confirmed ) {
157 Jifty->app_class('Notification','ConfirmEmail')->new( to => $self )->send;
158 }
159 }
160  
161 =head2 has_alternative_auth
162  
163 If your model supports other means of authentication, you should have
164 this method return true, so the C<password> field can optionally be
165 null and authentication with password is disabled in that case.
166  
167 =cut
168  
169 sub has_alternative_auth { }
170  
171 =head2 after_set_password
172  
173 Regenerate authentication tokens on password change
174  
175 =cut
176  
177 sub after_set_password {
178 my $self = shift;
179 $self->regenerate_auth_token;
180 }
181  
182 =head2 regenerate_auth_token
183  
184 Generate a new auth_token for this user. This will invalidate any
185 existing feed URLs.
186  
187 =cut
188  
189 sub regenerate_auth_token {
190 my $self = shift;
191 my $auth_token = '';
192  
193 $auth_token .= unpack('H2', chr(int rand(256))) for (1..16);
194  
195 $self->__set(column => 'auth_token', value => $auth_token);
196 }
197  
198 =head1 SEE ALSO
199  
200 L<Jifty::Plugin::Authentication::Password>, L<Jifty::Plugin::User::Mixin::Model>
201  
202 =head1 LICENSE
203  
204 Jifty is Copyright 2005-2010 Best Practical Solutions, LLC.
205 Jifty is distributed under the same terms as Perl itself.
206  
207 =cut
208  
209 1;
210