basicAuthEditor.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <!-- Notes: This should be open in its original path-->
  5. <link rel="stylesheet" href="../script/semantic/semantic.min.css">
  6. <script src="../script/jquery-3.6.0.min.js"></script>
  7. <script src="../script/semantic/semantic.min.js"></script>
  8. </head>
  9. <body>
  10. <br>
  11. <div class="ui container">
  12. <div class="ui header">
  13. <div class="content">
  14. Basic Auth Credential
  15. <div class="sub header" id="epname"></div>
  16. </div>
  17. </div>
  18. <div class="scrolling content ui form">
  19. <div id="inlineEditBasicAuthCredentials" class="field">
  20. <p>Enter the username and password for allowing them to access this proxy endpoint</p>
  21. <table class="ui very basic compacted unstackable celled table">
  22. <thead>
  23. <tr>
  24. <th>Username</th>
  25. <th>Password</th>
  26. <th>Remove</th>
  27. </tr></thead>
  28. <tbody id="inlineEditBasicAuthCredentialTable">
  29. <tr>
  30. <td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td>
  31. </tr>
  32. </tbody>
  33. </table>
  34. <div class="three small fields credentialEntry">
  35. <div class="field">
  36. <input id="inlineEditBasicAuthCredUsername" type="text" placeholder="Username" autocomplete="off">
  37. </div>
  38. <div class="field">
  39. <input id="inlineEditBasicAuthCredPassword" type="password" placeholder="Password" autocomplete="off">
  40. </div>
  41. <div class="field" >
  42. <button class="ui basic button" onclick="addCredentialsToEditingList();"><i class="blue add icon"></i> Add Credential</button>
  43. </div>
  44. <div class="ui divider"></div>
  45. <div class="field" >
  46. <button class="ui basic button" style="float: right;" onclick="saveCredentials();"><i class="green save icon"></i> Save</button>
  47. <button class="ui basic button" style="float: right;" onclick="cancelCredentialEdit();"><i class="remove icon"></i> Cancel</button>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. </div>
  53. <script>
  54. let editingCredentials = [];
  55. let editingEndpoint = {};
  56. if (window.location.hash.length > 1){
  57. let payloadHash = window.location.hash.substr(1);
  58. try{
  59. payloadHash = JSON.parse(decodeURIComponent(payloadHash));
  60. loadBasicAuthCredentials(payloadHash.ept, payloadHash.ep);
  61. $("#epname").text(payloadHash.ep);
  62. editingEndpoint = payloadHash;
  63. }catch(ex){
  64. console.log("Unable to load endpoint data from hash")
  65. }
  66. }
  67. function loadBasicAuthCredentials(endpointType, uuid){
  68. $.ajax({
  69. url: "/api/proxy/updateCredentials",
  70. method: "GET",
  71. data: {
  72. ep: uuid,
  73. ptype: endpointType
  74. },
  75. success: function(data){
  76. //Push the existing account to list
  77. for(var i = 0; i < data.length; i++){
  78. // Create a new credential object
  79. var credential = {
  80. username: data[i],
  81. password: ""
  82. };
  83. // Add the credential to the global credentials array
  84. editingCredentials.push(credential);
  85. }
  86. console.log(data);
  87. updateEditingCredentialList();
  88. }
  89. })
  90. }
  91. function addCredentialsToEditingList() {
  92. // Retrieve the username and password input values
  93. var username = $('#inlineEditBasicAuthCredUsername').val();
  94. var password = $('#inlineEditBasicAuthCredPassword').val();
  95. if(username == "" || password == ""){
  96. parent.msgbox("Username or password cannot be empty", false, 5000);
  97. return;
  98. }
  99. if (alreadyExists(username)){
  100. parent.msgbox("Credential with same username already exists", false, 5000);
  101. return;
  102. }
  103. // Create a new credential object
  104. var credential = {
  105. username: username,
  106. password: password
  107. };
  108. // Add the credential to the global credentials array
  109. editingCredentials.push(credential);
  110. // Clear the input fields
  111. $('#inlineEditBasicAuthCredUsername').val('');
  112. $('#inlineEditBasicAuthCredPassword').val('');
  113. // Update the table body with the credentials
  114. updateEditingCredentialList();
  115. }
  116. function updateEditingCredentialList() {
  117. var tableBody = $('#inlineEditBasicAuthCredentialTable');
  118. tableBody.empty();
  119. if (editingCredentials.length === 0) {
  120. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  121. } else {
  122. for (var i = 0; i < editingCredentials.length; i++) {
  123. var credential = editingCredentials[i];
  124. var username = credential.username;
  125. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  126. if (credential.password == ""){
  127. password = `<span style="color: #c9c9c9;"><i class="eye slash outline icon"></i> Hidden<span>`;
  128. }
  129. var row = '<tr>' +
  130. '<td>' + username + '</td>' +
  131. '<td>' + password + '</td>' +
  132. '<td><button class="ui basic button" onclick="removeCredentialFromEditingList(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  133. '</tr>';
  134. tableBody.append(row);
  135. }
  136. }
  137. }
  138. function removeCredentialFromEditingList(index) {
  139. // Remove the credential from the credentials array
  140. editingCredentials.splice(index, 1);
  141. // Update the table body
  142. updateEditingCredentialList();
  143. }
  144. function alreadyExists(username){
  145. let isExists = false;
  146. editingCredentials.forEach(function(cred){
  147. if (cred.username == username){
  148. isExists = true;
  149. }
  150. });
  151. return isExists;
  152. }
  153. function cancelCredentialEdit(){
  154. parent.hideSideWrapper(true);
  155. }
  156. function saveCredentials(){
  157. $.ajax({
  158. url: "/api/proxy/updateCredentials",
  159. method: "POST",
  160. data: {
  161. ep: editingEndpoint.ep,
  162. ptype: editingEndpoint.ept,
  163. creds: JSON.stringify(editingCredentials)
  164. },
  165. success: function(data){
  166. if (data.error != undefined){
  167. parent.msgbox(data.error, false, 6000);
  168. }else{
  169. parent.msgbox("Credentials Updated");
  170. parent.hideSideWrapper(true);
  171. }
  172. }
  173. })
  174. }
  175. </script>
  176. </body>
  177. </html>