pass client defined metadata from JWT to participant (#17)

This commit is contained in:
David Zhao
2021-02-10 23:13:36 -08:00
committed by GitHub
parent 38140debdf
commit 538461d5dc
23 changed files with 286 additions and 81 deletions
+1 -1
View File
@@ -230,7 +230,7 @@ func muteTrack(c *cli.Context) error {
func contextWithAccessToken(c *cli.Context, grant *auth.VideoGrant) context.Context {
ctx := context.Background()
token, err := accessToken(c, grant, "")
token, err := accessToken(c, grant, "").ToJWT()
if err != nil {
logger.Errorw("Could not get access token", "err", err)
}
+1 -1
View File
@@ -63,7 +63,7 @@ func joinRoom(c *cli.Context) error {
token, err = accessToken(c, &auth.VideoGrant{
RoomJoin: true,
Room: roomId,
}, identity)
}, identity).ToJWT()
if err != nil {
return err
}
+30 -9
View File
@@ -1,6 +1,7 @@
package commands
import (
"encoding/json"
"fmt"
"github.com/urfave/cli/v2"
@@ -25,6 +26,10 @@ var (
Name: "create",
Usage: "enable token to be used to create rooms",
},
&cli.BoolFlag{
Name: "admin",
Usage: "enable token to be used to manage a room",
},
&cli.StringFlag{
Name: "participant",
Aliases: []string{"p"},
@@ -35,6 +40,10 @@ var (
Aliases: []string{"r"},
Usage: "name of the room to join, empty to allow joining all rooms",
},
&cli.StringFlag{
Name: "metadata",
Usage: "JSON metadata to encode in the token, will be passed to participant",
},
devFlag,
},
},
@@ -46,6 +55,8 @@ func createToken(c *cli.Context) error {
return fmt.Errorf("api-key and api-secret are required")
}
p := c.String("participant") // required only for join
room := c.String("room")
metadata := c.String("metadata")
grant := &auth.VideoGrant{}
if c.Bool("create") {
@@ -53,21 +64,31 @@ func createToken(c *cli.Context) error {
}
if c.Bool("join") {
grant.RoomJoin = true
if room := c.String("room"); room != "" {
grant.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")
if c.Bool("admin") {
grant.RoomAdmin = true
grant.Room = room
}
token, err := accessToken(c, grant, p)
if !grant.RoomJoin && !grant.RoomCreate && !grant.RoomAdmin {
return fmt.Errorf("one of --join, --create, or --admin is required")
}
at := accessToken(c, grant, p)
if metadata != "" {
var md map[string]interface{}
if err := json.Unmarshal([]byte(metadata), &md); err != nil {
return err
}
at.SetMetadata(md)
}
token, err := at.ToJWT()
if err != nil {
return err
}
+3 -3
View File
@@ -62,12 +62,12 @@ func ExpandUser(p string) string {
return p
}
func accessToken(c *cli.Context, grant *auth.VideoGrant, identity string) (value string, err error) {
func accessToken(c *cli.Context, grant *auth.VideoGrant, identity string) *auth.AccessToken {
apiKey := c.String("api-key")
apiSecret := c.String("api-secret")
if apiKey == "" && apiSecret == "" {
// not provided, don't sign request
return
return nil
}
isDev := c.Bool("dev")
@@ -79,5 +79,5 @@ func accessToken(c *cli.Context, grant *auth.VideoGrant, identity string) (value
fmt.Println("creating dev token")
at.SetValidFor(time.Hour * 24 * 30)
}
return at.ToJWT()
return at
}