blib/lib/Smolder/Control/Public/Auth.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 162 CC 10 SCAR 19.4
CC
10
Cov
0%
process_forgot_pw
20.0
do_login
12.0
login
6.0
CRAP 7.0
1 package Smolder::Control::Public::Auth;
2 use strict;
3 use base 'Smolder::Control';
4 use Smolder::AuthInfo;
5 use Smolder::DB::Developer;
6 use Smolder::DB;
7 use Smolder::Email;
8 use HTML::FillInForm;
9 use CGI::Cookie;
10  
11 our $HOME_PAGE_URL = '/app/developer';
12 our $COOKIE_NAME = 'smolder';
13  
14 =head1 NAME
15  
16 Smolder::Control::Public::Auth
17  
18 =head1 DESCRIPTION
19  
20 Controller module for all auth related activities.
21  
22 =cut
23  
24 sub setup {
25 my $self = shift;
26 $self->start_mode('login');
27 $self->run_modes(
28 [
29 qw(
30 login
31 process_login
32 logout
33 show_logout
34 forgot_pw
35 process_forgot_pw
36 timeout
37 forbidden
38 )
39 ]
40 );
41 }
42  
43 =head1 RUN MODES
44  
45 =head2 login
46  
47 Show the login form. Uses the F<Public/Auth/login.tmpl> template.
48  
49 =cut
50  
51 sub login {
52 my ($self, $tt_params) = @_;
53 if ($tt_params) {
54 return $self->tt_process($tt_params);
55 } else {
56 return HTML::FillInForm->new()->fill(
57 scalarref => $self->tt_process({}),
58 fobject => $self->query,
59 );
60 }
61 }
62  
63 =head2 process_login
64  
65 Process the incoming data from the C<login> mode. If validation passes
66 the appropriate C<mod_auth_tkt> token is set and the user is either redirected
67 back to where they were trying to when they were redirected to the C<login> mode,
68 or taken to the developer home page.
69  
70 =cut
71  
72 sub process_login {
73 my $self = shift;
74 my $form = {required => [qw(username password)]};
75 my $results = $self->check_rm('login', $form)
76 || return $self->check_rm_error_page();
77  
78 my ($user, $pw) = ($results->valid('username'), $results->valid('password'));
79  
80 if ($self->do_login($user, $pw)) {
81  
82 # url of where to go next
83 my $url = $self->query->param('back') || $HOME_PAGE_URL;
84 $self->header_type('redirect');
85 $self->header_add(-uri => $url);
86 return "Redirecting...";
87 } else {
88 return $self->login({validation_failed => 1});
89 }
90 }
91  
92 sub do_login {
93 my ($self, $user, $pw) = @_;
94  
95 # see if we have a user with this password
96 my ($dev) = Smolder::DB::Developer->search(username => $user, guest => 0);
97 if ($dev) {
98  
99 # check to see if the password matches the encrypted one
100 if (crypt($pw, $dev->password) eq $dev->password) {
101  
102 # figure out which tokens to add
103 my @groups = $dev->groups;
104  
105 # now add the auth cookie going out
106 my $tkt = Smolder::AuthInfo->new->ticket(
107 id => $dev->id,
108 groups => \@groups,
109 );
110 my $cookie = CGI::Cookie->new(
111 -name => $COOKIE_NAME,
112 -value => $tkt,
113 -expires => '+96h',
114 );
115 $self->header_add(cookie => [$cookie]);
116 $ENV{REMOTE_USER} = $dev->id;
117 return $dev;
118 }
119 }
120 return;
121 }
122  
123 =head2 forgot_pw
124  
125 Show the Forgot-my-password form. Uses the F<Public/Auth/forgot_pw.tmpl> template.
126  
127 =cut
128  
129 sub forgot_pw {
130 my ($self, $tt_params) = @_;
131 $tt_params ||= {};
132 return $self->tt_process($tt_params);
133 }
134  
135 =head2 process_forgot_pw
136  
137 Process the incoming data from the C<forgot_pw> mode. If it passes validation
138 then reset the developer's password to a new random string and then send an
139 email with the new password (using the F<Email/forgot_pw.tmpl> template) to
140 that developer. If successful, then return to the C<forgot_pw> mode.
141  
142 =cut
143  
144 sub process_forgot_pw {
145 my $self = shift;
146 my ($dev) = Smolder::DB::Developer->search(username => $self->query->param('username'));
147 if ($dev) {
148 my $email = $dev->email;
149 my $username = $dev->username;
150 my $new_pw = $dev->reset_password();
151  
152 if ($self->log->would_log('debug')) {
153 $self->log->debug("New password for developer $username is '$new_pw'");
154 }
155  
156 # send an email with the new password
157 my $error = Smolder::Email->send_mime_mail(
158 name => 'forgot_pw',
159 to => $email,
160 subject => 'Forgot your Smolder password',
161 tt_params => {
162 developer => $dev,
163 new_pw => $new_pw,
164 },
165 );
166  
167 if ($error) {
168 $self->add_message(
169 msg => "Problems sending new password email! Please check the logs.",
170 type => 'warning'
171 );
172 } else {
173 $self->add_message(
174 msg => "An email with a new password has been successfully sent to $email.",
175 type => 'info'
176 );
177 }
178 } else {
179 $self->add_message(
180 msg => "A user with that username does not exist in this installation of smolder!",
181 type => 'warning',
182 );
183 }
184 return $self->forgot_pw();
185 }
186  
187 =head2 logout
188  
189 Logout the user by unsetting the C<mod_auth_tkt> cookie and then redirecting
190 them (so that the cookie is sent to the browser) to the C<show_logout> mode.
191  
192 =cut
193  
194 sub logout {
195 my ($self, $tt_params) = @_;
196 $tt_params ||= {};
197  
198 # remove their auth cookie
199 my $at = Smolder::AuthInfo->new();
200  
201 # now add the AuthTKT cookie going out
202 my $cookie = CGI::Cookie->new(
203 -name => $COOKIE_NAME,
204 -value => '',
205 -expires => '-1d',
206 );
207 $self->header_add(-cookie => [$cookie]);
208 $self->header_type('redirect');
209 $self->header_add(-uri => '/app/public_auth/show_logout');
210 return "Redirecting to '/app/public_auth/show_logout'.";
211 }
212  
213 =head2 show_logout
214  
215 Show the logout screen after a successful logout. Uses the
216 F<Public/Auth/show_logout.tmpl> template.
217  
218 =cut
219  
220 sub show_logout {
221 my ($self, $tt_params) = @_;
222 $tt_params ||= {};
223 return $self->tt_process($tt_params);
224 }
225  
226 =head2 timeout
227  
228 Show the timeout screen if C<mod_auth_tkt> determines
229 that their ticket is valid but expired.
230 Uses the F<Public/Auth/timeout.tmpl> template.
231  
232 =cut
233  
234 sub timeout {
235 my ($self, $tt_params) = @_;
236 if ($tt_params) {
237 return $self->tt_process($tt_params);
238 } else {
239 return HTML::FillInForm->new()->fill(
240 scalarref => $self->tt_process({}),
241 fobject => $self->query,
242 );
243 }
244 }
245  
246 =head2 forbidden
247  
248 Show the forbidden screen if C<mod_auth_tkt> determines
249 that their ticket is either missing or invalid.
250 Uses the F<Public/Auth/forbidden.tmpl> template.
251  
252 =cut
253  
254 sub forbidden {
255 my ($self, $tt_params) = @_;
256 if ($tt_params) {
257 return $self->tt_process($tt_params);
258 } else {
259 return HTML::FillInForm->new()->fill(
260 scalarref => $self->tt_process({}),
261 fobject => $self->query,
262 );
263 }
264 }
265  
266 1;