diff --git a/tests/unit/jwt/test_access_token.py b/tests/unit/jwt/test_access_token.py index 98af386277..56e5927465 100644 --- a/tests/unit/jwt/test_access_token.py +++ b/tests/unit/jwt/test_access_token.py @@ -4,7 +4,7 @@ from datetime import datetime from nose.tools import assert_equal from twilio.jwt import decode -from twilio.jwt.access_token import AccessToken, ConversationsGrant, IpMessagingGrant, SyncGrant, VoiceGrant, VideoGrant +from twilio.jwt.access_token import AccessToken, ConversationsGrant, IpMessagingGrant, SyncGrant, VoiceGrant, VideoGrant, TaskRouterGrant ACCOUNT_SID = 'AC123' SIGNING_KEY_SID = 'SK123' @@ -159,3 +159,20 @@ def test_programmable_voice_grant(self): } } }, payload['grants']['voice']) + + def test_task_router_grant(self): + grant = TaskRouterGrant(workspace_sid='WS123', worker_sid='WK123', role='worker') + + scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') + scat.add_grant(grant) + + token = str(scat) + assert_is_not_none(token) + payload = decode(token, 'secret') + self._validate_claims(payload) + assert_equal(1, len(payload['grants'])) + assert_equal({ + 'workspace_sid': 'WS123', + 'worker_sid': 'WK123', + 'role': 'worker' + }, payload['grants']['task_router']) diff --git a/twilio/jwt/__init__.py b/twilio/jwt/__init__.py index 93f6b60a34..661e2f9911 100644 --- a/twilio/jwt/__init__.py +++ b/twilio/jwt/__init__.py @@ -13,6 +13,7 @@ def binary(txt): return txt.encode('utf-8') + try: import json except ImportError: @@ -25,6 +26,7 @@ def binary(txt): class DecodeError(Exception): pass + signing_methods = { 'HS256': lambda msg, key: hmac.new(key, msg, hashlib.sha256).digest(), 'HS384': lambda msg, key: hmac.new(key, msg, hashlib.sha384).digest(), diff --git a/twilio/jwt/access_token.py b/twilio/jwt/access_token.py index b89adbe8dd..12112ecc34 100644 --- a/twilio/jwt/access_token.py +++ b/twilio/jwt/access_token.py @@ -122,6 +122,29 @@ def to_payload(self): return grant +class TaskRouterGrant(object): + """ Grant to access Twilio TaskRouter """ + def __init__(self, workspace_sid=None, worker_sid=None, role=None): + self.workspace_sid = workspace_sid + self.worker_sid = worker_sid + self.role = role + + @property + def key(self): + return "task_router" + + def to_payload(self): + grant = {} + if self.workspace_sid: + grant['workspace_sid'] = self.workspace_sid + if self.worker_sid: + grant['worker_sid'] = self.worker_sid + if self.role: + grant['role'] = self.role + + return grant + + class AccessToken(object): """ Access Token used to access Twilio Resources """ def __init__(self, account_sid, signing_key_sid, secret,