From 94e2c782ee45bd1f6aecb43659477f61ea2f7e1b Mon Sep 17 00:00:00 2001 From: David Zhao Date: Sun, 27 Dec 2020 22:21:04 -0800 Subject: [PATCH] cli to generate token --- cmd/cli/commands/token.go | 74 +++++++++++++++++++++++++++++++++++++++ cmd/cli/main.go | 1 + 2 files changed, 75 insertions(+) create mode 100644 cmd/cli/commands/token.go diff --git a/cmd/cli/commands/token.go b/cmd/cli/commands/token.go new file mode 100644 index 000000000..a25bfd76a --- /dev/null +++ b/cmd/cli/commands/token.go @@ -0,0 +1,74 @@ +package commands + +import ( + "fmt" + + "github.com/urfave/cli/v2" + + "github.com/livekit/livekit-server/pkg/auth" +) + +var ( + TokenCommands = []*cli.Command{ + { + Name: "token", + Usage: "create token for Room APIs", + Action: createToken, + Flags: []cli.Flag{ + apiKeyFlag, + secretFlag, + &cli.BoolFlag{ + Name: "join", + Usage: "enable token to be used to join a room", + }, + &cli.BoolFlag{ + Name: "create", + Usage: "enable token to be used to create rooms", + }, + &cli.StringFlag{ + Name: "participant", + Usage: "unique name of the participant, used with --join", + }, + &cli.StringFlag{ + Name: "room", + Usage: "name of the room to join, empty to allow joining all rooms", + }, + }, + }, + } +) + +func createToken(c *cli.Context) error { + if !c.IsSet("api-key") || !c.IsSet("api-secret") { + return fmt.Errorf("api-key and api-secret are required") + } + p := c.String("participant") // required only for join + + grant := &auth.VideoGrant{} + if c.Bool("create") { + grant.RoomCreate = true + } + if c.Bool("join") { + grant.RoomJoin = true + + if room := c.String("room"); room != "" { + grant.Room = room + } + + if p == "" { + return fmt.Errorf("participant name is required") + } + } + + if !grant.RoomJoin && !grant.RoomCreate { + return fmt.Errorf("one of --join or --create is required") + } + + token, err := accessToken(c, grant, p) + if err != nil { + return err + } + + fmt.Println("access token: ", token) + return nil +} diff --git a/cmd/cli/main.go b/cmd/cli/main.go index f2b2a849f..8720e8725 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -18,6 +18,7 @@ func main() { app.Commands = append(app.Commands, commands.RoomCommands...) app.Commands = append(app.Commands, commands.RTCCommands...) + app.Commands = append(app.Commands, commands.TokenCommands...) logger.InitDevelopment() if err := app.Run(os.Args); err != nil {