move oauth error handler to auth package

This commit is contained in:
Anish Mukherjee 2022-12-23 18:28:45 +05:30
parent 1a442aa299
commit 66efcf9eb3
8 changed files with 23 additions and 22 deletions

View file

@ -100,8 +100,7 @@ func InitializeAuthProvider() string {
// Note: not included in API reference as part of the OAuth process itself.
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)
handleOauthNotConfigured(w)
return
}
var functions = getCurrentAuthFunctions()

View file

@ -38,12 +38,12 @@ func initAzureAD(redirectURL string, clientID string, clientSecret string) {
func handleAzureLogin(w http.ResponseWriter, r *http.Request) {
var oauth_state_string = logic.RandomString(user_signin_length)
if auth_provider == nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
if err := logic.SetState(oauth_state_string); err != nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
@ -57,7 +57,7 @@ func handleAzureCallback(w http.ResponseWriter, r *http.Request) {
var content, err = getAzureUserInfo(rState, rCode)
if err != nil {
logger.Log(1, "error when getting user info from azure:", err.Error())
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
_, err = logic.GetUser(content.UserPrincipalName)

View file

@ -1,5 +1,7 @@
package auth
import "net/http"
// == define error HTML here ==
const oauthNotConfigured = `<!DOCTYPE html><html>
<body>
@ -7,3 +9,10 @@ const oauthNotConfigured = `<!DOCTYPE html><html>
<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>`
// 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(oauthNotConfigured))
}

View file

@ -38,12 +38,12 @@ func initGithub(redirectURL string, clientID string, clientSecret string) {
func handleGithubLogin(w http.ResponseWriter, r *http.Request) {
var oauth_state_string = logic.RandomString(user_signin_length)
if auth_provider == nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
if err := logic.SetState(oauth_state_string); err != nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
@ -57,7 +57,7 @@ func handleGithubCallback(w http.ResponseWriter, r *http.Request) {
var content, err = getGithubUserInfo(rState, rCode)
if err != nil {
logger.Log(1, "error when getting user info from github:", err.Error())
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
_, err = logic.GetUser(content.Login)

View file

@ -39,12 +39,12 @@ func initGoogle(redirectURL string, clientID string, clientSecret string) {
func handleGoogleLogin(w http.ResponseWriter, r *http.Request) {
var oauth_state_string = logic.RandomString(user_signin_length)
if auth_provider == nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
if err := logic.SetState(oauth_state_string); err != nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
@ -59,7 +59,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
var content, err = getGoogleUserInfo(rState, rCode)
if err != nil {
logger.Log(1, "error when getting user info from google:", err.Error())
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
_, err = logic.GetUser(content.Email)

View file

@ -40,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)
if err != nil {
logger.Log(0, "error when getting user info from callback:", err.Error())
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}

View file

@ -51,12 +51,12 @@ func initOIDC(redirectURL string, clientID string, clientSecret string, issuer s
func handleOIDCLogin(w http.ResponseWriter, r *http.Request) {
var oauth_state_string = logic.RandomString(user_signin_length)
if auth_provider == nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
if err := logic.SetState(oauth_state_string); err != nil {
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
var url = auth_provider.AuthCodeURL(oauth_state_string)
@ -70,7 +70,7 @@ func handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
var content, err = getOIDCUserInfo(rState, rCode)
if err != nil {
logger.Log(1, "error when getting user info from callback:", err.Error())
logic.HandleOauthNotConfigured(w)
handleOauthNotConfigured(w)
return
}
_, err = logic.GetUser(content.Email)

View file

@ -56,10 +56,3 @@ func ReturnErrorResponse(response http.ResponseWriter, request *http.Request, er
response.WriteHeader(errorMessage.Code)
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>"))
}