MCPcopy
hub / github.com/livekit/livekit / createToken

Function createToken

cmd/server/commands.go:104–168  ·  view source on GitHub ↗
(_ context.Context, c *cli.Command)

Source from the content-addressed store, hash-verified

102}
103
104func createToken(_ context.Context, c *cli.Command) error {
105 room := c.String("room")
106 identity := c.String("identity")
107
108 conf, err := getConfig(c)
109 if err != nil {
110 return err
111 }
112
113 // use the first API key from config
114 if len(conf.Keys) == 0 {
115 // try to load from file
116 if _, err := os.Stat(conf.KeyFile); err != nil {
117 return err
118 }
119 f, err := os.Open(conf.KeyFile)
120 if err != nil {
121 return err
122 }
123 defer func() {
124 _ = f.Close()
125 }()
126 decoder := yaml.NewDecoder(f)
127 if err = decoder.Decode(conf.Keys); err != nil {
128 return err
129 }
130
131 if len(conf.Keys) == 0 {
132 return fmt.Errorf("keys are not configured")
133 }
134 }
135
136 var apiKey string
137 var apiSecret string
138 for k, v := range conf.Keys {
139 apiKey = k
140 apiSecret = v
141 break
142 }
143
144 grant := &auth.VideoGrant{
145 RoomJoin: true,
146 Room: room,
147 }
148 if c.Bool("recorder") {
149 grant.Hidden = true
150 grant.Recorder = true
151 grant.SetCanPublish(false)
152 grant.SetCanPublishData(false)
153 }
154
155 at := auth.NewAccessToken(apiKey, apiSecret).
156 AddGrant(grant).
157 SetIdentity(identity).
158 SetValidFor(30 * 24 * time.Hour)
159
160 token, err := at.ToJWT()
161 if err != nil {

Callers

nothing calls this directly

Calls 3

getConfigFunction · 0.85
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected