forked from filecoin-project/lotus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
66 lines (53 loc) · 1.21 KB
/
auth.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
package cli
import (
"errors"
"fmt"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/api/apistruct"
)
var authCmd = &cli.Command{
Name: "auth",
Usage: "Manage RPC permissions",
Subcommands: []*cli.Command{
authCreateAdminToken,
},
}
var authCreateAdminToken = &cli.Command{
Name: "create-token",
Usage: "Create token",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "perm",
Usage: "permission to assign to the token, one of: read, write, sign, admin",
},
},
Action: func(cctx *cli.Context) error {
napi, closer, err := GetAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
if !cctx.IsSet("perm") {
return errors.New("--perm flag not set")
}
perm := cctx.String("perm")
idx := 0
for i, p := range apistruct.AllPermissions {
if perm == p {
idx = i + 1
}
}
if idx == 0 {
return fmt.Errorf("--perm flag has to be one of: %s", apistruct.AllPermissions)
}
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
token, err := napi.AuthNew(ctx, apistruct.AllPermissions[:idx])
if err != nil {
return err
}
// TODO: Log in audit log when it is implemented
fmt.Println(string(token))
return nil
},
}