update at 2023/08/14
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"mime/multipart"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -288,7 +287,7 @@ func CheckNorthboundValidRequest(w http.ResponseWriter, r *http.Request) (string
|
||||
// check media type(content type) only support "application/json"
|
||||
// response 415-1
|
||||
if !IsJsonContentType(r) {
|
||||
log.Error("Invalid Content-Type")
|
||||
log.Error("invalid Content-Type")
|
||||
ResponseUnsupportedMediaType415(w)
|
||||
return token, err
|
||||
}
|
||||
@@ -297,14 +296,14 @@ func CheckNorthboundValidRequest(w http.ResponseWriter, r *http.Request) (string
|
||||
// 401-1 response
|
||||
token, ret = oauth.IsCarriedToken(r)
|
||||
if ret == false {
|
||||
log.Error("AccessToken is not carried")
|
||||
log.Error("accessToken is not carried")
|
||||
ResponseUnauthorized401AccessTokenNotCarried(w)
|
||||
return token, err
|
||||
}
|
||||
|
||||
// 401-2 response
|
||||
if dborm.XormExistValidToken(token, config.GetExpiresFromConfig()) == false {
|
||||
log.Error("AccessToken fails or does not exist")
|
||||
log.Error("accessToken fails or does not exist")
|
||||
ResponseUnauthorized401AccessTokenNotExist(w)
|
||||
return token, err
|
||||
}
|
||||
@@ -385,14 +384,14 @@ func CheckCommonValidRequest(w http.ResponseWriter, r *http.Request) (string, er
|
||||
// 401-1 response
|
||||
token, ret = oauth.IsCarriedToken(r)
|
||||
if ret == false {
|
||||
log.Error("AccessToken is not carried")
|
||||
log.Error("accessToken is not carried")
|
||||
ResponseUnauthorized401AccessTokenNotCarried(w)
|
||||
return token, err
|
||||
}
|
||||
|
||||
// 401-2 response
|
||||
if dborm.XormExistValidToken(token, config.GetExpiresFromConfig()) == false {
|
||||
log.Error("AccessToken fails or does not exist")
|
||||
log.Error("accessToken fails or does not exist")
|
||||
ResponseUnauthorized401AccessTokenNotExist(w)
|
||||
return token, err
|
||||
}
|
||||
@@ -417,6 +416,20 @@ func CheckCommonValidRequest(w http.ResponseWriter, r *http.Request) (string, er
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func CheckUserPermission(token, method, dbname, tbname string) (bool, error) {
|
||||
if config.GetYamlConfig().OMC.RBACMode == true {
|
||||
exist, err := dborm.IsPermissionAllowed(token, method, dbname, tbname)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if !exist {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func IsLocalhost(host string) bool {
|
||||
if strings.Contains(host, "127.0.0.1") || strings.Contains(host, "::1") {
|
||||
return true
|
||||
@@ -455,7 +468,7 @@ func CheckFrontValidRequest(w http.ResponseWriter, r *http.Request) (string, err
|
||||
if config.GetYamlConfig().Auth.Token && IsLocalhost(r.RemoteAddr) == false {
|
||||
token, ret = oauth.IsCarriedToken(r)
|
||||
if ret == false {
|
||||
err = errors.New("AccessToken is not carried")
|
||||
err = errors.New("accessToken is not carried")
|
||||
log.Error(err)
|
||||
ResponseUnauthorized401AccessTokenNotCarried(w)
|
||||
return token, err
|
||||
@@ -463,7 +476,7 @@ func CheckFrontValidRequest(w http.ResponseWriter, r *http.Request) (string, err
|
||||
|
||||
// 401-2 response
|
||||
if dborm.XormExistValidToken(token, config.GetExpiresFromConfig()) == false {
|
||||
err = errors.New("AccessToken fails or does not exist")
|
||||
err = errors.New("accessToken fails or does not exist")
|
||||
log.Error(err)
|
||||
ResponseUnauthorized401AccessTokenNotExist(w)
|
||||
return token, err
|
||||
@@ -480,17 +493,6 @@ func CheckFrontValidRequest(w http.ResponseWriter, r *http.Request) (string, err
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
// response 403 Forbidden, permissions deny
|
||||
// todo...
|
||||
plist := globalSession.GetPermissionFromSession(token)
|
||||
log.Debug("permission list:", plist)
|
||||
if len(plist) == 0 || plist[0] == false {
|
||||
log.Debug("User permission deny")
|
||||
ResponseForbidden403NotPermission(w)
|
||||
return
|
||||
}
|
||||
*/
|
||||
vars := mux.Vars(r)
|
||||
apiVer := vars["apiVersion"]
|
||||
if apiVer != global.ApiVersionV1 {
|
||||
@@ -514,7 +516,7 @@ func CheckExtValidRequest(w http.ResponseWriter, r *http.Request) (string, error
|
||||
if config.GetYamlConfig().Auth.Token {
|
||||
token, ret = oauth.IsCarriedToken(r)
|
||||
if ret == false {
|
||||
err = errors.New("AccessToken is not carried")
|
||||
err = errors.New("accessToken is not carried")
|
||||
log.Error(err)
|
||||
ResponseUnauthorized401AccessTokenNotCarried(w)
|
||||
return token, err
|
||||
@@ -522,7 +524,7 @@ func CheckExtValidRequest(w http.ResponseWriter, r *http.Request) (string, error
|
||||
|
||||
// 401-2 response
|
||||
if dborm.XormExistValidToken(token, config.GetExpiresFromConfig()) == false {
|
||||
err = errors.New("AccessToken fails or does not exist")
|
||||
err = errors.New("accessToken fails or does not exist")
|
||||
log.Error(err)
|
||||
ResponseUnauthorized401AccessTokenNotExist(w)
|
||||
return token, err
|
||||
@@ -541,7 +543,7 @@ func CheckExtValidRequest(w http.ResponseWriter, r *http.Request) (string, error
|
||||
vars := mux.Vars(r)
|
||||
apiVer := vars["apiVersion"]
|
||||
if apiVer != global.ApiVersionV1 {
|
||||
err = errors.New("Uri is invalid")
|
||||
err = errors.New("uri is invalid")
|
||||
log.Error(err)
|
||||
ResponseNotFound404UriNotExist(w, r)
|
||||
return token, err
|
||||
@@ -568,11 +570,22 @@ func ResponseStatusOK204NoContent(w http.ResponseWriter) {
|
||||
ResponseWithJson(w, http.StatusNoContent, "")
|
||||
}
|
||||
|
||||
func ResponseRedirect(w http.ResponseWriter, redirectUrl string) {
|
||||
func ResponseStatusOK201Accepted(w http.ResponseWriter) {
|
||||
ResponseWithJson(w, http.StatusAccepted, "")
|
||||
}
|
||||
|
||||
type SSORedirect struct {
|
||||
User string `json:"user"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
func ResponseRedirect(w http.ResponseWriter, redirectUrl, user, token string) {
|
||||
w.Header().Set("Cache-Control", "must-revalidate, no-store")
|
||||
w.Header().Set("Content-Type", " text/html;charset=UTF-8")
|
||||
w.Header().Set("Location", redirectUrl) //跳转地址设置
|
||||
w.WriteHeader(http.StatusTemporaryRedirect) //重定向!
|
||||
w.Header().Set("Location", redirectUrl) //跳转地址设置
|
||||
//w.WriteHeader(http.StatusTemporaryRedirect) //重定向!
|
||||
ssoRedirect := &SSORedirect{user, token}
|
||||
ResponseWithJson(w, http.StatusTemporaryRedirect, *ssoRedirect)
|
||||
}
|
||||
|
||||
func ResponseBadRequest400RmUIDsIsInvalid(w http.ResponseWriter, rmUIDs []string) {
|
||||
@@ -594,13 +607,13 @@ func ResponseBadRequest400DuplicateAlarmId(w http.ResponseWriter, AlarmIds strin
|
||||
}
|
||||
|
||||
func ResponseBadRequest400IncorrectLogin(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"4", "Incorrect username and password"}
|
||||
errorMessage := ErrorMessage{"4", "incorrect username and password"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusBadRequest, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseBadRequest400WrongParamValue(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"5", "Wrong parameter value"}
|
||||
errorMessage := ErrorMessage{"5", "wrong parameter value"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusBadRequest, errorResponse)
|
||||
}
|
||||
@@ -612,43 +625,43 @@ func ResponseBadRequest400CMCALoginError(w http.ResponseWriter) {
|
||||
}
|
||||
|
||||
func ResponseBadRequest400InvalidJson(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"7", "Invalid json format"}
|
||||
errorMessage := ErrorMessage{"7", "invalid json format"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusUnauthorized, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseUnauthorized401AccessTokenNotCarried(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"1", "AccessToken is not carried"}
|
||||
errorMessage := ErrorMessage{"1", "accessToken is not carried"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusUnauthorized, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseUnauthorized401AccessTokenNotExist(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"2", "AccessToken fails or does not exist"}
|
||||
errorMessage := ErrorMessage{"2", "accessToken fails or does not exist"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusUnauthorized, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseForbidden403NotPermission(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"1", "Do not have the operation permissions"}
|
||||
errorMessage := ErrorMessage{"1", "do not have the operation permissions"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusForbidden, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseForbidden403MultiLoginNotAllowed(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"2", "Multiple logins are not allowed"}
|
||||
errorMessage := ErrorMessage{"2", "multiple logins are not allowed"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusForbidden, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotFound404UriNotExist(w http.ResponseWriter, r *http.Request) {
|
||||
errorMessage := ErrorMessage{"1", "The requested URI does not exist"}
|
||||
errorMessage := ErrorMessage{"1", "the requested URI does not exist"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotFound404UriNotExistExt(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"1", "The requested URI does not exist"}
|
||||
errorMessage := ErrorMessage{"1", "the requested URI does not exist"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
@@ -670,37 +683,41 @@ func ResponseNotFound404PMNotExist(w http.ResponseWriter, rmUIDs []string) {
|
||||
}
|
||||
|
||||
func ResponseNotFound404AlarmNotExist(w http.ResponseWriter, AlarmIds []string) {
|
||||
errorMessage := ErrorMessage{"4", "AlarmIds does not exist: " + strings.Join(AlarmIds, ",")}
|
||||
errorMessage := ErrorMessage{"4", "alarmIds does not exist: " + strings.Join(AlarmIds, ",")}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotFound404GetSubscriptionNotExist(w http.ResponseWriter, SubIds []string) {
|
||||
errorMessage := ErrorMessage{"5", "Subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorMessage := ErrorMessage{"5", "subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotFound404DeleteSubscriptionNotExist(w http.ResponseWriter, SubIds []string) {
|
||||
errorMessage := ErrorMessage{"6", "Subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorMessage := ErrorMessage{"6", "subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotFound404GetAlarmSubscriptionNotExist(w http.ResponseWriter, SubIds []string) {
|
||||
errorMessage := ErrorMessage{"7", "Subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorMessage := ErrorMessage{"7", "subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotFound404DeleteAlarmSubscriptionNotExist(w http.ResponseWriter, SubIds []string) {
|
||||
errorMessage := ErrorMessage{"8", "Subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorMessage := ErrorMessage{"8", "subscription id does not exist: " + strings.Join(SubIds, ",")}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotFound, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseMethodNotAllowed405(w http.ResponseWriter, r *http.Request) {
|
||||
errorMessage := ErrorMessage{"1", "Method not allowed"}
|
||||
if r.Method == "OPTIONS" {
|
||||
ResponseStatusOK204NoContent(w)
|
||||
return
|
||||
}
|
||||
errorMessage := ErrorMessage{"1", "method not allowed"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusMethodNotAllowed, errorResponse)
|
||||
}
|
||||
@@ -710,79 +727,79 @@ func CustomResponseMethodNotAllowed405Handler() http.Handler {
|
||||
}
|
||||
|
||||
func ResponseNotAcceptable406MissingParam(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"1", "Missing parameter: rmUIDs"}
|
||||
errorMessage := ErrorMessage{"1", "missing parameter: rmUIDs"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotAcceptable, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotAcceptable406ParamError(w http.ResponseWriter, errorParamsName []string) {
|
||||
errorMessage := ErrorMessage{"2", "Parameter name error: " + strings.Join(errorParamsName, ",")}
|
||||
errorMessage := ErrorMessage{"2", "parameter name error: " + strings.Join(errorParamsName, ",")}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotAcceptable, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseNotAcceptable406QuerySQLError(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"3", "Wrong or non-query SQL statement"}
|
||||
errorMessage := ErrorMessage{"3", "wrong or non-query SQL statement"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusNotAcceptable, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseRequestEntityTooLarge413SubscriptionExceed(w http.ResponseWriter, num int) {
|
||||
errorMessage := ErrorMessage{"1", "The number of subscriptions greater than " + strconv.Itoa(num)}
|
||||
errorMessage := ErrorMessage{"1", "the number of subscriptions greater than " + strconv.Itoa(num)}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusRequestEntityTooLarge, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseRequestEntityTooLarge413BodyToLarge(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"2", "The request entity too large"}
|
||||
errorMessage := ErrorMessage{"2", "the request entity too large"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusRequestEntityTooLarge, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseRequestURITooLong414NRMNumExceed(w http.ResponseWriter, num int) {
|
||||
errorMessage := ErrorMessage{"1", "The number of NRM rmUIDs greater than " + strconv.Itoa(num)}
|
||||
errorMessage := ErrorMessage{"1", "the number of NRM rmUIDs greater than " + strconv.Itoa(num)}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusRequestURITooLong, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseRequestURITooLong414AlarmNumExceed(w http.ResponseWriter, num int) {
|
||||
errorMessage := ErrorMessage{"2", "The number of alarmIds greater than " + strconv.Itoa(num)}
|
||||
errorMessage := ErrorMessage{"2", "the number of alarmIds greater than " + strconv.Itoa(num)}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusRequestURITooLong, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseRequestURITooLong414PMNumExceed(w http.ResponseWriter, num int) {
|
||||
errorMessage := ErrorMessage{"3", "The number of PM rmUIDs greater than " + strconv.Itoa(num)}
|
||||
errorMessage := ErrorMessage{"3", "the number of PM rmUIDs greater than " + strconv.Itoa(num)}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusRequestURITooLong, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseRequestURITooLong414UriTooLong(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"3", "Request URI too long"}
|
||||
errorMessage := ErrorMessage{"3", "request URI too long"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusRequestURITooLong, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseUnsupportedMediaType415(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"1", "Unsupported media type"}
|
||||
errorMessage := ErrorMessage{"1", "unsupported media type"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusUnsupportedMediaType, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseInternalServerError500NFConnectRefused(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"1", "Internal server error, NF connnect refused"}
|
||||
errorMessage := ErrorMessage{"1", "internal server error, NF connnect refused"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusInternalServerError, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseInternalServerError500DatabaseOperationFailed(w http.ResponseWriter) {
|
||||
errorMessage := ErrorMessage{"2", "Internal server error, database opration failed"}
|
||||
errorMessage := ErrorMessage{"2", "internal server error, database opration failed"}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusInternalServerError, errorResponse)
|
||||
}
|
||||
|
||||
func ResponseInternalServerError500ProcessError(w http.ResponseWriter, err error) {
|
||||
em := fmt.Sprintf("Internal server error: %v", err)
|
||||
em := fmt.Sprintf("internal server error: %v", err)
|
||||
errorMessage := ErrorMessage{"3", em}
|
||||
errorResponse := ErrorResponse{errorMessage}
|
||||
ResponseWithJson(w, http.StatusInternalServerError, errorResponse)
|
||||
@@ -919,7 +936,7 @@ func ResponseFile(w http.ResponseWriter, code int, filePath string) {
|
||||
|
||||
func ResponseFileWithNameAndMD5(w http.ResponseWriter, code int, fileName, path, md5Sum string) {
|
||||
filePath := path + "/" + fileName
|
||||
fileBytes, err := ioutil.ReadFile(filePath)
|
||||
fileBytes, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
log.Error("Failed to ReadFile:", err)
|
||||
ResponseInternalServerError500ProcessError(w, err)
|
||||
|
||||
Reference in New Issue
Block a user