Bug 16812: Revise JS script for z3950_search.tts and remove onclick events
[koha.git] / C4 / Auth_with_shibboleth.pm
1 package C4::Auth_with_shibboleth;
2
3 # Copyright 2014 PTFS Europe
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use C4::Debug;
23 use C4::Context;
24 use Koha::Database;
25 use Carp;
26 use CGI;
27
28 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
29
30 BEGIN {
31     require Exporter;
32     $debug   = $ENV{DEBUG};
33     @ISA     = qw(Exporter);
34     @EXPORT =
35       qw(shib_ok logout_shib login_shib_url checkpw_shib get_login_shib);
36 }
37
38 # Check that shib config is not malformed
39 sub shib_ok {
40     my $config = _get_shib_config();
41
42     if ($config) {
43         return 1;
44     }
45
46     return 0;
47 }
48
49 # Logout from Shibboleth
50 sub logout_shib {
51     my ($query) = @_;
52     my $uri = _get_uri();
53     print $query->redirect( $uri . "/Shibboleth.sso/Logout?return=$uri" );
54 }
55
56 # Returns Shibboleth login URL with callback to the requesting URL
57 sub login_shib_url {
58     my ($query) = @_;
59
60     my $param = _get_uri() . $query->script_name();
61     if ( $query->query_string() ) {
62         $param = $param . '%3F' . $query->query_string();
63     }
64     my $uri = _get_uri() . "/Shibboleth.sso/Login?target=$param";
65     return $uri;
66 }
67
68 # Returns shibboleth user login
69 sub get_login_shib {
70
71 # In case of a Shibboleth authentication, we expect a shibboleth user attribute
72 # to contain the login match point of the shibboleth-authenticated user. This match
73 # point is configured in koha-conf.xml
74
75 # Shibboleth attributes are mapped into http environmement variables, so we're getting
76 # the match point of the user this way
77
78     # Get shibboleth config
79     my $config = _get_shib_config();
80
81     my $matchAttribute = $config->{mapping}->{ $config->{matchpoint} }->{is};
82     $debug and warn $matchAttribute . " value: " . $ENV{$matchAttribute};
83
84     return $ENV{$matchAttribute} || '';
85 }
86
87 # Checks for password correctness
88 # In our case : does the given attribute match one of our users ?
89 sub checkpw_shib {
90     $debug and warn "checkpw_shib";
91
92     my ( $match ) = @_;
93     my $config = _get_shib_config();
94     $debug and warn "User Shibboleth-authenticated as: $match";
95
96     # Does the given shibboleth attribute value ($match) match a valid koha user ?
97     my $borrower =
98       Koha::Database->new()->schema()->resultset('Borrower')
99       ->find( { $config->{matchpoint} => $match } );
100     if ( defined($borrower) ) {
101         return ( 1, $borrower->get_column('cardnumber'), $borrower->get_column('userid') );
102     }
103
104     # If we reach this point, the user is not a valid koha user
105     $debug
106       and warn
107       "User with $config->{matchpoint} of $match is not a valid Koha user";
108     return 0;
109 }
110
111 sub _get_uri {
112
113     my $protocol = "https://";
114
115     my $uri = C4::Context->preference('OPACBaseURL') // '';
116     if ($uri eq '') {
117         $debug and warn 'OPACBaseURL not set!';
118     }
119     if ($uri =~ /(.*):\/\/(.*)/) {
120         my $oldprotocol = $1;
121         if ($oldprotocol ne 'https') {
122             $debug
123                 and warn
124                   'Shibboleth requires OPACBaseURL to use the https protocol!';
125         }
126         $uri = $2;
127     }
128
129     my $return = $protocol . $uri;
130     return $return;
131 }
132
133 sub _get_shib_config {
134     my $config = C4::Context->config('shibboleth');
135
136     if ( !$config ) {
137         carp 'shibboleth config not defined';
138         return 0;
139     }
140
141     if ( $config->{matchpoint}
142         && defined( $config->{mapping}->{ $config->{matchpoint} }->{is} ) )
143     {
144         if ($debug) {
145             warn "koha borrower field to match: " . $config->{matchpoint};
146             warn "shibboleth attribute to match: "
147               . $config->{mapping}->{ $config->{matchpoint} }->{is};
148         }
149         return $config;
150     }
151     else {
152         if ( !$config->{matchpoint} ) {
153             carp 'shibboleth matchpoint not defined';
154         }
155         else {
156             carp 'shibboleth matchpoint not mapped';
157         }
158         return 0;
159     }
160 }
161
162 1;
163 __END__
164
165 =head1 NAME
166
167 C4::Auth_with_shibboleth
168
169 =head1 SYNOPSIS
170
171 use C4::Auth_with_shibboleth;
172
173 =head1 DESCRIPTION
174
175 This module is specific to Shibboleth authentication in koha and relies heavily upon the native shibboleth service provider package in your operating system.
176
177 =head1 CONFIGURATION
178
179 To use this type of authentication these additional packages are required:
180
181 =over
182
183 =item *
184
185 libapache2-mod-shib2
186
187 =item *
188
189 libshibsp5:amd64
190
191 =item *
192
193 shibboleth-sp2-schemas
194
195 =back
196
197 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.
198
199 But to sum up, to get shibboleth working in koha, as a minimum you will need to:
200
201 =over
202
203 =item 1.
204
205 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)
206
207 =item 2.
208
209 Swap metadata with your Identidy Provider (IdP)
210
211 =item 3.
212
213 Map their attributes to what you want to see in koha
214
215 =item 4.
216
217 Tell apache that we wish to allow koha to authenticate via shibboleth.
218
219 This is as simple as adding the below to your virtualhost config:
220
221  <Location />
222    AuthType shibboleth
223    Require shibboleth
224  </Location>
225
226 =item 5.
227
228 Configure koha to listen for shibboleth environment variables.
229
230 This is as simple as enabling B<useshibboleth> in koha-conf.xml:
231
232  <useshibboleth>1</useshibboleth>
233
234 =item 6.
235
236 Map shibboleth attributes to koha fields, and configure authentication match point in koha-conf.xml.
237
238  <shibboleth>
239    <matchpoint>userid<matchpoint> <!-- koha borrower field to match upon -->
240    <mapping>
241      <userid is="eduPersonID"></userid> <!-- koha borrower field to shibboleth attribute mapping -->
242    </mapping>
243  </shibboleth>
244
245 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.
246
247 =back
248
249 It should be as simple as that; you should now be able to login via shibboleth in the opac.
250
251 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>
252
253 =head1 FUNCTIONS
254
255 =head2 logout_shib
256
257 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.
258
259   logout_shib($query);
260
261 =head2 login_shib_url
262
263 Given a query, this will return a shibboleth login url with return code to page with given given query.
264
265   my $shibLoginURL = login_shib_url($query);
266
267 =head2 get_login_shib
268
269 Returns the shibboleth login attribute should it be found present in the http session
270
271   my $shib_login = get_login_shib();
272
273 =head2 checkpw_shib
274
275 Given a database handle and 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.
276
277   my ( $retval, $retcard, $retuserid ) = C4::Auth_with_shibboleth::checkpw_shib( $shib_login );
278
279 =head1 SEE ALSO
280
281 =cut