CS UI Widgets Information

Shows information about a CS UI Widgets authenticated session.

  1. // ==UserScript==
  2. // @name CS UI Widgets Information
  3. // @namespace mailto:fprantl@opentext.com
  4. // @version 0.3
  5. // @description Shows information about a CS UI Widgets authenticated session.
  6. // It makes a call to the CS REST API using the CS UI Widgets to find
  7. // out the name of the Personal Volume of the authenticated user which
  8. // usually includes the user name. Before you execute it, authenticate
  9. // the CS UI Widgets on your page, otherwise you get the login dialog.
  10. // @match *://*/*
  11. // @copyright (c) 2013-2014 Ferdinand Prantl, OpenText GmbH
  12. // ==/UserScript==
  13.  
  14. if (typeof csui !== "undefined") {
  15. var modules = csui.require.defined("lib/jquery") &&
  16. ["lib/jquery", "util/connector", "model/volume"] ||
  17. ["csui/lib/jquery", "csui/util/connector", "csui/model/volume"];
  18. csui.require(modules, function($, Connector, VolumeModel) {
  19. // Declare the place with all CS UI Widgets information, the definition list
  20. // with entries for every stored authenticated session and a progress indicator.
  21. var place, list, progress;
  22. // Adds information about a CS UI Widgets session identified by the CS
  23. // REST API URL to the definition list
  24. function addSessionInformation(url, last) {
  25. // Create a connector object for the REST API URL; the supportPath
  26. // will not be used in this scenario; just not leave it undefined
  27. var connector = new Connector({
  28. connection: { url: url, supportPath: "/img" }
  29. }),
  30. // Declare an object referring to the personal volume of the current user
  31. personalVolume = new VolumeModel(
  32. { type: 142 }, { connector: connector }
  33. );
  34. // Fetch the personal volume information; authentication will be performed
  35. // on demand and if there is already an authenticated session in the session
  36. // storage of the web browser, there will be no login prompt.
  37. personalVolume.fetch({
  38. success: function() {
  39. console.log("Personal volume on " + url + ":");
  40. console.log(personalVolume);
  41. // Append a new entry to the definition list with the session information
  42. // containing just the name of the personal volume for now; this name
  43. // usually contains the user name and serves as a trick how to learn it
  44. // until there is a REST API request available which provides information
  45. // about the authenticated user.
  46. $("<dt>").text("URL: " + url).appendTo(list);
  47. $("<dd>").text("Personal Volume: " +
  48. personalVolume.get("name")).appendTo(list);
  49. if (last) {
  50. progress.hide();
  51. }
  52. },
  53. error: function(error) {
  54. // Append the message to the definition list with the session information
  55. $("<dt>").text("URL: " + url).appendTo(list);
  56. $("<dd>").text(error.toString()).appendTo(list);
  57. if (last) {
  58. progress.hide();
  59. }
  60. }
  61. });
  62. }
  63. // Divide the information from the page content above
  64. $("<hr>").appendTo(document.body);
  65. // Encapsulate the CS UI Widgets information in a jQuery UI styled element
  66. place = $("<div>", { "class": "ui-widget" }).appendTo(document.body);
  67. $("<h3>").text("CS UI Widgets").appendTo(place);
  68. // Add a button tu refresh the information displayed below
  69. $("<button>").text("Refresh authentication information")
  70. .click(function() {
  71. // Declare the map of stored sessions and the array of CS REST API URLs
  72. var sessions, urls;
  73. // Clear previously displayed information in the definition list
  74. list.empty();
  75. // The base authentiactor object stores the session in the session storage
  76. // as a string with a JSON object, where keyas are the CS REST API URLs
  77. sessions = sessionStorage.getItem("csui/util/authenticator") ||
  78. // Try older versions of the authenticastors too.
  79. sessionStorage.getItem("util/authenticator") ||
  80. sessionStorage.getItem("util/otcsauthenticator");
  81. if (sessions) {
  82. // Parsing the stored string should never fail, but just in case
  83. try {
  84. sessions = JSON.parse(sessions);
  85. } catch(error) {
  86. console.log("Reading the stored authenticated session failed:");
  87. console.log(error);
  88. }
  89. // Get the REST API URLs and add en entry to the definition list for each one
  90. urls = Object.keys(sessions);
  91. progress.show();
  92. urls.forEach(function(url, index) {
  93. // Hide the progress indication when processing the last URL; it is not
  94. // shown and hidden for every URLs, because they are processed in parallel.
  95. addSessionInformation(url, index == urls.length - 1);
  96. });
  97. }
  98. // If there was an error when parsing the stored sessions or no stored session
  99. // have been found add an entry to the definition list with a message only
  100. if (!urls) {
  101. $("<dt>").text("Reading the stored authenticated session failed.").appendTo(list);
  102. } else if (!urls.length) {
  103. $("<dt>").text("No authenticated session has been found.").appendTo(list);
  104. }
  105. }).appendTo(place);
  106. // Add a button to clear all stored authenticated CS UI Widgets sessions
  107. $("<button>").text("Clear authenticated sessions")
  108. .click(function() {
  109. if (confirm("Do you really want to clear all authenticated CS UI Widgets sessions?\n" +
  110. "New authentication will be performed on the next page load.")) {
  111. // Clear the session storage of the base authenticator
  112. if (sessionStorage.getItem("csui/util/authenticator")) {
  113. sessionStorage.removeItem("csui/util/authenticator");
  114. }
  115. // Deal with older versions of the authenticastors too.
  116. if (sessionStorage.getItem("util/authenticator")) {
  117. sessionStorage.removeItem("util/authenticator");
  118. }
  119. if (sessionStorage.getItem("util/otcsauthenticator")) {
  120. sessionStorage.removeItem("util/otcsauthenticator");
  121. }
  122. // Clear the displated definition list
  123. list.empty();
  124. $("<dt>").text("Authenticated sessions have been cleared.").appendTo(list);
  125. // You will usually want to reload the page to retry the authentication
  126. if (confirm("All authenticated CS UI Widgets sessions have been cleared.\n" +
  127. "New authentication will be performed on the next page load.\n\n" +
  128. "Do you want to reaload this page now?")) {
  129. window.location.reload();
  130. }
  131. }
  132. }).appendTo(place);
  133. // Add a button to store a new authenticated CS UI Widgets session
  134. $("<button>").text("Add authenticated session")
  135. .click(function() {
  136. var url = prompt("When authenticating a CS UI Widgets session, you may see " +
  137. "a login prompt if the automatic login has not been set up.\n\n" +
  138. "CS REST API URL: ");
  139. if (url) {
  140. // Getting the information for a new CS REST API URL will trigger the authentication.
  141. // Either there is SSO or other automated login or the login dialog will be shown.
  142. progress.show();
  143. addSessionInformation(url, true);
  144. }
  145. }).appendTo(place);
  146. // Add the definition list to show the stored authenticated session information
  147. list = $("<dl>", { "class": "ui-widget" }).appendTo(place);
  148. // Put an initial message to the definition list on the first page load
  149. $("<dt>").text("Authenticated session information has not been refreshed yet.")
  150. .appendTo(list);
  151. // Add a work in progress indicating text; initially hidden
  152. progress = $("<p>").hide().text("Working...").appendTo(place);
  153. });
  154. }