@@ -4,14 +4,14 @@ import (
4
4
"context"
5
5
"database/sql"
6
6
"fmt"
7
- "time"
8
7
9
8
"golang.org/x/xerrors"
10
9
11
10
"cdr.dev/slog"
12
11
13
12
"github.com/coder/coder/coderd/database"
14
13
"github.com/coder/coder/coderd/rbac"
14
+ "github.com/google/uuid"
15
15
)
16
16
17
17
var _ database.Store = (* AuthzQuerier )(nil )
@@ -75,19 +75,6 @@ func New(db database.Store, authorizer rbac.Authorizer, logger slog.Logger) *Aut
75
75
}
76
76
}
77
77
78
- func (q * AuthzQuerier ) Ping (ctx context.Context ) (time.Duration , error ) {
79
- return q .db .Ping (ctx )
80
- }
81
-
82
- // InTx runs the given function in a transaction.
83
- func (q * AuthzQuerier ) InTx (function func (querier database.Store ) error , txOpts * sql.TxOptions ) error {
84
- return q .db .InTx (func (tx database.Store ) error {
85
- // Wrap the transaction store in an AuthzQuerier.
86
- wrapped := New (tx , q .auth , q .log )
87
- return function (wrapped )
88
- }, txOpts )
89
- }
90
-
91
78
// authorizeContext is a helper function to authorize an action on an object.
92
79
func (q * AuthzQuerier ) authorizeContext (ctx context.Context , action rbac.Action , object rbac.Objecter ) error {
93
80
act , ok := ActorFromContext (ctx )
@@ -102,6 +89,32 @@ func (q *AuthzQuerier) authorizeContext(ctx context.Context, action rbac.Action,
102
89
return nil
103
90
}
104
91
92
+ type authContextKey struct {}
93
+
94
+ // ActorFromContext returns the authorization subject from the context.
95
+ // All authentication flows should set the authorization subject in the context.
96
+ // If no actor is present, the function returns false.
97
+ func ActorFromContext (ctx context.Context ) (rbac.Subject , bool ) {
98
+ a , ok := ctx .Value (authContextKey {}).(rbac.Subject )
99
+ return a , ok
100
+ }
101
+
102
+ func WithAuthorizeContext (ctx context.Context , actor rbac.Subject ) context.Context {
103
+ return context .WithValue (ctx , authContextKey {}, actor )
104
+ }
105
+
106
+ func WithAuthorizeSystemContext (ctx context.Context , roles rbac.ExpandableRoles ) context.Context {
107
+ // TODO: Add protections to search for user roles. If user roles are found,
108
+ // this should panic. That is a developer error that should be caught
109
+ // in unit tests.
110
+ return context .WithValue (ctx , authContextKey {}, rbac.Subject {
111
+ ID : uuid .Nil .String (),
112
+ Roles : roles ,
113
+ Scope : rbac .ScopeAll ,
114
+ Groups : []string {},
115
+ })
116
+ }
117
+
105
118
//
106
119
// Generic functions used to implement the database.Store methods.
107
120
//
0 commit comments