lib/Apache/AppSamurai/AuthSimple.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 110 CC 5 SCAR 22.8
CC
5
Cov
0%
Initialize
30.0
Configure
2.0
Authenticator
2.0
CRAP 9.8
1 # Apache::AppSamurai::AuthSimple - AppSamurai "Simple" authentication framework
2 # plugin
3  
4 # $Id: AuthSimple.pm,v 1.2 2008/05/01 22:36:10 pauldoom Exp $
5  
6 ##
7 # Copyright (c) 2008 Paul M. Hirsch (paul@voltagenoir.org).
8 # All rights reserved.
9 #
10 # This program is free software; you can redistribute it and/or modify it under
11 # the same terms as Perl itself.
12 ##
13  
14 package Apache::AppSamurai::AuthSimple;
15 use strict;
16 use warnings;
17  
18 use vars qw($VERSION @ISA);
19 $VERSION = substr(q$Revision: 1.2 $, 10, -1);
20  
21 use Carp;
22 use Apache::AppSamurai::AuthBase;
23 use Authen::Simple;
24  
25 @ISA = qw( Apache::AppSamurai::AuthBase );
26  
27 sub Configure {
28 my $self = shift;
29  
30 # Pull defaults from AuthBase and save.
31 $self->SUPER::Configure();
32 my $conft = $self->{conf};
33
34 # Initial configuration. Put defaults here before the @_ args are
35 # pulled in.
36 $self->{conf} = { %{$conft},
37 # Send log messages to Apache::AppSamurai::AuthSimple
38 # itself (see debug, error, info, and warn methods below)
39 log => $self,
40 @_,
41 };
42  
43 return 1;
44 }
45  
46 sub Initialize {
47 my $self = shift;
48  
49 # A submodule MUST be selected at this point
50 unless ($self->{conf}{SubModule}) {
51 $self->AddError("Please specify a submodule for Authen::Simple to use!");
52 return 0;
53 }
54
55 my $fullsub = 'Authen::Simple::' . $self->{conf}{SubModule};
56  
57 # Attempt to load the submodule
58 unless (eval "require $fullsub") {
59 $self->AddError("Unable to load Authen::Simple submodule, $fullsub: $@");
60 return 0;
61 }
62  
63 # Create a limited config hash with only the keys supported by the
64 # adaptor submodule
65 my %simpleconf;
66
67 if ($fullsub->can('options')) {
68 # Pull in only supported values
69 foreach (keys %{$fullsub->options}) {
70 exists $self->{conf}{$_} and $simpleconf{$_} = $self->{conf}{$_};
71 }
72 } else {
73 # Params::Validate appears not to be used for this adaptor, so
74 # just copy in the full conf
75 %simpleconf = %{$self->{conf}};
76 }
77  
78 # Prepare an eval to create our Authen::Simple instance with our
79 # chosen auth adaptor
80 my $asconf = '$self->{authen} = Authen::Simple->new(' .
81 $fullsub . '->new(%simpleconf))';
82  
83 unless (eval $asconf && $self->{authen}) {
84 $self->AddError("Initialization of Authen::Simple failed: $@");
85 return 0;
86 }
87  
88 $self->{init} = 1;
89 return 1;
90 }
91  
92  
93 # Authenticate using Authen::Simple (which in turn calls the configured
94 # adaptor submodule)
95 sub Authenticator {
96 my $self = shift;
97 my $user = shift;
98 my $pass = shift;
99  
100 # Multiple layers of abstraction rock! (We are passing authentication
101 # information to Authen::Simple which is passing it to its adaptor
102 # submodule...)
103 ($self->{authen}->authenticate($user, $pass)) && (return 1);
104  
105 $self->AddError('warn', "Authentication failure with $self->{conf}{SubModule} for user: \"$user\"");
106  
107 # DEFAULT DENY #
108 return 0;
109 }
110  
111  
112 # Logging methods for the Authen:Simple submodules to callback to
113 sub debug {
114 shift->_simplelog('debug', @_);
115 }
116  
117 sub error {
118 shift->_simplelog('error', @_);
119 }
120  
121 sub info {
122 shift->_simplelog('info', @_);
123 }
124  
125 sub warn {
126 shift->_simplelog('warn', @_);
127 }
128  
129 sub _simplelog {
130 my $self = shift;
131 my $severity = shift;
132  
133 # For now, squeezing multiple log lines into one seems more reasonable
134 # for auth submodules. This can be changed if submodules are shown
135 # to send a lot of multiline log messages
136 my $msg = join(", ", @_);
137  
138 $msg =~ s/\n/ /gs;
139  
140 $self->AddError($severity, $msg);
141 }
142  
143 1; # End of Apache::AppSamurai::AuthSimple
144  
145 __END__