def generate_view(charset='utf-8'): worker_sid = request.args.get('WorkerSid') # TaskRouter Worker Token worker_capability = WorkerCapabilityToken( account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, worker_sid=worker_sid) # generate worker capability token worker_capability.allow_update_activities( ) # allow agent to update their activity status e.g. go offline worker_capability.allow_update_reservations( ) # allow agents to update reservations e.g. accept/reject worker_token = worker_capability.to_jwt(ttl=28800) capability = ClientCapabilityToken( account_sid, auth_token) # agent Twilio Client capability token capability.allow_client_outgoing(twiml_app) capability.allow_client_incoming(worker_sid) client_token = capability.to_jwt() # render client/worker tokens to the agent desktop so that they can be queried on the client side return render_template('agent_desktop.html', token=client_token.decode("utf-8"), worker_token=worker_token.decode("utf-8"), client_=worker_sid, activity=activity, caller_id=caller_id)
def agentChat(): worker_sid = request.args.get('WorkerSid') # TaskRouter Worker Token worker_capability = WorkerCapabilityToken( account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, worker_sid=worker_sid) # generate worker capability token worker_capability.allow_update_activities( ) # allow agent to update their activity status e.g. go offline worker_capability.allow_update_reservations( ) # allow agents to update reservations e.g. accept/reject worker_token = worker_capability.to_jwt(ttl=28800) return render_template('agent_desktop_chat.html', worker_token=worker_token.decode("utf-8"))
def get_worker_token(worker_sid): logger.info(f'Generating Worker token for {worker_sid}') token = WorkerCapabilityToken(account_sid=settings.TWILIO_ACCOUNT_SID, auth_token=settings.TWILIO_AUTH_TOKEN, workspace_sid=get_workspace().sid, worker_sid=worker_sid) token.allow_fetch_subresources() token.allow_update_activities() token.allow_update_reservations() # Expire token in three minutes expiration = 180 jwt_token = token.to_jwt(ttl=expiration).decode("utf-8") # Cache the token, set to expire after the token expires cache.set(f'tokens:workers:{worker_sid}', jwt_token, expiration) return jwt_token
def noClientView(): # Agent desktop without Twilio Client worker_sid = request.args.get('WorkerSid') # TaskRouter Worker Token worker_capability = WorkerCapabilityToken( account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, worker_sid=worker_sid) # generate worker capability token worker_capability.allow_update_activities( ) # allow agent to update their activity status e.g. go offline worker_capability.allow_update_reservations( ) # allow agents to update reservations e.g. accept/reject worker_token = worker_capability.to_jwt(ttl=28800) return render_template( 'agent_desktop_no_client.html.html', worker_token=worker_token.decode("utf-8") ) # render worker token to the agent desktop so that they can be queried on the client side
def get_worker_token(worker_sid): logger.info('Generating Worker token for {}'.format(worker_sid)) capability = WorkerCapabilityToken(account_sid=settings.TWILIO_ACCOUNT_SID, auth_token=settings.TWILIO_AUTH_TOKEN, workspace_sid=get_workspace().sid, worker_sid=worker_sid) capability.allow_fetch_subresources() capability.allow_update_activities() capability.allow_update_reservations() # Expire token in three minutes expiration = 180 token = capability.to_jwt(ttl=expiration) # Cache the token, set to expire when the token expires cache.set('tokens:workers:{}'.format(worker_sid), token, expiration) return token
# Download the Python helper library from twilio.com/docs/python/install from twilio.jwt.taskrouter.capabilities import WorkerCapabilityToken # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" worker_sid = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" capability = WorkerCapabilityToken( account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, worker_sid=worker_sid ) capability.allow_update_activities() capability.allow_update_reservations() token = capability.to_jwt() # By default, tokens are good for one hour. # Override this default timeout by specifiying a new value (in seconds). # For example, to generate a token good for 8 hours: # 60 * 60 * 8 = 28800 token = capability.to_jwt(ttl=28800) print(token)
class WorkerCapabilityTokenTest(unittest.TestCase): def check_policy(self, method, url, policy): self.assertEqual(url, policy['url']) self.assertEqual(method, policy['method']) self.assertTrue(policy['allow']) self.assertEqual({}, policy['query_filter']) self.assertEqual({}, policy['post_filter']) def check_decoded(self, decoded, account_sid, workspace_sid, channel_id, channel_sid=None): self.assertEqual(decoded["iss"], account_sid) self.assertEqual(decoded["account_sid"], account_sid) self.assertEqual(decoded["workspace_sid"], workspace_sid) self.assertEqual(decoded["channel"], channel_id) self.assertEqual(decoded["version"], "v1") self.assertEqual(decoded["friendly_name"], channel_id) if 'worker_sid' in decoded.keys(): self.assertEqual(decoded['worker_sid'], channel_sid) if 'taskqueue_sid' in decoded.keys(): self.assertEqual(decoded['taskqueue_sid'], channel_sid) def setUp(self): self.account_sid = "AC123" self.auth_token = "foobar" self.workspace_sid = "WS456" self.worker_sid = "WK789" self.capability = WorkerCapabilityToken(self.account_sid, self.auth_token, self.workspace_sid, self.worker_sid) def test_generate_token(self): token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) self.check_decoded(decoded.payload, self.account_sid, self.workspace_sid, self.worker_sid, self.worker_sid) def test_generate_token_with_default_ttl(self): token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) self.assertAlmostEqual(int(time.time()) + 3600, decoded.valid_until, delta=5) def test_generate_token_with_custom_ttl(self): ttl = 10000 token = self.capability.to_jwt(ttl=ttl) self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) self.assertAlmostEqual(int(time.time()) + 10000, decoded.valid_until, delta=5) def test_defaults(self): token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.decode(token, self.auth_token) self.assertNotEqual(None, decoded) websocket_url = 'https://event-bridge.twilio.com/v1/wschannels/{0}/{1}'.format( self.account_sid, self.worker_sid) # expect 6 policies policies = decoded.payload['policies'] self.assertEqual(len(policies), 6) # should expect 6 policies for method, url, policy in [ ('GET', websocket_url, policies[0]), ('POST', websocket_url, policies[1]), ('GET', "https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789", policies[2]), ('GET', "https://taskrouter.twilio.com/v1/Workspaces/WS456/Activities", policies[3]), ('GET', "https://taskrouter.twilio.com/v1/Workspaces/WS456/Tasks/**", policies[4]), ('GET', "https://taskrouter.twilio.com/v1/Workspaces/WS456/Workers/WK789/Reservations/**", policies[5]) ]: yield self.check_policy, method, url, policy def test_allow_activity_updates(self): # allow activity updates to the worker self.capability.allow_update_activities() token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 7) policy = policies[6] url = "https://taskrouter.twilio.com/v1/Workspaces/{0}/Workers/{1}".format( self.workspace_sid, self.worker_sid) self.assertEqual(url, policy["url"]) self.assertEqual("POST", policy["method"]) self.assertTrue(policy["allow"]) self.assertNotEqual(None, policy['post_filter']) self.assertEqual({}, policy['query_filter']) self.assertTrue(policy['post_filter']['ActivitySid']) def test_allow_reservation_updates(self): # allow reservation updates self.capability.allow_update_reservations() token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 8) taskPolicy = policies[6] tasksUrl = "https://taskrouter.twilio.com/v1/Workspaces/{0}/Tasks/**".format( self.workspace_sid) self.check_policy('POST', tasksUrl, taskPolicy) workerReservationsPolicy = policies[7] reservationsUrl = "https://taskrouter.twilio.com/v1/Workspaces/{0}/Workers/{1}/Reservations/**".format( self.workspace_sid, self.worker_sid) self.check_policy('POST', reservationsUrl, workerReservationsPolicy) def test_pass_policies_in_constructor(self): # allow reservation updates self.capability = WorkerCapabilityToken(self.account_sid, self.auth_token, self.workspace_sid, self.worker_sid, allow_update_reservations=True) token = self.capability.to_jwt() self.assertNotEqual(None, token) decoded = WorkerCapabilityToken.from_jwt(token, self.auth_token) self.assertNotEqual(None, decoded) policies = decoded.payload['policies'] self.assertEqual(len(policies), 8) taskPolicy = policies[6] tasksUrl = "https://taskrouter.twilio.com/v1/Workspaces/{0}/Tasks/**".format( self.workspace_sid) self.check_policy('POST', tasksUrl, taskPolicy) workerReservationsPolicy = policies[7] reservationsUrl = "https://taskrouter.twilio.com/v1/Workspaces/{0}/Workers/{1}/Reservations/**".format( self.workspace_sid, self.worker_sid) self.check_policy('POST', reservationsUrl, workerReservationsPolicy)
# Download the Python helper library from twilio.com/docs/python/install from twilio.jwt.taskrouter.capabilities import WorkerCapabilityToken # Your Account Sid and Auth Token from twilio.com/user/account account_sid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" auth_token = "your_auth_token" workspace_sid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" worker_sid = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" capability = WorkerCapabilityToken(account_sid=account_sid, auth_token=auth_token, workspace_sid=workspace_sid, worker_sid=worker_sid) capability.allow_update_activities() capability.allow_update_reservations() token = capability.to_jwt() # By default, tokens are good for one hour. # Override this default timeout by specifiying a new value (in seconds). # For example, to generate a token good for 8 hours: # 60 * 60 * 8 = 28800 token = capability.to_jwt(ttl=28800) print(token)