Skip to content

Commit

Permalink
Add SIP grants.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennwc committed Jun 20, 2024
1 parent f50408c commit a6da318
Show file tree
Hide file tree
Showing 5 changed files with 50 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
30 changes: 30 additions & 0 deletions auth/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,30 @@ type VideoGrant struct {
Agent bool `json:"agent,omitempty"`
}

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

// TrunkEdit allows creating and deleting SIP Trunks.
TrunkEdit bool `json:"trunkEdit,omitempty"`
// TrunkList allows listing SIP Trunks.
TrunkList bool `json:"trunkList,omitempty"`

// DispatchEdit allows creating and deleting SIP Dispatch Rules.
DispatchEdit bool `json:"dispatchRuleEdit,omitempty"`
// DispatchList allows listing SIP Dispatch Rules.
DispatchList bool `json:"dispatchRuleList,omitempty"`

// Call allows making outbound SIP calls.
Call bool `json:"sipCall,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 +103,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 +286,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 a6da318

Please sign in to comment.