Explorar el Código

Added auto redirect in login.system

AY hace 4 años
padre
commit
46ace2bede
Se han modificado 3 ficheros con 66 adiciones y 9 borrados
  1. 33 6
      mod/auth/oauth2/oauth2.go
  2. 10 0
      web/SystemAO/advance/oauth.html
  3. 23 3
      web/login.system

+ 33 - 6
mod/auth/oauth2/oauth2.go

@@ -25,6 +25,7 @@ type OauthHandler struct {
 
 type Config struct {
 	Enabled      bool   `json:"enabled"`
+	AutoRedirect bool   `json:"auto_redirect"`
 	IDP          string `json:"idp"`
 	RedirectURL  string `json:"redirect_url"`
 	ServerURL    string `json:"server_url"`
@@ -152,11 +153,27 @@ func (oh *OauthHandler) HandleAuthorize(w http.ResponseWriter, r *http.Request)
 
 //CheckOAuth check if oauth is enabled
 func (oh *OauthHandler) CheckOAuth(w http.ResponseWriter, r *http.Request) {
+	enabledB := false
 	enabled := oh.readSingleConfig("enabled")
-	if enabled == "" {
-		enabled = "false"
+	if enabled == "true" {
+		enabledB = true
 	}
-	sendJSONResponse(w, enabled)
+
+	autoredirectB := false
+	autoredirect := oh.readSingleConfig("autoredirect")
+	if autoredirect == "true" {
+		autoredirectB = true
+	}
+
+	type returnFormat struct {
+		Enabled      bool `json:"enabled"`
+		AutoRedirect bool `json:"auto_redirect"`
+	}
+	json, err := json.Marshal(returnFormat{Enabled: enabledB, AutoRedirect: autoredirectB})
+	if err != nil {
+		sendErrorResponse(w, "Error occurred while marshalling JSON response")
+	}
+	sendJSONResponse(w, string(json))
 }
 
 //https://golangcode.com/add-a-http-cookie/
@@ -176,7 +193,11 @@ func (oh *OauthHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
 		sendTextResponse(w, "Invalid config value [key=enabled].")
 		return
 	}
-
+	autoredirect, err := strconv.ParseBool(oh.readSingleConfig("autoredirect"))
+	if err != nil {
+		sendTextResponse(w, "Invalid config value [key=autoredirect].")
+		return
+	}
 	idp := oh.readSingleConfig("idp")
 	redirecturl := oh.readSingleConfig("redirecturl")
 	serverurl := oh.readSingleConfig("serverurl")
@@ -185,6 +206,7 @@ func (oh *OauthHandler) ReadConfig(w http.ResponseWriter, r *http.Request) {
 
 	config, err := json.Marshal(Config{
 		Enabled:      enabled,
+		AutoRedirect: autoredirect,
 		IDP:          idp,
 		ServerURL:    serverurl,
 		RedirectURL:  redirecturl,
@@ -207,8 +229,11 @@ func (oh *OauthHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
 		sendErrorResponse(w, "enabled field can't be empty")
 		return
 	}
-
-	oh.coredb.Write("oauth", "enabled", enabled)
+	autoredirect, err := mv(r, "autoredirect", true)
+	if err != nil {
+		sendErrorResponse(w, "enabled field can't be empty")
+		return
+	}
 
 	showError := true
 	if enabled != "true" {
@@ -259,6 +284,8 @@ func (oh *OauthHandler) WriteConfig(w http.ResponseWriter, r *http.Request) {
 		}
 	}
 
+	oh.coredb.Write("oauth", "enabled", enabled)
+	oh.coredb.Write("oauth", "autoredirect", autoredirect)
 	oh.coredb.Write("oauth", "idp", idp)
 	oh.coredb.Write("oauth", "redirecturl", redirecturl)
 	oh.coredb.Write("oauth", "serverurl", serverurl)

+ 10 - 0
web/SystemAO/advance/oauth.html

@@ -79,6 +79,12 @@
                     <label>Enable OAuth</label>
                 </div>
             </div>
+            <div class="field">
+                <div class="ui toggle checkbox">
+                    <input type="checkbox" id="autoredirect" name="autoredirect">
+                    <label>Auto redirect</label>
+                </div>
+            </div>
             <div class="field">
                 <label>Select OAuth IdP (aka Service provider)</label>
                 <div class="ui selection fluid dropdown" autocomplete="false">
@@ -149,6 +155,9 @@
                 if (data.enabled) {
                     $("#enable").parent().checkbox("check")
                 }
+                if (data.autoredirect) {
+                    $("#autoredirect").parent().checkbox("check")
+                }
                 $("#idp").parent().dropdown("set selected", data.idp);
                 $("#serverurl").val(data.server_url);
                 $("#redirecturl").val(data.redirect_url);
@@ -160,6 +169,7 @@
         function update() {
             $.post("../../system/auth/oauth/config/write", {
                     enabled: $("#enable").parent().checkbox("is checked"),
+                    autoredirect: $("#autoredirect").parent().checkbox("is checked"),
                     idp: $("#idp").val(),
                     redirecturl: $("#redirecturl").val(),
                     clientid: $("#clientid").val(),

+ 23 - 3
web/login.system

@@ -122,6 +122,7 @@
     <script>
         var redirectionAddress = "{{redirection_addr}}";
         var systemUserCount = "{{usercount}}" - 0; //Magic way to convert string to int :)
+        var autoRedirectTimer;
         var isMobile = false; //initiate as false
         // device detection
         if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent) 
@@ -178,15 +179,28 @@
             });
 
             //OAuth related code, check if system is open for ext login
-            $.get("system/auth/oauth/checkoauth",function(data){
-                if (data == true){
+            $.getJSON("system/auth/oauth/checkoauth",function(data){
+                if (data.enabled == true){
                     $(".signin").show();
                 }else{
                     $(".signin").remove();
                 }
+                //if auto redirect is on
+                if(data.auto_redirect == true) {
+                    //checking if they come from desktop.system or mobile.system
+                    //if they come from that two pages, usually mean they are just logged out.
+                    if(document.referrer != window.location.origin + "/desktop.system" && document.referrer != window.location.origin + "/mobile.system"){
+                        $(".ts.borderless.basic.segment").attr("style","display: none;");
+                        $(".ts.borderless.basic.segment").attr("id","aoLogin");
+                        $(".ts.borderless.basic.segment").after('<div id="autoRedirectSegment" class="ts borderless basic segment"><p><i class="key icon"></i>Redirecting to your organization sign in page...</p><br><a style="cursor: pointer;" onclick="stopAutoRedirect()">Cancel</a></div>');
+                        autoRedirectTimer = setTimeout(function(){
+                        window.location.href = "system/auth/oauth/login?redirect=" + redirectionAddress;
+                        }, 3000);
+                    }
+                }
             });
             if(get('redirect') != undefined){
-                $(".section.signin").attr("href","system/auth/oauth/login?redirect=" + get('redirect'));
+                $(".section.signin").attr("href","system/auth/oauth/login?redirect=" + redirectionAddress);
             }
 
             //Get the system hostname and replace the hostname fields
@@ -245,6 +259,12 @@
                 return decodeURIComponent(name[1]);
         }
 
+        function stopAutoRedirect(){
+            clearTimeout(autoRedirectTimer);
+            $("#aoLogin").removeAttr("style");
+            $("#autoRedirectSegment").attr("style", "display:none");
+        }
+
         $(".thisyear").text(new Date().getFullYear());
     </script>
     </body>