This repository has been archived by the owner on Apr 22, 2022. It is now read-only.
forked from saintpete/twilio-go
/
access_token_grant.go
146 lines (127 loc) · 4.21 KB
/
access_token_grant.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package token
const (
ipMessagingGrant = "ip_messaging"
conversationsGrant = "rtc"
voiceGrant = "voice"
videoGrant = "video"
keyServiceSid = "service_id"
keyEndpointId = "endpoint_id"
keyDepRoleSide = "deployment_role_sid"
keyPushCredSid = "push_credential_sid"
keyVoiceOutgoing = "outgoing"
keyConfProfSid = "configuration_profile_sid"
keyAppSid = "application_sid"
keyVoiceParams = "params"
)
// Grant is a Twilio SID resource that can be added to an AccessToken for extra
// services. Implement this interface to create a custom Grant.
type Grant interface {
ToPayload() map[string]interface{}
Key() string
}
// IPMessageGrant is a grant for accessing Twilio IP Messaging
type IPMessageGrant struct {
serviceSid string
endpointID string
deploymentRoleSid string
pushCredentialSid string
}
func NewIPMessageGrant(serviceSid, endpointID, deploymentRoleSid, pushCredentialSid string) *IPMessageGrant {
return &IPMessageGrant{
serviceSid: serviceSid,
endpointID: endpointID,
deploymentRoleSid: deploymentRoleSid,
pushCredentialSid: pushCredentialSid,
}
}
func (gr *IPMessageGrant) ToPayload() map[string]interface{} {
grant := make(map[string]interface{})
if len(gr.serviceSid) > 0 {
grant[keyServiceSid] = gr.serviceSid
}
if len(gr.endpointID) > 0 {
grant[keyEndpointId] = gr.endpointID
}
if len(gr.deploymentRoleSid) > 0 {
grant[keyDepRoleSide] = gr.deploymentRoleSid
}
if len(gr.pushCredentialSid) > 0 {
grant[keyPushCredSid] = gr.pushCredentialSid
}
return grant
}
func (gr *IPMessageGrant) Key() string {
return ipMessagingGrant
}
// ConversationsGrant is for Twilio Conversations
type ConversationsGrant struct {
configurationProfileSid string
}
func NewConversationsGrant(sid string) *ConversationsGrant {
return &ConversationsGrant{configurationProfileSid: sid}
}
func (gr *ConversationsGrant) ToPayload() map[string]interface{} {
if len(gr.configurationProfileSid) > 0 {
return map[string]interface{}{
keyConfProfSid: gr.configurationProfileSid,
}
}
return make(map[string]interface{})
}
func (gr *ConversationsGrant) Key() string {
return conversationsGrant
}
// VoiceGrant is a grant for accessing Twilio IP Messaging
type VoiceGrant struct {
outgoingApplicationSid string // application sid to call when placing outgoing call
outgoingApplicationParams map[string]interface{} // request params to pass to the application
endpointID string // Specify an endpoint identifier for this device, which will allow the developer to direct calls to a specific endpoint when multiple devices are associated with a single identity
pushCredentialSid string // Push Credential Sid to use when registering to receive incoming call notifications
}
func NewVoiceGrant(outAppSid string, outAppParams map[string]interface{}, endpointID string, pushCredentialSid string) *VoiceGrant {
return &VoiceGrant{
outgoingApplicationSid: outAppSid,
outgoingApplicationParams: outAppParams,
endpointID: endpointID,
pushCredentialSid: pushCredentialSid,
}
}
func (gr *VoiceGrant) ToPayload() map[string]interface{} {
outVoice := make(map[string]interface{})
if len(gr.outgoingApplicationSid) > 0 {
outVoice[keyAppSid] = gr.outgoingApplicationSid
}
if len(gr.outgoingApplicationParams) > 0 {
outVoice[keyVoiceParams] = gr.outgoingApplicationParams
}
grant := make(map[string]interface{})
grant[keyVoiceOutgoing] = outVoice
if len(gr.endpointID) > 0 {
grant[keyEndpointId] = gr.endpointID
}
if len(gr.pushCredentialSid) > 0 {
grant[keyPushCredSid] = gr.pushCredentialSid
}
return grant
}
func (gr *VoiceGrant) Key() string {
return voiceGrant
}
// VideoGrant is for Twilio Programmable Video access
type VideoGrant struct {
configurationProfileSid string
}
func NewVideoGrant(sid string) *VideoGrant {
return &VideoGrant{configurationProfileSid: sid}
}
func (gr *VideoGrant) ToPayload() map[string]interface{} {
if len(gr.configurationProfileSid) > 0 {
return map[string]interface{}{
keyConfProfSid: gr.configurationProfileSid,
}
}
return make(map[string]interface{})
}
func (gr *VideoGrant) Key() string {
return videoGrant
}