mirror of
https://github.com/notherealmarco/WASAPhoto.git
synced 2025-05-05 12:22:35 +02:00
Add auth error description
This commit is contained in:
parent
038730da3b
commit
44eb1e1fa6
7 changed files with 17 additions and 16 deletions
|
@ -4,8 +4,10 @@ import (
|
|||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/notherealmarco/WASAPhoto/service/api/helpers"
|
||||
"github.com/notherealmarco/WASAPhoto/service/api/reqcontext"
|
||||
"github.com/notherealmarco/WASAPhoto/service/database"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func BuildAuth(header string) (reqcontext.Authorization, error) {
|
||||
|
@ -19,24 +21,23 @@ func BuildAuth(header string) (reqcontext.Authorization, error) {
|
|||
return auth, nil
|
||||
}
|
||||
|
||||
func SendAuthorizationError(f func(db database.AppDatabase, uid string) (reqcontext.AuthStatus, error), uid string, db database.AppDatabase, w http.ResponseWriter, notFoundStatus int) bool {
|
||||
func SendAuthorizationError(f func(db database.AppDatabase, uid string) (reqcontext.AuthStatus, error), uid string, db database.AppDatabase, w http.ResponseWriter, l logrus.FieldLogger, notFoundStatus int) bool {
|
||||
auth, err := f(db, uid)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
// todo: log error and write it to the response
|
||||
helpers.SendInternalError(err, "Authorization error", w, l)
|
||||
return false
|
||||
}
|
||||
if auth == reqcontext.UNAUTHORIZED {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
helpers.SendStatus(http.StatusUnauthorized, w, "Unauthorized", l)
|
||||
return false
|
||||
}
|
||||
if auth == reqcontext.FORBIDDEN {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
helpers.SendStatus(http.StatusForbidden, w, "Forbidden", l)
|
||||
return false
|
||||
}
|
||||
// requested user is not found -> 404 as the resource is not found
|
||||
if auth == reqcontext.USER_NOT_FOUND {
|
||||
w.WriteHeader(notFoundStatus)
|
||||
helpers.SendStatus(notFoundStatus, w, "Resource not found", l)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue