-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathtoken.go
More file actions
372 lines (352 loc) · 9.32 KB
/
token.go
File metadata and controls
372 lines (352 loc) · 9.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"slices"
"time"
"github.com/charmbracelet/huh"
"github.com/urfave/cli/v3"
"github.com/livekit/protocol/auth"
"github.com/livekit/protocol/livekit"
)
const (
usageCreate = "Ability to create or delete rooms"
usageList = "Ability to list rooms"
usageJoin = "Ability to join a room (requires --room and --identity)"
usageAdmin = "Ability to moderate a room (requires --room)"
usageEgress = "Ability to interact with Egress services"
usageIngress = "Ability to interact with Ingress services"
usageMetadata = "Ability to update their own name and metadata"
)
var (
TokenCommands = []*cli.Command{
{
Name: "token",
Usage: "Create access tokens with granular capabilities",
Before: loadProjectConfig,
Commands: []*cli.Command{
{
Name: "create",
Usage: "Creates an access token",
Action: createToken,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "create",
Usage: usageCreate,
},
&cli.BoolFlag{
Name: "list",
Usage: usageList,
},
&cli.BoolFlag{
Name: "join",
Usage: usageJoin,
},
&cli.BoolFlag{
Name: "admin",
Usage: usageAdmin,
},
&cli.BoolFlag{
Name: "egress",
Usage: usageEgress,
},
&cli.BoolFlag{
Name: "ingress",
Usage: usageIngress,
},
&cli.BoolFlag{
Name: "allow-update-metadata",
Usage: usageMetadata,
},
&cli.StringSliceFlag{
Name: "allow-source",
Usage: "Restrict publishing to only `SOURCE` types (e.g. --allow-source camera,microphone), defaults to all",
},
&cli.StringFlag{
Name: "identity",
Aliases: []string{"i"},
Usage: "Unique `ID` of the participant, used with --join",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "`NAME` of the participant, used with --join. defaults to identity",
},
&cli.StringFlag{
Name: "room",
Aliases: []string{"r"},
Usage: "`NAME` of the room to join",
},
&cli.StringFlag{
Name: "metadata",
Usage: "`JSON` metadata to encode in the token, will be passed to participant",
},
&cli.StringFlag{
Name: "valid-for",
Usage: "`TIME` that the token is valid for, e.g. \"5m\", \"1h10m\" (s: seconds, m: minutes, h: hours)",
Value: "5m",
},
&cli.StringFlag{
Name: "grant",
Usage: "Additional `VIDEO_GRANT` fields. It'll be merged with other arguments (JSON formatted)",
},
},
},
},
},
// Deprecated commands kept for compatibility
{
Hidden: true, // deprecated: use `token create`
Name: "create-token",
Usage: "Creates an access token",
Action: createToken,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "create",
Usage: usageCreate,
},
&cli.BoolFlag{
Name: "list",
Usage: usageList,
},
&cli.BoolFlag{
Name: "join",
Usage: usageJoin,
},
&cli.BoolFlag{
Name: "admin",
Usage: usageAdmin,
},
&cli.BoolFlag{
Name: "recorder",
Usage: "UNUSED",
},
&cli.BoolFlag{
Name: "egress",
Usage: usageEgress,
},
&cli.BoolFlag{
Name: "ingress",
Usage: usageIngress,
},
&cli.BoolFlag{
Name: "allow-update-metadata",
Usage: usageMetadata,
},
&cli.StringSliceFlag{
Name: "allow-source",
Usage: "Allow one or more `SOURCE`s to be published (i.e. --allow-source camera,microphone). if left blank, all sources are allowed",
},
&cli.StringFlag{
Name: "identity",
Aliases: []string{"i"},
Usage: "Unique `ID` of the participant, used with --join",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "`NAME` of the participant, used with --join. defaults to identity",
},
&cli.StringFlag{
Name: "room",
Aliases: []string{"r"},
Usage: "`NAME` of the room to join",
},
&cli.StringFlag{
Name: "room-configuration",
Usage: "name of the room configuration to use when creating a room",
},
&cli.StringFlag{
Name: "metadata",
Usage: "`JSON` metadata to encode in the token, will be passed to participant",
},
&cli.StringFlag{
Name: "valid-for",
Usage: "Amount of `TIME` that the token is valid for. i.e. \"5m\", \"1h10m\" (s: seconds, m: minutes, h: hours)",
Value: "5m",
},
&cli.StringFlag{
Name: "grant",
Usage: "Additional `VIDEO_GRANT` fields. It'll be merged with other arguments (JSON formatted)",
},
},
},
}
)
func createToken(ctx context.Context, c *cli.Command) error {
p := c.String("identity") // required only for join
name := c.String("name")
room := c.String("room")
metadata := c.String("metadata")
validFor := c.String("valid-for")
roomPreset := c.String("room-preset")
grant := &auth.VideoGrant{
Room: room,
}
hasPerms := false
if c.Bool("create") {
grant.RoomCreate = true
hasPerms = true
}
if c.Bool("join") {
grant.RoomJoin = true
if p == "" {
return errors.New("participant identity is required")
}
if room == "" {
return errors.New("room is required")
}
hasPerms = true
}
if c.Bool("admin") {
grant.RoomAdmin = true
hasPerms = true
}
if c.Bool("list") {
grant.RoomList = true
hasPerms = true
}
// in the future, this will change to more room specific permissions
if c.Bool("egress") {
grant.RoomRecord = true
hasPerms = true
}
if c.Bool("ingress") {
grant.IngressAdmin = true
hasPerms = true
}
if c.IsSet("allow-source") {
sourcesStr := c.StringSlice("allow-source")
sources := make([]livekit.TrackSource, 0, len(sourcesStr))
for _, s := range sourcesStr {
var source livekit.TrackSource
switch s {
case "camera":
source = livekit.TrackSource_CAMERA
case "microphone":
source = livekit.TrackSource_MICROPHONE
case "screen_share":
source = livekit.TrackSource_SCREEN_SHARE
case "screen_share_audio":
source = livekit.TrackSource_SCREEN_SHARE_AUDIO
default:
return fmt.Errorf("invalid source: %s", s)
}
sources = append(sources, source)
}
grant.SetCanPublishSources(sources)
}
if c.Bool("allow-update-metadata") {
grant.SetCanUpdateOwnMetadata(true)
}
if str := c.String("grant"); str != "" {
if err := json.Unmarshal([]byte(str), grant); err != nil {
return err
}
hasPerms = true
}
if !hasPerms {
type permission uint
const (
pCreate permission = iota
pList
pJoin
pAdmin
pEgress
pIngress
pMetadata
)
permissions := make([]permission, 0)
if err := huh.NewMultiSelect[permission]().
Options(
huh.NewOption("Create", pCreate),
huh.NewOption("List", pList),
huh.NewOption("Join", pJoin),
huh.NewOption("Admin", pAdmin),
huh.NewOption("Egress", pEgress),
huh.NewOption("Ingress", pIngress),
huh.NewOption("Update metadata", pMetadata),
).
Title("Token Permissions").
Description("See https://docs.livekit.io/home/get-started/authentication/#Video-grant").
Value(&permissions).
WithTheme(theme).
Run(); err != nil || len(permissions) == 0 {
return errors.New("no permissions were given in this grant, see --help")
} else {
grant.RoomCreate = slices.Contains(permissions, pCreate)
if slices.Contains(permissions, pJoin) {
grant.RoomJoin = true
if p == "" {
return errors.New("participant identity is required")
}
if room == "" {
return errors.New("room is required")
}
}
grant.RoomAdmin = slices.Contains(permissions, pAdmin)
grant.RoomList = slices.Contains(permissions, pList)
if slices.Contains(permissions, pEgress) {
grant.RoomRecord = true
}
grant.SetCanUpdateOwnMetadata(slices.Contains(permissions, pMetadata))
}
}
pc, err := loadProjectDetails(c, ignoreURL)
if err != nil {
return err
}
at := accessToken(pc.APIKey, pc.APISecret, grant, p)
if metadata != "" {
at.SetMetadata(metadata)
}
if roomPreset != "" {
at.SetRoomPreset(roomPreset)
}
if name == "" {
name = p
}
at.SetName(name)
if validFor != "" {
if dur, err := time.ParseDuration(validFor); err == nil {
fmt.Println("valid for (mins): ", int(dur/time.Minute))
at.SetValidFor(dur)
} else {
return err
}
}
token, err := at.ToJWT()
if err != nil {
return err
}
fmt.Println("Token grants:")
PrintJSON(grant)
fmt.Println()
fmt.Println("Access token:", token)
return nil
}
func accessToken(apiKey, apiSecret string, grant *auth.VideoGrant, identity string) *auth.AccessToken {
if apiKey == "" && apiSecret == "" {
// not provided, don't sign request
return nil
}
at := auth.NewAccessToken(apiKey, apiSecret).
SetVideoGrant(grant).
SetIdentity(identity)
return at
}