Bug 15395: Allow correct handling of plural translation
[koha.git] / C4 / Auth_with_shibboleth.pm
blob24246d51ecd00b664b7389bf4995d81345e4ed1b
1 package C4::Auth_with_shibboleth;
3 # Copyright 2014 PTFS Europe
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
22 use C4::Debug;
23 use C4::Context;
24 use Koha::AuthUtils qw(get_script_name);
25 use Koha::Database;
26 use Koha::Patrons;
27 use C4::Members::Messaging;
28 use Carp;
29 use CGI;
30 use List::Util qw(any);
32 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
34 BEGIN {
35 require Exporter;
36 $debug = $ENV{DEBUG};
37 @ISA = qw(Exporter);
38 @EXPORT =
39 qw(shib_ok logout_shib login_shib_url checkpw_shib get_login_shib);
42 # Check that shib config is not malformed
43 sub shib_ok {
44 my $config = _get_shib_config();
46 if ($config) {
47 return 1;
50 return 0;
53 # Logout from Shibboleth
54 sub logout_shib {
55 my ($query) = @_;
56 my $uri = _get_uri();
57 print $query->redirect( $uri . "/Shibboleth.sso/Logout?return=$uri" );
60 # Returns Shibboleth login URL with callback to the requesting URL
61 sub login_shib_url {
62 my ($query) = @_;
64 my $param = _get_uri() . get_script_name();
65 if ( $query->query_string() ) {
66 $param = $param . '%3F' . $query->query_string();
68 my $uri = _get_uri() . "/Shibboleth.sso/Login?target=$param";
69 return $uri;
72 # Returns shibboleth user login
73 sub get_login_shib {
75 # In case of a Shibboleth authentication, we expect a shibboleth user attribute
76 # to contain the login match point of the shibboleth-authenticated user. This match
77 # point is configured in koha-conf.xml
79 # Shibboleth attributes are mapped into http environmement variables, so we're getting
80 # the match point of the user this way
82 # Get shibboleth config
83 my $config = _get_shib_config();
85 my $matchAttribute = $config->{mapping}->{ $config->{matchpoint} }->{is};
87 if ( any { /(^psgi\.|^plack\.)/i } keys %ENV ) {
88 $debug and warn $matchAttribute . " value: " . $ENV{"HTTP_".uc($matchAttribute)};
89 return $ENV{"HTTP_".uc($matchAttribute)} || '';
90 } else {
91 $debug and warn $matchAttribute . " value: " . $ENV{$matchAttribute};
92 return $ENV{$matchAttribute} || '';
96 # Checks for password correctness
97 # In our case : does the given attribute match one of our users ?
98 sub checkpw_shib {
99 $debug and warn "checkpw_shib";
101 my ( $match ) = @_;
102 my $config = _get_shib_config();
103 $debug and warn "User Shibboleth-authenticated as: $match";
105 # Does the given shibboleth attribute value ($match) match a valid koha user ?
106 my $borrower =
107 Koha::Database->new()->schema()->resultset('Borrower')
108 ->find( { $config->{matchpoint} => $match } );
109 if ( defined($borrower) ) {
110 return ( 1, $borrower->get_column('cardnumber'), $borrower->get_column('userid') );
113 if ( $config->{'autocreate'} ) {
114 return _autocreate( $config, $match );
115 } else {
116 # If we reach this point, the user is not a valid koha user
117 $debug and warn "User with $config->{matchpoint} of $match is not a valid Koha user";
118 return 0;
122 sub _autocreate {
123 my ( $config, $match ) = @_;
125 my %borrower = ( $config->{matchpoint} => $match );
127 while ( my ( $key, $entry ) = each %{$config->{'mapping'}} ) {
128 if ( any { /(^psgi|^plack)/i } keys %ENV ) {
129 $borrower{$key} = ( $entry->{'is'} && $ENV{"HTTP_" . uc($entry->{'is'}) } ) || $entry->{'content'} || '';
130 } else {
131 $borrower{$key} = ( $entry->{'is'} && $ENV{ $entry->{'is'} } ) || $entry->{'content'} || '';
135 my $patron = Koha::Patron->new( \%borrower )->store;
136 C4::Members::Messaging::SetMessagingPreferencesFromDefaults( { borrowernumber => $patron->borrowernumber, categorycode => $patron->categorycode } );
138 return ( 1, $patron->cardnumber, $patron->userid );
141 sub _get_uri {
143 my $protocol = "https://";
144 my $interface = C4::Context->interface;
145 $debug and warn "shibboleth interface: " . $interface;
147 my $uri;
148 if ( $interface eq 'intranet' ) {
150 $uri = C4::Context->preference('staffClientBaseURL') // '';
151 if ($uri eq '') {
152 $debug and warn 'staffClientBaseURL not set!';
154 } else {
155 $uri = C4::Context->preference('OPACBaseURL') // '';
156 if ($uri eq '') {
157 $debug and warn 'OPACBaseURL not set!';
161 if ($uri =~ /(.*):\/\/(.*)/) {
162 my $oldprotocol = $1;
163 if ($oldprotocol ne 'https') {
164 $debug
165 and warn
166 'Shibboleth requires OPACBaseURL/staffClientBaseURL to use the https protocol!';
168 $uri = $2;
170 my $return = $protocol . $uri;
171 return $return;
174 sub _get_shib_config {
175 my $config = C4::Context->config('shibboleth');
177 if ( !$config ) {
178 carp 'shibboleth config not defined' if $debug;
179 return 0;
182 if ( $config->{matchpoint}
183 && defined( $config->{mapping}->{ $config->{matchpoint} }->{is} ) )
185 if ($debug) {
186 warn "koha borrower field to match: " . $config->{matchpoint};
187 warn "shibboleth attribute to match: "
188 . $config->{mapping}->{ $config->{matchpoint} }->{is};
190 return $config;
192 else {
193 if ( !$config->{matchpoint} ) {
194 carp 'shibboleth matchpoint not defined';
196 else {
197 carp 'shibboleth matchpoint not mapped';
199 return 0;
204 __END__
206 =head1 NAME
208 C4::Auth_with_shibboleth
210 =head1 SYNOPSIS
212 use C4::Auth_with_shibboleth;
214 =head1 DESCRIPTION
216 This module is specific to Shibboleth authentication in koha and relies heavily upon the native shibboleth service provider package in your operating system.
218 =head1 CONFIGURATION
220 To use this type of authentication these additional packages are required:
222 =over
224 =item *
226 libapache2-mod-shib2
228 =item *
230 libshibsp5:amd64
232 =item *
234 shibboleth-sp2-schemas
236 =back
238 We let the native shibboleth service provider packages handle all the complexities of shibboleth negotiation for us, and configuring this is beyond the scope of this documentation.
240 But to sum up, to get shibboleth working in koha, as a minimum you will need to:
242 =over
244 =item 1.
246 Create some metadata for your koha instance (if you're in a single instance setup then the default metadata available at https://youraddress.com/Shibboleth.sso/Metadata should be adequate)
248 =item 2.
250 Swap metadata with your Identidy Provider (IdP)
252 =item 3.
254 Map their attributes to what you want to see in koha
256 =item 4.
258 Tell apache that we wish to allow koha to authenticate via shibboleth.
260 This is as simple as adding the below to your virtualhost config (for CGI running):
262 <Location />
263 AuthType shibboleth
264 Require shibboleth
265 </Location>
267 Or (for Plack running):
269 <Location />
270 AuthType shibboleth
271 Require shibboleth
272 ShibUseEnvironment Off
273 ShibUseHeaders On
274 </Location>
276 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.)
278 =item 5.
280 Configure koha to listen for shibboleth environment variables.
282 This is as simple as enabling B<useshibboleth> in koha-conf.xml:
284 <useshibboleth>1</useshibboleth>
286 =item 6.
288 Map shibboleth attributes to koha fields, and configure authentication match point in koha-conf.xml.
290 <shibboleth>
291 <matchpoint>userid</matchpoint> <!-- koha borrower field to match upon -->
292 <mapping>
293 <userid is="eduPersonID"></userid> <!-- koha borrower field to shibboleth attribute mapping -->
294 </mapping>
295 </shibboleth>
297 Note: The minimum you need here is a <matchpoint> block, containing a valid column name from the koha borrowers table, and a <mapping> block containing a relation between the chosen matchpoint and the shibboleth attribute name.
299 =back
301 It should be as simple as that; you should now be able to login via shibboleth in the opac.
303 If you need more help configuring your B<S>ervice B<P>rovider to authenticate against a chosen B<Id>entity B<P>rovider then it might be worth taking a look at the community wiki L<page|http://wiki.koha-community.org/wiki/Shibboleth_Configuration>
305 =head1 FUNCTIONS
307 =head2 logout_shib
309 Sends a logout signal to the native shibboleth service provider and then logs out of koha. Depending upon the native service provider configuration and identity provider capabilities this may or may not perform a single sign out action.
311 logout_shib($query);
313 =head2 login_shib_url
315 Given a query, this will return a shibboleth login url with return code to page with given given query.
317 my $shibLoginURL = login_shib_url($query);
319 =head2 get_login_shib
321 Returns the shibboleth login attribute should it be found present in the http session
323 my $shib_login = get_login_shib();
325 =head2 checkpw_shib
327 Given a shib_login attribute, this routine checks for a matching local user and if found returns true, their cardnumber and their userid. If a match is not found, then this returns false.
329 my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $shib_login );
331 =head2 _get_uri
333 _get_uri();
335 A sugar function to that simply returns the current page URI with appropriate protocol attached
337 This routine is NOT exported
339 =head2 _get_shib_config
341 my $config = _get_shib_config();
343 A sugar function that checks for a valid shibboleth configuration, and if found returns a hashref of it's contents
345 This routine is NOT exported
347 =head2 _autocreate
349 my ( $retval, $retcard, $retuserid ) = _autocreate( $config, $match );
351 Given a shibboleth attribute reference and a userid this internal routine will add the given user to Koha and return their user credentials.
353 This routine is NOT exported
355 =head1 SEE ALSO
357 =cut