Skip to content

Commit 923b8ad

Browse files
authored
feat: add participant attributes to token creation (#763)
1 parent 4f432a6 commit 923b8ad

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

autocomplete/fish_autocomplete

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ complete -c lk -n '__fish_seen_subcommand_from create' -f -l identity -s i -r -d
296296
complete -c lk -n '__fish_seen_subcommand_from create' -f -l name -s n -r -d '`NAME` of the participant, used with --join. defaults to identity'
297297
complete -c lk -n '__fish_seen_subcommand_from create' -f -l room -s r -r -d '`NAME` of the room to join'
298298
complete -c lk -n '__fish_seen_subcommand_from create' -f -l metadata -r -d '`JSON` metadata to encode in the token, will be passed to participant'
299+
complete -c lk -n '__fish_seen_subcommand_from create' -f -l attribute -r -d 'set attributes in key=value format, can be used multiple times'
300+
complete -c lk -n '__fish_seen_subcommand_from create' -l attribute-file -r -d 'read attributes from a `JSON` file'
299301
complete -c lk -n '__fish_seen_subcommand_from create' -f -l valid-for -r -d '`TIME` that the token is valid for, e.g. "5m", "1h10m" (s: seconds, m: minutes, h: hours)'
300302
complete -c lk -n '__fish_seen_subcommand_from create' -f -l grant -r -d 'Additional `VIDEO_GRANT` fields. It\'ll be merged with other arguments (JSON formatted)'
301303
complete -c lk -n '__fish_seen_subcommand_from create' -f -l help -s h -d 'show help'
@@ -315,6 +317,8 @@ complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l name -s n -r
315317
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l room -s r -r -d '`NAME` of the room to join'
316318
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l room-configuration -r -d 'name of the room configuration to use when creating a room'
317319
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l metadata -r -d '`JSON` metadata to encode in the token, will be passed to participant'
320+
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l attribute -r -d 'set attributes in key=value format, can be used multiple times'
321+
complete -c lk -n '__fish_seen_subcommand_from create-token' -l attribute-file -r -d 'read attributes from a `JSON` file'
318322
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l valid-for -r -d 'Amount of `TIME` that the token is valid for. i.e. "5m", "1h10m" (s: seconds, m: minutes, h: hours)'
319323
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l grant -r -d 'Additional `VIDEO_GRANT` fields. It\'ll be merged with other arguments (JSON formatted)'
320324
complete -c lk -n '__fish_seen_subcommand_from create-token' -f -l help -s h -d 'show help'

cmd/lk/token.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"encoding/json"
2020
"errors"
2121
"fmt"
22+
"os"
2223
"slices"
2324
"time"
2425

@@ -98,6 +99,15 @@ var (
9899
Name: "metadata",
99100
Usage: "`JSON` metadata to encode in the token, will be passed to participant",
100101
},
102+
&cli.StringSliceFlag{
103+
Name: "attribute",
104+
Usage: "set attributes in key=value format, can be used multiple times",
105+
},
106+
&cli.StringFlag{
107+
Name: "attribute-file",
108+
Usage: "read attributes from a `JSON` file",
109+
TakesFile: true,
110+
},
101111
&cli.StringFlag{
102112
Name: "valid-for",
103113
Usage: "`TIME` that the token is valid for, e.g. \"5m\", \"1h10m\" (s: seconds, m: minutes, h: hours)",
@@ -183,6 +193,15 @@ var (
183193
Name: "metadata",
184194
Usage: "`JSON` metadata to encode in the token, will be passed to participant",
185195
},
196+
&cli.StringSliceFlag{
197+
Name: "attribute",
198+
Usage: "set attributes in key=value format, can be used multiple times",
199+
},
200+
&cli.StringFlag{
201+
Name: "attribute-file",
202+
Usage: "read attributes from a `JSON` file",
203+
TakesFile: true,
204+
},
186205
&cli.StringFlag{
187206
Name: "valid-for",
188207
Usage: "Amount of `TIME` that the token is valid for. i.e. \"5m\", \"1h10m\" (s: seconds, m: minutes, h: hours)",
@@ -202,6 +221,29 @@ func createToken(ctx context.Context, c *cli.Command) error {
202221
metadata := c.String("metadata")
203222
validFor := c.String("valid-for")
204223
roomPreset := c.String("room-preset")
224+
participantAttributes, err := parseKeyValuePairs(c, "attribute")
225+
if err != nil {
226+
return fmt.Errorf("failed to parse participant attributes: %w", err)
227+
}
228+
229+
if attrFile := c.String("attribute-file"); attrFile != "" {
230+
fileData, err := os.ReadFile(attrFile)
231+
if err != nil {
232+
return fmt.Errorf("failed to read attribute file: %w", err)
233+
}
234+
235+
var fileAttrs map[string]string
236+
if err := json.Unmarshal(fileData, &fileAttrs); err != nil {
237+
return fmt.Errorf("failed to parse attribute file as JSON: %w", err)
238+
}
239+
240+
if participantAttributes == nil {
241+
participantAttributes = make(map[string]string)
242+
}
243+
for key, value := range fileAttrs {
244+
participantAttributes[key] = value
245+
}
246+
}
205247

206248
// required only for join, will be generated if not provided
207249
participant := c.String("identity")
@@ -323,7 +365,7 @@ func createToken(ctx context.Context, c *cli.Command) error {
323365
}
324366
}
325367

326-
_, err := requireProjectWithOpts(ctx, c, ignoreURL)
368+
_, err = requireProjectWithOpts(ctx, c, ignoreURL)
327369
if err != nil {
328370
return err
329371
}
@@ -346,6 +388,9 @@ func createToken(ctx context.Context, c *cli.Command) error {
346388
if metadata != "" {
347389
at.SetMetadata(metadata)
348390
}
391+
if len(participantAttributes) != 0 {
392+
at.SetAttributes(participantAttributes)
393+
}
349394
if roomPreset != "" {
350395
at.SetRoomPreset(roomPreset)
351396
}

0 commit comments

Comments
 (0)