Skip to content

Commit

Permalink
Add SIP grants. (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc authored Jun 20, 2024
1 parent 7814d0f commit 79b5ea9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/thick-bottles-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@livekit/protocol": minor
"github.com/livekit/protocol": minor
---

Add SIP grants.
5 changes: 5 additions & 0 deletions auth/accesstoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (t *AccessToken) AddGrant(grant *VideoGrant) *AccessToken {
return t
}

func (t *AccessToken) AddSIPGrant(grant *SIPGrant) *AccessToken {
t.grant.SIP = grant
return t
}

func (t *AccessToken) SetMetadata(md string) *AccessToken {
t.grant.Metadata = md
return t
Expand Down
3 changes: 3 additions & 0 deletions auth/accesstoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func TestAccessToken(t *testing.T) {
t.Run("generates a decode-able key", func(t *testing.T) {
apiKey, secret := apiKeypair()
videoGrant := &VideoGrant{RoomJoin: true, Room: "myroom"}
sipGrant := &SIPGrant{Admin: true}
at := NewAccessToken(apiKey, secret).
AddGrant(videoGrant).
AddSIPGrant(sipGrant).
SetValidFor(time.Minute * 5).
SetKind(livekit.ParticipantInfo_AGENT).
SetIdentity("user")
Expand All @@ -60,6 +62,7 @@ func TestAccessToken(t *testing.T) {

require.EqualValues(t, livekit.ParticipantInfo_AGENT, decodedGrant.GetParticipantKind())
require.EqualValues(t, videoGrant, decodedGrant.Video)
require.EqualValues(t, sipGrant, decodedGrant.SIP)
})

t.Run("missing kind should be interpreted as standard", func(t *testing.T) {
Expand Down
20 changes: 20 additions & 0 deletions auth/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,20 @@ type VideoGrant struct {
Agent bool `json:"agent,omitempty"`
}

type SIPGrant struct {
// Admin grants access to all SIP features.
Admin bool `json:"admin,omitempty"`

// Call allows making outbound SIP calls.
Call bool `json:"call,omitempty"`
}

type ClaimGrants struct {
Identity string `json:"-"`
Name string `json:"name,omitempty"`
Kind string `json:"kind,omitempty"`
Video *VideoGrant `json:"video,omitempty"`
SIP *SIPGrant `json:"sip,omitempty"`
// for verifying integrity of the message body
Sha256 string `json:"sha256,omitempty"`
Metadata string `json:"metadata,omitempty"`
Expand All @@ -84,6 +93,7 @@ func (c *ClaimGrants) Clone() *ClaimGrants {

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

return &clone
Expand Down Expand Up @@ -266,6 +276,16 @@ func (v *VideoGrant) Clone() *VideoGrant {
return &clone
}

func (s *SIPGrant) Clone() *SIPGrant {
if s == nil {
return nil
}

clone := *s

return &clone
}

func sourceToString(source livekit.TrackSource) string {
return strings.ToLower(source.String())
}
Expand Down
10 changes: 6 additions & 4 deletions sip/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ func BuildSIPToken(
t := true
at := auth.NewAccessToken(apiKey, secret).
AddGrant(&auth.VideoGrant{
RoomJoin: true,
Room: roomName,
CanSubscribe: &t,
CanPublish: &t,
RoomJoin: true,
Room: roomName,
CanSubscribe: &t,
CanPublish: &t,
CanPublishData: &t,
CanUpdateOwnMetadata: &t,
}).
SetIdentity(participantIdentity).
SetName(participantName).
Expand Down

0 comments on commit 79b5ea9

Please sign in to comment.