Wombat/Authenticator/AuthenticatorBase.pm untested

filter: Statementstmt 0.0% 0 / 146 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 20 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 235 CC 25 SCAR 36.1
CC
25
Cov
0%
invoke
110.0
checkAccess
42.0
checkUserData
30.0
CRAP 36.9
1 # -*- Mode: Perl; indent-tabs-mode: nil; -*-
2  
3 package Wombat::Authenticator::AuthenticatorBase;
4  
5 =pod
6  
7 =head1 NAME
8  
9 Wombat::Authenticator::AuthenticatorBase - internal authenticator base
10 class
11  
12 =head1 SYNOPSIS
13  
14 =head1 DESCRIPTION
15  
16 Convenience base implementation of B<Wombat::Valve> that enforces the
17 I<security-constraint> elements in the web application deployment
18 descriptor. This functionality is implemented as a Valve so that it
19 can be omitted in environments that do not require these
20 features. Individual implementations of each supported authentication
21 method can subclass this base class as required. Subclasses B<MUST>
22 implement C<authenticate()> and C<getName()>.
23  
24 When this class is utilized, the Application to which it is attached
25 must have an associated Realm that can be used for authenticating
26 users and enumerating the roles to which they have been assigned.
27  
28 This Valve is only useful when processing HTTP requests. Requests of
29 any other type will simply be passed through.
30  
31 =cut
32  
33 use base qw(Wombat::Authenticator Wombat::Valve::ValveBase);
34 use fields qw();
35 use strict;
36 use warnings;
37  
38 use Servlet::Http::HttpServletResponse ();
39 use Servlet::Util::Exception ();
40 use URI::Escape ();
41 use Wombat::Globals ();
42  
43 use constant TRANSPORT_NONE => 'NONE';
44  
45 =pod
46  
47 =head1 ACCESSOR METHODS
48  
49 =over
50  
51 =item setContainer($container)
52  
53 Set the Application to which this Authenticator is attached.
54  
55 B<Parameters:>
56  
57 =over
58  
59 =item $container
60  
61 the B<Wombat::Core::Application> to which this Authenticator is attached
62  
63 =back
64  
65 B<Throws:>
66  
67 =over
68  
69 =item B<Servlet::Util::IllegalArgumentException>
70  
71 if the Container is not B<Wombat::Core::Application>
72  
73 =back
74  
75 =cut
76  
77 sub setContainer {
78 my $self = shift;
79 my $container = shift;
80  
81 unless ($container->isa('Wombat::Core::Application')) {
82 my $msg = "setContainer: container not Application";
83 Servlet::Util::IllegalArgumentException->throw($msg);
84 }
85  
86 $self->SUPER::setContainer($container);
87  
88 return 1;
89 }
90  
91 =pod
92  
93 =back
94  
95 =head1 PUBLIC METHODS
96  
97 =over
98  
99 =item invoke ($request, $response, $context)
100  
101 Perform request processing as required by this Valve.
102  
103 B<Parameters>
104  
105 =over
106  
107 =item $request
108  
109 the B<Wombat::Request> to be processed
110  
111 =item $response
112  
113 the B<Wombat::Response> to be created
114  
115 =item $context
116  
117 the B<Wombat::ValveContext> allowing access to the next Valve in the
118 Pipeline being processed
119  
120 =back
121  
122 B<Throws:>
123  
124 =over
125  
126 =item B<Servlet::ServletException>
127  
128 if a servlet error occurs or is thrown by a subsequently invoked
129 Valve, Filter or Servlet
130  
131 =item B<Servlet::Util::IOException>
132  
133 if an input or output error occurs
134  
135 =back
136  
137 =cut
138  
139 sub invoke {
140 my $self = shift;
141 my $request = shift;
142 my $response = shift;
143 my $context = shift;
144  
145 # do nothing if the protocol is not HTTP
146 unless ($request->isa('Wombat::HttpRequest') &&
147 $response->isa('Wombat::HttpResponse')) {
148 $context->invokeNext($request, $response);
149 return 1;
150 }
151  
152 my $freq = $request->getRequest();
153  
154 # Wombat::Globals::DEBUG &&
155 # $self->log(sprintf("Security checking request %s %s",
156 # $freq->getMethod(),
157 # $freq->getRequestURI()),
158 # undef, 'DEBUG');
159  
160 my $config = $self->getContainer()->getLoginConfig();
161  
162 my $principal = $freq->getUserPrincipal();
163 unless ($principal) {
164 # check to see if the auth type and principal are cached in
165 # the session
166 my $session = $self->getSession($request);
167 if ($session) {
168 $principal = $session->getPrincipal();
169 if ($principal) {
170 $request->setAuthType($session->getAuthType());
171 $request->setUserPrincipal($principal);
172 }
173 }
174 }
175  
176 # is this request URI subject to a security constraint?
177 my $constraint = $self->findConstraint($request);
178 unless ($constraint) {
179 #$ Wombat::Globals::DEBUG &&
180 # $self->log("No security constraint found", undef, 'DEBUG');
181 $context->invokeNext($request, $response);
182 return 1;
183 }
184  
185 # enforce user data constraint for the security constraint
186 unless ($self->checkUserData($request, $response, $constraint)) {
187 # Wombat::Globals::DEBUG &&
188 # $self->log("Failed to satisfy user data constraint",
189 # undef, 'DEBUG');
190 return 1;
191 }
192  
193 my $authConstraint = $constraint->getAuthConstraint();
194 if ($authConstraint) {
195 # authenticate based upon the specified login configuration
196 unless ($self->authenticate($request, $response, $config)) {
197 # Wombat::Globals::DEBUG &&
198 # $self->log("Failed to authenticate", undef, 'DEBUG');
199 return 1;
200 }
201  
202 # check access based on the specified role
203 unless ($self->checkAccess($request, $response, $constraint)) {
204 # Wombat::Globals::DEBUG &&
205 # $self->log("Failed access check", undef, 'DEBUG');
206 return 1;
207 }
208 }
209  
210 $context->invokeNext($request, $response);
211  
212 return 1;
213 }
214  
215 =pod
216  
217 =back
218  
219 =head1 PACKAGE METHODS
220  
221 =over
222  
223 =item authenticate($request, $response, $config)
224  
225 Authenticate the user making this request, based on the specified
226 login configuration. Return true if any specified constraint has been
227 satisfied, or false if we have created a response already.
228  
229 B<Parameters:>
230  
231 =over
232  
233 =item $request
234  
235 the B<Wombat::HttpRequest> being processed
236  
237 =item $response
238  
239 the B<Wombat::HttpResponse> being created
240  
241 =item $constraint
242  
243 the B<Wombat::Deploy::LoginConfig> describing the authentication
244 procedure
245  
246 =back
247  
248 B<Throws:>
249  
250 =over
251  
252 =item B<Servlet::Util::IOException>
253  
254 if an input or output error occurs
255  
256 =back
257  
258 =cut
259  
260 sub authenticate {}
261  
262 =pod
263  
264 =item checkAccess($request, $response, $constraint)
265  
266 Perform access control based on the specified authorization
267 constraint. Return true if this constraint was not violated and
268 processing should continue, of false if we have created a response
269 already.
270  
271 B<Parameters:>
272  
273 =over
274  
275 =item $request
276  
277 the B<Wombat::HttpRequest> being processed
278  
279 =item $response
280  
281 the B<Wombat::HttpResponse> being created
282  
283 =item $constraint
284  
285 the B<Wombat::Deploy::SecurityConstraint> being checked
286  
287 =back
288  
289 B<Throws:>
290  
291 =over
292  
293 =item B<Servlet::Util::IOException>
294  
295 if an input or output error occurs
296  
297 =back
298  
299 =cut
300  
301 sub checkAccess {
302 my $self = shift;
303 my $request = shift;
304 my $response = shift;
305 my $constraint = shift;
306  
307 return 1 unless $constraint;
308  
309 my $freq = $request->getRequest();
310 my $fres = $request->getResponse();
311  
312 my $principal = $freq->getUserPrincipal();
313 unless ($principal) {
314 # Wombat::Globals::DEBUG &&
315 # $self->log("checkAccess: no principal", undef, 'DEBUG');
316 my $code =
317 Servlet::Http::HttpServletResponse::SC_INTERNAL_SERVER_ERROR;
318 $fres->sendError($code);
319 return undef;
320 }
321  
322 my $realm = $self->getContainer()->getRealm();
323 my $roles = $constraint->getAuthRoles();
324 unless (@$roles) {
325 if ($constraint->getAuthConstraint() &&
326 !$constraint->getAllRoles()) {
327 # there is an auth constraint but no specified roles;
328 # means all access is forbidden
329 my $code = Servlet::Http::HttpServletResponse::SC_FORBIDDEN;
330 $fres->sendError($code);
331 return undef;
332 }
333 # unneccessary to check roles
334 return 1;
335 }
336  
337 for my $role (@$roles) {
338 return 1 if $realm->hasRole($principal, $role);
339 }
340  
341 # principal is not associated with any role in the realm
342 my $code = Servlet::Http::HttpServletResponse::SC_FORBIDDEN;
343 $fres->sendError($code);
344 return undef;
345 }
346  
347 =pod
348  
349 =item checkUserData($request, $response, $constraint)
350  
351 Enforce any user data constraint required by the security constraint
352 guarding this request URI. Return true if this constraint was not
353 violated and processing should continue, of false if we have created a
354 response already.
355  
356 B<Parameters:>
357  
358 =over
359  
360 =item $request
361  
362 the B<Wombat::HttpRequest> being processed
363  
364 =item $response
365  
366 the B<Wombat::HttpResponse> being created
367  
368 =item $constraint
369  
370 the B<Wombat::Deploy::SecurityConstraint> being checked
371  
372 =back
373  
374 B<Throws:>
375  
376 =over
377  
378 =item B<Servlet::Util::IOException>
379  
380 if an input or output error occurs
381  
382 =back
383  
384 =cut
385  
386 sub checkUserData {
387 my $self = shift;
388 my $request = shift;
389 my $response = shift;
390 my $constraint = shift;
391  
392 return 1 unless $constraint;
393  
394 my $userConstraint = $constraint->getUserConstraint();
395 return 1 unless $userConstraint;
396  
397 # no transport security required
398 return 1 if $userConstraint eq TRANSPORT_NONE;
399  
400 my $freq = $request->getRequest();
401 my $fres = $response->getResponse();
402  
403 # transport security is confirmed
404 return 1 if $freq->isSecure();
405  
406 # XXX: support "redirect port" configuration on the connector that
407 # allows us to redirect to an SSL port?
408  
409 $fres->sendError(Servlet::Http::HttpServletResponse::SC_FORBIDDEN,
410 $freq->getRequestURI());
411 return undef;
412 }
413  
414 =pod
415  
416 =item findConstraint($request)
417  
418 Return the B<Wombat::Deploy::SecurityConstraint> configured to guard
419 the request URI for this request, or C<undef> if there is no
420 constraint.
421  
422 B<Parameters:>
423  
424 =over
425  
426 =item $request
427  
428 the B<Wombat::HttpRequest> being processed
429  
430 =back
431  
432 =cut
433  
434 sub findConstraint {
435 my $self = shift;
436 my $request = shift;
437  
438 my $freq = $request->getRequest();
439 my $method = $freq->getMethod();
440 my $uri = $freq->getRequestURI();
441  
442 my $contextPath = $freq->getContextPath();
443 $uri =~ s|^$contextPath|| if $contextPath;
444 $uri = URI::Escape::uri_unescape($uri);
445  
446 for my $constraint ($self->getContainer()->getConstraints()) {
447 return $constraint if $constraint->included($uri, $method);
448 }
449  
450 return undef;
451 }
452  
453 =pod
454  
455 =item getName()
456  
457 Return a short name for this Authenticator implementation. Must be
458 overridden by subclasses.
459  
460 =cut
461  
462 sub getName {}
463  
464 =pod
465  
466 =item register($request, $response, $principal, $authType)
467  
468 Register an authenticated Principal and authentication tyhpe in the
469 request and in the current session (if there is one).
470  
471 B<Parameters:>
472  
473 =over
474  
475 =item $request
476  
477 the B<Wombat::HttpRequest> being processed
478  
479 =item $response
480  
481 the B<Wombat::HttpResponse> being created
482  
483 =item $principal
484  
485 the authenticated B<Servlet::Util::Principal> to be registered
486  
487 =item $uathType
488  
489 the authentication type to be registered
490  
491 =back
492  
493 =cut
494  
495 sub register {
496 my $self = shift;
497 my $request = shift;
498 my $response = shift;
499 my $principal = shift;
500 my $authType = shift;
501  
502 $request->setAuthType($authType);
503 $request->setUserPrincipal($principal);
504  
505 my $session = $self->getSession($request);
506 if ($session) {
507 $session->setAuthType($authType);
508 $session->setPrincipal($principal);
509 }
510  
511 return 1;
512 }
513  
514 =pod
515  
516 =back
517  
518 =cut
519  
520 # lifecycle methods.. no need to document
521  
522 sub start {
523 my $self = shift;
524  
525 $self->SUPER::start();
526 $self->log(sprintf("%s started", $self->getName()), undef, 'INFO');
527  
528 return 1;
529 }
530  
531 sub stop {
532 my $self = shift;
533  
534 $self->SUPER::stop();
535 $self->log(sprintf("%s stopped", $self->getName()), undef, 'DEBUG');
536  
537 return 1;
538 }
539  
540 # private methods
541  
542 sub getSession {
543 my $self = shift;
544 my $request = shift;
545  
546 my $freq = $request->getRequest();
547 my $fses = $freq->getSession(1);
548 return undef unless $fses;
549  
550 my $manager = $self->getContainer()->getSessionManager();
551 return undef unless $manager;
552  
553 my $session;
554 eval {
555 $session = $manager->getSession($fses->getId());
556 };
557  
558 return $session;
559 }
560  
561 1;
562 __END__