chore: add Auth function to handle all permission verification; remove all old auth codes
This commit is contained in:
@@ -23,9 +23,10 @@ func CloseFrpcDatabase() error {
|
||||
}
|
||||
|
||||
func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[CreateInstanceHandler] Invalid request method: %s", r.Method))
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[CreateInstanceHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -74,13 +75,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
instanceInfo.Additional = additional
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
req := CreateInstanceRequest{
|
||||
InstanceInfo: instanceInfo,
|
||||
BootAtStart: bootAtStart,
|
||||
@@ -88,12 +82,6 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
Additional: instanceInfo.Additional,
|
||||
}
|
||||
|
||||
if err := CheckPermission(userID, "superuser", "admin"); err != nil {
|
||||
postLog.Error(fmt.Sprintf("[CreateInstanceHandler] Failed to check permission: %v", err))
|
||||
SendErrorResponse(w, http.StatusForbidden, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if req.InstanceInfo.Name == "" || req.InstanceInfo.ServerAddr == "" ||
|
||||
req.InstanceInfo.ServerPort == "" || req.InstanceInfo.AuthMethod == "" {
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Missing required fields in instanceInfo")
|
||||
@@ -177,8 +165,10 @@ func CreateInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[DeleteInstanceHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -209,19 +199,6 @@ func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := CheckPermission(userID, "superuser", "admin"); err != nil {
|
||||
postLog.Error(fmt.Sprintf("[DeleteInstanceHandler] Failed to check permission: %v", err))
|
||||
SendErrorResponse(w, http.StatusForbidden, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
@@ -261,9 +238,10 @@ func DeleteInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Invalid request method: %s", r.Method))
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[ModifyInstanceHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -311,19 +289,6 @@ func ModifyInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if err := CheckPermission(userID, "superuser", "admin"); err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] Failed to check permission: %v", err))
|
||||
SendErrorResponse(w, http.StatusForbidden, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
postLog.Error(fmt.Sprintf("[ModifyInstanceHandler] User %d tried to modify a not existed instance: %d", userID, instanceID))
|
||||
@@ -526,16 +491,10 @@ func getStringFromMap(m map[string]interface{}, key string) string {
|
||||
}
|
||||
|
||||
func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[ListInstancesHandler] Invalid request method: %s", r.Method))
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
userID, err := Auth(w, r, http.MethodGet)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[ListInstancesHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[ListInstancesHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -564,9 +523,10 @@ func ListInstancesHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[StartInstanceHandler] Invalid request method: %s", r.Method))
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[StartInstanceHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -597,25 +557,6 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
|
||||
return
|
||||
}
|
||||
|
||||
userType, err := GetUserType(userID)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Failed to get user type: %v", err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user type")
|
||||
return
|
||||
}
|
||||
if userType != "admin" && userType != "superuser" {
|
||||
SendErrorResponse(w, http.StatusForbidden, "Permission Denied")
|
||||
postLog.Error(fmt.Sprintf("[StartInstanceHandler] Permission Denied for user %d (type: %s)", userID, userType))
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
@@ -678,9 +619,10 @@ func StartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[StopInstanceHandler] Invalid request method: %s", r.Method))
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[StopInstanceHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -711,25 +653,6 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[StopInstanceHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
|
||||
return
|
||||
}
|
||||
|
||||
userType, err := GetUserType(userID)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[StopInstanceHandler] Failed to get user type: %v", err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user type")
|
||||
return
|
||||
}
|
||||
if userType != "admin" && userType != "superuser" {
|
||||
SendErrorResponse(w, http.StatusForbidden, "Permission Denied")
|
||||
postLog.Error(fmt.Sprintf("[StopInstanceHandler] Permission Denied for user %d (type: %s)", userID, userType))
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
@@ -792,9 +715,10 @@ func StopInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[RestartInstanceHandler] Invalid request method: %s", r.Method))
|
||||
userID, err := Auth(w, r, http.MethodPost, "superuser", "admin")
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[RestartInstanceHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -825,25 +749,6 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
|
||||
return
|
||||
}
|
||||
|
||||
userType, err := GetUserType(userID)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Failed to get user type: %v", err))
|
||||
SendErrorResponse(w, http.StatusInternalServerError, "Failed to get user type")
|
||||
return
|
||||
}
|
||||
if userType != "admin" && userType != "superuser" {
|
||||
SendErrorResponse(w, http.StatusForbidden, "Permission Denied")
|
||||
postLog.Error(fmt.Sprintf("[RestartInstanceHandler] Permission Denied for user %d (type: %s)", userID, userType))
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
@@ -906,9 +811,10 @@ func RestartInstanceHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[GetInstanceStatusHandler] Invalid request method: %s", r.Method))
|
||||
userID, err := Auth(w, r, http.MethodGet)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[GetInstanceStatusHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -925,13 +831,6 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[GetInstanceStatusHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
@@ -974,9 +873,10 @@ func GetInstanceStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
SendErrorResponse(w, http.StatusMethodNotAllowed, "Invalid request method")
|
||||
postLog.Debug(fmt.Sprintf("[GetInstanceInfoHandler] Invalid request method: %s", r.Method))
|
||||
userID, err := Auth(w, r, http.MethodGet)
|
||||
if err != nil {
|
||||
SendErrorResponse(w, http.StatusUnauthorized, err.Error())
|
||||
postLog.Warning(fmt.Sprintf("[GetInstanceInfoHandler] Auth failed: %v", err))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -993,13 +893,6 @@ func GetInstanceInfoHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
userID, _, err := ValidateRequestWithHeader(w, r)
|
||||
if err != nil {
|
||||
postLog.Error(fmt.Sprintf("[GetInstanceInfoHandler] Failed to validate request header: %v", err))
|
||||
SendErrorResponse(w, http.StatusBadRequest, "Invalid request header")
|
||||
return
|
||||
}
|
||||
|
||||
instance, err := DBQueryFrpcInstanceByID(instanceID)
|
||||
if err == sql.ErrNoRows {
|
||||
SendErrorResponse(w, http.StatusNotFound, "Instance not found")
|
||||
|
||||
Reference in New Issue
Block a user