From a93650be26cfcd0118c8c816955a2cb2e8724acc Mon Sep 17 00:00:00 2001
From: Dan Wells <dbw2@calvin.edu>
Date: Fri, 15 Jun 2012 16:11:57 -0400
Subject: [PATCH] Extend AuthProxy.pm Support to TPAC

AuthProxy-enabled logins (e.g. LDAP) were not yet wired into TPAC.
This adds TPAC support equivalent to that in JSPAC.

Signed-off-by: Dan Wells <dbw2@calvin.edu>
Signed-off-by: Dan Scott <dan@coffeecode.net>
---
 .../src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm    | 35 ++++++++++++++++------
 1 file changed, 26 insertions(+), 9 deletions(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
index 7154f666ec..af25ecd204 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm
@@ -349,14 +349,16 @@ sub load_login {
     # initial log form only
     return Apache2::Const::OK unless $username and $password;
 
-	my $seed = $U->simplereq(
-        'open-ils.auth', 
-		'open-ils.auth.authenticate.init', $username);
+    my $auth_proxy_enabled = 0; # default false
+    try { # if the service is not running, just let this fail silently
+        $auth_proxy_enabled = $U->simplereq(
+            'open-ils.auth_proxy',
+            'open-ils.auth_proxy.enabled');
+    } catch Error with {};
 
     my $args = {	
-        username => $username, 
-        password => md5_hex($seed . md5_hex($password)), 
         type => ($persist) ? 'persist' : 'opac',
+        org => $org_unit,
         agent => 'opac'
     };
 
@@ -365,11 +367,26 @@ sub load_login {
     # To avoid surprises, default to "Barcodes start with digits"
     $bc_regex = '^\d' unless $bc_regex;
 
-    $args->{barcode} = delete $args->{username} 
-        if $bc_regex and ($username =~ /$bc_regex/);
+    if ($bc_regex and ($username =~ /$bc_regex/)) {
+        $args->{barcode} = $username;
+    } else {
+        $args->{username} = $username;
+    }
 
-	my $response = $U->simplereq(
-        'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
+    my $response;
+    if (!$auth_proxy_enabled) {
+        my $seed = $U->simplereq(
+            'open-ils.auth',
+            'open-ils.auth.authenticate.init', $username);
+        $args->{password} = md5_hex($seed . md5_hex($password));
+	    $response = $U->simplereq(
+            'open-ils.auth', 'open-ils.auth.authenticate.complete', $args);
+    } else {
+        $args->{password} = $password;
+	    $response = $U->simplereq(
+            'open-ils.auth_proxy',
+            'open-ils.auth_proxy.login', $args);
+    }
 
     if($U->event_code($response)) { 
         # login failed, report the reason to the template
-- 
2.11.0