Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Adding TaskRouter grant #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion tests/unit/jwt/test_access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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'])
2 changes: 2 additions & 0 deletions twilio/jwt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
def binary(txt):
return txt.encode('utf-8')


try:
import json
except ImportError:
Expand All @@ -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(),
Expand Down
23 changes: 23 additions & 0 deletions twilio/jwt/access_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down