| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl -w | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Apache::Sling::Authn; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 11 |  |  | 11 |  | 180 | use 5.008001; | 
|  | 11 |  |  |  |  | 33 |  | 
|  | 11 |  |  |  |  | 430 |  | 
| 6 | 11 |  |  | 11 |  | 57 | use strict; | 
|  | 11 |  |  |  |  | 24 |  | 
|  | 11 |  |  |  |  | 379 |  | 
| 7 | 11 |  |  | 11 |  | 60 | use warnings; | 
|  | 11 |  |  |  |  | 20 |  | 
|  | 11 |  |  |  |  | 815 |  | 
| 8 | 11 |  |  | 11 |  | 62 | use Carp; | 
|  | 11 |  |  |  |  | 24 |  | 
|  | 11 |  |  |  |  | 994 |  | 
| 9 | 11 |  |  | 11 |  | 13305 | use LWP::UserAgent (); | 
|  | 11 |  |  |  |  | 4746759 |  | 
|  | 11 |  |  |  |  | 1984 |  | 
| 10 | 11 |  |  | 11 |  | 8886 | use Apache::Sling::AuthnUtil; | 
|  | 11 |  |  |  |  | 28 |  | 
|  | 11 |  |  |  |  | 553 |  | 
| 11 | 11 |  |  | 11 |  | 6865 | use Apache::Sling::Print; | 
|  | 11 |  |  |  |  | 40 |  | 
|  | 11 |  |  |  |  | 540 |  | 
| 12 | 11 |  |  | 11 |  | 7285 | use Apache::Sling::Request; | 
|  | 11 |  |  |  |  | 39 |  | 
|  | 11 |  |  |  |  | 563 |  | 
| 13 | 11 |  |  | 11 |  | 8880 | use Apache::Sling::URL; | 
|  | 11 |  |  |  |  | 35 |  | 
|  | 11 |  |  |  |  | 588 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | require Exporter; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 11 |  |  | 11 |  | 57 | use base qw(Exporter); | 
|  | 11 |  |  |  |  | 21 |  | 
|  | 11 |  |  |  |  | 17407 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | our @EXPORT_OK = (); | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | our $VERSION = '0.27'; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | #{{{sub new | 
| 24 |  |  |  |  |  |  | sub new { | 
| 25 | 14 |  |  | 14 | 1 | 6552 | my ( $class, $sling ) = @_; | 
| 26 | 14 |  |  |  |  | 34 | my $url = Apache::Sling::URL::url_input_sanitize( ${$sling}->{'URL'} ); | 
|  | 14 |  |  |  |  | 132 |  | 
| 27 | 14 |  |  |  |  | 58 | my $verbose = | 
| 28 | 14 | 100 |  |  |  | 33 | ( defined ${$sling}->{'Verbose'} ? ${$sling}->{'Verbose'} : 0 ); | 
|  | 2 |  |  |  |  | 6 |  | 
| 29 |  |  |  |  |  |  |  | 
| 30 | 14 |  |  |  |  | 30 | my $lwp_user_agent = $class->user_agent( ${$sling}->{'Referer'} ); | 
|  | 14 |  |  |  |  | 162 |  | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 14 |  |  |  |  | 54 | my $response; | 
| 33 | 14 |  |  |  |  | 57 | my $authn = { | 
| 34 |  |  |  |  |  |  | BaseURL  => "$url", | 
| 35 |  |  |  |  |  |  | LWP      => $lwp_user_agent, | 
| 36 | 14 |  |  |  |  | 39 | Type     => ${$sling}->{'Auth'}, | 
| 37 | 14 |  |  |  |  | 52 | Username => ${$sling}->{'User'}, | 
| 38 | 14 |  |  |  |  | 157 | Password => ${$sling}->{'Pass'}, | 
| 39 |  |  |  |  |  |  | Message  => q{}, | 
| 40 |  |  |  |  |  |  | Response => \$response, | 
| 41 |  |  |  |  |  |  | Verbose  => $verbose, | 
| 42 | 14 |  |  |  |  | 45 | Log      => ${$sling}->{'Log'} | 
| 43 |  |  |  |  |  |  | }; | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | # Authn references itself to be compatible with Apache::Sling::Request::request | 
| 46 | 14 |  |  |  |  | 53 | $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 |  |  |  |  | 28 | ${$sling}->{'Authn'} = \$authn; | 
|  | 14 |  |  |  |  | 130 |  | 
| 51 | 14 |  |  |  |  | 154 | bless $authn, $class; | 
| 52 | 14 |  |  |  |  | 66 | return $authn; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 |  |  |  |  |  |  | #}}} | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | #{{{sub set_results | 
| 58 |  |  |  |  |  |  | sub set_results { | 
| 59 | 1 |  |  | 1 | 1 | 2519 | my ( $class, $message, $response ) = @_; | 
| 60 | 1 |  |  |  |  | 4 | $class->{'Message'}  = $message; | 
| 61 | 1 |  |  |  |  | 4 | $class->{'Response'} = $response; | 
| 62 | 1 |  |  |  |  | 4 | return 1; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | #}}} | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | #{{{sub basic_login | 
| 68 |  |  |  |  |  |  | sub basic_login { | 
| 69 | 0 |  |  | 0 | 1 | 0 | my ($authn) = @_; | 
| 70 | 0 |  |  |  |  | 0 | my $res = | 
| 71 |  |  |  |  |  |  | Apache::Sling::Request::request( \$authn, | 
| 72 |  |  |  |  |  |  | Apache::Sling::AuthnUtil::basic_login_setup( $authn->{'BaseURL'} ) ); | 
| 73 | 0 |  |  |  |  | 0 | my $success = Apache::Sling::AuthnUtil::basic_login_eval($res); | 
| 74 | 0 |  |  |  |  | 0 | my $message = 'Basic auth log in '; | 
| 75 | 0 | 0 |  |  |  | 0 | $message .= ( $success ? 'succeeded!' : 'failed!' ); | 
| 76 | 0 |  |  |  |  | 0 | $authn->set_results( "$message", $res ); | 
| 77 | 0 |  |  |  |  | 0 | return $success; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | #}}} | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | #{{{sub login_user | 
| 83 |  |  |  |  |  |  | sub login_user { | 
| 84 | 8 |  |  | 8 | 1 | 1179 | my ($authn) = @_; | 
| 85 | 8 | 100 |  |  |  | 93 | $authn->{'Type'} = | 
| 86 |  |  |  |  |  |  | ( defined $authn->{'Type'} ? $authn->{'Type'} : 'basic' ); | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | # Apply basic authentication to the user agent if url, username and | 
| 89 |  |  |  |  |  |  | # password are supplied: | 
| 90 | 8 | 50 | 66 |  |  | 110 | if (   defined $authn->{'BaseURL'} | 
|  |  |  | 33 |  |  |  |  | 
| 91 |  |  |  |  |  |  | && defined $authn->{'Username'} | 
| 92 |  |  |  |  |  |  | && defined $authn->{'Password'} ) | 
| 93 |  |  |  |  |  |  | { | 
| 94 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Type'} eq 'basic' ) { | 
| 95 | 0 |  |  |  |  | 0 | my $success = $authn->basic_login(); | 
| 96 | 0 | 0 |  |  |  | 0 | if ( !$success ) { | 
| 97 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Verbose'} >= 1 ) { | 
| 98 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_result($authn); | 
| 99 |  |  |  |  |  |  | } | 
| 100 | 0 |  |  |  |  | 0 | croak 'Basic Auth log in for user "' | 
| 101 |  |  |  |  |  |  | . $authn->{'Username'} | 
| 102 |  |  |  |  |  |  | . '" at URL "' | 
| 103 |  |  |  |  |  |  | . $authn->{'BaseURL'} | 
| 104 |  |  |  |  |  |  | . "\" was unsuccessful\n"; | 
| 105 |  |  |  |  |  |  | } | 
| 106 |  |  |  |  |  |  | } | 
| 107 |  |  |  |  |  |  | else { | 
| 108 | 0 |  |  |  |  | 0 | croak 'Unsupported auth type: "' . $authn->{'Type'} . "\"\n"; | 
| 109 |  |  |  |  |  |  | } | 
| 110 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Verbose'} >= 1 ) { | 
| 111 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_result($authn); | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  | } | 
| 114 | 8 |  |  |  |  | 26 | return 1; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | #}}} | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | #{{{sub switch_user | 
| 120 |  |  |  |  |  |  | sub switch_user { | 
| 121 | 2 |  |  | 2 | 1 | 62 | my ( $authn, $new_username, $new_password, $type, $check_basic ) = @_; | 
| 122 | 2 | 100 |  |  |  | 8 | if ( !defined $new_username ) { | 
| 123 | 1 |  |  |  |  | 30 | croak 'New username to switch to not defined'; | 
| 124 |  |  |  |  |  |  | } | 
| 125 | 1 | 50 |  |  |  | 49 | if ( !defined $new_password ) { | 
| 126 | 1 |  |  |  |  | 13 | croak 'New password to use in switch not defined'; | 
| 127 |  |  |  |  |  |  | } | 
| 128 | 0 | 0 | 0 |  |  | 0 | if (   ( $authn->{'Username'} !~ /^$new_username$/msx ) | 
| 129 |  |  |  |  |  |  | || ( $authn->{'Password'} !~ /^$new_password$/msx ) ) | 
| 130 |  |  |  |  |  |  | { | 
| 131 | 0 |  |  |  |  | 0 | my $old_username = $authn->{'Username'}; | 
| 132 | 0 |  |  |  |  | 0 | my $old_password = $authn->{'Password'}; | 
| 133 | 0 |  |  |  |  | 0 | my $old_type     = $authn->{'Type'}; | 
| 134 | 0 |  |  |  |  | 0 | $authn->{'Username'} = $new_username; | 
| 135 | 0 |  |  |  |  | 0 | $authn->{'Password'} = $new_password; | 
| 136 | 0 | 0 |  |  |  | 0 | if ( defined $type ) { | 
| 137 | 0 |  |  |  |  | 0 | $authn->{'Type'} = $type; | 
| 138 |  |  |  |  |  |  | } | 
| 139 | 0 | 0 |  |  |  | 0 | $check_basic = ( defined $check_basic ? $check_basic : 0 ); | 
| 140 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Type'} eq 'basic' ) { | 
| 141 | 0 | 0 |  |  |  | 0 | if ($check_basic) { | 
| 142 | 0 |  |  |  |  | 0 | my $success = $authn->basic_login(); | 
| 143 | 0 | 0 |  |  |  | 0 | if ( !$success ) { | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | # Reset credentials: | 
| 146 | 0 |  |  |  |  | 0 | $authn->{'Username'} = $old_username; | 
| 147 | 0 |  |  |  |  | 0 | $authn->{'Password'} = $old_password; | 
| 148 | 0 |  |  |  |  | 0 | $authn->{'Type'}     = $old_type; | 
| 149 | 0 |  |  |  |  | 0 | croak | 
| 150 |  |  |  |  |  |  | "Basic Auth log in for user \"$new_username\" at URL \"" | 
| 151 |  |  |  |  |  |  | . $authn->{'BaseURL'} | 
| 152 |  |  |  |  |  |  | . "\" was unsuccessful\n"; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  | } | 
| 155 |  |  |  |  |  |  | else { | 
| 156 | 0 |  |  |  |  | 0 | $authn->{'Message'} = 'Fast User Switch completed!'; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  | else { | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | # Reset credentials: | 
| 162 | 0 |  |  |  |  | 0 | $authn->{'Username'} = $old_username; | 
| 163 | 0 |  |  |  |  | 0 | $authn->{'Password'} = $old_password; | 
| 164 | 0 |  |  |  |  | 0 | $authn->{'Type'}     = $old_type; | 
| 165 | 0 |  |  |  |  | 0 | croak "Unsupported auth type: \"$type\"\n"; | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  | } | 
| 168 |  |  |  |  |  |  | else { | 
| 169 | 0 |  |  |  |  | 0 | $authn->{'Message'} = 'User already active, no need to switch!'; | 
| 170 |  |  |  |  |  |  | } | 
| 171 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Verbose'} >= 1 ) { | 
| 172 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_result($authn); | 
| 173 |  |  |  |  |  |  | } | 
| 174 | 0 |  |  |  |  | 0 | return 1; | 
| 175 |  |  |  |  |  |  | } | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | #}}} | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | #{{{sub user_agent | 
| 180 |  |  |  |  |  |  | sub user_agent { | 
| 181 | 14 |  |  | 14 | 0 | 48 | my ( $class, $referer ) = @_; | 
| 182 | 14 |  |  |  |  | 178 | my $lwp_user_agent = LWP::UserAgent->new( keep_alive => 1 ); | 
| 183 | 14 |  |  |  |  | 68465 | push @{ $lwp_user_agent->requests_redirectable }, 'POST'; | 
|  | 14 |  |  |  |  | 107 |  | 
| 184 | 14 |  |  |  |  | 276 | my $tmp_cookie_file_name; | 
| 185 | 14 |  |  |  |  | 101 | $lwp_user_agent->cookie_jar( { file => \$tmp_cookie_file_name } ); | 
| 186 | 14 | 100 |  |  |  | 1712222 | if ( defined $referer ) { | 
| 187 | 1 |  |  |  |  | 4 | $lwp_user_agent->default_header( 'Referer' => $referer ); | 
| 188 |  |  |  |  |  |  | } | 
| 189 | 14 |  |  |  |  | 106 | return \$lwp_user_agent; | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | #}}} | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | 1; | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | __END__ |