Skip to content

Commit b831260

Browse files
committed
Use rbac.object directly
1 parent f36ae37 commit b831260

File tree

2 files changed

+5
-16
lines changed

2 files changed

+5
-16
lines changed

coderd/httpmw/authorize.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,15 @@ import (
1313
"github.com/coder/coder/coderd/rbac"
1414
)
1515

16-
// AuthObject wraps the rbac object type for middleware to customize this value
17-
// before being passed to Authorize().
18-
type AuthObject struct {
19-
// Object is that base static object the above functions can modify.
20-
Object rbac.Object
21-
}
22-
2316
// Authorize will enforce if the user roles can complete the action on the AuthObject.
2417
// The organization and owner are found using the ExtractOrganization and
2518
// ExtractUser middleware if present.
2619
func Authorize(logger slog.Logger, auth *rbac.RegoAuthorizer, action rbac.Action) func(http.Handler) http.Handler {
2720
return func(next http.Handler) http.Handler {
2821
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
2922
roles := UserRoles(r)
30-
args := GetAuthObject(r)
23+
object := authObject(r)
3124

32-
object := args.Object
3325
if object.Type == "" {
3426
panic("developer error: auth object has no type")
3527
}
@@ -80,8 +72,8 @@ func Authorize(logger slog.Logger, auth *rbac.RegoAuthorizer, action rbac.Action
8072
type authObjectKey struct{}
8173

8274
// APIKey returns the API key from the ExtractAPIKey handler.
83-
func GetAuthObject(r *http.Request) AuthObject {
84-
obj, ok := r.Context().Value(authObjectKey{}).(AuthObject)
75+
func authObject(r *http.Request) rbac.Object {
76+
obj, ok := r.Context().Value(authObjectKey{}).(rbac.Object)
8577
if !ok {
8678
panic("developer error: auth object middleware not provided")
8779
}
@@ -93,10 +85,7 @@ func GetAuthObject(r *http.Request) AuthObject {
9385
func WithRBACObject(object rbac.Object) func(http.Handler) http.Handler {
9486
return func(next http.Handler) http.Handler {
9587
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
96-
ao := GetAuthObject(r)
97-
ao.Object = object
98-
99-
ctx := context.WithValue(r.Context(), authObjectKey{}, ao)
88+
ctx := context.WithValue(r.Context(), authObjectKey{}, object)
10089
next.ServeHTTP(rw, r.WithContext(ctx))
10190
})
10291
}

coderd/roles_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestListRoles(t *testing.T) {
3939
Roles: []string{rbac.RoleOrgMember(admin.OrganizationID), rbac.RoleOrgAdmin(admin.OrganizationID)},
4040
},
4141
)
42-
require.NoError(t, err)
42+
require.NoError(t, err, "update org member roles")
4343

4444
testCases := []struct {
4545
Name string

0 commit comments

Comments
 (0)