+=head2 logout_if_required
+
+ If using CAS, this subroutine will trigger single-signout of the CAS server.
+
+=cut
+
+sub logout_if_required {
+ my ( $query ) = @_;
+ # Check we havent been hit by a logout call
+ my $xml = $query->param('logoutRequest');
+ return 0 unless $xml;
+
+ my $dom = XML::LibXML->load_xml(string => $xml);
+ my $ticket;
+ foreach my $node ($dom->findnodes('/samlp:LogoutRequest')){
+ # We got a cas single logout request from a cas server;
+ $ticket = $node->findvalue('./samlp:SessionIndex');
+ }
+
+ return 0 unless $ticket;
+
+ # We've been called as part of the single logout destroy the session associated with the cas ticket
+ my $params = C4::Auth::_get_session_params();
+ my $success = CGI::Session->find( $params->{dsn}, sub {delete_cas_session(@_, $ticket)}, $params->{dsn_args} );
+
+ sub delete_cas_session {
+ my $session = shift;
+ my $ticket = shift;
+ if ($session->param('cas_ticket') && $session->param('cas_ticket') eq $ticket ) {
+ $session->delete;
+ $session->flush;
+ }
+ }
+
+ print $query->header;
+ exit;
+}
+