-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.go
More file actions
115 lines (92 loc) · 2.81 KB
/
Copy pathtoken.go
File metadata and controls
115 lines (92 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package omniavatar
import (
"time"
"github.com/livekit/protocol/auth"
"github.com/plexusone/omniavatar-core/live"
)
// TokenOptions configures avatar token generation.
type TokenOptions struct {
// APIKey is the LiveKit API key.
// Required.
APIKey string
// APISecret is the LiveKit API secret.
// Required.
APISecret string
// RoomName is the room the avatar will join.
// Required.
RoomName string
// AvatarIdentity is the participant identity for the avatar.
// Required.
AvatarIdentity string
// AvatarName is the display name for the avatar participant.
// Optional, defaults to AvatarIdentity.
AvatarName string
// PublishOnBehalf is the identity of the agent participant.
// The avatar will publish tracks that appear as if they're from this participant.
// Required.
PublishOnBehalf string
// TTL is the token validity duration.
// Default: 5 minutes
TTL time.Duration
// Metadata is optional participant metadata.
Metadata string
}
// GenerateAvatarToken creates a JWT token for an avatar to join a room.
//
// The token includes the special "lk.publish_on_behalf" attribute that allows
// the avatar participant to publish tracks that appear in the UI as if they
// came from the agent participant.
func GenerateAvatarToken(opts TokenOptions) (string, error) {
if opts.APIKey == "" || opts.APISecret == "" {
return "", live.ErrInvalidConfig
}
if opts.RoomName == "" || opts.AvatarIdentity == "" {
return "", live.ErrInvalidConfig
}
if opts.PublishOnBehalf == "" {
return "", live.ErrInvalidConfig
}
ttl := opts.TTL
if ttl == 0 {
ttl = 5 * time.Minute
}
name := opts.AvatarName
if name == "" {
name = opts.AvatarIdentity
}
at := auth.NewAccessToken(opts.APIKey, opts.APISecret)
grant := &auth.VideoGrant{
RoomJoin: true,
Room: opts.RoomName,
}
at.SetVideoGrant(grant).
SetIdentity(opts.AvatarIdentity).
SetName(name).
SetValidFor(ttl).
SetAttributes(map[string]string{
// This attribute allows the avatar to publish tracks
// that appear as if they're from the agent
"lk.publish_on_behalf": opts.PublishOnBehalf,
})
if opts.Metadata != "" {
at.SetMetadata(opts.Metadata)
}
return at.ToJWT()
}
// AvatarMetadata is the standard metadata structure for avatar participants.
type AvatarMetadata struct {
// Kind identifies this as an avatar participant.
Kind string `json:"kind"`
// Provider is the avatar provider name.
Provider string `json:"provider,omitempty"`
// AgentIdentity is the identity of the agent this avatar represents.
AgentIdentity string `json:"agent_identity,omitempty"`
}
// DefaultAvatarMetadata returns the default metadata for avatar participants.
func DefaultAvatarMetadata(provider, agentIdentity string) AvatarMetadata {
return AvatarMetadata{
Kind: "avatar",
Provider: provider,
AgentIdentity: agentIdentity,
}
}