Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for key/value attributes on Participant #733

Merged
merged 14 commits into from
Jun 20, 2024
6 changes: 6 additions & 0 deletions .changeset/orange-bottles-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"github.com/livekit/protocol": minor
"@livekit/protocol": minor
---

add Participant attributes key/val storage
13 changes: 13 additions & 0 deletions auth/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ func (t *AccessToken) SetMetadata(md string) *AccessToken {
return t
}

func (t *AccessToken) SetAttributes(attrs map[string]string) *AccessToken {
if len(attrs) == 0 {
return t
}
if t.grant.Attributes == nil {
t.grant.Attributes = make(map[string]string)
}
for k, v := range attrs {
t.grant.Attributes[k] = v
}
return t
}

func (t *AccessToken) SetSha256(sha string) *AccessToken {
t.grant.Sha256 = sha
return t
Expand Down
4 changes: 4 additions & 0 deletions auth/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package auth

import (
"maps"
"strings"

"golang.org/x/exp/slices"
Expand Down Expand Up @@ -64,6 +65,8 @@ type ClaimGrants struct {
// for verifying integrity of the message body
Sha256 string `json:"sha256,omitempty"`
Metadata string `json:"metadata,omitempty"`
// Key/value attributes to attach to the participant
Attributes map[string]string `json:"attributes,omitempty"`
}

func (c *ClaimGrants) SetParticipantKind(kind livekit.ParticipantInfo_Kind) {
Expand All @@ -81,6 +84,7 @@ func (c *ClaimGrants) Clone() *ClaimGrants {

clone := *c
clone.Video = c.Video.Clone()
clone.Attributes = maps.Clone(c.Attributes)

return &clone
}
Expand Down
5 changes: 4 additions & 1 deletion auth/verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ func TestVerifier(t *testing.T) {
"number": float64(3),
}
md, _ := json.Marshal(metadata)
attrs := map[string]string{"mykey": "myval", "secondkey": "secondval"}
at := auth.NewAccessToken(apiKey, secret).
AddGrant(&auth.VideoGrant{
RoomAdmin: true,
Room: "myroom",
}).
SetMetadata(string(md))
SetMetadata(string(md)).
SetAttributes(attrs)

authToken, err := at.ToJWT()
require.NoError(t, err)
Expand All @@ -90,6 +92,7 @@ func TestVerifier(t *testing.T) {
require.NoError(t, err)

require.EqualValues(t, string(md), decoded.Metadata)
require.EqualValues(t, attrs, decoded.Attributes)
})

t.Run("nil permissions are handled", func(t *testing.T) {
Expand Down
19 changes: 19 additions & 0 deletions livekit/attrs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package livekit

// Names of participant attributes for SIP.
const (
// AttrSIPPrefix is shared for all SIP attributes.
AttrSIPPrefix = "sip."
// AttrSIPCallID attribute contains LiveKit SIP call ID.
AttrSIPCallID = AttrSIPPrefix + "callID"
// AttrSIPTrunkID attribute contains LiveKit SIP Trunk ID used for the call.
AttrSIPTrunkID = AttrSIPPrefix + "trunkID"
// AttrSIPDispatchRuleID attribute contains LiveKit SIP DispatchRule ID used for the inbound call.
AttrSIPDispatchRuleID = AttrSIPPrefix + "ruleID"
// AttrSIPTrunkNumber attribute contains number associate with LiveKit SIP Trunk.
// This attribute will be omitted if HidePhoneNumber is set.
AttrSIPTrunkNumber = AttrSIPPrefix + "trunkPhoneNumber"
// AttrSIPPhoneNumber attribute contains number external to LiveKit SIP (caller for inbound and called number for outbound).
// This attribute will be omitted if HidePhoneNumber is set.
AttrSIPPhoneNumber = AttrSIPPrefix + "phoneNumber"
)
1,111 changes: 569 additions & 542 deletions livekit/livekit_models.pb.go

Large diffs are not rendered by default.

305 changes: 163 additions & 142 deletions livekit/livekit_room.pb.go

Large diffs are not rendered by default.

142 changes: 73 additions & 69 deletions livekit/livekit_room.twirp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading