8000 Add TaskRouter Grant · jstacoder/twilio-python@ff1c2b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff1c2b3

Browse files
committed
Add TaskRouter Grant
1 parent 63d7b51 commit ff1c2b3

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tests/unit/jwt/test_access_token.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
IpMessagingGrant,
1111
SyncGrant,
1212
VoiceGrant,
13-
VideoGrant
13+
VideoGrant,
14+
TaskRouterGrant
1415
)
1516

1617
ACCOUNT_SID = 'AC123'
@@ -174,6 +175,27 @@ def test_programmable_voice_grant(self):
174175
}
175176
}, decoded_token.payload['grants']['voice'])
176177

178+
def test_task_router_grant(self):
179+
grant = TaskRouterGrant(
180+
workspace_sid='WS123',
181+
worker_sid='WK123',
182+
role='worker'
183+
)
184+
185+
scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
186+
scat.add_grant(grant)
187+
188+
token = scat.to_jwt()
189+
assert_is_not_none(token)
190+
decoded_token = AccessToken.from_jwt(token, 'secret')
191+
self._validate_claims(decoded_token.payload)
192+
assert_equal(1, len(decoded_token.payload['grants']))
193+
assert_equal({
194+
'workspace_sid': 'WS123',
195+
'worker_sid': 'WK123',
196+
'role': 'worker'
197+
}, decoded_token.payload['grants']['task_router'])
198+
177199
def test_pass_grants_in_constructor(self):
178200
grants = [
179201
ConversationsGrant(),

twilio/jwt/access_token/grants.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,26 @@ def to_payload(self):
118118
grant['configuration_profile_sid'] = self.configuration_profile_sid
119119

120120
return grant
121+
122+
123+
class TaskRouterGrant(AccessTokenGrant):
124+
"""Grant to access Twilio TaskRouter"""
125+
def __init__(self, workspace_sid=None, worker_sid=None, role=None):
126+
self.workspace_sid = workspace_sid
127+
self.worker_sid = worker_sid
128+
self.role = role
129+
130+
@property
131+
def key(self):
132+
return "task_router"
133+
134+
def to_payload(self):
135+
grant = {}
136+
if self.workspace_sid:
137+
grant['workspace_sid'] = self.workspace_sid
138+
if self.worker_sid:
139+
grant['worker_sid'] = self.worker_sid
140+
if self.role:
141+
grant['role'] = self.role
142+
143+
return grant

0 commit comments

Comments
 (0)
0