[Koha-patches] [PATCH 04/11] Adds the ability to authenticate against multiple CAS servers

paul.poulain at biblibre.com paul.poulain at biblibre.com
Wed Jan 19 21:24:36 CET 2011


From: Matthias Meusburger <matthias.meusburger at biblibre.com>

---
 C4/Auth.pm                    |    7 ++-
 C4/Auth_cas_servers.yaml.orig |   12 ++++
 C4/Auth_with_cas.pm           |  127 ++++++++++++++++++++++++++--------------
 3 files changed, 99 insertions(+), 47 deletions(-)
 create mode 100644 C4/Auth_cas_servers.yaml.orig

diff --git a/C4/Auth.pm b/C4/Auth.pm
index 41fc760..14caf3f 100644
--- a/C4/Auth.pm
+++ b/C4/Auth.pm
@@ -613,6 +613,10 @@ sub checkauth {
     my ( $userid, $cookie, $sessionID, $flags, $barshelves, $pubshelves );
     my $logout = $query->param('logout.x');
 
+    # This parameter is the name of the CAS server we want to authenticate against,
+    # when using authentication against multiple CAS servers, as configured in Auth_cas_servers.yaml
+    my $casparam = $query->param('cas');
+
     if ( $userid = $ENV{'REMOTE_USER'} ) {
         # Using Basic Authentication, no cookies required
         $cookie = $query->cookie(
@@ -947,10 +951,9 @@ sub checkauth {
 
     if ($cas) { 
 	$template->param(
-	    casServerUrl    => login_cas_url(),
+        casServerUrl    => login_cas_url($query),
 	    invalidCasLogin => $info{'invalidCasLogin'}
 	);
-    }
 
     my $self_url = $query->url( -absolute => 1 );
     $template->param(
diff --git a/C4/Auth_cas_servers.yaml.orig b/C4/Auth_cas_servers.yaml.orig
new file mode 100644
index 0000000..8ad171e
--- /dev/null
+++ b/C4/Auth_cas_servers.yaml.orig
@@ -0,0 +1,12 @@
+# This file is used for authenticating against multiple CAS servers
+# If the file Auth_cas_servers.yaml exists, then the casServerUrl syspref will be ignored
+
+# If you have to authenticate against only one CAS server, which is usually the case,
+# don't use this file, but the casServerUrl syspref instead
+
+default: ServerName
+---
+  ServerName: "https://example.com/cas"
+  OtherServerName: "https://example.org/cas"
+  ThirdServerName: "https://example.edu/cas"
+
diff --git a/C4/Auth_with_cas.pm b/C4/Auth_with_cas.pm
index e7ad72b..6c69fb5 100644
--- a/C4/Auth_with_cas.pm
+++ b/C4/Auth_with_cas.pm
@@ -25,6 +25,7 @@ use C4::Context;
 use C4::Utils qw( :all );
 use Authen::CAS::Client;
 use CGI;
+use FindBin;
 
 
 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug);
@@ -36,29 +37,57 @@ BEGIN {
 	@ISA    = qw(Exporter);
 	@EXPORT = qw(check_api_auth_cas checkpw_cas login_cas logout_cas login_cas_url);
 }
-
-
-my $context = C4::Context->new() 	or die 'C4::Context->new failed';
-my $casserver = C4::Context->preference('casServerUrl');
+my $context = C4::Context->new() or die 'C4::Context->new failed';
+my $defaultcasserver;
+my $casservers;
+
+
+# If there's a configuration for multiple cas servers, then we get it
+if (-e qq($FindBin::Bin/../C4/Auth_cas_servers.yaml)) {
+    ($defaultcasserver, $casservers) = YAML::LoadFile(qq($FindBin::Bin/../C4/Auth_cas_servers.yaml));
+    $defaultcasserver = $defaultcasserver->{'default'};
+} else {
+# Else, we fall back to casServerUrl syspref
+    $defaultcasserver = 'default';
+    $casservers = { 'default' => C4::Context->preference('casServerUrl') };
+}
 
 # Logout from CAS
 sub logout_cas {
     my ($query) = @_;
-    my $cas = Authen::CAS::Client->new($casserver);
-    print $query->redirect($cas->logout_url(url => $ENV{'SCRIPT_URI'}));
+    my $uri = $ENV{'SCRIPT_URI'};
+    my $casparam = $query->param('cas');
+    # FIXME: This should be more generic and handle whatever parameters there might be
+    $uri .= "?cas=" . $casparam if (defined $casparam);
+    $casparam = $defaultcasserver if (not defined $casparam);
+    my $cas = Authen::CAS::Client->new($casservers->{$casparam});
+    print $query->redirect( $cas->logout_url($uri));
 }
 
 # Login to CAS
 sub login_cas {
     my ($query) = @_;
-    my $cas = Authen::CAS::Client->new($casserver);
-    print $query->redirect($cas->login_url($ENV{'SCRIPT_URI'})); 
+    my $uri = $ENV{'SCRIPT_URI'};
+    my $casparam = $query->param('cas');
+    # FIXME: This should be more generic and handle whatever parameters there might be
+    $uri .= "?cas=" . $casparam if (defined $casparam);
+    warn $defaultcasserver;
+    $casparam = $defaultcasserver if (not defined $casparam);
+    my $cas = Authen::CAS::Client->new($casservers->{$casparam});
+    print $query->redirect( $cas->login_url($uri));
 }
 
 # Returns CAS login URL with callback to the requesting URL
 sub login_cas_url {
-    my $cas = Authen::CAS::Client->new($casserver);
-    return $cas->login_url($ENV{'SCRIPT_URI'});
+    my ($query) = @_;
+    my $uri = $ENV{'SCRIPT_URI'};
+    my $casparam = $query->param('cas');
+    # FIXME: This should be more generic and handle whatever parameters there might be
+    $uri .= "?cas=" . $casparam if (defined $casparam);
+    $casparam = $defaultcasserver if (not defined $casparam);
+    warn $defaultcasserver;
+    my $cas = Authen::CAS::Client->new($casservers->{$casparam});
+    return $cas->login_url($uri);
 }
 
 # Checks for password correctness
@@ -67,42 +96,47 @@ sub checkpw_cas {
     $debug and warn "checkpw_cas";
     my ($dbh, $ticket, $query) = @_;
     my $retnumber;
-    my $cas = Authen::CAS::Client->new($casserver);
+    my $uri = $ENV{'SCRIPT_URI'};
+    my $casparam = $query->param('cas');
+    # FIXME: This should be more generic and handle whatever parameters there might be
+    $uri .= "?cas=" . $casparam if (defined $casparam);
+    $casparam = $defaultcasserver if (not defined $casparam);
+    my $cas = Authen::CAS::Client->new($casservers->{$casparam});
 
     # If we got a ticket
     if ($ticket) {
-	$debug and warn "Got ticket : $ticket";
-	
-	# We try to validate it
-	my $val = $cas->service_validate($ENV{'SCRIPT_URI'}, $ticket);
-	
-	# If it's valid
-	if( $val->is_success() ) {
-
-	    my $userid = $val->user();
-	    $debug and warn "User CAS authenticated as: $userid";
-
-	    # Does it match one of our users ?
-    	    my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
-    	    $sth->execute($userid);
-    	    if ( $sth->rows ) {
-		$retnumber = $sth->fetchrow;
-		return (1, $retnumber, $userid);
-	    }
-	    $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
-	    $sth->execute($userid);
-	    if ( $sth->rows ) {
-	    	$retnumber = $sth->fetchrow;
-		return (1, $retnumber, $userid);
-	    }
-	    
-	    # If we reach this point, then the user is a valid CAS user, but not a Koha user
-	    $debug and warn "User $userid is not a valid Koha user";
-
-    	} else {
-    	    $debug and warn "Invalid session ticket : $ticket";
-    	    return 0;
-	}
+        $debug and warn "Got ticket : $ticket";
+
+        # We try to validate it
+        my $val = $cas->service_validate($uri, $ticket );
+
+        # If it's valid
+        if ( $val->is_success() ) {
+
+            my $userid = $val->user();
+            $debug and warn "User CAS authenticated as: $userid";
+
+            # Does it match one of our users ?
+            my $sth = $dbh->prepare("select cardnumber from borrowers where userid=?");
+            $sth->execute($userid);
+            if ( $sth->rows ) {
+                $retnumber = $sth->fetchrow;
+                return ( 1, $retnumber, $userid );
+            }
+            $sth = $dbh->prepare("select userid from borrowers where cardnumber=?");
+            $sth->execute($userid);
+            if ( $sth->rows ) {
+                $retnumber = $sth->fetchrow;
+                return ( 1, $retnumber, $userid );
+            }
+
+            # If we reach this point, then the user is a valid CAS user, but not a Koha user
+            $debug and warn "User $userid is not a valid Koha user";
+
+        } else {
+            $debug and warn "Invalid session ticket : $ticket";
+            return 0;
+        }
     }
     return 0;
 }
@@ -113,8 +147,11 @@ sub check_api_auth_cas {
     my ($dbh, $PT, $query) = @_;
     my $retnumber;
     my $url = $query->url();
-    my $cas = Authen::CAS::Client->new($casserver);
-    
+
+    my $casparam = $query->param('cas');
+    $casparam = $defaultcasserver if (not defined $casparam);
+    my $cas = Authen::CAS::Client->new($casservers->{$casparam});
+
     # If we have a Proxy Ticket
     if ($PT) {
         my $r = $cas->proxy_validate( $url, $PT );
-- 
1.7.1



More information about the Koha-patches mailing list