Sling/Authn.pm

filter: Statementstmt 63.7% 74 / 116 Branchbran 27.7% 10 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 93.7% 15 / 16 Podpod 83.3% 5 / 6 totaltotal 56.9% 107 / 188 CC 22 SCAR 35.3
CC
22
Cov
54%
switch_user
84.3
login_user
34.2
basic_login
6.0
CRAP 34.2
« Sling/User.pm ↑ Summary Sling/GroupMember.pm »
1 #!/usr/bin/perl -w
2  
3 package Apache::Sling::Authn;
4  
5 11 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use LWP::UserAgent ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Apache::Sling::AuthnUtil;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Apache::Sling::Print;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Apache::Sling::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use Apache::Sling::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 11 require Exporter;
16  
17 11 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 our @EXPORT_OK = ();
20  
21 11 our $VERSION = '0.27';
22  
23 #{{{sub new
24 14 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
25 14 my ( $class, $sling ) = @_;
26 14 my $url = Apache::Sling::URL::url_input_sanitize( ${$sling}->{'URL'} );
27 my $verbose =
28 14 ▶ ( defined ${$sling}->{'Verbose'} ? ${$sling}->{'Verbose'} : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined ${$sling;}->{'Verbose'} ? : 2 12 14
29  
30 14 my $lwp_user_agent = $class->user_agent( ${$sling}->{'Referer'} );
31  
32 14 my $response;
33 my $authn = {
34 BaseURL => "$url",
35 LWP => $lwp_user_agent,
36 14 Type => ${$sling}->{'Auth'},
37 14 Username => ${$sling}->{'User'},
38 14 Password => ${$sling}->{'Pass'},
39 Message => q{},
40 Response => \$response,
41 Verbose => $verbose,
42 14 Log => ${$sling}->{'Log'}
43 };
44  
45 # Authn references itself to be compatible with Apache::Sling::Request::request
46 14 $authn->{'Authn'} = \$authn;
47  
48 # Add a reference to the authn object to the sling object to make it easier to
49 # pass a subclassed authn object through:
50 14 ${$sling}->{'Authn'} = \$authn;
51 14 bless $authn, $class;
52 14 return $authn;
53 }
54  
55 #}}}
56  
57 #{{{sub set_results
58 1 ▶sub set_results {
Subroutine set_results: called CC=1 SCAR=0.0
Pod documented
59 1 my ( $class, $message, $response ) = @_;
60 1 $class->{'Message'} = $message;
61 1 $class->{'Response'} = $response;
62 1 return 1;
63 }
64  
65 #}}}
66  
67 #{{{sub basic_login
68 0 ▶sub basic_login {
Subroutine basic_login: not called CC=2 SCAR=17.9
Pod documented
69 0 my ($authn) = @_;
70 my $res =
71 Apache::Sling::Request::request( \$authn,
72 0 Apache::Sling::AuthnUtil::basic_login_setup( $authn->{'BaseURL'} ) );
73 0 my $success = Apache::Sling::AuthnUtil::basic_login_eval($res);
74 0 my $message = 'Basic auth log in ';
75 0 ▶ $message .= ( $success ? 'succeeded!' : 'failed!' );
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 0 0 0
76 0 $authn->set_results( "$message", $res );
77 0 return $success;
78 }
79  
80 #}}}
81  
82 #{{{sub login_user
83 8 ▶sub login_user {
Subroutine login_user: called CC=9 SCAR=35.3
Pod documented
84 8 my ($authn) = @_;
85 $authn->{'Type'} =
86 8 ▶ ( defined $authn->{'Type'} ? $authn->{'Type'} : 'basic' );
Branch: 1 branch
BranchTrueFalseTotal
defined $authn->{'Type'} ? : 1 7 8
87  
88 # Apply basic authentication to the user agent if url, username and
89 # password are supplied:
90 8 ▶ if ( defined $authn->{'BaseURL'}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $authn->{"BaseURL"} and defined $authn->{"Username"} and defined $authn->{"Password"}) 0 8 8
Condition50%3/6
defined $authn->{"BaseURL"} and defined $authn->{"Username"} and defined $authn->{"Password"}
!ll&&!rl&&r
800
defined $authn->{"BaseURL"} and defined $authn->{"Username"}
!ll&&!rl&&r
170
MC/DC0%0/3
defined $authn->{"BaseURL"} and defined $authn->{"Username"} and defined $authn->{"Password"}
defined $authn->{"BaseURL"} defined $authn->{"Username"} defined $authn->{"Password"}
Truth table50%2/4
A and B and C
A= defined $authn->{"BaseURL"}B= defined $authn->{"Username"}C= defined $authn->{"Password"}
ABCresult
0XX0
10X0
1100
1111
91 && defined $authn->{'Username'}
92 && defined $authn->{'Password'} )
93 {
94 0 ▶ if ( $authn->{'Type'} eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authn->{'Type'} eq 'basic') { } 0 0 0
95 0 my $success = $authn->basic_login();
96 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
97 0 ▶ if ( $authn->{'Verbose'} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authn->{"Verbose"} >= 1) 0 0 0
98 0 Apache::Sling::Print::print_result($authn);
99 }
100 croak 'Basic Auth log in for user "'
101 . $authn->{'Username'}
102 . '" at URL "'
103 0 . $authn->{'BaseURL'}
104 . "\" was unsuccessful\n";
105 }
106 }
107 else {
108 0 croak 'Unsupported auth type: "' . $authn->{'Type'} . "\"\n";
109 }
110 0 ▶ if ( $authn->{'Verbose'} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authn->{"Verbose"} >= 1) 0 0 0
111 0 Apache::Sling::Print::print_result($authn);
112 }
113 }
114 8 return 1;
115 }
116  
117 #}}}
118  
119 #{{{sub switch_user
120 2 ▶sub switch_user {
Subroutine switch_user: called CC=11 SCAR=44.3
Pod documented
121 2 my ( $authn, $new_username, $new_password, $type, $check_basic ) = @_;
122 2 ▶ if ( !defined $new_username ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_username) 1 1 2
123 1 croak 'New username to switch to not defined';
124 }
125 1 ▶ if ( !defined $new_password ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_password) 1 0 1
126 1 croak 'New password to use in switch not defined';
127 }
128 0 ▶ if ( ( $authn->{'Username'} !~ /^$new_username$/msx )
Branch: 1 branch
BranchTrueFalseTotal
if (not $authn->{'Username'} =~ /^$new_username$/msx && $authn->{'Password'} =~ /^$new_password$/msx) { } 0 0 0
Condition0%0/3
$authn->{'Username'} =~ /^$new_username$/msx && $authn->{'Password'} =~ /^$new_password$/msx
!ll&&!rl&&r
000
MC/DC0%0/2
$authn->{'Username'} =~ /^$new_username$/msx && $authn->{'Password'} =~ /^$new_password$/msx
$authn->{'Username'} =~ /^$new_username$/msx $authn->{'Password'} =~ /^$new_password$/msx
Truth table0%0/3
A && B
A= $authn->{'Username'} =~ /^$new_username$/msxB= $authn->{'Password'} =~ /^$new_password$/msx
ABresult
0X0
100
111
129 || ( $authn->{'Password'} !~ /^$new_password$/msx ) )
130 {
131 0 my $old_username = $authn->{'Username'};
132 0 my $old_password = $authn->{'Password'};
133 0 my $old_type = $authn->{'Type'};
134 0 $authn->{'Username'} = $new_username;
135 0 $authn->{'Password'} = $new_password;
136 0 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 0 0 0
137 0 $authn->{'Type'} = $type;
138 }
139 0 ▶ $check_basic = ( defined $check_basic ? $check_basic : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $check_basic ? : 0 0 0
140 0 ▶ if ( $authn->{'Type'} eq 'basic' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authn->{'Type'} eq 'basic') { } 0 0 0
141 0 ▶ if ($check_basic) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check_basic) { } 0 0 0
142 0 my $success = $authn->basic_login();
143 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
144  
145 # Reset credentials:
146 0 $authn->{'Username'} = $old_username;
147 0 $authn->{'Password'} = $old_password;
148 0 $authn->{'Type'} = $old_type;
149 croak
150 "Basic Auth log in for user \"$new_username\" at URL \""
151 0 . $authn->{'BaseURL'}
152 . "\" was unsuccessful\n";
153 }
154 }
155 else {
156 0 $authn->{'Message'} = 'Fast User Switch completed!';
157 }
158 }
159 else {
160  
161 # Reset credentials:
162 0 $authn->{'Username'} = $old_username;
163 0 $authn->{'Password'} = $old_password;
164 0 $authn->{'Type'} = $old_type;
165 0 croak "Unsupported auth type: \"$type\"\n";
166 }
167 }
168 else {
169 0 $authn->{'Message'} = 'User already active, no need to switch!';
170 }
171 0 ▶ if ( $authn->{'Verbose'} >= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($authn->{"Verbose"} >= 1) 0 0 0
172 0 Apache::Sling::Print::print_result($authn);
173 }
174 0 return 1;
175 }
176  
177 #}}}
178  
179 #{{{sub user_agent
180 14 ▶sub user_agent {
Subroutine user_agent: called CC=2 SCAR=6.9
Pod undocumented
181 14 my ( $class, $referer ) = @_;
182 14 my $lwp_user_agent = LWP::UserAgent->new( keep_alive => 1 );
183 14 push @{ $lwp_user_agent->requests_redirectable }, 'POST';
184 14 my $tmp_cookie_file_name;
185 14 $lwp_user_agent->cookie_jar( { file => \$tmp_cookie_file_name } );
186 14 ▶ if ( defined $referer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $referer) 1 13 14
187 1 $lwp_user_agent->default_header( 'Referer' => $referer );
188 }
189 14 return \$lwp_user_agent;
190 }
191  
192 #}}}
193  
194 11 1;
195  
196 __END__
« Sling/User.pm ↑ Summary Sling/GroupMember.pm »