- $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);
- }
- my $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;
+}
+
+# Proxy CAS auth
+sub check_api_auth_cas {
+ $debug and warn "check_api_auth_cas";
+ my ($dbh, $PT, $query) = @_;
+ my $retnumber;
+ my $url = C4::Context->preference('OPACBaseURL') . $query->script_name();
+
+ 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 );
+
+ # If the PT is valid
+ if ( $r->is_success ) {
+
+ # We've got a username !
+ $debug and warn "User authenticated as: ", $r->user, "\n";
+ $debug and warn "Proxied through:\n";
+ $debug and warn " $_\n" for $r->proxies;
+
+ my $userid = $r->user;
+
+ # 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=?");
+ return $r->user;
+ $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 "Proxy Ticket authentication failed";
+ return 0;
+ }