From 9b875e7f6fc7f3d4528c2652cd002149c4de349b Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Mon, 10 Mar 2014 13:04:19 -0400
Subject: [PATCH] Added login page for TPS UI.

The TPS UI has been modified to provide an unprotected front page.
The main TPS UI has been moved into a protected area. The front
page provides a login button which when clicked will ask the user
to authenticate with the client certificate. If the authentication
is successful, the main page will appear. There is also a logout
link on the upper right corner of the main page. When clicked it
will destroy both the client and server sessions.

Ticket #846
---
 base/server/share/webapps/pki/js/pki-ui.js         |  9 ++-
 base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml | 13 ++++
 base/tps-tomcat/shared/webapps/tps/index.html      | 86 ++++++----------------
 base/tps-tomcat/shared/webapps/tps/js/account.js   | 37 ++++++++++
 .../shared/webapps/tps/{ => ui}/activities.html    |  0
 .../webapps/tps/{ => ui}/authenticators.html       |  0
 .../shared/webapps/tps/{ => ui}/certs.html         |  0
 .../shared/webapps/tps/{ => ui}/connections.html   |  0
 .../shared/webapps/tps/{ => ui}/groups.html        |  0
 .../shared/webapps/tps/{ => ui}/index.html         | 15 +++-
 .../shared/webapps/tps/{ => ui}/profiles.html      |  0
 .../shared/webapps/tps/{ => ui}/selftests.html     |  0
 .../shared/webapps/tps/{ => ui}/tokens.html        |  0
 .../shared/webapps/tps/{ => ui}/users.html         |  0
 14 files changed, 94 insertions(+), 66 deletions(-)
 create mode 100644 base/tps-tomcat/shared/webapps/tps/js/account.js
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/activities.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/authenticators.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/certs.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/connections.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/groups.html (100%)
 copy base/tps-tomcat/shared/webapps/tps/{ => ui}/index.html (90%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/profiles.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/selftests.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/tokens.html (100%)
 rename base/tps-tomcat/shared/webapps/tps/{ => ui}/users.html (100%)

diff --git a/base/server/share/webapps/pki/js/pki-ui.js b/base/server/share/webapps/pki/js/pki-ui.js
index 7626d5082d52da4c77da13ba2f71baccfea89ed5..73ba63b26f2f4b555e3c95d29922d52f3954204b 100644
--- a/base/server/share/webapps/pki/js/pki-ui.js
+++ b/base/server/share/webapps/pki/js/pki-ui.js
@@ -132,8 +132,13 @@ var Navigation = Backbone.View.extend({
             var link = $("a", li);
             var url = link.attr("href");
             link.click(function(e) {
-                // get page name
-                if (url.charAt(0) == "#" && url.length > 1) {
+                if (url == "#logout") {
+                    if (options.logout) {
+                        options.logout.call(self);
+                    }
+
+                } else if (url.charAt(0) == "#" && url.length > 1) {
+                    // get page name
                     var name = url.substring(1);
                     self.load(name);
                 }
diff --git a/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml b/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
index 11964a00ba5ebd98d1d1e67d400d037809cb3b49..17aaf511ae56b2935e0144fa374855872846343a 100644
--- a/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
+++ b/base/tps-tomcat/shared/webapps/tps/WEB-INF/web.xml
@@ -286,6 +286,19 @@
         </user-data-constraint>
     </security-constraint>
 
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>UI</web-resource-name>
+            <url-pattern>/ui/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>*</role-name>
+        </auth-constraint>
+        <user-data-constraint>
+            <transport-guarantee>CONFIDENTIAL</transport-guarantee>
+        </user-data-constraint>
+    </security-constraint>
+
     <login-config>
         <realm-name>Token Processing Service</realm-name>
     </login-config>
diff --git a/base/tps-tomcat/shared/webapps/tps/index.html b/base/tps-tomcat/shared/webapps/tps/index.html
index 790ab1489ae3a09528ee7c89a50483ea1ec57eb9..ce0756b43f8278fb96e79c0b66f3d9e164b15728 100644
--- a/base/tps-tomcat/shared/webapps/tps/index.html
+++ b/base/tps-tomcat/shared/webapps/tps/index.html
@@ -19,38 +19,22 @@
 <head>
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <link href="/pki/css/rcue.css" rel="stylesheet" media="screen, print">
-    <link href="/pki/css/pki-ui.css" rel="stylesheet" type="text/css">
     <script src="/pki/js/jquery.js"></script>
-    <script src="/pki/js/underscore.js"></script>
-    <script src="/pki/js/backbone.js"></script>
     <script src="/pki/js/bootstrap.js"></script>
-    <script src="/pki/js/pki-ui.js"></script>
-    <script src="/tps/js/activity.js"></script>
-    <script src="/tps/js/authenticator.js"></script>
-    <script src="/tps/js/cert.js"></script>
-    <script src="/tps/js/connection.js"></script>
-    <script src="/tps/js/group.js"></script>
-    <script src="/tps/js/profile.js"></script>
-    <script src="/tps/js/selftest.js"></script>
-    <script src="/tps/js/token.js"></script>
-    <script src="/tps/js/user.js"></script>
+    <script src="/tps/js/account.js"></script>
     <script>
 $(function() {
-    new Navigation({
-        el: $("#navigation"),
-        content: $("#content"),
-        pages: {
-            activities: new ActivityPage({ url: "activities.html" }),
-            authenticators: new AuthenticatorPage({ url: "authenticators.html" }),
-            certs: new CertificatePage({ url: "certs.html" }),
-            connections: new ConnectionPage({ url: "connections.html" }),
-            groups: new GroupPage({ url: "groups.html" }),
-            profiles: new ProfilePage({ url: "profiles.html" }),
-            selftests: new SelfTestPage({ url: "selftests.html" }),
-            tokens: new TokenPage({ url: "tokens.html" }),
-            users: new UserPage({ url: "users.html" })
-        },
-        homePage: "tokens"
+    var account = new Account();
+    $("form").submit(function(e) {
+        account.login({
+            success: function() {
+                window.location.href = "/tps/ui";
+            },
+            error: function() {
+                window.crypto.logout();
+            }
+        });
+        e.preventDefault();
     });
 });
     </script>
@@ -69,44 +53,20 @@ $(function() {
         <b>Token Processing Service</b>
     </a>
 </div>
-<div class="collapse navbar-collapse navbar-collapse-1">
-    <ul class="nav navbar-nav navbar-utility">
-    <li><a href="#">Status</a></li>
-    <li class="dropdown">
-        <a href="#" class="dropdown-toggle" data-toggle="dropdown">
-        <span class="rcueicon rcueicon-user"></span>
-        Administrator<b class="caret"></b>
-        </a>
-        <ul class="dropdown-menu">
-        <li><a href="settings.html">Settings</a></li>
-        <li class="divider"></li>
-        <li><a href="logout.html">Logout</a></li>
-        </ul>
-    </li>
-    </ul>
-    <ul class="nav navbar-nav navbar-primary">
-    <li><a href="#tokens">Tokens</a></li>
-    <li><a href="#certs">Certificates</a></li>
-    <li><a href="#activities">Activities</a></li>
-    <li><a href="#profiles">Profiles</a></li>
-    <li><a href="#selftests">SelfTests</a></li>
-    <li><a href="#users">Users</a></li>
-    <li><a href="#groups">Groups</a></li>
-    <li class="dropdown context">
-      <a href="#" class="dropdown-toggle" data-toggle="dropdown">
-        Configuration
-        <b class="caret"></b>
-      </a>
-      <ul class="dropdown-menu">
-        <li><a href="#authenticators">Authenticators</a></li>
-        <li><a href="#connections">Connections</a></li>
-      </ul>
-    </li>
-    </ul>
-</div>
 </nav>
 
 <div id="content">
+
+<form action="/tps/ui">
+<div class="col-sm-5 col-md-6 col-lg-7 details">
+<p><strong>Welcome to the Dogtag Token Processing System 10.2</strong>
+<p>The Token Processing System (TPS) is the conduit between the user-centered Enterprise Security Client,
+which interacts with the tokens, and the Certificate System backend subsystems, such as the Certificate Manager.
+</p>
+<button type="submit" class="btn btn-primary btn-lg" tabindex="4">Log In</button>
+</div>
+</form>
+
 </div>
 
 </body>
diff --git a/base/tps-tomcat/shared/webapps/tps/js/account.js b/base/tps-tomcat/shared/webapps/tps/js/account.js
new file mode 100644
index 0000000000000000000000000000000000000000..50a8cab70f446139947f6eeaf181b8155d560853
--- /dev/null
+++ b/base/tps-tomcat/shared/webapps/tps/js/account.js
@@ -0,0 +1,37 @@
+/* --- BEGIN COPYRIGHT BLOCK ---
+ * 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; version 2 of the License.
+ *
+ * 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.
+ *
+ * Copyright (C) 2013 Red Hat, Inc.
+ * All rights reserved.
+ * --- END COPYRIGHT BLOCK ---
+ *
+ * @author Endi S. Dewata
+ */
+
+function Account() {
+
+    this.url = "/tps/rest/account";
+
+    this.login = function(options) {
+        var jqxhr = $.get(this.url + "/login");
+        jqxhr.done(options.success);
+        jqxhr.fail(options.error);
+    };
+
+    this.logout = function(options) {
+        var jqxhr = $.get(this.url + "/logout");
+        jqxhr.done(options.success);
+        jqxhr.fail(options.error);
+    };
+};
\ No newline at end of file
diff --git a/base/tps-tomcat/shared/webapps/tps/activities.html b/base/tps-tomcat/shared/webapps/tps/ui/activities.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/activities.html
rename to base/tps-tomcat/shared/webapps/tps/ui/activities.html
diff --git a/base/tps-tomcat/shared/webapps/tps/authenticators.html b/base/tps-tomcat/shared/webapps/tps/ui/authenticators.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/authenticators.html
rename to base/tps-tomcat/shared/webapps/tps/ui/authenticators.html
diff --git a/base/tps-tomcat/shared/webapps/tps/certs.html b/base/tps-tomcat/shared/webapps/tps/ui/certs.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/certs.html
rename to base/tps-tomcat/shared/webapps/tps/ui/certs.html
diff --git a/base/tps-tomcat/shared/webapps/tps/connections.html b/base/tps-tomcat/shared/webapps/tps/ui/connections.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/connections.html
rename to base/tps-tomcat/shared/webapps/tps/ui/connections.html
diff --git a/base/tps-tomcat/shared/webapps/tps/groups.html b/base/tps-tomcat/shared/webapps/tps/ui/groups.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/groups.html
rename to base/tps-tomcat/shared/webapps/tps/ui/groups.html
diff --git a/base/tps-tomcat/shared/webapps/tps/index.html b/base/tps-tomcat/shared/webapps/tps/ui/index.html
similarity index 90%
copy from base/tps-tomcat/shared/webapps/tps/index.html
copy to base/tps-tomcat/shared/webapps/tps/ui/index.html
index 790ab1489ae3a09528ee7c89a50483ea1ec57eb9..b086eacd0d111e771faf04df21eec9e888035a6c 100644
--- a/base/tps-tomcat/shared/webapps/tps/index.html
+++ b/base/tps-tomcat/shared/webapps/tps/ui/index.html
@@ -25,6 +25,7 @@
     <script src="/pki/js/backbone.js"></script>
     <script src="/pki/js/bootstrap.js"></script>
     <script src="/pki/js/pki-ui.js"></script>
+    <script src="/tps/js/account.js"></script>
     <script src="/tps/js/activity.js"></script>
     <script src="/tps/js/authenticator.js"></script>
     <script src="/tps/js/cert.js"></script>
@@ -36,6 +37,7 @@
     <script src="/tps/js/user.js"></script>
     <script>
 $(function() {
+    var account = new Account();
     new Navigation({
         el: $("#navigation"),
         content: $("#content"),
@@ -50,6 +52,17 @@ $(function() {
             tokens: new TokenPage({ url: "tokens.html" }),
             users: new UserPage({ url: "users.html" })
         },
+        logout: function() {
+            account.logout({
+                success: function() {
+                    window.crypto.logout();
+                    window.location.href = "/tps";
+                },
+                error: function() {
+                    alert("Logout failed");
+                }
+            });
+        },
         homePage: "tokens"
     });
 });
@@ -80,7 +93,7 @@ $(function() {
         <ul class="dropdown-menu">
         <li><a href="settings.html">Settings</a></li>
         <li class="divider"></li>
-        <li><a href="logout.html">Logout</a></li>
+        <li><a href="#logout">Logout</a></li>
         </ul>
     </li>
     </ul>
diff --git a/base/tps-tomcat/shared/webapps/tps/profiles.html b/base/tps-tomcat/shared/webapps/tps/ui/profiles.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/profiles.html
rename to base/tps-tomcat/shared/webapps/tps/ui/profiles.html
diff --git a/base/tps-tomcat/shared/webapps/tps/selftests.html b/base/tps-tomcat/shared/webapps/tps/ui/selftests.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/selftests.html
rename to base/tps-tomcat/shared/webapps/tps/ui/selftests.html
diff --git a/base/tps-tomcat/shared/webapps/tps/tokens.html b/base/tps-tomcat/shared/webapps/tps/ui/tokens.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/tokens.html
rename to base/tps-tomcat/shared/webapps/tps/ui/tokens.html
diff --git a/base/tps-tomcat/shared/webapps/tps/users.html b/base/tps-tomcat/shared/webapps/tps/ui/users.html
similarity index 100%
rename from base/tps-tomcat/shared/webapps/tps/users.html
rename to base/tps-tomcat/shared/webapps/tps/ui/users.html
-- 
1.8.4.2

