From 0d66ebf315aa9d8279f9b1c426687cc90c5ad2a8 Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Thu, 24 May 2018 13:00:25 +0200
Subject: [PATCH] wrap content, instead of including it

---
 lib/AccountManager/App.pm                 | 113 ++++++++--------------
 templates/web/complete_challenge.tt2.html |   2 +
 templates/web/create_accounts.tt2.html    |   2 +
 templates/web/errors.tt2.html             |   2 +
 templates/web/home.tt2.html               |   3 +
 templates/web/index.tt2.html              |   2 +-
 templates/web/select_email.tt2.html       |  10 +-
 templates/web/select_sp.tt2.html          |   2 +
 8 files changed, 56 insertions(+), 80 deletions(-)

diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 062d39e..24075d7 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -70,9 +70,8 @@ sub new {
             "No mailer defined in configuration, aborting"
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ 'internal' ]
             }
         );
@@ -83,9 +82,8 @@ sub new {
             "No IDP defined in configuration, aborting"
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ 'internal' ]
             }
         );
@@ -96,9 +94,8 @@ sub new {
             "No database defined in configuration, aborting"
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors => [ 'internal' ]
             }
         );
@@ -156,9 +153,8 @@ sub run {
         ## unknown action
         $self->{logger}->error( "Unknown action '$action'");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "Unknown action '$action'" ]
             }
         );
@@ -185,7 +181,7 @@ sub respond {
         INCLUDE_PATH => $self->{configuration}->{setup}->{templates_dir} . "/web"
     });
 
-    $self->{logger}->debug("Responding with outer template '$in{template}' and inner template '$in{data}->{content}'");
+    $self->{logger}->debug("Responding with outer template '$in{template}'");
 
     binmode(STDOUT, ":utf8");
 
@@ -215,22 +211,20 @@ sub req_select_sp {
     if ($EVAL_ERROR) {
         $self->{logger}->error("Failed to load federation metadata: $EVAL_ERROR");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "internal" ]
             }
         );
     }
 
     $self->respond(
-        template => 'index.tt2.html',
+        template => 'select_sp.tt2.html',
         data     => {
             env => {
                 SCRIPT_NAME => $ENV{SCRIPT_NAME}
             },
             metadata => $metadata->parse(type => 'sp'),
-            content  => 'select_sp.tt2.html'
         }
     );
 }
@@ -241,9 +235,8 @@ sub req_select_email {
     if (! $self->{in}->{entityid}) {
         $self->{logger}->error("Missing parameter: entityid");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_entityid" ]
             }
         );
@@ -252,9 +245,8 @@ sub req_select_email {
     if ($self->{in}->{entityid} !~ $entity_id_pattern) {
         $self->{logger}->error("Incorrect parameter format: entityid");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "format_entityid" ]
             }
         );
@@ -280,9 +272,8 @@ sub req_select_email {
         if ($EVAL_ERROR) {
             $self->{logger}->error("Failed to load federation metadata: $EVAL_ERROR");
             $self->respond(
-                template => 'index.tt2.html',
+                template => 'errors.tt2.html',
                 data     => {
-                    content => 'errors.tt2.html',
                     errors  => [ "internal" ]
                 }
             );
@@ -294,9 +285,8 @@ sub req_select_email {
                 "No such SP '%s' in metadata", $self->{in}->{entityid}
             );
             $self->respond(
-                template => 'index.tt2.html',
+                template => 'errors.tt2.html',
                 data     => {
-                    content => 'errors.tt2.html',
                     errors  => [ "no_such_entity" ]
                 }
             );
@@ -312,9 +302,8 @@ sub req_select_email {
         unless ($sp->save()) {
             $self->{logger}->error("Failed to save service provider object");
             $self->respond(
-                template => 'index.tt2.html',
+                template => 'errors.tt2.html',
                 data     => {
-                    content => 'errors.tt2.html',
                     errors  => [ "internal" ]
                 }
             );
@@ -337,10 +326,9 @@ sub req_select_email {
     }
 
     $self->respond(
-        template => 'index.tt2.html',
+        template => 'select_email.tt2.html',
         data     => {
-            sp      => $sp,
-            content => 'select_email.tt2.html'
+            sp => $sp,
         }
     );
 }
@@ -351,7 +339,7 @@ sub req_complete_challenge {
     unless ($self->{in}->{entityid}) {
         $self->{logger}->error("Missing parameter entityid");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
                 errors => [ "missing_entityid" ]
             }
@@ -361,9 +349,8 @@ sub req_complete_challenge {
     unless ($self->{in}->{email}) {
         $self->{logger}->error("Missing parameter email");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_email" ]
             }
         );
@@ -377,9 +364,8 @@ sub req_complete_challenge {
     unless ($provider->load(speculative => 1)) {
         $self->{logger}->errorf("No such SP '%s' in database", $self->{in}->{entityid});
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "no_such_entity" ]
             }
         );
@@ -409,9 +395,8 @@ sub req_complete_challenge {
             $self->{in}->{email}
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "internal" ]
             }
         );
@@ -431,9 +416,8 @@ sub req_complete_challenge {
                 $old_token->id()
             );
             $self->respond(
-                template => 'index.tt2.html',
+                template => 'errors.tt2.html',
                 data     => {
-                    content => 'errors.tt2.html',
                     errors  => [ "internal" ]
                 }
             );
@@ -455,9 +439,8 @@ sub req_complete_challenge {
     unless ($token->save()) {
         $self->{logger}->error("Failed to save authentication token");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "internal" ]
             }
         );
@@ -471,9 +454,8 @@ sub req_complete_challenge {
     open(my $handle, '|-', "$sendmail -f $sender $recipient") or do {
         $self->{logger}->errorf("Unable to run sendmail executable: %s", $ERRNO);
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "mail_notification_error" ]
             }
         );
@@ -504,9 +486,8 @@ sub req_complete_challenge {
     unless ($tt2->process($template, $data, $handle)) {
         $self->{logger}->errorf("Mail notification error: %s", $tt2->error());
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "mail_notification_failure" ]
             }
         );
@@ -522,11 +503,10 @@ sub req_complete_challenge {
     );
 
     $self->respond(
-        template => 'index.tt2.html',
+        template  => 'complete_challenge.tt2.html',
         data     => {
             email    => $self->{in}->{email},
             entityid => $self->{in}->{entityid},
-            content  => 'complete_challenge.tt2.html'
         }
     );
 }
@@ -537,9 +517,8 @@ sub req_create_accounts {
     unless ($self->{in}->{entityid}) {
         $self->{logger}->error("Missing parameter entityid");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_entityid" ]
             }
         );
@@ -548,9 +527,8 @@ sub req_create_accounts {
     unless ($self->{in}->{token}) {
         $self->{logger}->error("Missing parameter token");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_token" ]
             }
         );
@@ -559,9 +537,8 @@ sub req_create_accounts {
     unless ($self->{in}->{email}) {
         $self->{logger}->error("Missing parameter email");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_email" ]
             }
         );
@@ -579,9 +556,8 @@ sub req_create_accounts {
             $self->{in}->{entityid}
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "wrong_token" ]
             }
         );
@@ -594,9 +570,8 @@ sub req_create_accounts {
             $self->{in}->{entityid}
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "wrong_token_for_sp" ]
             }
         );
@@ -634,9 +609,8 @@ sub req_create_accounts {
     unless ($download_token->save()) {
         $self->{logger}->error("Failed to save authentication token");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "internal" ]
             }
         );
@@ -668,9 +642,8 @@ sub req_create_accounts {
             $self->{in}->{entityid}
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "accounts_creation_failure" ]
             }
         );
@@ -694,9 +667,8 @@ sub req_create_accounts {
             $EVAL_ERROR
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "accounts_creation_failed" ]
             }
         );
@@ -709,13 +681,12 @@ sub req_create_accounts {
     );
 
     $self->respond(
-        template => 'index.tt2.html',
+        template => 'create_accounts.tt2.html',
         data     => {
             accounts => \@accounts,
             entityid => $self->{in}->{entityid},
             key      => $key,
             token    => $download_token->token(),
-            content  => 'create_accounts.tt2.html'
         }
     );
 }
@@ -726,9 +697,8 @@ sub req_download_accounts {
     unless ($self->{in}->{entityid}) {
         $self->{logger}->error("Missing parameter entityid");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_entityid" ]
             }
         );
@@ -737,9 +707,8 @@ sub req_download_accounts {
     unless ($self->{in}->{token}) {
         $self->{logger}->error("Missing parameter token");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_token" ]
             }
         );
@@ -748,9 +717,8 @@ sub req_download_accounts {
     unless ($self->{in}->{key}) {
         $self->{logger}->error("Missing parameter key");
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "missing_key" ]
             }
         );
@@ -767,9 +735,8 @@ sub req_download_accounts {
             $self->{in}->{token},
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "wrong_token" ]
             }
         );
@@ -782,9 +749,8 @@ sub req_download_accounts {
             $self->{in}->{entityid}
         );
         $self->respond(
-            template => 'index.tt2.html',
+            template => 'errors.tt2.html',
             data     => {
-                content => 'errors.tt2.html',
                 errors  => [ "wrong_token_for_sp" ]
             }
         );
@@ -857,10 +823,7 @@ sub req_home {
     my ($self) = @_;
 
     $self->respond(
-        template => 'index.tt2.html',
-        data     => {
-            content => 'home.tt2.html'
-        }
+        template => 'home.tt2.html',
     );
 }
 
diff --git a/templates/web/complete_challenge.tt2.html b/templates/web/complete_challenge.tt2.html
index 7f1bb34..a848339 100644
--- a/templates/web/complete_challenge.tt2.html
+++ b/templates/web/complete_challenge.tt2.html
@@ -1,3 +1,4 @@
+[% WRAPPER index.tt2.html %]
 <div class="wizard clearfix vertical">
     <div class="steps clearfix">
         <ol>
@@ -23,3 +24,4 @@
         </form>
     </div>
 </div>
+[% END %]
diff --git a/templates/web/create_accounts.tt2.html b/templates/web/create_accounts.tt2.html
index 9166655..4a5c6e5 100644
--- a/templates/web/create_accounts.tt2.html
+++ b/templates/web/create_accounts.tt2.html
@@ -1,3 +1,4 @@
+[% WRAPPER index.tt2.html %]
 <div class="grid-x align-center">
     <div class="cell shrink">
         <div class="callout success">
@@ -91,3 +92,4 @@
         <strong>[% lh.maketext("Thank you for using the eduGAIN Access Check") %]</strong>
     </div>
 </div>
+[% END %]
diff --git a/templates/web/errors.tt2.html b/templates/web/errors.tt2.html
index 59dfc88..840581f 100644
--- a/templates/web/errors.tt2.html
+++ b/templates/web/errors.tt2.html
@@ -1,3 +1,4 @@
+[% WRAPPER index.tt2.html %]
 <div class="ui-widget">
     [% FOREACH err IN errors %]
 
@@ -36,3 +37,4 @@
 
     [% lh.maketext("You can report the issue to the administrators") %] (<a href="mailto:[% app.support_email %]">[% app.support_email %]</a>).
 </div>
+[% END %]
diff --git a/templates/web/home.tt2.html b/templates/web/home.tt2.html
index d8a1ae1..c4376cb 100644
--- a/templates/web/home.tt2.html
+++ b/templates/web/home.tt2.html
@@ -1,3 +1,5 @@
+[% WRAPPER index.tt2.html %]
+
 <h2>[% lh.maketext("Objective") %]</h2>
 <p>
 [%
@@ -17,3 +19,4 @@
 <p>[% lh.maketext("To start testing your own services, start by selecting one your are administrator for.") %]</p>
 <p class="text-center"><a href="[% app.url %]?action=select_sp" class="button">[% lh.maketext("Get started") %]</a></p>
 
+[% END %]
diff --git a/templates/web/index.tt2.html b/templates/web/index.tt2.html
index ef7a88a..c213af5 100644
--- a/templates/web/index.tt2.html
+++ b/templates/web/index.tt2.html
@@ -29,7 +29,7 @@
         </header>
 
         <main class="row">
-            [% INCLUDE $content %]
+            [% content %]
         </main>
 
         <footer>
diff --git a/templates/web/select_email.tt2.html b/templates/web/select_email.tt2.html
index fb7cb51..fdbd456 100644
--- a/templates/web/select_email.tt2.html
+++ b/templates/web/select_email.tt2.html
@@ -1,3 +1,4 @@
+[% WRAPPER index.tt2.html %]
 <div class="wizard clearfix">
     <div class="steps clearfix">
         <ol>
@@ -11,20 +12,20 @@
         <h2>[% lh.maketext("Select your email address") %]</h2>
         <p>[% lh.maketext("Before you can create test accounts at this Identity Provider, we need to ensure you are a legitimate administrator of '[_1]'.", sp.displayname) %]</p>
 
-[% IF sp.contacts.defined %]
+    [% IF sp.contacts.defined %]
         <form action="[% env.SCRIPT_NAME %]" method="get">
             <p>[% lh.maketext("Select the email address where an email challenge can be sent to validate your identity:") %]</p>
-    [% FOREACH email IN sp.contacts.sort %]
+        [% FOREACH email IN sp.contacts.sort %]
             <input id="[% email %]" name="email" value="[% email %]" type="radio" class="required"/>
             <label for="[% email %]">[% email %]</label>
             <br/>
-    [% END %]
+        [% END %]
             <p>[% lh.maketext("Those email addresses have been extracted from your service metadata.") %]</p>
             <input type="hidden" name="entityid" value="[% sp.entityid %]"/>
             <button class="button" type="submit" name="action" value="select_sp">[% lh.maketext("Previous") %]</button>
             <button class="button" type="submit" name="action" value="complete_challenge">[% lh.maketext("Next") %]</button>
         </form>
-[% ELSE %]
+    [% ELSE %]
         <p>
         [% lh.maketext("No ContactPerson element could be found in your service metadata, therefore we are unable to provide test accounts for this service.") %]
         [% lh.maketext("Please contact your federation administrators to add needed information to the metadata.") %]
@@ -32,3 +33,4 @@
 [% END %]
     </div>
 </div>
+[% END %]
diff --git a/templates/web/select_sp.tt2.html b/templates/web/select_sp.tt2.html
index ec022c1..d39a6ef 100644
--- a/templates/web/select_sp.tt2.html
+++ b/templates/web/select_sp.tt2.html
@@ -1,3 +1,4 @@
+[% WRAPPER index.tt2.html %]
 <div class="wizard clearfix">
     <div class="steps clearfix">
         <ol>
@@ -163,3 +164,4 @@ $( document ).ready(function() {
     $( "#entityid" ).combobox();
 });
 </script>
+[% END %]
-- 
GitLab