lotus/cli/auth.go

67 lines
1.2 KiB
Go
Raw Normal View History

2019-09-18 17:53:48 +00:00
package cli
import (
"errors"
2019-09-18 17:53:48 +00:00
"fmt"
"gopkg.in/urfave/cli.v2"
"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{
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)
if !cctx.IsSet("perm") {
return errors.New("--perm flag not set")
}
perm := cctx.String("perm")
2019-11-18 16:48:58 +00:00
idx := 0
for i, p := range api.AllPermissions {
if perm == p {
2019-11-18 16:48:58 +00:00
idx = i + 1
}
}
2019-11-18 16:48:58 +00:00
if idx == 0 {
return fmt.Errorf("--perm flag has to be one of: %s", api.AllPermissions)
}
// slice on [:idx] so for example: 'sign' gives you [read, write, sign]
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
},
}