basicAuthEditor.html 9.9 KB

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