Browse Source

remove need of frontend url for error pages

Anish Mukherjee 2 years ago
parent
commit
1a442aa299
7 changed files with 25 additions and 31 deletions
  1. 4 7
      auth/azure-ad.go
  2. 4 7
      auth/github.go
  3. 4 7
      auth/google.go
  4. 1 2
      auth/nodecallback.go
  5. 4 7
      auth/oidc.go
  6. 1 1
      cli/cmd/root.go
  7. 7 0
      logic/errors.go

+ 4 - 7
auth/azure-ad.go

@@ -37,16 +37,13 @@ func initAzureAD(redirectURL string, clientID string, clientSecret string) {
 
 
 func handleAzureLogin(w http.ResponseWriter, r *http.Request) {
 func handleAzureLogin(w http.ResponseWriter, r *http.Request) {
 	var oauth_state_string = logic.RandomString(user_signin_length)
 	var oauth_state_string = logic.RandomString(user_signin_length)
-	if auth_provider == nil && servercfg.GetFrontendURL() != "" {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
-		return
-	} else if auth_provider == nil {
-		fmt.Fprintf(w, "%s", []byte("no frontend URL was provided and an OAuth login was attempted\nplease reconfigure server to use OAuth or use basic credentials"))
+	if auth_provider == nil {
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
 	if err := logic.SetState(oauth_state_string); err != nil {
 	if err := logic.SetState(oauth_state_string); err != nil {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
@@ -60,7 +57,7 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) {
 	var content, err = getAzureUserInfo(rState, rCode)
 	var content, err = getAzureUserInfo(rState, rCode)
 	if err != nil {
 	if err != nil {
 		logger.Log(1, "error when getting user info from azure:", err.Error())
 		logger.Log(1, "error when getting user info from azure:", err.Error())
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 	_, err = logic.GetUser(content.UserPrincipalName)
 	_, err = logic.GetUser(content.UserPrincipalName)

+ 4 - 7
auth/github.go

@@ -37,16 +37,13 @@ func initGithub(redirectURL string, clientID string, clientSecret string) {
 
 
 func handleGithubLogin(w http.ResponseWriter, r *http.Request) {
 func handleGithubLogin(w http.ResponseWriter, r *http.Request) {
 	var oauth_state_string = logic.RandomString(user_signin_length)
 	var oauth_state_string = logic.RandomString(user_signin_length)
-	if auth_provider == nil && servercfg.GetFrontendURL() != "" {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
-		return
-	} else if auth_provider == nil {
-		fmt.Fprintf(w, "%s", []byte("no frontend URL was provided and an OAuth login was attempted\nplease reconfigure server to use OAuth or use basic credentials"))
+	if auth_provider == nil {
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
 	if err := logic.SetState(oauth_state_string); err != nil {
 	if err := logic.SetState(oauth_state_string); err != nil {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
@@ -60,7 +57,7 @@ func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
 	var content, err = getGithubUserInfo(rState, rCode)
 	var content, err = getGithubUserInfo(rState, rCode)
 	if err != nil {
 	if err != nil {
 		logger.Log(1, "error when getting user info from github:", err.Error())
 		logger.Log(1, "error when getting user info from github:", err.Error())
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 	_, err = logic.GetUser(content.Login)
 	_, err = logic.GetUser(content.Login)

+ 4 - 7
auth/google.go

@@ -38,16 +38,13 @@ func initGoogle(redirectURL string, clientID string, clientSecret string) {
 
 
 func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {
 func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {
 	var oauth_state_string = logic.RandomString(user_signin_length)
 	var oauth_state_string = logic.RandomString(user_signin_length)
-	if auth_provider == nil && servercfg.GetFrontendURL() != "" {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
-		return
-	} else if auth_provider == nil {
-		fmt.Fprintf(w, "%s", []byte("no frontend URL was provided and an OAuth login was attempted\nplease reconfigure server to use OAuth or use basic credentials"))
+	if auth_provider == nil {
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
 	if err := logic.SetState(oauth_state_string); err != nil {
 	if err := logic.SetState(oauth_state_string); err != nil {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
@@ -62,7 +59,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
 	var content, err = getGoogleUserInfo(rState, rCode)
 	var content, err = getGoogleUserInfo(rState, rCode)
 	if err != nil {
 	if err != nil {
 		logger.Log(1, "error when getting user info from google:", err.Error())
 		logger.Log(1, "error when getting user info from google:", err.Error())
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 	_, err = logic.GetUser(content.Email)
 	_, err = logic.GetUser(content.Email)

+ 1 - 2
auth/nodecallback.go

@@ -13,7 +13,6 @@ import (
 	"github.com/gravitl/netmaker/logic/pro/netcache"
 	"github.com/gravitl/netmaker/logic/pro/netcache"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models"
 	"github.com/gravitl/netmaker/models/promodels"
 	"github.com/gravitl/netmaker/models/promodels"
-	"github.com/gravitl/netmaker/servercfg"
 )
 )
 
 
 var (
 var (
@@ -41,7 +40,7 @@ func HandleNodeSSOCallback(w http.ResponseWriter, r *http.Request) {
 	var userClaims, err = functions[get_user_info].(func(string, string) (*OAuthUser, error))(state, code)
 	var userClaims, err = functions[get_user_info].(func(string, string) (*OAuthUser, error))(state, code)
 	if err != nil {
 	if err != nil {
 		logger.Log(0, "error when getting user info from callback:", err.Error())
 		logger.Log(0, "error when getting user info from callback:", err.Error())
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 

+ 4 - 7
auth/oidc.go

@@ -50,16 +50,13 @@ func initOIDC(redirectURL string, clientID string, clientSecret string, issuer s
 
 
 func handleOIDCLogin(w http.ResponseWriter, r *http.Request) {
 func handleOIDCLogin(w http.ResponseWriter, r *http.Request) {
 	var oauth_state_string = logic.RandomString(user_signin_length)
 	var oauth_state_string = logic.RandomString(user_signin_length)
-	if auth_provider == nil && servercfg.GetFrontendURL() != "" {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
-		return
-	} else if auth_provider == nil {
-		fmt.Fprintf(w, "%s", []byte("no frontend URL was provided and an OAuth login was attempted\nplease reconfigure server to use OAuth or use basic credentials"))
+	if auth_provider == nil {
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 
 
 	if err := logic.SetState(oauth_state_string); err != nil {
 	if err := logic.SetState(oauth_state_string); err != nil {
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 	var url = auth_provider.AuthCodeURL(oauth_state_string)
 	var url = auth_provider.AuthCodeURL(oauth_state_string)
@@ -73,7 +70,7 @@ func handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
 	var content, err = getOIDCUserInfo(rState, rCode)
 	var content, err = getOIDCUserInfo(rState, rCode)
 	if err != nil {
 	if err != nil {
 		logger.Log(1, "error when getting user info from callback:", err.Error())
 		logger.Log(1, "error when getting user info from callback:", err.Error())
-		http.Redirect(w, r, servercfg.GetFrontendURL()+"/login?oauth=callback-error", http.StatusTemporaryRedirect)
+		logic.HandleOauthNotConfigured(w)
 		return
 		return
 	}
 	}
 	_, err = logic.GetUser(content.Email)
 	_, err = logic.GetUser(content.Email)

+ 1 - 1
cli/cmd/root.go

@@ -20,7 +20,7 @@ import (
 
 
 // rootCmd represents the base command when called without any subcommands
 // rootCmd represents the base command when called without any subcommands
 var rootCmd = &cobra.Command{
 var rootCmd = &cobra.Command{
-	Use:   "netmaker",
+	Use:   "nmctl",
 	Short: "CLI for interacting with Netmaker Server",
 	Short: "CLI for interacting with Netmaker Server",
 	Long:  `CLI for interacting with Netmaker Server`,
 	Long:  `CLI for interacting with Netmaker Server`,
 	// Uncomment the following line if your bare application
 	// Uncomment the following line if your bare application

+ 7 - 0
logic/errors.go

@@ -56,3 +56,10 @@ func ReturnErrorResponse(response http.ResponseWriter, request *http.Request, er
 	response.WriteHeader(errorMessage.Code)
 	response.WriteHeader(errorMessage.Code)
 	response.Write(jsonResponse)
 	response.Write(jsonResponse)
 }
 }
+
+// HandleOauthNotConfigured - returns an appropriate html page when oauth is not configured on netmaker server but an oauth login was attempted
+func HandleOauthNotConfigured(response http.ResponseWriter) {
+	response.Header().Set("Content-Type", "text/html; charset=utf-8")
+	response.WriteHeader(http.StatusInternalServerError)
+	response.Write([]byte("<html><body><h1>OAuth Login Failed, check if server is configured for OAuth.</h1></body></html>"))
+}