From b0bb87b2f0fbdcb87a8785d2ea5859c3d90d59f1 Mon Sep 17 00:00:00 2001 From: George Shaw Date: Fri, 3 Nov 2017 17:23:10 -0500 Subject: [PATCH] refactored user api and removed some debug things in webhost --- pkg/api/api_handler.go | 6 +++--- pkg/api/user/{user_auth.go => auth.go} | 4 ++-- pkg/api/user/{user_token.go => get_secret.go} | 4 ++-- pkg/api/user/{user_logout.go => logout.go} | 4 ++-- .../user/{user_register.go => register.go} | 4 ++-- pkg/webhost/webhost.go | 21 +------------------ 6 files changed, 12 insertions(+), 31 deletions(-) rename pkg/api/user/{user_auth.go => auth.go} (93%) rename pkg/api/user/{user_token.go => get_secret.go} (79%) rename pkg/api/user/{user_logout.go => logout.go} (83%) rename pkg/api/user/{user_register.go => register.go} (91%) diff --git a/pkg/api/api_handler.go b/pkg/api/api_handler.go index 920e836..aed1a24 100644 --- a/pkg/api/api_handler.go +++ b/pkg/api/api_handler.go @@ -17,11 +17,11 @@ func HandleAPI(path string, res http.ResponseWriter, req *http.Request) (bool, b switch suspectApi { case "user_auth": - return true, user.UserAuthentication(res, req) + return true, user.Auth(res, req) case "user_register": - return true, user.UserRegistration(res, req) + return true, user.Register(res, req) case "user_logout": - return true, user.UserLogout(res, req) + return true, user.Logout(res, req) default: return false, false } diff --git a/pkg/api/user/user_auth.go b/pkg/api/user/auth.go similarity index 93% rename from pkg/api/user/user_auth.go rename to pkg/api/user/auth.go index 328811f..4eedb23 100644 --- a/pkg/api/user/user_auth.go +++ b/pkg/api/user/auth.go @@ -12,10 +12,10 @@ import ( jwt "github.com/dgrijalva/jwt-go" ) -// UserAuthentication function is accessed by an API call from the webhost root +// Auth function is accessed by an API call from the webhost root // by accessing /user_auth and sending it a post request with userRequestData // struct in JSON format. -func UserAuthentication(res http.ResponseWriter, req *http.Request) bool { +func Auth(res http.ResponseWriter, req *http.Request) bool { if req.Method != "POST" { http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) return false diff --git a/pkg/api/user/user_token.go b/pkg/api/user/get_secret.go similarity index 79% rename from pkg/api/user/user_token.go rename to pkg/api/user/get_secret.go index deb4526..951bced 100644 --- a/pkg/api/user/user_token.go +++ b/pkg/api/user/get_secret.go @@ -3,10 +3,10 @@ package user import "github.com/Ennovar/gPanel/pkg/database" -// UserSecret is not accessible from the any client side request. It is +// GetSecret is not accessible from the any client side request. It is // only used on the server side to help verify users are who they say they // are. -func UserSecret(user string) (string, error) { +func GetSecret(user string) (string, error) { ds, err := database.Open(database.DBLOC_MAIN) if err != nil { return "", err diff --git a/pkg/api/user/user_logout.go b/pkg/api/user/logout.go similarity index 83% rename from pkg/api/user/user_logout.go rename to pkg/api/user/logout.go index b147747..a6ec8f2 100644 --- a/pkg/api/user/user_logout.go +++ b/pkg/api/user/logout.go @@ -7,10 +7,10 @@ import ( "github.com/Ennovar/gPanel/pkg/networking" ) -// UserLogout function is accessed by an API call from the webhost root +// Logout function is accessed by an API call from the webhost root // by accessing /user_logout and sending it an empty POST request. This function will // delete the user-auth cookie session store -func UserLogout(res http.ResponseWriter, req *http.Request) bool { +func Logout(res http.ResponseWriter, req *http.Request) bool { if req.Method != "POST" { http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) return false diff --git a/pkg/api/user/user_register.go b/pkg/api/user/register.go similarity index 91% rename from pkg/api/user/user_register.go rename to pkg/api/user/register.go index da00c7a..23f5f76 100644 --- a/pkg/api/user/user_register.go +++ b/pkg/api/user/register.go @@ -9,10 +9,10 @@ import ( "github.com/Ennovar/gPanel/pkg/encryption" ) -// UserRegistration function is accessed by an API call from the webhost root +// Register function is accessed by an API call from the webhost root // by accessing /user_register and sending it a post request with userRequestData // struct in JSON format. -func UserRegistration(res http.ResponseWriter, req *http.Request) bool { +func Register(res http.ResponseWriter, req *http.Request) bool { if req.Method != "POST" { http.Error(res, req.Method+" HTTP method is unsupported for this API.", http.StatusMethodNotAllowed) return false diff --git a/pkg/webhost/webhost.go b/pkg/webhost/webhost.go index 66b4224..9117bf6 100644 --- a/pkg/webhost/webhost.go +++ b/pkg/webhost/webhost.go @@ -67,58 +67,44 @@ func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) { session_value, err := store.Read(w, req, "user") if err != nil { - logging.Console("DEBUG::", logging.NORMAL_LOG, "1") http.Error(w, err.Error(), http.StatusInternalServerError) return } if session_value == nil { - logging.Console("DEBUG::", logging.NORMAL_LOG, "2") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } username, ok := session_value.(string) if !ok { - logging.Console("DEBUG::", logging.NORMAL_LOG, "3") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } - stored_secret, err := user.UserSecret(username) + stored_secret, err := user.GetSecret(username) if stored_secret == "" { - logging.Console("DEBUG::", logging.NORMAL_LOG, "4") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } session_value, err = store.Read(w, req, "token") if err != nil { - logging.Console("DEBUG::", logging.NORMAL_LOG, "5") http.Error(w, err.Error(), http.StatusInternalServerError) return } if session_value == nil { - logging.Console("DEBUG::", logging.NORMAL_LOG, "6") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } tokenString, ok := session_value.(string) if !ok { - logging.Console("DEBUG::", logging.NORMAL_LOG, "7") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } - // if len(tokenString) < 7 { - // logging.Console("DEBUG::", logging.NORMAL_LOG, "8") - // http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) - // return - // } - // tokenString = tokenString[7:] - keyfunc := func(t *jwt.Token) (interface{}, error) { return []byte(stored_secret), nil } @@ -129,17 +115,12 @@ func (priv *PrivateHost) ServeHTTP(w http.ResponseWriter, req *http.Request) { t, err := p.ParseWithClaims(tokenString, &jwt.StandardClaims{}, keyfunc) if err != nil { - logging.Console("DEBUG::", logging.NORMAL_LOG, username) - logging.Console("DEBUG::", logging.NORMAL_LOG, tokenString) - logging.Console("DEBUG::", logging.NORMAL_LOG, stored_secret) - logging.Console("DEBUG::", logging.NORMAL_LOG, err.Error()) http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return } claims := t.Claims.(*jwt.StandardClaims) if claims.Subject != username { - logging.Console("DEBUG::", logging.NORMAL_LOG, "10") http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) return }