diff --git a/docs/packages/http/router_utils.html b/docs/packages/http/router_utils.html index 9ac53123..965c8402 100644 --- a/docs/packages/http/router_utils.html +++ b/docs/packages/http/router_utils.html @@ -209,7 +209,7 @@
ReadOrganization retrieves organization id from request +
ReadOrganizationID retrieves organization id from request if it's not possible, it writes http error to the writer and returns false
func ReadOrganization(writer http.ResponseWriter, request *http.Request, auth bool) (types.OrgID, bool) {
+ func ReadOrganizationID(writer http.ResponseWriter, request *http.Request, auth bool) (types.OrgID, bool) {
organizationID, err := GetRouterPositiveIntParam(request, "organization")
if err != nil {
- handleOrgIDError(writer, err)
+ HandleOrgIDError(writer, err)
return 0, false
}
- successful := checkPermissions(writer, request, types.OrgID(organizationID), auth)
+ successful := CheckPermissions(writer, request, types.OrgID(organizationID), auth)
return types.OrgID(organizationID), successful
}
@@ -308,8 +308,8 @@
}
clusterNamesConverted := make([]types.ClusterName, 0)
- for _, clusterName := range splitRequestParamArray(clusterNamesParam) {
- convertedName, err := validateClusterName(clusterName)
+ for _, clusterName := range SplitRequestParamArray(clusterNamesParam) {
+ convertedName, err := ValidateClusterName(clusterName)
if err != nil {
types.HandleServerError(writer, err)
return []types.ClusterName{}, false
@@ -325,17 +325,17 @@
ReadOrganizations does the same as readOrganizationID
, except for multiple organizations.
ReadOrganizationIDs does the same as readOrganizationID
, except for multiple organizations.
func ReadOrganizations(writer http.ResponseWriter, request *http.Request) ([]types.OrgID, bool) {
+ func ReadOrganizationIDs(writer http.ResponseWriter, request *http.Request) ([]types.OrgID, bool) {
organizationsParam, err := GetRouterParam(request, "organizations")
if err != nil {
- handleOrgIDError(writer, err)
+ HandleOrgIDError(writer, err)
return []types.OrgID{}, false
}
organizationsConverted := make([]types.OrgID, 0)
- for _, orgStr := range splitRequestParamArray(organizationsParam) {
+ for _, orgStr := range SplitRequestParamArray(organizationsParam) {
orgInt, err := strconv.ParseUint(orgStr, 10, 64)
if err != nil {
types.HandleServerError(writer, &types.RouterParsingError{
@@ -351,12 +351,25 @@
return organizationsConverted, true
}
-func handleOrgIDError(writer http.ResponseWriter, err error) {
+
+
HandleOrgIDError logs org id error and writes corresponding http response
+func HandleOrgIDError(writer http.ResponseWriter, err error) {
log.Error().Err(err).Msg("error getting organization ID from request")
types.HandleServerError(writer, err)
}
-func checkPermissions(writer http.ResponseWriter, request *http.Request, orgID types.OrgID, auth bool) bool {
+
CheckPermissions checks whether user with a provided token(from request) can access current organization +and handled the error on negative result by logging the error and writing a corresponding http response
+func CheckPermissions(writer http.ResponseWriter, request *http.Request, orgID types.OrgID, auth bool) bool {
identityContext := request.Context().Value(types.ContextKeyUser)
if identityContext != nil && auth {
@@ -364,7 +377,7 @@
if identity.Internal.OrgID != orgID {
const message = "you have no permissions to get or change info about this organization"
log.Error().Msg(message)
- types.HandleServerError(writer, &types.AuthenticationError{ErrString: message})
+ types.HandleServerError(writer, &types.ForbiddenError{ErrString: message})
return false
}
@@ -376,10 +389,10 @@
validateClusterName checks that the cluster name is a valid UUID. +
ValidateClusterName checks that the cluster name is a valid UUID. Converted cluster name is returned if everything is okay, otherwise an error is returned.
func validateClusterName(clusterName string) (types.ClusterName, error) {
+ func ValidateClusterName(clusterName string) (types.ClusterName, error) {
if _, err := uuid.Parse(clusterName); err != nil {
message := fmt.Sprintf("invalid cluster name: '%s'. Error: %s", clusterName, err.Error())
@@ -415,8 +428,7 @@
SplitRequestParamArray takes a single HTTP request parameter and splits it
into a slice of strings. This assumes that the parameter is a comma-separated array.
-
-func splitRequestParamArray(arrayParam string) []string {
+ func SplitRequestParamArray(arrayParam string) []string {
return strings.Split(arrayParam, ",")
}
diff --git a/docs/packages/http/router_utils_test.html b/docs/packages/http/router_utils_test.html
index dc254b36..41c58759 100644
--- a/docs/packages/http/router_utils_test.html
+++ b/docs/packages/http/router_utils_test.html
@@ -242,16 +242,16 @@
case "error_key":
result, successful = httputils.ReadErrorKey(recorder, request)
case "organization":
- result, successful = httputils.ReadOrganization(recorder, request, false)
+ result, successful = httputils.ReadOrganizationID(recorder, request, false)
case "organization/with_auth":
- result, successful = httputils.ReadOrganization(recorder, request, true)
+ result, successful = httputils.ReadOrganizationID(recorder, request, true)
case "clusters":
var results []types.ClusterName
results, successful = httputils.ReadClusterNames(recorder, request)
result = paramsToString(",", results)
case "organizations":
var results []types.OrgID
- results, successful = httputils.ReadOrganizations(recorder, request)
+ results, successful = httputils.ReadOrganizationIDs(recorder, request)
result = paramsToString(",", results)
}
@@ -415,7 +415,7 @@
case "error_key":
_, successful = httputils.ReadErrorKey(recorder, request)
case "organization":
- _, successful = httputils.ReadOrganization(recorder, request, false)
+ _, successful = httputils.ReadOrganizationID(recorder, request, false)
case "organization/with_auth":
ctx := context.WithValue(request.Context(), types.ContextKeyUser, types.Identity{
AccountNumber: testdata.UserID,
@@ -424,9 +424,9 @@
},
})
request = request.WithContext(ctx)
- _, successful = httputils.ReadOrganization(recorder, request, true)
+ _, successful = httputils.ReadOrganizationID(recorder, request, true)
case "organizations":
- _, successful = httputils.ReadOrganizations(recorder, request)
+ _, successful = httputils.ReadOrganizationIDs(recorder, request)
case "clusters":
_, successful = httputils.ReadClusterNames(recorder, request)
default:
diff --git a/docs/packages/responses/responses.html b/docs/packages/responses/responses.html
index 4725022a..6fe1eed1 100644
--- a/docs/packages/responses/responses.html
+++ b/docs/packages/responses/responses.html
@@ -248,8 +248,8 @@
SendBadRequest returns error response with status Bad Request 400
- func SendBadRequest(w http.ResponseWriter, err string) error {
- return Send(http.StatusBadRequest, w, err)
+ func SendBadRequest(w http.ResponseWriter, errorMessage string) error {
+ return Send(http.StatusBadRequest, w, errorMessage)
}
@@ -258,8 +258,8 @@
SendUnauthorized returns error response for unauthorized access with status Unauthorized 401
- func SendUnauthorized(w http.ResponseWriter, data map[string]interface{}) error {
- return Send(http.StatusUnauthorized, w, data)
+ func SendUnauthorized(w http.ResponseWriter, errorMessage string) error {
+ return Send(http.StatusUnauthorized, w, errorMessage)
}
@@ -268,8 +268,8 @@
SendForbidden returns response with status Forbidden 403
- func SendForbidden(w http.ResponseWriter, err string) error {
- return Send(http.StatusForbidden, w, err)
+ func SendForbidden(w http.ResponseWriter, errorMessage string) error {
+ return Send(http.StatusForbidden, w, errorMessage)
}
@@ -278,8 +278,8 @@
SendNotFound returns response with status Not Found 404
- func SendNotFound(w http.ResponseWriter, err string) error {
- return Send(http.StatusNotFound, w, err)
+ func SendNotFound(w http.ResponseWriter, errorMessage string) error {
+ return Send(http.StatusNotFound, w, errorMessage)
}
@@ -288,8 +288,8 @@
SendInternalServerError returns response with status Internal Server Error 500
- func SendInternalServerError(w http.ResponseWriter, err string) error {
- return Send(http.StatusInternalServerError, w, err)
+ func SendInternalServerError(w http.ResponseWriter, errorMessage string) error {
+ return Send(http.StatusInternalServerError, w, errorMessage)
}
@@ -298,8 +298,8 @@
SendServiceUnavailable returns response with status Service Unavailable 503
- func SendServiceUnavailable(w http.ResponseWriter, err string) error {
- return Send(http.StatusServiceUnavailable, w, err)
+ func SendServiceUnavailable(w http.ResponseWriter, errorMessage string) error {
+ return Send(http.StatusServiceUnavailable, w, errorMessage)
}
diff --git a/docs/packages/responses/responses_test.html b/docs/packages/responses/responses_test.html
index 2e429ce0..a2ab8ce8 100644
--- a/docs/packages/responses/responses_test.html
+++ b/docs/packages/responses/responses_test.html
@@ -189,7 +189,6 @@
{"responses.SendOK", responses.SendOK, http.StatusOK},
{"responses.SendCreated", responses.SendCreated, http.StatusCreated},
{"responses.SendAccepted", responses.SendAccepted, http.StatusAccepted},
- {"responses.SendUnauthorized", responses.SendUnauthorized, http.StatusUnauthorized},
}
var headerTestsWithoutData = []struct {
@@ -198,6 +197,8 @@
expectedHeader int
}{
{"responses.SendBadRequest", responses.SendBadRequest, http.StatusBadRequest},
+ {"responses.SendUnauthorized", responses.SendUnauthorized, http.StatusUnauthorized},
+ {"responses.SendForbidden", responses.SendForbidden, http.StatusForbidden},
{"responses.SendForbidden", responses.SendForbidden, http.StatusForbidden},
{"responses.SendNotFound", responses.SendNotFound, http.StatusNotFound},
{"responses.SendInternalServerError", responses.SendInternalServerError, http.StatusInternalServerError},
diff --git a/docs/packages/types/errors.html b/docs/packages/types/errors.html
index e117b6a5..31f50662 100644
--- a/docs/packages/types/errors.html
+++ b/docs/packages/types/errors.html
@@ -188,13 +188,28 @@
- AuthenticationError happens during auth problems, for example malformed token
+ UnauthorizedError means server can't authorize you, for example the token is missing or malformed
- type AuthenticationError struct {
+ type UnauthorizedError struct {
ErrString string
}
-func (e *AuthenticationError) Error() string {
+func (e *UnauthorizedError) Error() string {
+ return e.ErrString
+}
+
+
+
+
+
+ ForbiddenError means you don't have permission to do a particular action,
+for example your account belongs to a different organization
+
+ type ForbiddenError struct {
+ ErrString string
+}
+
+func (e *ForbiddenError) Error() string {
return e.ErrString
}
@@ -228,7 +243,9 @@
respErr = responses.SendBadRequest(writer, "bad type in json data")
case *ItemNotFoundError:
respErr = responses.SendNotFound(writer, err.Error())
- case *AuthenticationError:
+ case *UnauthorizedError:
+ respErr = responses.SendUnauthorized(writer, err.Error())
+ case *ForbiddenError:
respErr = responses.SendForbidden(writer, err.Error())
default:
respErr = responses.SendInternalServerError(writer, "Internal Server Error")