Beispiel #1
0
def token():
  user_id = request.args.get('user')
  
  if not user_id:
    return "Invalid user"

  platform = request.args.get('platform')

  if not platform:
    return "Invalid platform"

  account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
  api_key = os.environ.get("API_KEY", API_KEY)
  api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
  user_identity = 'agent_' + user_id
  
  if platform == 'android':
    push_credential = os.environ.get("ANDROID_PUSH_CREDENTIAL_SID", ANDROID_PUSH_CREDENTIAL_SID)
  else:
    push_credential = os.environ.get("IOS_PUSH_CREDENTIAL_SID", IOS_PUSH_CREDENTIAL_SID)

  grant = VoiceGrant(
    push_credential_sid=push_credential
  )

  token = AccessToken(account_sid, api_key, api_key_secret, user_identity)
  token.add_grant(grant)

  return str(token)
def token():
    client_name = request.values.get('client')
    platform = request.values.get('platform')
    account_sid = os.environ.get('ACCOUNT_SID', ACCOUNT_SID)
    api_key = os.environ.get('API_KEY', API_KEY)
    api_key_secret = os.environ.get('API_KEY_SECRET', API_KEY_SECRET)
    app_sid = os.environ.get('APP_SID', APP_SID)

    if platform == 'ios':
        push_credential_sid = os.environ.get('PUSH_CREDENTIAL_SID_IOS',
                PUSH_CREDENTIAL_SID_IOS)
    else:
        push_credential_sid = \
            os.environ.get('PUSH_CREDENTIAL_SID_ANDROID',
                           PUSH_CREDENTIAL_SID_ANDROID)

    if client_name:
        IDENTITY = client_name
        grant = VoiceGrant(push_credential_sid=push_credential_sid,
                           outgoing_application_sid=app_sid)

    token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
    token.add_grant(grant)

    return str(token)
def token():
    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
    push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID",
                                         PUSH_CREDENTIAL_SID)
    app_sid = os.environ.get("APP_SID", APP_SID)

    grant = VoiceGrant(push_credential_sid=push_credential_sid,
                       outgoing_application_sid=app_sid)

    token = AccessToken(account_sid, api_key, api_key_secret, IDENTITY)
    token.add_grant(grant)

    return str(token)
Beispiel #4
0
    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 token():
    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
    api_key = os.environ.get("API_KEY", API_KEY)
    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
    ipm_service_sid = os.environ.get("IPM_SERVICE_SID", IPM_SERVICE_SID)
    push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID",
                                         PUSH_CREDENTIAL_SID)
    app_sid = os.environ.get("APP_SID", APP_SID)

    device_id = request.values.get('device')
    identity = request.values.get('identity')

    voiceGrant = VoiceGrant(push_credential_sid=push_credential_sid,
                            outgoing_application_sid=app_sid)
    chatGrant = IpMessagingGrant(service_sid=ipm_service_sid,
                                 endpoint_id="DeepReason:{0}:{1}".format(
                                     device_id, device_id))

    token = AccessToken(account_sid, api_key, api_key_secret, identity)
    token.add_grant(voiceGrant)
    token.add_grant(chatGrant)
    return str(token)
Beispiel #6
0
from twilio.jwt.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())
Beispiel #7
0
def generateToken():
#    account_sid = os.environ.get("ACCOUNT_SID", ACCOUNT_SID)
#    api_key = os.environ.get("API_KEY", API_KEY)
#    api_key_secret = os.environ.get("API_KEY_SECRET", API_KEY_SECRET)
#    push_credential_sid = os.environ.get("PUSH_CREDENTIAL_SID", PUSH_CREDENTIAL_SID)
#    app_sid = os.environ.get("APP_SID", APP_SID)
#    sync_service_sid = os.environ.get("SYNC_SERVICE_SID", SYNC_SERVICE_SID)
#    auth_token = os.environ.get("AUTH_TOKEN", AUTH_TOKEN)
    account_sid         = os.environ['TWILIO_ACCOUNT_SID']
    api_key             = os.environ['TWILIO_API_KEY']
    api_key_secret      = os.environ['TWILIO_API_KEY_SECRET']
    push_credential_sid = os.environ['TWILIO_PUSH_CREDENTIAL_SID']
    app_sid             = os.environ['TWILIO_TWIML_APP_SID']
    sync_service_sid    = os.environ['TWILIO_SYNC_SERVICE_SID']
    auth_token          = os.environ['TWILIO_AUTH_TOKEN']
    
    identity = request.values.get('id')
    
    if identity[0] == ' ':
        identity = "+" + identity[1:]
    
    identity = getIdentity(identity)
    print(identity)

    token = AccessToken(account_sid, api_key, api_key_secret, identity=identity)

    # Create a Voice grant and add to token
    grant = VoiceGrant(
        push_credential_sid=push_credential_sid,
        outgoing_application_sid=app_sid
    )
    token.add_grant(grant)

    # Create a Sync grant and add to token
    if sync_service_sid:
        sync_grant = SyncGrant(service_sid=sync_service_sid)
        token.add_grant(sync_grant)

#    client = Client(account_sid, auth_token)
#    data = json.dumps({'board':{
#                      'date_updated': str(datetime.now()),
#                      'status': "none",
#                      }})
#    documents = client.preview \
#                        .sync \
#                            .services(sync_service_sid) \
#                                .documents \
#                                    .list()
#    isMatch = False
#    for document in documents:
#        print(document.unique_name)
#        print(document.data)
#        did_delete = client.preview \
#                            .sync \
#                                .services(sync_service_sid) \
#                                    .documents(document.unique_name) \
#                                        .delete()
#        print("Delete: ",did_delete)
#        if document.unique_name == identity:
#            isMatch = True
#            print("Fetch")
#            document = client.preview.sync.services(sync_service_sid).documents(identity).fetch()
#            print(document.unique_name)#
#    if not isMatch:
#        print ("Create")
#        document = client.preview.sync.services(sync_service_sid).documents.create(unique_name=identity, data=data)
#        print(document.unique_name)
#    else:
#        print("Already created.")

    return jsonify(identity=identity, token=token.to_jwt())