-
Notifications
You must be signed in to change notification settings - Fork 108
/
claims.go
230 lines (192 loc) · 6.27 KB
/
claims.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package auth
import (
"context"
"errors"
"fmt"
"sync"
"github.com/rilldata/rill/admin"
"github.com/rilldata/rill/admin/pkg/authtoken"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
"go.uber.org/zap"
)
// OwnerType is an enum of types of claim owners
type OwnerType string
const (
OwnerTypeAnon OwnerType = "anon"
OwnerTypeUser OwnerType = "user"
OwnerTypeService OwnerType = "service"
OwnerTypeDeployment OwnerType = "deployment"
)
// Claims resolves permissions for a requester.
type Claims interface {
OwnerType() OwnerType
OwnerID() string
AuthTokenID() string
Superuser(ctx context.Context) bool
OrganizationPermissions(ctx context.Context, orgID string) *adminv1.OrganizationPermissions
ProjectPermissions(ctx context.Context, orgID, projectID string) *adminv1.ProjectPermissions
}
// claimsContextKey is used to set and get Claims on a request context.
type claimsContextKey struct{}
// GetClaims retrieves Claims from a request context.
// It should only be used in handlers intercepted by UnaryServerInterceptor or StreamServerInterceptor.
func GetClaims(ctx context.Context) Claims {
claims, ok := ctx.Value(claimsContextKey{}).(Claims)
if !ok {
return nil
}
return claims
}
// anonClaims represents claims for an unauthenticated user.
type anonClaims struct{}
// ensure anonClaims implements Claims
var _ Claims = anonClaims{}
func (c anonClaims) OwnerType() OwnerType {
return OwnerTypeAnon
}
func (c anonClaims) OwnerID() string {
return ""
}
func (c anonClaims) AuthTokenID() string {
return ""
}
func (c anonClaims) Superuser(ctx context.Context) bool {
return false
}
func (c anonClaims) OrganizationPermissions(ctx context.Context, orgID string) *adminv1.OrganizationPermissions {
return &adminv1.OrganizationPermissions{}
}
func (c anonClaims) ProjectPermissions(ctx context.Context, orgID, projectID string) *adminv1.ProjectPermissions {
return &adminv1.ProjectPermissions{}
}
// authTokenClaims represents claims for an admin.AuthToken.
type authTokenClaims struct {
sync.Mutex
token admin.AuthToken
admin *admin.Service
orgPermissionsCache map[string]*adminv1.OrganizationPermissions
projectPermissionsCache map[string]*adminv1.ProjectPermissions
superuser *bool
}
func newAuthTokenClaims(token admin.AuthToken, adminService *admin.Service) Claims {
return &authTokenClaims{
token: token,
admin: adminService,
orgPermissionsCache: make(map[string]*adminv1.OrganizationPermissions),
projectPermissionsCache: make(map[string]*adminv1.ProjectPermissions),
superuser: nil,
}
}
// ensure *authTokenClaims implements Claims
var _ Claims = &authTokenClaims{}
func (c *authTokenClaims) OwnerType() OwnerType {
t := c.token.Token().Type
switch t {
case authtoken.TypeUser:
return OwnerTypeUser
case authtoken.TypeService:
return OwnerTypeService
case authtoken.TypeDeployment:
return OwnerTypeDeployment
default:
panic(fmt.Errorf("unexpected token type %q", t))
}
}
func (c *authTokenClaims) OwnerID() string {
return c.token.OwnerID()
}
func (c *authTokenClaims) AuthTokenID() string {
return c.token.Token().ID.String()
}
func (c *authTokenClaims) Superuser(ctx context.Context) bool {
switch c.token.Token().Type {
case authtoken.TypeUser:
// continue
case authtoken.TypeService:
// services can't be superusers
return false
case authtoken.TypeDeployment:
// deployments can't be superusers
return false
default:
panic(fmt.Errorf("unexpected token type %q", c.token.Token().Type))
}
c.Lock()
defer c.Unlock()
if c.superuser != nil {
return *c.superuser
}
user, err := c.admin.DB.FindUser(ctx, c.token.OwnerID())
if err != nil {
if !errors.Is(err, ctx.Err()) {
c.admin.Logger.Error("failed to get superuser info", zap.Error(err))
}
return false
}
c.superuser = &user.Superuser
return *c.superuser
}
func (c *authTokenClaims) OrganizationPermissions(ctx context.Context, orgID string) *adminv1.OrganizationPermissions {
c.Lock()
defer c.Unlock()
perms, _ := c.organizationPermissionsUnsafe(ctx, orgID)
return perms
}
func (c *authTokenClaims) ProjectPermissions(ctx context.Context, orgID, projectID string) *adminv1.ProjectPermissions {
c.Lock()
defer c.Unlock()
perm, ok := c.projectPermissionsCache[projectID]
if ok {
return perm
}
orgPerms, ok := c.organizationPermissionsUnsafe(ctx, orgID)
if !ok {
return &adminv1.ProjectPermissions{}
}
var err error
switch c.token.Token().Type {
case authtoken.TypeUser:
perm, err = c.admin.ProjectPermissionsForUser(ctx, projectID, c.token.OwnerID(), orgPerms)
case authtoken.TypeService:
perm, err = c.admin.ProjectPermissionsForService(ctx, projectID, c.token.OwnerID(), orgPerms)
case authtoken.TypeDeployment:
perm, err = c.admin.ProjectPermissionsForDeployment(ctx, projectID, c.token.OwnerID(), orgPerms)
default:
err = fmt.Errorf("unexpected token type %q", c.token.Token().Type)
}
if err != nil {
if !errors.Is(err, ctx.Err()) {
c.admin.Logger.Error("failed to get project permissions", zap.Error(err))
}
return &adminv1.ProjectPermissions{}
}
c.projectPermissionsCache[projectID] = perm
return perm
}
// organizationPermissionsUnsafe resolves organization permissions.
// organizationPermissionsUnsafe accesses the cache without locking, so it should only be called from a function that already has a lock.
func (c *authTokenClaims) organizationPermissionsUnsafe(ctx context.Context, orgID string) (*adminv1.OrganizationPermissions, bool) {
perm, ok := c.orgPermissionsCache[orgID]
if ok {
return perm, true
}
var err error
switch c.token.Token().Type {
case authtoken.TypeUser:
perm, err = c.admin.OrganizationPermissionsForUser(ctx, orgID, c.token.OwnerID())
case authtoken.TypeService:
perm, err = c.admin.OrganizationPermissionsForService(ctx, orgID, c.token.OwnerID())
case authtoken.TypeDeployment:
perm, err = c.admin.OrganizationPermissionsForDeployment(ctx, orgID, c.token.OwnerID())
default:
err = fmt.Errorf("unexpected token type %q", c.token.Token().Type)
}
if err != nil {
if !errors.Is(err, ctx.Err()) {
c.admin.Logger.Error("failed to get organization permissions", zap.Error(err))
}
return &adminv1.OrganizationPermissions{}, false
}
c.orgPermissionsCache[orgID] = perm
return perm, true
}