Template Toolkit for RemoteAuth
authorJeff Davis <jeff.davis@bc.libraries.coop>
Thu, 21 Nov 2019 22:27:00 +0000 (14:27 -0800)
committerGalen Charlton <gmc@equinoxinitiative.org>
Tue, 25 Aug 2020 20:45:31 +0000 (16:45 -0400)
With this commit, RemoteAuth handlers can now use TT2 templates to
manage the format and content of responses.  This will make it easier
for admins to control what patron information is included in a
successful auth response, among other things.

TT2 support is provided by a new OpenILS::WWW::RemoteAuth::Template Perl
module.  (EGWeb has too much overhead and is not well-suited to this use
case.)

Signed-off-by: Jeff Davis <jeff.davis@bc.libraries.coop>
Signed-off-by: Jane Sandberg <sandbej@linnbenton.edu>
Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>

Open-ILS/src/perlmods/lib/OpenILS/WWW/RemoteAuth/Template.pm [new file with mode: 0644]

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/RemoteAuth/Template.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/RemoteAuth/Template.pm
new file mode 100644 (file)
index 0000000..4dd81eb
--- /dev/null
@@ -0,0 +1,80 @@
+# Copyright (C) 2019 BC Libraries Cooperative
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+# 
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+# ====================================================================== 
+# Template Toolkit processing for RemoteAuth
+# ====================================================================== 
+
+package OpenILS::WWW::RemoteAuth::Template;
+use strict; use warnings;
+
+use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN AUTH_REQUIRED HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
+use List::MoreUtils qw/uniq/;
+use Template;
+
+sub new {
+    my( $class, $args ) = @_;
+    $args ||= {};
+    $class = ref $class || $class;
+    return bless($args, $class);
+}
+
+sub process {
+    my ($self, $tname, $ctx, $r) = @_;
+
+    if (!$tname) {
+        $r->log->warn('RemoteAuth template name not defined');
+        return Apache2::Const::DECLINED;
+    }
+
+    my $template;
+    my @template_paths = uniq $r->dir_config->get('OILSRemoteAuthTemplatePath');
+    for my $tpath (reverse @template_paths) {
+        if (-r "$tpath/$tname.tt2") {
+            $template = "$tname.tt2";
+            $ctx->{template_path} = $tpath;
+            last;
+        }
+    }
+    if (!$template) {
+        $r->log->warn("RemoteAuth template $tname not found");
+        return Apache2::Const::DECLINED;
+    }
+
+    $ctx->{locale} = $r->dir_config('OILSRemoteAuthLocale') || 'en_us';
+
+    # create template processor
+    my $tt = Template->new({
+        ENCODING => 'utf-8',
+        INCLUDE_PATH => $ctx->{template_path}
+    });
+
+    if (!$tt) {
+        $r->log->error("Error creating RemoteAuth template processor: $@");
+        return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
+    }   
+
+    # process template
+    unless($tt->process($template, { ctx => $ctx }, $r)) {
+        $r->log->warn('RemoteAuth template error: ' . $tt->error);
+        return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR;
+    }
+
+    return Apache2::Const::OK;
+}
+
+1;
+