2019-09-18 17:53:48 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2019-11-14 20:19:52 +00:00
|
|
|
"errors"
|
2019-09-18 17:53:48 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-09-18 17:53:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var authCmd = &cli.Command{
|
|
|
|
Name: "auth",
|
|
|
|
Usage: "Manage RPC permissions",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
authCreateAdminToken,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var authCreateAdminToken = &cli.Command{
|
2019-11-14 20:19:52 +00:00
|
|
|
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",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2019-09-18 17:53:48 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-10-03 18:12:30 +00:00
|
|
|
napi, closer, err := GetFullNodeAPI(cctx)
|
2019-09-18 17:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
|
|
|
|
2019-09-18 17:53:48 +00:00
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-11-14 20:19:52 +00:00
|
|
|
if !cctx.IsSet("perm") {
|
|
|
|
return errors.New("--perm flag not set")
|
|
|
|
}
|
|
|
|
|
|
|
|
perm := cctx.String("perm")
|
|
|
|
idx := -1
|
|
|
|
for i, p := range api.AllPermissions {
|
|
|
|
if perm == p {
|
|
|
|
idx = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf("--perm flag has to be one of: %s", api.AllPermissions)
|
|
|
|
}
|
|
|
|
|
2019-11-14 20:37:11 +00:00
|
|
|
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
|
2019-11-14 20:19:52 +00:00
|
|
|
token, err := napi.AuthNew(ctx, api.AllPermissions[:idx])
|
2019-09-18 17:53:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Log in audit log when it is implemented
|
|
|
|
|
|
|
|
fmt.Println(string(token))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|