diff --git a/auth/auth.go b/auth/auth.go index eb2b0f91..cd1e55e7 100644 --- a/auth/auth.go +++ b/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 diff --git a/auth/error.go b/auth/error.go new file mode 100644 index 00000000..7b338cfc --- /dev/null +++ b/auth/error.go @@ -0,0 +1,10 @@ +package auth + +// == define error HTML here == +const oauthNotConfigured = ` + +

Your Netmaker server does not have OAuth configured.

+

Please visit the docs here to learn how to.

+ + +` diff --git a/controllers/userHttpController.go b/controllers/userHttpController.go index 37e75240..28b14fc9 100644 --- a/controllers/userHttpController.go +++ b/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.