X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FAuth_with_shibboleth.pm;h=bb0a342fd1f421b10cc20dd54a9ac17b32c6f5d8;hb=26a779eded6fe24abe0be904da64e0186c3d91ec;hp=6b20511df5854a666ddde4a15b72643b9ff25556;hpb=f8144691cc47aeb37e976f1d4c6dcb2b75510d26;p=koha.git diff --git a/C4/Auth_with_shibboleth.pm b/C4/Auth_with_shibboleth.pm index 6b20511df5..bb0a342fd1 100644 --- a/C4/Auth_with_shibboleth.pm +++ b/C4/Auth_with_shibboleth.pm @@ -23,10 +23,11 @@ use C4::Debug; use C4::Context; use Koha::AuthUtils qw(get_script_name); use Koha::Database; -use C4::Members qw( AddMember_Auto ); +use Koha::Patrons; use C4::Members::Messaging; use Carp; use CGI; +use List::MoreUtils qw(any); use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug); @@ -82,9 +83,14 @@ sub get_login_shib { my $config = _get_shib_config(); my $matchAttribute = $config->{mapping}->{ $config->{matchpoint} }->{is}; - $debug and warn $matchAttribute . " value: " . $ENV{$matchAttribute}; - return $ENV{$matchAttribute} || ''; + if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) { + $debug and warn $matchAttribute . " value: " . $ENV{"HTTP_".uc($matchAttribute)}; + return $ENV{"HTTP_".uc($matchAttribute)} || ''; + } else { + $debug and warn $matchAttribute . " value: " . $ENV{$matchAttribute}; + return $ENV{$matchAttribute} || ''; + } } # Checks for password correctness @@ -101,6 +107,9 @@ sub checkpw_shib { Koha::Database->new()->schema()->resultset('Borrower') ->find( { $config->{matchpoint} => $match } ); if ( defined($borrower) ) { + if ($config->{'sync'}) { + _sync($borrower->borrowernumber, $config, $match); + } return ( 1, $borrower->get_column('cardnumber'), $borrower->get_column('userid') ); } @@ -119,33 +128,63 @@ sub _autocreate { my %borrower = ( $config->{matchpoint} => $match ); while ( my ( $key, $entry ) = each %{$config->{'mapping'}} ) { - $borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || ''; + if ( any { /(^psgi|^plack)/i } keys %ENV ) { + $borrower{$key} = ( $entry->{'is'} && $ENV{"HTTP_" . uc($entry->{'is'}) } ) || $entry->{'content'} || ''; + } else { + $borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || ''; + } } - %borrower = AddMember_Auto( %borrower ); - C4::Members::Messaging::SetMessagingPreferencesFromDefaults( { borrowernumber => $borrower{'borrowernumber'}, categorycode => $borrower{'categorycode'} } ); + my $patron = Koha::Patron->new( \%borrower )->store; + C4::Members::Messaging::SetMessagingPreferencesFromDefaults( { borrowernumber => $patron->borrowernumber, categorycode => $patron->categorycode } ); - return ( 1, $borrower{'cardnumber'}, $borrower{'userid'} ); + return ( 1, $patron->cardnumber, $patron->userid ); +} + +sub _sync { + my ($borrowernumber, $config, $match ) = @_; + my %borrower; + $borrower{'borrowernumber'} = $borrowernumber; + while ( my ( $key, $entry ) = each %{$config->{'mapping'}} ) { + if ( any { /(^psgi|^plack)/i } keys %ENV ) { + $borrower{$key} = ( $entry->{'is'} && $ENV{"HTTP_" . uc($entry->{'is'}) } ) || $entry->{'content'} || ''; + } else { + $borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || ''; + } + } + my $patron = Koha::Patrons->find( $borrowernumber ); + $patron->set(\%borrower)->store; } sub _get_uri { my $protocol = "https://"; + my $interface = C4::Context->interface; + $debug and warn "shibboleth interface: " . $interface; + + my $uri; + if ( $interface eq 'intranet' ) { - my $uri = C4::Context->preference('OPACBaseURL') // ''; - if ($uri eq '') { - $debug and warn 'OPACBaseURL not set!'; + $uri = C4::Context->preference('staffClientBaseURL') // ''; + if ($uri eq '') { + $debug and warn 'staffClientBaseURL not set!'; + } + } else { + $uri = C4::Context->preference('OPACBaseURL') // ''; + if ($uri eq '') { + $debug and warn 'OPACBaseURL not set!'; + } } + if ($uri =~ /(.*):\/\/(.*)/) { my $oldprotocol = $1; if ($oldprotocol ne 'https') { $debug and warn - 'Shibboleth requires OPACBaseURL to use the https protocol!'; + 'Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!'; } $uri = $2; } - my $return = $protocol . $uri; return $return; } @@ -154,7 +193,7 @@ sub _get_shib_config { my $config = C4::Context->config('shibboleth'); if ( !$config ) { - carp 'shibboleth config not defined'; + carp 'shibboleth config not defined' if $debug; return 0; } @@ -236,13 +275,24 @@ Map their attributes to what you want to see in koha Tell apache that we wish to allow koha to authenticate via shibboleth. -This is as simple as adding the below to your virtualhost config: +This is as simple as adding the below to your virtualhost config (for CGI running): + + + AuthType shibboleth + Require shibboleth + + +Or (for Plack running): AuthType shibboleth Require shibboleth + ShibUseEnvironment Off + ShibUseHeaders On +IMPORTANT: Please note, if you are running in the plack configuration you should consult https://wiki.shibboleth.net/confluence/display/SHIB2/NativeSPSpoofChecking for security advice regarding header spoof checking settings. (See also bug 17776 on Bugzilla about enabling ShibUseHeaders.) + =item 5. Configure koha to listen for shibboleth environment variables. @@ -296,6 +346,22 @@ Given a shib_login attribute, this routine checks for a matching local user and my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $shib_login ); +=head2 _get_uri + + _get_uri(); + +A sugar function to that simply returns the current page URI with appropriate protocol attached + +This routine is NOT exported + +=head2 _get_shib_config + + my $config = _get_shib_config(); + +A sugar function that checks for a valid shibboleth configuration, and if found returns a hashref of it's contents + +This routine is NOT exported + =head2 _autocreate my ( $retval, $retcard, $retuserid ) = _autocreate( $config, $match );