mirror of
https://github.com/livekit/livekit.git
synced 2026-04-03 17:05:42 +00:00
67 lines
1.3 KiB
Go
67 lines
1.3 KiB
Go
package commands
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"github.com/livekit/livekit-server/pkg/auth"
|
|
)
|
|
|
|
var (
|
|
roomFlag = &cli.StringFlag{
|
|
Name: "room",
|
|
Usage: "name or id of the room",
|
|
}
|
|
roomHostFlag = &cli.StringFlag{
|
|
Name: "host",
|
|
Value: "http://localhost:7880",
|
|
}
|
|
rtcHostFlag = &cli.StringFlag{
|
|
Name: "host",
|
|
Value: "ws://localhost:7881",
|
|
}
|
|
apiKeyFlag = &cli.StringFlag{
|
|
Name: "api-key",
|
|
EnvVars: []string{"LK_API_KEY"},
|
|
Required: true,
|
|
}
|
|
secretFlag = &cli.StringFlag{
|
|
Name: "api-secret",
|
|
EnvVars: []string{"LK_API_SECRET"},
|
|
Required: true,
|
|
}
|
|
)
|
|
|
|
func PrintJSON(obj interface{}) {
|
|
txt, _ := json.MarshalIndent(obj, "", " ")
|
|
fmt.Println(string(txt))
|
|
}
|
|
|
|
func ExpandUser(p string) string {
|
|
if strings.HasPrefix(p, "~") {
|
|
home, _ := os.UserHomeDir()
|
|
return filepath.Join(home, p[1:])
|
|
}
|
|
|
|
return p
|
|
}
|
|
|
|
func accessToken(c *cli.Context, grant *auth.VideoGrant, identity string) (value string, err error) {
|
|
apiKey := c.String("api-key")
|
|
apiSecret := c.String("api-secret")
|
|
if apiKey == "" && apiSecret == "" {
|
|
// not provided, don't sign request
|
|
return
|
|
}
|
|
|
|
at := auth.NewAccessToken(apiKey, apiSecret).
|
|
AddGrant(grant).
|
|
SetIdentity(identity)
|
|
return at.ToJWT()
|
|
}
|