basicAuthEditor.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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="green add icon"></i> Add Credential</button>
  45. </div>
  46. <div class="ui divider"></div>
  47. </div>
  48. </div>
  49. </div>
  50. <div class="ui divider"></div>
  51. <h3 class="ui header">Authentication Exclusion Paths</h3>
  52. <div class="scrolling content ui form">
  53. <p>Exclude specific directories / paths which contains the following subpath prefix from authentication. Useful if you are hosting services require remote API access.</p>
  54. <table class="ui very basic compacted unstackable celled table">
  55. <thead>
  56. <tr>
  57. <th>Path Prefix</th>
  58. <th>Remove</th>
  59. </tr></thead>
  60. <tbody id="exclusionPaths">
  61. <tr>
  62. <td colspan="2"><i class="ui green circle check icon"></i> No Path Excluded</td>
  63. </tr>
  64. </tbody>
  65. </table>
  66. <div class="field">
  67. <input id="newExclusionPath" type="text" placeholder="/public/api/" autocomplete="off">
  68. <small>Make sure you add the tailing slash for only selecting the files / folder inside that path.</small>
  69. </div>
  70. <div class="field" >
  71. <button class="ui basic button" onclick="addExceptionPath();"><i class="yellow add icon"></i> Add Exception</button>
  72. </div>
  73. <div class="field">
  74. <div class="ui basic message">
  75. <h4>How to use set excluded paths?</h4>
  76. <p>All request URI that contains the given prefix will be allowed to bypass authentication and <b>the prefix must start with a slash.</b> For example, given the following prefix.<br>
  77. <code>/public/res/</code><br>
  78. <br>
  79. Zoraxy will allow authentication bypass of any subdirectories or resources under the /public/res/ directory. For example, the following paths access will be able to bypass basic auth mechanism under this setting.<br>
  80. <code>/public/res/photo.png</code><br>
  81. <code>/public/res/far/boo/</code></p>
  82. </div>
  83. </div>
  84. <div class="ui divider"></div>
  85. <div class="field" >
  86. <button class="ui basic button" style="float: right;" onclick="closeThisWrapper();">Close</button>
  87. </div>
  88. </div>
  89. <br><br><br><br>
  90. </div>
  91. <script>
  92. let editingCredentials = [];
  93. let editingEndpoint = {};
  94. if (window.location.hash.length > 1){
  95. let payloadHash = window.location.hash.substr(1);
  96. try{
  97. payloadHash = JSON.parse(decodeURIComponent(payloadHash));
  98. loadBasicAuthCredentials(payloadHash.ep);
  99. $("#epname").text(payloadHash.ep);
  100. editingEndpoint = payloadHash;
  101. }catch(ex){
  102. console.log("Unable to load endpoint data from hash")
  103. }
  104. }
  105. function loadBasicAuthCredentials(uuid){
  106. $.ajax({
  107. url: "/api/proxy/updateCredentials",
  108. method: "GET",
  109. data: {
  110. ep: uuid,
  111. },
  112. success: function(data){
  113. //Push the existing account to list
  114. for(var i = 0; i < data.length; i++){
  115. // Create a new credential object
  116. var credential = {
  117. username: data[i],
  118. password: ""
  119. };
  120. // Add the credential to the global credentials array
  121. editingCredentials.push(credential);
  122. }
  123. console.log(data);
  124. updateEditingCredentialList();
  125. }
  126. })
  127. }
  128. function addCredentialsToEditingList() {
  129. // Retrieve the username and password input values
  130. var username = $('#inlineEditBasicAuthCredUsername').val();
  131. var password = $('#inlineEditBasicAuthCredPassword').val();
  132. if(username == "" || password == ""){
  133. parent.msgbox("Username or password cannot be empty", false, 5000);
  134. return;
  135. }
  136. if (alreadyExists(username)){
  137. parent.msgbox("Credential with same username already exists", false, 5000);
  138. return;
  139. }
  140. // Create a new credential object
  141. var credential = {
  142. username: username,
  143. password: password
  144. };
  145. // Add the credential to the global credentials array
  146. editingCredentials.push(credential);
  147. // Clear the input fields
  148. $('#inlineEditBasicAuthCredUsername').val('');
  149. $('#inlineEditBasicAuthCredPassword').val('');
  150. // Update the table body with the credentials
  151. updateEditingCredentialList();
  152. //Save the table
  153. saveCredentials();
  154. }
  155. function addExceptionPath(){
  156. // Retrieve the username and password input values
  157. var newExclusionPathMatchingPrefix = $('#newExclusionPath').val().trim();
  158. if (newExclusionPathMatchingPrefix == ""){
  159. parent.msgbox("Matching prefix cannot be empty!", false, 5000);
  160. return;
  161. }
  162. $.ajax({
  163. url: "/api/proxy/auth/exceptions/add",
  164. data:{
  165. ep: editingEndpoint.ep,
  166. prefix: newExclusionPathMatchingPrefix
  167. },
  168. method: "POST",
  169. success: function(data){
  170. if (data.error != undefined){
  171. parent.msgbox(data.error, false, 5000);
  172. }else{
  173. initExceptionPaths();
  174. parent.msgbox("New exception path added", true);
  175. $('#newExclusionPath').val("");
  176. }
  177. }
  178. });
  179. }
  180. function removeExceptionPath(object){
  181. let matchingPrefix = $(object).attr("prefix");
  182. $.ajax({
  183. url: "/api/proxy/auth/exceptions/delete",
  184. data:{
  185. ep: editingEndpoint.ep,
  186. prefix: matchingPrefix
  187. },
  188. method: "POST",
  189. success: function(data){
  190. if (data.error != undefined){
  191. parent.msgbox(data.error, false, 5000);
  192. }else{
  193. initExceptionPaths();
  194. parent.msgbox("Exception path removed", true);
  195. }
  196. }
  197. });
  198. }
  199. //Load exception paths from server
  200. function initExceptionPaths(){
  201. $.get(`/api/proxy/auth/exceptions/list?ptype=${editingEndpoint.ept}&ep=${editingEndpoint.ep}`, function(data){
  202. if (data.error != undefined){
  203. parent.msgbox(data.error, false, 5000);
  204. }else{
  205. if (data.length == 0){
  206. $("#exclusionPaths").html(` <tr>
  207. <td colspan="2"><i class="ui green circle check icon"></i> No Path Excluded</td>
  208. </tr>`);
  209. }else{
  210. $("#exclusionPaths").html("");
  211. data.forEach(function(rule){
  212. $("#exclusionPaths").append(` <tr>
  213. <td>${rule.PathPrefix}</td>
  214. <td><button class="ui red basic mini icon button" onclick="removeExceptionPath(this);" prefix="${rule.PathPrefix}"><i class="ui red times icon"></i></button></td>
  215. </tr>`);
  216. })
  217. }
  218. }
  219. });
  220. }
  221. initExceptionPaths();
  222. function updateEditingCredentialList() {
  223. var tableBody = $('#inlineEditBasicAuthCredentialTable');
  224. tableBody.empty();
  225. if (editingCredentials.length === 0) {
  226. tableBody.append('<tr><td colspan="3"><i class="ui green circle check icon"></i> No Entered Credential</td></tr>');
  227. } else {
  228. for (var i = 0; i < editingCredentials.length; i++) {
  229. var credential = editingCredentials[i];
  230. var username = credential.username;
  231. var password = credential.password.replace(/./g, '*'); // Replace each character with '*'
  232. if (credential.password == ""){
  233. password = `<span style="color: #c9c9c9;"><i class="eye slash outline icon"></i> Hidden<span>`;
  234. }
  235. var row = '<tr>' +
  236. '<td>' + username + '</td>' +
  237. '<td>' + password + '</td>' +
  238. '<td><button class="ui basic button" onclick="removeCredentialFromEditingList(' + i + ');"><i class="red remove icon"></i> Remove</button></td>' +
  239. '</tr>';
  240. tableBody.append(row);
  241. }
  242. }
  243. }
  244. function removeCredentialFromEditingList(index) {
  245. // Remove the credential from the credentials array
  246. editingCredentials.splice(index, 1);
  247. // Update the table body
  248. updateEditingCredentialList();
  249. saveCredentials();
  250. }
  251. function alreadyExists(username){
  252. let isExists = false;
  253. editingCredentials.forEach(function(cred){
  254. if (cred.username == username){
  255. isExists = true;
  256. }
  257. });
  258. return isExists;
  259. }
  260. function closeThisWrapper(){
  261. parent.hideSideWrapper(true);
  262. }
  263. function saveCredentials(){
  264. $.ajax({
  265. url: "/api/proxy/updateCredentials",
  266. method: "POST",
  267. data: {
  268. ep: editingEndpoint.ep,
  269. creds: JSON.stringify(editingCredentials)
  270. },
  271. success: function(data){
  272. if (data.error != undefined){
  273. parent.msgbox(data.error, false, 6000);
  274. }else{
  275. parent.msgbox("Credentials Updated");
  276. //parent.hideSideWrapper(true);
  277. }
  278. }
  279. })
  280. }
  281. </script>
  282. </body>
  283. </html>