Skip to content

Commit

Permalink
fixed an issue where a unauthorized returned a 200
Browse files Browse the repository at this point in the history
  • Loading branch information
cjlapao committed Oct 20, 2023
1 parent 9e3d200 commit 0f25f0c
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
8 changes: 4 additions & 4 deletions controllers/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func GetTokenController() restapi.Controller {
if user == nil {
ReturnApiError(w, models.ApiErrorResponse{
Message: "User not found",
Code: http.StatusInternalServerError,
Code: http.StatusUnauthorized,
})
return
}
Expand All @@ -61,7 +61,7 @@ func GetTokenController() restapi.Controller {
if hashedPassword != user.Password {
ReturnApiError(w, models.ApiErrorResponse{
Message: "Invalid Password",
Code: http.StatusInternalServerError,
Code: http.StatusUnauthorized,
})
return
}
Expand All @@ -86,7 +86,7 @@ func GetTokenController() restapi.Controller {
key := []byte(services.GetServices().HardwareSecret)
tokenString, err := token.SignedString(key)
if err != nil {
ReturnApiError(w, models.NewFromError(err))
ReturnApiError(w, models.NewFromErrorWithCode(err, 401))
return
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func ValidateTokenController() restapi.Controller {
})

if err != nil {
ReturnApiError(w, models.NewFromError(err))
ReturnApiError(w, models.NewFromErrorWithCode(err, 401))
return
}

Expand Down
3 changes: 1 addition & 2 deletions controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
)

func ReturnApiError(w http.ResponseWriter, err models.ApiErrorResponse) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.WriteHeader(err.Code)
json.NewEncoder(w).Encode(err)
}
4 changes: 2 additions & 2 deletions models/api_error_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package models

type ApiErrorResponse struct {
Message string `json:"message"`
Code int32 `json:"code"`
Code int `json:"code"`
}

func NewFromError(err error) ApiErrorResponse {
return NewFromErrorWithCode(err, 500)
}

func NewFromErrorWithCode(err error, code int32) ApiErrorResponse {
func NewFromErrorWithCode(err error, code int) ApiErrorResponse {
return ApiErrorResponse{
Message: err.Error(),
Code: code,
Expand Down

0 comments on commit 0f25f0c

Please sign in to comment.