mirror of
https://github.com/donl/gPanel.git
synced 2026-05-25 22:06:55 -06:00
refactored user api and removed some debug things in webhost
This commit is contained in:
parent
3461ffaec2
commit
b0bb87b2f0
6 changed files with 12 additions and 31 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue