cli to generate token

This commit is contained in:
David Zhao
2020-12-27 22:21:04 -08:00
parent 43a6db8a3a
commit 94e2c782ee
2 changed files with 75 additions and 0 deletions

74
cmd/cli/commands/token.go Normal file
View File

@@ -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
}

View File

@@ -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 {