basicAuthEditor.html 14 KB

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