github.com/livekit/protocol@v1.39.3/sip/token.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package sip
    16  
    17  import (
    18  	"time"
    19  
    20  	"github.com/livekit/protocol/auth"
    21  	"github.com/livekit/protocol/livekit"
    22  )
    23  
    24  type SIPTokenParams struct {
    25  	APIKey                string
    26  	APISecret             string
    27  	RoomName              string
    28  	ParticipantIdentity   string
    29  	ParticipantName       string
    30  	ParticipantMetadata   string
    31  	ParticipantAttributes map[string]string
    32  	RoomPreset            string
    33  	RoomConfig            *livekit.RoomConfiguration
    34  }
    35  
    36  func BuildSIPToken(params SIPTokenParams) (string, error) {
    37  	t := true
    38  	at := auth.NewAccessToken(params.APIKey, params.APISecret).
    39  		SetVideoGrant(&auth.VideoGrant{
    40  			RoomJoin:             true,
    41  			Room:                 params.RoomName,
    42  			CanSubscribe:         &t,
    43  			CanPublish:           &t,
    44  			CanPublishData:       &t,
    45  			CanUpdateOwnMetadata: &t,
    46  		}).
    47  		SetIdentity(params.ParticipantIdentity).
    48  		SetName(params.ParticipantName).
    49  		SetMetadata(params.ParticipantMetadata).
    50  		SetAttributes(params.ParticipantAttributes).
    51  		SetRoomPreset(params.RoomPreset).
    52  		SetRoomConfig(params.RoomConfig).
    53  		SetKind(livekit.ParticipantInfo_SIP).
    54  		SetValidFor(24 * time.Hour)
    55  
    56  	return at.ToJWT()
    57  }