sso.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <div class="standardContainer">
  2. <div class="ui basic segment">
  3. <h2>Zoraxy SSO / Oauth</h2>
  4. <p>A centralized authentication system for all your subdomains</p>
  5. <div class="ui divider"></div>
  6. <div class="ui basic segment enabled ssoRunningState">
  7. <h4 class="ui header" id="ssoRunningState">
  8. <i class="circle check icon"></i>
  9. <div class="content">
  10. <span class="webserv_status">Running</span>
  11. <div class="sub header">Listen port :<span class="oauthserv_port">8081</span></div>
  12. </div>
  13. </h4>
  14. </div>
  15. <div class="ui form">
  16. <h4 class="ui dividing header">Oauth2 Server</h4>
  17. <div class="field">
  18. <div class="ui toggle checkbox">
  19. <input type="checkbox" name="enableOauth2">
  20. <label>Enable Oauth2 Server<br>
  21. <small>Oauth2 server for handling external authentication requests</small></label>
  22. </div>
  23. </div>
  24. <div class="field">
  25. <label>Oauth2 Server Port</label>
  26. <div class="ui action input">
  27. <input type="number" name="oauth2Port" placeholder="Port" value="5488">
  28. <button id="saveOauthServerPortBtn" class="ui basic green button"><i class="ui green circle check icon"></i> Update</button>
  29. </div>
  30. <small>Listening port of the Zoraxy internal Oauth2 Server.You can create a subdomain proxy rule to <code>127.0.0.1:<span class="ssoPort">5488</span></code></small>
  31. </div>
  32. <div class="field">
  33. <label>Auth URL</label>
  34. <div class="ui action input">
  35. <input type="text" name="authURL" placeholder="https://auth.yourdomain.com">
  36. <button id="saveAuthURLBtn" class="ui basic blue button"><i class="ui blue save icon"></i> Save</button>
  37. </div>
  38. <small>The exposed authentication URL of the Oauth2 server, usually <code>https://auth.example.com</code> or <code>https://sso.yourdomain.com</code>. <b>Remember to include the http:// or https:// in your URL.</b></small>
  39. </div>
  40. </div>
  41. <div class="ui divider"></div>
  42. <div>
  43. <h3 class="ui header">
  44. <i class="ui blue user circle icon"></i>
  45. <div class="content">
  46. Registered Users
  47. <div class="sub header">A list of users that are registered with the SSO server</div>
  48. </div>
  49. </h3>
  50. </div>
  51. <div class="ui divider"></div>
  52. <div>
  53. <h3 class="ui header">
  54. <i class="ui green th icon"></i>
  55. <div class="content">
  56. Registered Apps
  57. <div class="sub header">A list of apps that are registered with the SSO server</div>
  58. </div>
  59. </h3>
  60. <p></p>
  61. </div>
  62. </div>
  63. </div>
  64. <script>
  65. $("input[name=oauth2Port]").on("change", function() {
  66. $(".ssoPort").text($(this).val());
  67. });
  68. function updateSSOStatus(){
  69. $.get("/api/sso/status", function(data){
  70. if(data.error != undefined){
  71. //Show error message
  72. $(".ssoRunningState").removeClass("enabled").addClass("disabled");
  73. $("#ssoRunningState .webserv_status").html('Error: '+data.error);
  74. }else{
  75. if (data.Enabled){
  76. $(".ssoRunningState").addClass("enabled");
  77. $("#ssoRunningState .webserv_status").html('Running');
  78. $(".ssoRunningState i").attr("class", "circle check icon");
  79. $("input[name=enableOauth2]").parent().checkbox("set checked");
  80. }else{
  81. $(".ssoRunningState").removeClass("enabled");
  82. $("#ssoRunningState .webserv_status").html('Stopped');
  83. $(".ssoRunningState i").attr("class", "circle times icon");
  84. $("input[name=enableOauth2]").parent().checkbox("set unchecked");
  85. }
  86. $("input[name=oauth2Port]").val(data.ListeningPort);
  87. $(".oauthserv_port").text(data.ListeningPort);
  88. $("input[name=authURL]").val(data.AuthURL);
  89. }
  90. });
  91. }
  92. function initSSOStatus(){
  93. $.get("/api/sso/status", function(data){
  94. //Update the SSO status from the server
  95. updateSSOStatus();
  96. //Bind events to the enable checkbox
  97. $("input[name=enableOauth2]").off("change").on("change", function(){
  98. var checked = $(this).prop("checked");
  99. $.cjax({
  100. url: "/api/sso/enable",
  101. method: "POST",
  102. data: {
  103. enable: checked
  104. },
  105. success: function(data){
  106. if(data.error != undefined){
  107. msgbox("Failed to toggle SSO: " + data.error, false);
  108. //Unbind the event to prevent infinite loop
  109. $("input[name=enableOauth2]").off("change");
  110. }else{
  111. initSSOStatus();
  112. }
  113. }
  114. });
  115. });
  116. });
  117. }
  118. initSSOStatus();
  119. /* Save the Oauth server port */
  120. function saveOauthServerPort(){
  121. var port = $("input[name=oauth2Port]").val();
  122. //Check if the port is valid
  123. if (port < 1 || port > 65535){
  124. msgbox("Invalid port number", false);
  125. return;
  126. }
  127. //Use cjax to send the port to the server with csrf token
  128. $.cjax({
  129. url: "/api/sso/setPort",
  130. method: "POST",
  131. data: {
  132. port: port
  133. },
  134. success: function(data) {
  135. if (data.error != undefined) {
  136. msgbox("Failed to update Oauth server port: " + data.error, false);
  137. } else {
  138. msgbox("Oauth server port updated", true);
  139. }
  140. updateSSOStatus();
  141. }
  142. });
  143. }
  144. //Bind the save button to the saveOauthServerPort function
  145. $("#saveOauthServerPortBtn").on("click", function() {
  146. saveOauthServerPort();
  147. });
  148. $("input[name=oauth2Port]").on("keypress", function(e) {
  149. if (e.which == 13) {
  150. saveOauthServerPort();
  151. }
  152. });
  153. /* Save the Oauth server URL (aka AuthURL) */
  154. function saveAuthURL(){
  155. var url = $("input[name=authURL]").val();
  156. //Make sure the url contains http:// or https://
  157. if (!url.startsWith("http://") && !url.startsWith("https://")){
  158. msgbox("Invalid URL. Make sure to include http:// or https://", false);
  159. $("input[name=authURL]").parent().parent().addClass("error");
  160. return;
  161. }else{
  162. $("input[name=authURL]").parent().parent().removeClass("error");
  163. }
  164. //Use cjax to send the port to the server with csrf token
  165. $.cjax({
  166. url: "/api/sso/setAuthURL",
  167. method: "POST",
  168. data: {
  169. "auth_url": url
  170. },
  171. success: function(data) {
  172. if (data.error != undefined) {
  173. msgbox("Failed to update Oauth server port: " + data.error, false);
  174. } else {
  175. msgbox("Oauth server port updated", true);
  176. }
  177. updateSSOStatus();
  178. }
  179. });
  180. }
  181. //Bind the save button to the saveAuthURL function
  182. $("#saveAuthURLBtn").on("click", function() {
  183. saveAuthURL();
  184. });
  185. $("input[name=authURL]").on("keypress", function(e) {
  186. if (e.which == 13) {
  187. saveAuthURL();
  188. }
  189. });
  190. </script>