Improve how HTTP status on error is sent

This commit is contained in:
Denis Arh
2021-04-29 07:46:07 +02:00
parent b245726c9b
commit 10ef5b0df5
3 changed files with 32 additions and 24 deletions
+3 -6
View File
@@ -1,8 +1,7 @@
package auth
import (
"errors"
"github.com/cortezaproject/corteza-server/pkg/api"
"github.com/cortezaproject/corteza-server/pkg/errors"
"net/http"
)
@@ -17,15 +16,13 @@ func AccessTokenCheck(scope ...string) func(http.Handler) http.Handler {
for _, s := range scope {
if !CheckScope(ctx.Value(scopeCtxKey{}), s) {
w.WriteHeader(http.StatusUnauthorized)
api.Send(w, r, errors.New("unauthorized scope"))
errors.ProperlyServeHTTP(w, r, errors.Unauthorized("unauthorized scope"), false)
return
}
}
if !GetIdentityFromContext(ctx).Valid() {
w.WriteHeader(http.StatusUnauthorized)
api.Send(w, r, errors.New("unauthorized"))
errors.ProperlyServeHTTP(w, r, errors.Unauthorized("unauthorized"), false)
return
}