fixed minor bug surrounding oauth error

This commit is contained in:
0xdcarns 2021-10-28 10:20:37 -04:00
parent f7156551b2
commit 5322c44a99
3 changed files with 26 additions and 5 deletions

View file

@ -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
auth/error.go Normal file
View file

@ -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>
`

View file

@ -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.