lib/Apache/AppSamurai/AuthBase.pm

filter: Statementstmt 15.3% 14 / 91 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 22 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 30.7% 4 / 13 Podpod 100% 9 / 9 totaltotal 14.3% 27 / 188 CC 29 SCAR 38.0
CC
29
Cov
9%
Authenticate
90.0
CheckInputUser
72.0
CheckInputPass
42.0
CRAP 44.6
« lib/Apache/AppSamurai/AuthBasic.pm ↑ Summary lib/Apache/AppSamurai/Util.pm »
1 # Apache::AppSamurai::AuthBase - AppSamurai authentication plugin base
2 # module.
3  
4 # $Id: AuthBase.pm,v 1.15 2008/04/30 21:40:05 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 # This is a base authentication wrapper for Apache::AppSamurai.
15 # AppSamurai can use one or more authentication methods to authenticate
16 # users. Each method, besides the special AuthServer method, requires
17 # a module name Apache::AppSamurai::<AUTHNAME>. All AppSamurai::AuthXXX
18 # modules should be derived from this base module.
19  
20 package Apache::AppSamurai::AuthBase;
21 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 $VERSION = substr(q$Revision: 1.15 $, 10, -1);
26  
27 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
30 0 my $this = shift;
31 0 ▶ my $class = (ref($this) || ($this));
Condition0%0/3
ref $this || $this
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table0%0/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
32 0 my $me = {};
33  
34 0 bless($me, $class);
35  
36 # Call Configure to fill out the $me->{conf} hash
37 0 $me->Configure(@_);
38 0 $me->{init} = 0;
39 0 $me->{errors} = [];
40  
41 # If the username and password were passed with the new request,
42 # process and return immediately.
43 0 ▶ if (defined($me->{conf}{user}) && defined($me->{conf}{pass})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $me->{"conf"}{"user"} and defined $me->{"conf"}{"pass"}) 0 0 0
Condition0%0/3
defined $me->{"conf"}{"user"} and defined $me->{"conf"}{"pass"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $me->{"conf"}{"user"} and defined $me->{"conf"}{"pass"}
defined $me->{"conf"}{"user"} defined $me->{"conf"}{"pass"}
Truth table0%0/3
A and B
A= defined $me->{"conf"}{"user"}B= defined $me->{"conf"}{"pass"}
ABresult
0X0
100
111
44 0 return $me->Authenticate($me->{conf}{user}, $me->{conf}{pass});
45 }
46 0 return $me;
47 }
48  
49 #### OVERRIDE Configure(), Initialize(), and Authenticator() FOR NEW ####
50 #### AuthXXX MODULES ####
51  
52 # Configure the conf hash. If you want to override theses defaults, or
53 # add new ones, create your own Configure() in your AuthXXX module
54 0 ▶sub Configure {
Subroutine Configure: not called CC=1 SCAR=6.9
Pod documented
55 0 my $self = shift;
56 # Initial configuration. Put defaults here before the @_ args are
57 # pulled in.
58 0 $self->{conf} = { UserMin => 3,
59 UserMax => 256,
60 UserChars => '\w\d_\-\.',
61 UserStripWhite => 1,
62 UserUc => 0,
63 UserLc => 0,
64 PassMin => 4,
65 PassMax => 16384,
66 PassChars => '\w\d !\@\#\$\%\^\&\*:\,\.\?\-_=\+',
67 PassStripWhite => 0,
68 DefaultLogLevel => 'error',
69 @_,
70 };
71  
72 0 return 1;
73 }
74  
75  
76 # All setup should go here, including configuring authentication modules.
77 # This will be called by Authenticate() 99% of the time, but for some auth
78 # types, may be useful to be called in new() as well.
79 0 ▶sub Initialize {
Subroutine Initialize: not called CC=4 SCAR=30.0
Pod documented
80 0 my $self = shift;
81 # May only be initialized once
82 0 ▶ ($self->{init} == 1) && (return 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->{"init"} == 1 and return 1 0 0 0
83
84 # Make sure we have config (in case someone tries to call us directly)
85 0 ▶ (defined($self->{conf}) && (scalar keys %{$self->{conf}})) || (croak "Could not initialize! Module not configured!");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{"conf"} and scalar keys %{$$self{"conf"};} or croak("Could not initialize! Module not configured!") 0 0 0
Condition0%0/2
defined $self->{"conf"} and scalar keys %{$$self{"conf"};}
!ll
00
MC/DC0%0/1
defined $self->{"conf"} and scalar keys %{$$self{"conf"};}
defined $self->{"conf"}
Truth table0%0/2
A
A= defined $self->{"conf"}
Aresult
00
11
86  
87 # Make sure to complain if something goes wrong
88 0 (1 == 1) || ($self->AddError("Failed to initialize in " . __PACKAGE__ . "::Initialize") && return 0);
89  
90 # Set this after successful initialization
91 0 $self->{init} = 1;
92 0 return 1;
93 }
94  
95  
96 # Perform the authentication. Returns a "yes" value (1, true) on success
97 # and "no" value (0, false, or undef) on failure. The function takes
98 # three arguments. 1) The object itself 2) The username 3) The password
99 # The username and password at this point have been through ALL checks in
100 # the module. (Valid chars, valid length, etc,)
101 0 ▶sub Authenticator {
Subroutine Authenticator: not called CC=1 SCAR=6.9
Pod documented
102 0 my $self = shift;
103 0 my $user = shift;
104 0 my $pass = shift;
105  
106 # Enter stuff here
107  
108 # DEFAULT DENY #
109 0 return 0;
110 }
111  
112 ###########################################################################
113  
114  
115 # The bulk of the work happens here, calling Initialize(), CheckInput(), and
116 # Authenticator(), and returning a true (1)/ false (0) status.
117 0 ▶sub Authenticate {
Subroutine Authenticate: not called CC=9 SCAR=45.0
Pod documented
118 0 my $self = shift;
119 0 ▶ (scalar(@_) == 2) || (croak 'Usage: $a->Authenticate($user, $pass);');
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 2 or croak("Usage: \$a->Authenticate(\$user, \$pass);") 0 0 0
120 0 my ($user, $pass) = @_;
121
122 ## DEFAULT DENY ##
123 0 my $authenticated = 0;
124  
125 # Check for clean input.
126 0 ▶ ($user = $self->CheckInputUser($user)) || ($self->AddError('warn', 'Invalid username') && return 0);
Branch: 1 branch
BranchTrueFalseTotal
$user = $self->CheckInputUser($user) or $self->AddError("warn", "Invalid username") and return 0 0 0 0
Condition0%0/5
($user = $self->CheckInputUser($user)) || $self->AddError('warn', 'Invalid username') && (return 0)
l!l&&r!l&&!r
000
$self->AddError("warn", "Invalid username") and return 0
!ll
00
MC/DC0%0/2
($user = $self->CheckInputUser($user)) || $self->AddError('warn', 'Invalid username') && (return 0)
($user = $self->CheckInputUser($user)) $self->AddError("warn", "Invalid username")
Truth table0%0/3
A || B
A= ($user = $self->CheckInputUser($user))B= $self->AddError("warn", "Invalid username")
ABresult
1X1
011
000
127 0 ▶ ($pass = $self->CheckInputPass($pass)) || ($self->AddError('warn', 'Invalid password') && return 0);
Branch: 1 branch
BranchTrueFalseTotal
$pass = $self->CheckInputPass($pass) or $self->AddError("warn", "Invalid password") and return 0 0 0 0
Condition0%0/5
($pass = $self->CheckInputPass($pass)) || $self->AddError('warn', 'Invalid password') && (return 0)
l!l&&r!l&&!r
000
$self->AddError("warn", "Invalid password") and return 0
!ll
00
MC/DC0%0/2
($pass = $self->CheckInputPass($pass)) || $self->AddError('warn', 'Invalid password') && (return 0)
($pass = $self->CheckInputPass($pass)) $self->AddError("warn", "Invalid password")
Truth table0%0/3
A || B
A= ($pass = $self->CheckInputPass($pass))B= $self->AddError("warn", "Invalid password")
ABresult
1X1
011
000
128  
129 # Initialize if not yet done
130 0 ▶ if (!$self->{init}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"init"}) 0 0 0
131 0 ▶ ($self->Initialize()) || (return 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->Initialize or return 0 0 0 0
132 }
133  
134 ## This is where the Authentication happens. Create your own overridden
135 # Authenticator functions today!
136 0 ▶ ($self->Authenticator($user, $pass)) && ($authenticated = 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->Authenticator($user, $pass) and $authenticated = 1 0 0 0
137  
138 0 return $authenticated;
139 }
140  
141  
142 # Filter or reject the username. Return the username (with
143 # modifications, if needed) on success, or nothing on failure.
144 # CUSTOMIZE THIS TO ONLY ALLOW VALID USERNAMES FOR YOUR AUTH MODULE!
145 # BE CAREFUL IF YOU RETURN AN ALTERED USERNAME! Most bases should be covered
146 # by the various UserXXX config options, but add more as needed here.
147 0 ▶sub CheckInputUser {
Subroutine CheckInputUser: not called CC=8 SCAR=42.8
Pod documented
148 0 my $self = shift;
149 0 ▶ my $user = (shift || return undef);
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
150  
151 # Strip surrounding whitespace, if so configured
152 0 ▶ if ($self->{conf}{UserStripWhite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"UserStripWhite"}) 0 0 0
153 0 $user =~ s/^\s*(.+?)\s*$/$1/;
154 }
155 0 my $ulen = length($user);
156
157 # Check username against the list of valid username characters
158 0 ▶ unless ($user =~ /^([$self->{conf}{UserChars}]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user =~ /^([$self->{"conf"}{"UserChars"}]+)$/) 0 0 0
159 0 $self->AddError('warn', 'Username contains invalid characters');
160 0 return undef;
161 }
162  
163 # Check for a valid username length.
164 0 ▶ if ($ulen < $self->{conf}{UserMin}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ulen < $self->{'conf'}{'UserMin'}) { } 0 0 0
elsif ($ulen > $self->{'conf'}{'UserMax'}) { } 0 0 0
165 0 $self->AddError('warn', "Username too small ($ulen)");
166 0 return undef;
167 } elsif ($ulen > $self->{conf}{UserMax}) {
168 0 $self->AddError('warn', "Username too large ($ulen)");
169 0 return undef;
170 }
171  
172 # uc() or lc() if so configured.
173 0 ▶ if ($self->{conf}{UserUc}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'conf'}{'UserUc'}) { } 0 0 0
elsif ($self->{'conf'}{'UserLc'}) { } 0 0 0
174 0 $user = uc($user);
175 } elsif ($self->{conf}{UserLc}) {
176 0 $user = lc($user);
177 }
178  
179 0 return $user;
180 }
181  
182  
183 # Filter or reject the password. Return the password (with
184 # modifications, if needed) on success, or nothing on failure.
185 # CUSTOMIZE THIS TO ONLY ALLOW VALID PASSWORDS FOR YOUR AUTH MODULE!
186 # BE CAREFUL IF YOU RETURN AN ALTERED PASSWORD! In almost all cases,
187 # you should fail out instead of trying to help a user. No lc($pass)
188 # unless your backend authentication checker really is case insensitive.
189 0 ▶sub CheckInputPass {
Subroutine CheckInputPass: not called CC=6 SCAR=37.4
Pod documented
190 0 my $self = shift;
191 0 ▶ my $pass = (shift || return undef);
Condition0%0/2
shift() || (return undef)
l!l
00
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
192  
193 # Strip surrounding whitespace, if so configured
194 0 ▶ if ($self->{conf}{PassStripWhite}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"PassStripWhite"}) 0 0 0
195 0 $pass =~ s/^\s*(.+?)\s*$/$1/;
196 }
197 0 my $plen = length($pass);
198  
199 # Check password against the list of valid password characters
200 0 ▶ unless ($pass =~ /^([$self->{conf}{PassChars}]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pass =~ /^([$self->{"conf"}{"PassChars"}]+)$/) 0 0 0
201 0 $self->AddError('warn', 'Password contains invalid characters');
202 0 return undef;
203 }
204  
205 # Check for a valid password length.
206 0 ▶ if ($plen < $self->{conf}{PassMin}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($plen < $self->{'conf'}{'PassMin'}) { } 0 0 0
elsif ($plen > $self->{'conf'}{'PassMax'}) { } 0 0 0
207 0 $self->AddError('warn', "Password too small ($plen)");
208 0 return undef;
209 } elsif ($plen > $self->{conf}{PassMax}) {
210 0 $self->AddError('warn', "Password too large ($plen)");
211 0 return undef;
212 }
213  
214 0 return $pass;
215 }
216  
217  
218 # Add error to the list
219 0 ▶sub AddError {
Subroutine AddError: not called CC=2 SCAR=17.9
Pod documented
220 0 my $self = shift;
221 0 ▶ if (scalar(@_) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) { } 0 0 0
222 0 push(@{$self->{errors}}, [$_[0], ref($self) . ": " . $_[1]]);
223 } else {
224 0 push(@{$self->{errors}}, [$self->{conf}{DefaultLogLevel}, ref($self) . ": " . $_[0]]);
225 }
226 0 return 1;
227 }
228  
229 # Return an array of errors if there are any, or undef if there are not.
230 0 ▶sub Errors {
Subroutine Errors: not called CC=2 SCAR=17.9
Pod documented
231 0 my $self = shift;
232 0 ▶ if (scalar(@{$self->{errors}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"errors"};}) 0 0 0
233 0 return $self->{errors};
234 }
235
236 0 return undef;
237 }
238  
239 1 1; # End of Apache::AppSamurai::AuthBase
240  
241 __END__
« lib/Apache/AppSamurai/AuthBasic.pm ↑ Summary lib/Apache/AppSamurai/Util.pm »