-
Notifications
You must be signed in to change notification settings - Fork 28
/
dispatcher.go
55 lines (49 loc) · 1.7 KB
/
dispatcher.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package lib
import (
"fmt"
"strings"
"v.io/v23/context"
"v.io/v23/conventions"
"v.io/v23/rpc"
"v.io/v23/security"
"v.io/x/ref/services/groups/internal/server"
"v.io/x/ref/services/groups/internal/store/mem"
)
// Authorizer implementing the authorization policy for Create operations.
//
// A user is allowed to create any group that begins with the user id.
//
// TODO(ashankar): This is experimental use of the "conventions" API and of a
// creation policy. This policy was thought of in a 5 minute period. Think
// about this more!
type createAuthorizer struct{}
func (createAuthorizer) Authorize(ctx *context.T, call security.Call) error {
userids := conventions.GetClientUserIds(ctx, call)
for _, uid := range userids {
if strings.HasPrefix(call.Suffix(), uid+"/") {
return nil
}
}
// Revert to the default authorization policy.
if err := security.DefaultAuthorizer().Authorize(ctx, call); err == nil {
return nil
}
return fmt.Errorf("creator user ids %v are not authorized to create group %v, group name must begin with one of the user ids", userids, call.Suffix())
}
// NewGroupsDispatcher creates a new dispatcher for the groups service.
//
// rootDir is the directory for persisting groups.
//
// engine is the storage engine for groups. Currently, only "memstore"
// is supported.
func NewGroupsDispatcher(rootDir, engine string) (rpc.Dispatcher, error) {
switch engine {
case "memstore":
return server.NewManager(mem.New(), createAuthorizer{}), nil
default:
return nil, fmt.Errorf("unknown storage engine %v", engine)
}
}