浏览代码

fixed minor bug surrounding oauth error

0xdcarns 3 年之前
父节点
当前提交
5322c44a99
共有 3 个文件被更改,包括 26 次插入5 次删除
  1. 16 0
      auth/auth.go
  2. 10 0
      auth/error.go
  3. 0 5
      controllers/userHttpController.go

+ 16 - 0
auth/auth.go

@@ -3,6 +3,7 @@ package auth
 import (
 	"encoding/base64"
 	"encoding/json"
+	"fmt"
 	"net/http"
 
 	"github.com/gravitl/netmaker/logic"
@@ -65,6 +66,11 @@ func InitializeAuthProvider() string {
 
 // HandleAuthCallback - handles oauth callback
 func HandleAuthCallback(w http.ResponseWriter, r *http.Request) {
+	if auth_provider == nil {
+		w.Header().Set("Content-Type", "text/html; charset=utf-8")
+		fmt.Fprintln(w, oauthNotConfigured)
+		return
+	}
 	var functions = getCurrentAuthFunctions()
 	if functions == nil {
 		return
@@ -74,6 +80,16 @@ func HandleAuthCallback(w http.ResponseWriter, r *http.Request) {
 
 // HandleAuthLogin - handles oauth login
 func HandleAuthLogin(w http.ResponseWriter, r *http.Request) {
+	if auth_provider == nil {
+		var referer = r.Header.Get("referer")
+		if referer != "" {
+			http.Redirect(w, r, referer+"?oauth=callback-error", http.StatusTemporaryRedirect)
+			return
+		}
+		w.Header().Set("Content-Type", "text/html; charset=utf-8")
+		fmt.Fprintln(w, oauthNotConfigured)
+		return
+	}
 	var functions = getCurrentAuthFunctions()
 	if functions == nil {
 		return

+ 10 - 0
auth/error.go

@@ -0,0 +1,10 @@
+package auth
+
+// == define error HTML here ==
+const oauthNotConfigured = `<!DOCTYPE html><html>
+<body>
+<h3>Your Netmaker server does not have OAuth configured.</h3>
+<p>Please visit the docs <a href="https://docs.netmaker.org/oauth.html" target="_blank" rel="noopener">here</a> to learn how to.</p>
+</body>
+</html>
+`

+ 0 - 5
controllers/userHttpController.go

@@ -29,11 +29,6 @@ func userHandlers(r *mux.Router) {
 	r.HandleFunc("/api/users", authorizeUserAdm(http.HandlerFunc(getUsers))).Methods("GET")
 	r.HandleFunc("/api/oauth/login", auth.HandleAuthLogin).Methods("GET")
 	r.HandleFunc("/api/oauth/callback", auth.HandleAuthCallback).Methods("GET")
-	r.HandleFunc("/api/oauth/error", throwOauthError).Methods("GET")
-}
-
-func throwOauthError(response http.ResponseWriter, request *http.Request) {
-	returnErrorResponse(response, request, formatError(errors.New("No token returned"), "unauthorized"))
 }
 
 // Node authenticates using its password and retrieves a JWT for authorization.