2022-05-17 18:43:19 +00:00
|
|
|
package coderd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
"cdr.dev/slog"
|
|
|
|
"github.com/coder/coder/coderd/httpmw"
|
|
|
|
"github.com/coder/coder/coderd/rbac"
|
|
|
|
)
|
|
|
|
|
2022-08-24 18:44:22 +00:00
|
|
|
func AuthorizeFilter[O rbac.Objecter](h *HTTPAuthorizer, r *http.Request, action rbac.Action, objects []O) ([]O, error) {
|
2022-09-19 17:39:02 +00:00
|
|
|
roles := httpmw.UserAuthorization(r)
|
|
|
|
objects, err := rbac.Filter(r.Context(), h.Authorizer, roles.ID.String(), roles.Roles, roles.Scope.ToRBAC(), action, objects)
|
2022-08-11 22:07:48 +00:00
|
|
|
if err != nil {
|
|
|
|
// Log the error as Filter should not be erroring.
|
2022-08-24 18:44:22 +00:00
|
|
|
h.Logger.Error(r.Context(), "filter failed",
|
2022-08-11 22:07:48 +00:00
|
|
|
slog.Error(err),
|
|
|
|
slog.F("user_id", roles.ID),
|
|
|
|
slog.F("username", roles.Username),
|
2022-09-19 17:39:02 +00:00
|
|
|
slog.F("scope", roles.Scope),
|
2022-08-11 22:07:48 +00:00
|
|
|
slog.F("route", r.URL.Path),
|
|
|
|
slog.F("action", action),
|
|
|
|
)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return objects, nil
|
2022-05-24 13:43:34 +00:00
|
|
|
}
|
|
|
|
|
2022-08-22 22:02:50 +00:00
|
|
|
type HTTPAuthorizer struct {
|
|
|
|
Authorizer rbac.Authorizer
|
|
|
|
Logger slog.Logger
|
|
|
|
}
|
|
|
|
|
2022-06-14 15:14:05 +00:00
|
|
|
// Authorize will return false if the user is not authorized to do the action.
|
|
|
|
// This function will log appropriately, but the caller must return an
|
|
|
|
// error to the api client.
|
|
|
|
// Eg:
|
2022-08-21 22:32:53 +00:00
|
|
|
//
|
2022-06-14 15:14:05 +00:00
|
|
|
// if !api.Authorize(...) {
|
|
|
|
// httpapi.Forbidden(rw)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
func (api *API) Authorize(r *http.Request, action rbac.Action, object rbac.Objecter) bool {
|
2022-09-20 04:11:01 +00:00
|
|
|
return api.HTTPAuth.Authorize(r, action, object)
|
2022-08-22 22:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Authorize will return false if the user is not authorized to do the action.
|
|
|
|
// This function will log appropriately, but the caller must return an
|
|
|
|
// error to the api client.
|
|
|
|
// Eg:
|
2022-08-24 14:45:14 +00:00
|
|
|
//
|
2022-08-22 22:02:50 +00:00
|
|
|
// if !h.Authorize(...) {
|
|
|
|
// httpapi.Forbidden(rw)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
func (h *HTTPAuthorizer) Authorize(r *http.Request, action rbac.Action, object rbac.Objecter) bool {
|
2022-09-19 17:39:02 +00:00
|
|
|
roles := httpmw.UserAuthorization(r)
|
|
|
|
err := h.Authorizer.ByRoleName(r.Context(), roles.ID.String(), roles.Roles, roles.Scope.ToRBAC(), action, object.RBACObject())
|
2022-05-17 18:43:19 +00:00
|
|
|
if err != nil {
|
|
|
|
// Log the errors for debugging
|
|
|
|
internalError := new(rbac.UnauthorizedError)
|
2022-08-22 22:02:50 +00:00
|
|
|
logger := h.Logger
|
2022-05-17 18:43:19 +00:00
|
|
|
if xerrors.As(err, internalError) {
|
2022-08-22 22:02:50 +00:00
|
|
|
logger = h.Logger.With(slog.F("internal", internalError.Internal()))
|
2022-05-17 18:43:19 +00:00
|
|
|
}
|
|
|
|
// Log information for debugging. This will be very helpful
|
|
|
|
// in the early days
|
|
|
|
logger.Warn(r.Context(), "unauthorized",
|
|
|
|
slog.F("roles", roles.Roles),
|
|
|
|
slog.F("user_id", roles.ID),
|
|
|
|
slog.F("username", roles.Username),
|
2022-09-19 17:39:02 +00:00
|
|
|
slog.F("scope", roles.Scope),
|
2022-05-17 18:43:19 +00:00
|
|
|
slog.F("route", r.URL.Path),
|
|
|
|
slog.F("action", action),
|
|
|
|
slog.F("object", object),
|
|
|
|
)
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|