Skip to content

Commit becd101

Browse files
Adding the inference grant to the Claim (#1139)
* adding the inference grant to the Claim * Added gateway guid values. These are used when connecting to the PSQL db with metrics and app name * renamed the permission from Admin to Perform * remove gateway guids since the gateway is no longer using a connection to the db
1 parent 52633ee commit becd101

File tree

4 files changed

+121
-6
lines changed

4 files changed

+121
-6
lines changed

auth/accesstoken.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ func (t *AccessToken) SetAgentGrant(grant *AgentGrant) *AccessToken {
8787
return t
8888
}
8989

90+
func (t *AccessToken) SetInferenceGrant(grant *InferenceGrant) *AccessToken {
91+
t.grant.Inference = grant
92+
return t
93+
}
94+
9095
func (t *AccessToken) SetMetadata(md string) *AccessToken {
9196
t.grant.Metadata = md
9297
return t

auth/accesstoken_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,13 @@ func TestAccessToken(t *testing.T) {
4242
apiKey, secret := apiKeypair()
4343
videoGrant := &VideoGrant{RoomJoin: true, Room: "myroom"}
4444
sipGrant := &SIPGrant{Admin: true}
45+
agentGrant := &AgentGrant{Admin: true}
46+
inferenceGrant := &InferenceGrant{Perform: true}
4547
at := NewAccessToken(apiKey, secret).
4648
SetVideoGrant(videoGrant).
4749
SetSIPGrant(sipGrant).
50+
SetInferenceGrant(inferenceGrant).
51+
SetAgentGrant(agentGrant).
4852
SetValidFor(time.Minute * 5).
4953
SetKind(livekit.ParticipantInfo_AGENT).
5054
SetIdentity("user")
@@ -65,6 +69,8 @@ func TestAccessToken(t *testing.T) {
6569
require.EqualValues(t, livekit.ParticipantInfo_AGENT, decodedGrant.GetParticipantKind())
6670
require.EqualValues(t, videoGrant, decodedGrant.Video)
6771
require.EqualValues(t, sipGrant, decodedGrant.SIP)
72+
require.EqualValues(t, agentGrant, decodedGrant.Agent)
73+
require.EqualValues(t, inferenceGrant, decodedGrant.Inference)
6874
})
6975

7076
t.Run("missing kind should be interpreted as standard", func(t *testing.T) {

auth/grants.go

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ func checkOutputForCredentials(output any) error {
162162
}
163163

164164
type ClaimGrants struct {
165-
Identity string `json:"identity,omitempty"`
166-
Name string `json:"name,omitempty"`
167-
Kind string `json:"kind,omitempty"`
168-
Video *VideoGrant `json:"video,omitempty"`
169-
SIP *SIPGrant `json:"sip,omitempty"`
170-
Agent *AgentGrant `json:"agent,omitempty"`
165+
Identity string `json:"identity,omitempty"`
166+
Name string `json:"name,omitempty"`
167+
Kind string `json:"kind,omitempty"`
168+
Video *VideoGrant `json:"video,omitempty"`
169+
SIP *SIPGrant `json:"sip,omitempty"`
170+
Agent *AgentGrant `json:"agent,omitempty"`
171+
Inference *InferenceGrant `json:"inference,omitempty"`
171172
// Room configuration to use if this participant initiates the room
172173
RoomConfig *RoomConfiguration `json:"roomConfig,omitempty"`
173174
// Cloud-only, config preset to use
@@ -203,6 +204,8 @@ func (c *ClaimGrants) Clone() *ClaimGrants {
203204
clone := *c
204205
clone.Video = c.Video.Clone()
205206
clone.SIP = c.SIP.Clone()
207+
clone.Agent = c.Agent.Clone()
208+
clone.Inference = c.Inference.Clone()
206209
clone.Attributes = maps.Clone(c.Attributes)
207210
clone.RoomConfig = c.RoomConfig.Clone()
208211

@@ -218,6 +221,8 @@ func (c *ClaimGrants) MarshalLogObject(e zapcore.ObjectEncoder) error {
218221
e.AddString("Kind", c.Kind)
219222
e.AddObject("Video", c.Video)
220223
e.AddObject("SIP", c.SIP)
224+
e.AddObject("Agent", c.Agent)
225+
e.AddObject("Inference", c.Inference)
221226
e.AddObject("RoomConfig", logger.Proto((*livekit.RoomConfiguration)(c.RoomConfig)))
222227
e.AddString("RoomPreset", c.RoomPreset)
223228
return nil
@@ -554,6 +559,32 @@ func (s *AgentGrant) MarshalLogObject(e zapcore.ObjectEncoder) error {
554559

555560
// ------------------------------------------------------------------
556561

562+
type InferenceGrant struct {
563+
// Admin grants to all inference features (LLM, STT, TTS)
564+
Perform bool `json:"perform,omitempty"`
565+
}
566+
567+
func (s *InferenceGrant) Clone() *InferenceGrant {
568+
if s == nil {
569+
return nil
570+
}
571+
572+
clone := *s
573+
574+
return &clone
575+
}
576+
577+
func (s *InferenceGrant) MarshalLogObject(e zapcore.ObjectEncoder) error {
578+
if s == nil {
579+
return nil
580+
}
581+
582+
e.AddBool("Perform", s.Perform)
583+
return nil
584+
}
585+
586+
// ------------------------------------------------------------------
587+
557588
func sourceToString(source livekit.TrackSource) string {
558589
return strings.ToLower(source.String())
559590
}

auth/grants_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ func TestGrants(t *testing.T) {
3232
clone := grants.Clone()
3333
require.NotSame(t, grants, clone)
3434
require.Same(t, grants.Video, clone.Video)
35+
require.Same(t, grants.Agent, clone.Agent)
36+
require.Same(t, grants.Inference, clone.Inference)
37+
require.Same(t, grants.SIP, clone.SIP)
3538
require.True(t, reflect.DeepEqual(grants, clone))
3639
require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
3740
})
@@ -48,6 +51,16 @@ func TestGrants(t *testing.T) {
4851
require.Same(t, grants.Video, clone.Video)
4952
require.True(t, reflect.DeepEqual(grants, clone))
5053
require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
54+
55+
// require SIP
56+
require.Same(t, grants.SIP, clone.SIP)
57+
require.True(t, reflect.DeepEqual(grants.SIP, clone.SIP))
58+
// require Agent
59+
require.Same(t, grants.Agent, clone.Agent)
60+
require.True(t, reflect.DeepEqual(grants.Agent, clone.Agent))
61+
// require Inference
62+
require.Same(t, grants.Inference, clone.Inference)
63+
require.True(t, reflect.DeepEqual(grants.Inference, clone.Inference))
5164
})
5265

5366
t.Run("clone with video", func(t *testing.T) {
@@ -84,6 +97,66 @@ func TestGrants(t *testing.T) {
8497
require.True(t, reflect.DeepEqual(grants, clone))
8598
require.True(t, reflect.DeepEqual(grants.Video, clone.Video))
8699
})
100+
101+
t.Run("clone with SIP", func(t *testing.T) {
102+
sip := &SIPGrant{
103+
Admin: true,
104+
}
105+
grants := &ClaimGrants{
106+
Identity: "identity",
107+
Name: "name",
108+
Kind: "kind",
109+
SIP: sip,
110+
Sha256: "sha256",
111+
Metadata: "metadata",
112+
}
113+
clone := grants.Clone()
114+
require.NotSame(t, grants, clone)
115+
require.NotSame(t, grants.SIP, clone.SIP)
116+
require.Equal(t, grants.SIP.Admin, clone.SIP.Admin)
117+
require.True(t, reflect.DeepEqual(grants, clone))
118+
require.True(t, reflect.DeepEqual(grants.SIP, clone.SIP))
119+
})
120+
121+
t.Run("clone with Agent", func(t *testing.T) {
122+
agent := &AgentGrant{
123+
Admin: true,
124+
}
125+
grants := &ClaimGrants{
126+
Identity: "identity",
127+
Name: "name",
128+
Kind: "kind",
129+
Agent: agent,
130+
Sha256: "sha256",
131+
Metadata: "metadata",
132+
}
133+
clone := grants.Clone()
134+
require.NotSame(t, grants, clone)
135+
require.NotSame(t, grants.Agent, clone.Agent)
136+
require.Equal(t, grants.Agent.Admin, clone.Agent.Admin)
137+
require.True(t, reflect.DeepEqual(grants, clone))
138+
require.True(t, reflect.DeepEqual(grants.Agent, clone.Agent))
139+
})
140+
141+
t.Run("clone with Inference", func(t *testing.T) {
142+
inference := &InferenceGrant{
143+
Perform: true,
144+
}
145+
grants := &ClaimGrants{
146+
Identity: "identity",
147+
Name: "name",
148+
Kind: "kind",
149+
Inference: inference,
150+
Sha256: "sha256",
151+
Metadata: "metadata",
152+
}
153+
clone := grants.Clone()
154+
require.NotSame(t, grants, clone)
155+
require.NotSame(t, grants.Inference, clone.Inference)
156+
require.Equal(t, grants.Inference.Perform, clone.Inference.Perform)
157+
require.True(t, reflect.DeepEqual(grants, clone))
158+
require.True(t, reflect.DeepEqual(grants.Inference, clone.Inference))
159+
})
87160
}
88161

89162
func TestParticipantKind(t *testing.T) {

0 commit comments

Comments
 (0)