| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/perl -w | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Sakai::Nakamura::Authn; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 7 |  |  | 7 |  | 9252 | use 5.008008; | 
|  | 7 |  |  |  |  | 25 |  | 
|  | 7 |  |  |  |  | 273 |  | 
| 6 | 7 |  |  | 7 |  | 56 | use strict; | 
|  | 7 |  |  |  |  | 14 |  | 
|  | 7 |  |  |  |  | 283 |  | 
| 7 | 7 |  |  | 7 |  | 36 | use warnings; | 
|  | 7 |  |  |  |  | 12 |  | 
|  | 7 |  |  |  |  | 282 |  | 
| 8 | 7 |  |  | 7 |  | 36 | use Carp; | 
|  | 7 |  |  |  |  | 18 |  | 
|  | 7 |  |  |  |  | 592 |  | 
| 9 | 7 |  |  | 7 |  | 41 | use base qw(Apache::Sling::Authn); | 
|  | 7 |  |  |  |  | 10 |  | 
|  | 7 |  |  |  |  | 757 |  | 
| 10 | 7 |  |  | 7 |  | 4843 | use Sakai::Nakamura::AuthnUtil; | 
|  | 7 |  |  |  |  | 20 |  | 
|  | 7 |  |  |  |  | 497 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | require Exporter; | 
| 13 |  |  |  |  |  |  |  | 
| 14 | 7 |  |  | 7 |  | 37 | use base qw(Exporter); | 
|  | 7 |  |  |  |  | 20 |  | 
|  | 7 |  |  |  |  | 6263 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | our @EXPORT_OK = (); | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '0.13'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | #{{{sub new | 
| 21 |  |  |  |  |  |  | sub new { | 
| 22 | 6 |  |  | 6 | 1 | 4183 | my ( $class, $nakamura ) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 6 |  |  |  |  | 81 | my $authn = $class->SUPER::new($nakamura); | 
| 25 | 6 |  |  |  |  | 100980 | ${$nakamura}->{'Authn'} = \$authn; | 
|  | 6 |  |  |  |  | 26 |  | 
| 26 | 6 |  |  |  |  | 20 | bless $authn, $class; | 
| 27 | 6 |  |  |  |  | 35 | return $authn; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | #}}} | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | #{{{sub form_login | 
| 33 |  |  |  |  |  |  | sub form_login { | 
| 34 | 0 |  |  | 0 | 1 | 0 | my ($authn)  = @_; | 
| 35 | 0 |  |  |  |  | 0 | my $username = $authn->{'Username'}; | 
| 36 | 0 |  |  |  |  | 0 | my $password = $authn->{'Password'}; | 
| 37 | 0 |  |  |  |  | 0 | my $res      = Apache::Sling::Request::request( | 
| 38 |  |  |  |  |  |  | \$authn, | 
| 39 |  |  |  |  |  |  | Sakai::Nakamura::AuthnUtil::form_login_setup( | 
| 40 |  |  |  |  |  |  | $authn->{'BaseURL'}, $username, $password | 
| 41 |  |  |  |  |  |  | ) | 
| 42 |  |  |  |  |  |  | ); | 
| 43 | 0 |  |  |  |  | 0 | my $success = Sakai::Nakamura::AuthnUtil::form_login_eval($res); | 
| 44 | 0 |  |  |  |  | 0 | my $message = "Form log in as user \"$username\" "; | 
| 45 | 0 | 0 |  |  |  | 0 | $message .= ( $success ? 'succeeded!' : 'failed!' ); | 
| 46 | 0 |  |  |  |  | 0 | $authn->set_results( "$message", $res ); | 
| 47 | 0 |  |  |  |  | 0 | return $success; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | #}}} | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | #{{{sub form_logout | 
| 53 |  |  |  |  |  |  | sub form_logout { | 
| 54 | 0 |  |  | 0 | 1 | 0 | my ($authn) = @_; | 
| 55 | 0 |  |  |  |  | 0 | my $res = | 
| 56 |  |  |  |  |  |  | Apache::Sling::Request::request( \$authn, | 
| 57 |  |  |  |  |  |  | Sakai::Nakamura::AuthnUtil::form_logout_setup( $authn->{'BaseURL'} ) ); | 
| 58 | 0 |  |  |  |  | 0 | my $success = Sakai::Nakamura::AuthnUtil::form_logout_eval($res); | 
| 59 | 0 |  |  |  |  | 0 | my $message = 'Form log out '; | 
| 60 | 0 | 0 |  |  |  | 0 | $message .= ( $success ? 'succeeded!' : 'failed!' ); | 
| 61 | 0 |  |  |  |  | 0 | $authn->set_results( "$message", $res ); | 
| 62 | 0 |  |  |  |  | 0 | return $success; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | #}}} | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | #{{{sub switch_user | 
| 68 |  |  |  |  |  |  | sub switch_user { | 
| 69 | 0 |  |  | 0 | 1 | 0 | my ( $authn, $new_username, $new_password ) = @_; | 
| 70 | 0 | 0 |  |  |  | 0 | if ( !defined $new_username ) { | 
| 71 | 0 |  |  |  |  | 0 | croak 'New username to switch to not defined'; | 
| 72 |  |  |  |  |  |  | } | 
| 73 | 0 | 0 |  |  |  | 0 | if ( !defined $new_password ) { | 
| 74 | 0 |  |  |  |  | 0 | croak 'New password to use in switch not defined'; | 
| 75 |  |  |  |  |  |  | } | 
| 76 | 0 | 0 | 0 |  |  | 0 | if (   ( $authn->{'Username'} !~ /^$new_username$/msx ) | 
| 77 |  |  |  |  |  |  | || ( $authn->{'Password'} !~ /^$new_password$/msx ) ) | 
| 78 |  |  |  |  |  |  | { | 
| 79 | 0 |  |  |  |  | 0 | $authn->{'Username'} = $new_username; | 
| 80 | 0 |  |  |  |  | 0 | $authn->{'Password'} = $new_password; | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 0 |  |  |  |  | 0 | my $success = $authn->form_logout(); | 
| 83 | 0 | 0 |  |  |  | 0 | if ( !$success ) { | 
| 84 | 0 |  |  |  |  | 0 | croak 'Form Auth log out for user "' | 
| 85 |  |  |  |  |  |  | . $authn->{'Username'} | 
| 86 |  |  |  |  |  |  | . '" at URL "' | 
| 87 |  |  |  |  |  |  | . $authn->{'BaseURL'} | 
| 88 |  |  |  |  |  |  | . "\" was unsuccessful\n"; | 
| 89 |  |  |  |  |  |  | } | 
| 90 | 0 |  |  |  |  | 0 | $success = $authn->form_login(); | 
| 91 | 0 | 0 |  |  |  | 0 | if ( !$success ) { | 
| 92 | 0 |  |  |  |  | 0 | croak "Form Auth log in for user \"$new_username\" at URL \"" | 
| 93 |  |  |  |  |  |  | . $authn->{'BaseURL'} | 
| 94 |  |  |  |  |  |  | . "\" was unsuccessful\n"; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | else { | 
| 98 | 0 |  |  |  |  | 0 | $authn->{'Message'} = 'User already active, no need to switch!'; | 
| 99 |  |  |  |  |  |  | } | 
| 100 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Verbose'} >= 1 ) { | 
| 101 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_result($authn); | 
| 102 |  |  |  |  |  |  | } | 
| 103 | 0 |  |  |  |  | 0 | return 1; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | #}}} | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | #{{{sub login_user | 
| 109 |  |  |  |  |  |  | sub login_user { | 
| 110 | 3 |  |  | 3 | 1 | 95 | my ($authn) = @_; | 
| 111 | 3 |  |  |  |  | 5 | my $success = 1; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | # Log in if url, username and | 
| 114 |  |  |  |  |  |  | # password are supplied: | 
| 115 | 3 | 50 | 33 |  |  | 43 | if (   defined $authn->{'BaseURL'} | 
|  |  |  | 33 |  |  |  |  | 
| 116 |  |  |  |  |  |  | && defined $authn->{'Username'} | 
| 117 |  |  |  |  |  |  | && defined $authn->{'Password'} ) | 
| 118 |  |  |  |  |  |  | { | 
| 119 | 0 |  |  |  |  | 0 | $success = $authn->form_login(); | 
| 120 | 0 | 0 |  |  |  | 0 | if ( !$success ) { | 
| 121 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Verbose'} >= 1 ) { | 
| 122 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_result($authn); | 
| 123 |  |  |  |  |  |  | } | 
| 124 | 0 |  |  |  |  | 0 | croak 'Form Auth log in for user "' | 
| 125 |  |  |  |  |  |  | . $authn->{'Username'} | 
| 126 |  |  |  |  |  |  | . '" at URL "' | 
| 127 |  |  |  |  |  |  | . $authn->{'BaseURL'} | 
| 128 |  |  |  |  |  |  | . "\" was unsuccessful\n"; | 
| 129 |  |  |  |  |  |  | } | 
| 130 | 0 | 0 |  |  |  | 0 | if ( $authn->{'Verbose'} >= 1 ) { | 
| 131 | 0 |  |  |  |  | 0 | Apache::Sling::Print::print_result($authn); | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | } | 
| 134 | 3 |  |  |  |  | 9 | return $success; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | #}}} | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | 1; | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | __END__ |