github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/agent/token.go (about)

     1  package agent
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/livekit/protocol/auth"
     7  	"github.com/livekit/protocol/livekit"
     8  )
     9  
    10  func BuildAgentToken(apiKey, secret, roomName, participantIdentity, participantName, participantMetadata string, permissions *livekit.ParticipantPermission) (string, error) {
    11  	grant := &auth.VideoGrant{
    12  		RoomJoin:             true,
    13  		Agent:                true,
    14  		Room:                 roomName,
    15  		CanSubscribe:         &permissions.CanSubscribe,
    16  		CanPublish:           &permissions.CanPublish,
    17  		CanPublishData:       &permissions.CanPublishData,
    18  		Hidden:               permissions.Hidden,
    19  		CanUpdateOwnMetadata: &permissions.CanUpdateMetadata,
    20  	}
    21  
    22  	at := auth.NewAccessToken(apiKey, secret).
    23  		AddGrant(grant).
    24  		SetIdentity(participantIdentity).
    25  		SetName(participantName).
    26  		SetKind(livekit.ParticipantInfo_AGENT).
    27  		SetValidFor(1 * time.Hour).
    28  		SetMetadata(participantMetadata)
    29  
    30  	return at.ToJWT()
    31  }