def test_enable_nts(self): scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret') scat.enable_nts() token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(1, len(payload['grants'])) assert_equal('https://api.twilio.com/2010-04-01/Accounts/AC123/Tokens.json', payload['grants'][0]['res']) assert_equal(['POST'], payload['grants'][0]['act'])
def test_identity(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret', identity='*****@*****.**') token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal({'identity': '*****@*****.**'}, payload['grants'])
def test_conversations_grant(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(ConversationsGrant(configuration_profile_sid='CP123')) token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(1, len(payload['grants'])) assert_equal({'configuration_profile_sid': 'CP123'}, payload['grants']['rtc'])
def test_endpoint_grant(self): scat = AccessToken(SIGNING_KEY_SID, ACCOUNT_SID, 'secret') scat.add_endpoint_grant('bob') token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(1, len(payload['grants'])) assert_equal('sip:[email protected]', payload['grants'][0]['res']) assert_equal(['listen', 'invite'], payload['grants'][0]['act'])
def test_grants(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(ConversationsGrant()) scat.add_grant(IpMessagingGrant()) token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(2, len(payload['grants'])) assert_equal({}, payload['grants']['rtc']) assert_equal({}, payload['grants']['ip_messaging'])
def token(request): device_id = request.GET.get('device', 'unknown') identity = request.GET.get('identity', 'guest').encode('utf-8') endpoint_id = "NeighborChat:{0}, {1}".format(device_id, identity) token = AccessToken(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_API_KEY, settings.TWILIO_API_SECRET, identity) grant = IpMessagingGrant() grant.service_sid = settings.TWILIO_IPM_SERVICE_SID grant.endpoint_id = endpoint_id token.add_grant(grant) response = {'identity': identity, 'token': token.to_jwt()} return JsonResponse(response)
def token(): identity = request.args.get('identity') device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) token = AccessToken( constants.TWILIO_ACCOUNT_SID, constants.TWILIO_API_KEY, constants.TWILIO_API_SECRET, identity) ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=constants.TWILIO_SERVICE_SID) token.add_grant(ipm_grant) return jsonify(identity=identity, token=token.to_jwt())
def test_ip_messaging_grant(self): scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret') scat.add_grant(IpMessagingGrant(service_sid='IS123', push_credential_sid='CR123')) token = str(scat) assert_is_not_none(token) payload = decode(token, 'secret') self._validate_claims(payload) assert_equal(1, len(payload['grants'])) assert_equal({ 'service_sid': 'IS123', 'push_credential_sid': 'CR123' }, payload['grants']['ip_messaging'])
def twilio_token(): # get credentials for environment variables account_sid = current_app.config['TWILIO_ACCOUNT_SID'] api_key = current_app.config['TWILIO_API_KEY'] api_secret = current_app.config['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Grant access to Conversations grant = ConversationsGrant() grant.configuration_profile_sid = current_app.config[ 'TWILIO_CONFIGURATION_SID'] token.add_grant(grant) return token
def token(): # get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = fake.user_name() # Grant access to Conversations grant = ConversationsGrant() grant.configuration_profile_sid = os.environ['TWILIO_CONFIGURATION_SID'] token.add_grant(grant) # Return token info as JSON return jsonify(identity=token.identity, token=token.to_jwt())
def token(): # get credentials for environment variables account_sid = 'ACd9b72d3e2fd1c7afee885f62d2d95a95' api_key = 'SK3a1be8585f189c540584a1757e10ba69' api_secret = 'aNovO6gcHsTOUjByHkUruUHyhq1Aserx' # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token token.identity = session[secret] # Grant access to Video grant = VideoGrant() grant.configuration_profile_sid = 'VSa37a06ac2dac126260d6675aae39566f' token.add_grant(grant) # Return token info as JSON return jsonify(identity=token.identity, token=token.to_jwt())
def test_programmable_voice_grant(self): grant = VoiceGrant(outgoing_application_sid='AP123', outgoing_application_params={'foo': 'bar'}) 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( { 'outgoing': { 'application_sid': 'AP123', 'params': { 'foo': 'bar' } } }, payload['grants']['voice'])
def tokens(request): identity = request.user['sub'] endpoint_id = request.GET["endpoint_id"] print("identity: {}\nendpoint: {}".format(identity, endpoint_id)) account_sid = "ACd3a83bf4c0646baaba77cdc355567d1f" api_key = "SK4949cc25487c7bd8001f3e0f21165064" api_secret = "keQ3hukqsJ8PP9WNLs2g6Fels5umPCzM" token = AccessToken(account_sid, api_key, api_secret, identity, ttl=40000) ipm_service_sid = "IS3ba6938016464f7cbcba3d2fea297dca" endpoint_id = ipm_service_sid + identity + endpoint_id push_credential_sid = "CRe9c5eff29e744709d7df875f8a797bf0" ipm_grant = IpMessagingGrant(endpoint_id=endpoint_id, service_sid=ipm_service_sid, push_credential_sid=push_credential_sid) token.add_grant(ipm_grant) return HttpResponse(json.dumps({ 'identity': identity, 'token': token.to_jwt() }))
def get_token(request): account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] # Create an Access Token token = AccessToken(account_sid, api_key, api_secret) # Set the Identity of this token random_names = [ "Christian Blatter", "Tanaz Kesariwala", "Shanaya Kapoor", "Alisha Chauhan" ] token.identity = random.choice(random_names) # Grant access to Conversations # grant = ConversationsGrant() # grant.configuration_profile_sid = os.environ['TWILIO_CONFIGURATION_SID'] # token.add_grant(grant) # Return token info as JSON return JsonResponse({"identity": token.identity, "token": token.to_jwt()})
def token(): # get credentials for environment variables account_sid = os.environ['TWILIO_ACCOUNT_SID'] api_key = os.environ['TWILIO_API_KEY'] api_secret = os.environ['TWILIO_API_SECRET'] service_sid = os.environ['TWILIO_IPM_SERVICE_SID'] # create a randomly generated username for the client identity = os.environ['TWILIO_USERNAME'] # Create a unique endpoint ID for the device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=service_sid) token.add_grant(ipm_grant) # Return token info as JSON return jsonify(identity=identity, token=token.to_jwt())
def token(): # get credentials for environment variables account_sid = "ACcdbab0f13e08eb8b19b6d3025a9ad6f7" api_key = "SK2f52e17a9ca74d4714d28a7c575e1e21" api_secret = "6XYHaD6O5zPKDpM4wU34NknCQj7L1d6C" service_sid = "IS27b6d9077d6c48838881fc41b4748bb2" # create a randomly generated username for the client identity = fake.user_name() # Create a unique endpoint ID for the device_id = request.args.get('device') endpoint = "TwilioChatDemo:{0}:{1}".format(identity, device_id) # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create an IP Messaging grant and add to token ipm_grant = IpMessagingGrant(endpoint_id=endpoint, service_sid=service_sid) token.add_grant(ipm_grant) # Return token info as JSON return jsonify(identity=identity, token=token.to_jwt())
from twilio.access_token import AccessToken, VoiceGrant # required for all twilio access tokens account_sid = 'ACxxxxxxxxxxxx' api_key = 'SKxxxxxxxxxxxx' api_secret = 'xxxxxxxxxxxxxx' # required for Voice grant outgoing_application_sid = 'APxxxxxxxxxxxxx' identity = 'user' # Create access token with credentials token = AccessToken(account_sid, api_key, api_secret, identity) # Create a Voice grant and add to token voice_grant = VoiceGrant(outgoing_application_sid=outgoing_application_sid) token.add_grant(voice_grant) # Return token info as JSON print(token.to_jwt())
from twilio.access_token import AccessToken # You will need your Account Sid and a SigningKey Sid and Secret # to generate an Access Token for your SDK endpoint to connect to Twilio. account_sid = "{{ account_sid }}" signingkey_sid = SID signingkey_secret = SECRET token = AccessToken(signingkey_sid, account_sid, signingkey_secret) token.add_endpoint_grant(ENDPOINT_ADDRESS) token.enable_nts() print token.to_jwt()