コード例 #1
0
def generateToken(identity):
    # Get credentials from environment variables
    account_sid = settings.TWILIO_ACCT_SID
    chat_service_sid = settings.TWILIO_CHAT_SID
    sync_service_sid = settings.TWILIO_SYNC_SID
    api_sid = settings.TWILIO_API_SID
    api_secret = settings.TWILIO_API_SECRET

    # Create access token with credentials
    token = AccessToken(account_sid, api_sid, api_secret, identity=identity)

    # 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)

    # Create a Chat grant and add to token
    if chat_service_sid:
        chat_grant = ChatGrant(service_sid=chat_service_sid)
        token.add_grant(chat_grant)

    # Return token info as JSON
    return JsonResponse({
        'identity': identity,
        'token': token.to_jwt().decode('utf-8')
    })
コード例 #2
0
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']
    sync_service_sid = os.environ['TWILIO_SYNC_SERVICE_SID']
    chat_service_sid = os.environ['TWILIO_CHAT_SERVICE_SID']

    # create a randomly generated username for the client
    identity = fake.user_name()

    # Create access token with credentials
    token = AccessToken(account_sid, api_key, api_secret, identity=identity)

    # 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)

    # Create a Video grant and add to token
    video_grant = VideoGrant()
    token.add_grant(video_grant)

    # Create an Chat grant and add to token
    if chat_service_sid:
        chat_grant = IpMessagingGrant(service_sid=chat_service_sid)
        token.add_grant(chat_grant)

    # Return token info as JSON
    return jsonify(identity=identity, token=token.to_jwt().decode('utf-8'))
コード例 #3
0
def chat_token(request):
    identity = request.GET.get('identity', fake.user_name())
    device_id = request.GET.get('device', 'default')  # unique device ID

    account_sid = settings.TWILIO_ACCOUNT_SID
    api_key = settings.TWILIO_API_KEY
    api_secret = settings.TWILIO_API_SECRET
    chat_service_sid = settings.TWILIO_CHAT_SERVICE_SID
    sync_service_sid = settings.TWILIO_SYNC_SID

    chat_token = AccessToken(account_sid, api_key, api_secret, identity=identity)

    # Create a unique endpoint ID for the device
    endpoint = "MyDjangoChatRoom:{0}:{1}".format(identity, device_id)

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

    if chat_service_sid:
        chat_grant = ChatGrant(endpoint_id=endpoint,
                               service_sid=chat_service_sid)
        chat_token.add_grant(chat_grant)

    response = {
        'identity': identity,
        'chat_token': chat_token.to_jwt().decode('utf-8')
    }

    return JsonResponse(response)
コード例 #4
0
def generateToken(identity):
    # 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']
    sync_service_sid = os.environ.get('TWILIO_SYNC_SERVICE_SID', 'default')
    chat_service_sid = os.environ.get('TWILIO_CHAT_SERVICE_SID', None)

    # Create access token with credentials
    token = AccessToken(account_sid, api_key, api_secret, identity=identity)

    # 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)

    # Create a Video grant and add to token
    video_grant = VideoGrant()
    token.add_grant(video_grant)

    # Create an Chat grant and add to token
    if chat_service_sid:
        chat_grant = ChatGrant(service_sid=chat_service_sid)
        token.add_grant(chat_grant)

    # Return token info as JSON
    return jsonify(identity=identity, token=token.to_jwt().decode('utf-8'))
コード例 #5
0
def get_chat_token(username):
    logger.info(f'Generating Chat token for {username}')

    # This call is simply to ensure the service exists
    service = get_service()

    token = AccessToken(settings.TWILIO_ACCOUNT_SID,
                        settings.TWILIO_API_KEY,
                        settings.TWILIO_API_SECRET,
                        identity=username)

    sync_grant = SyncGrant(service_sid='default')
    token.add_grant(sync_grant)

    chat_grant = ChatGrant(service_sid=service.sid)
    token.add_grant(chat_grant)

    # 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:chat:{username}', jwt_token, expiration)

    return jwt_token
コード例 #6
0
def token():
    token = AccessToken(
        os.environ['TWILIO_ACCOUNT_SID'],
        os.environ['TWILIO_API_KEY_SID'],
        os.environ['TWILIO_API_KEY_SECRET'],
        grants=[SyncGrant(os.environ['TWILIO_SYNC_SERVICE_SID'])],
        identity=uuid.uuid4().hex)
    return {'token': token.to_jwt().decode()}
コード例 #7
0
def token():
    # get the userid from the incoming request
    identity = request.values.get('identity', None)
    # Create access token with credentials
    token = AccessToken(twilio_account_sid, twilio_api_key, twilio_api_secret, identity=identity)
    # Create a Sync grant and add to token
    sync_grant = SyncGrant(service_sid=twilio_sync_service_id)
    token.add_grant(sync_grant)
    # Return token info as JSON
    return jsonify(identity=identity, token=token.to_jwt().decode('utf-8'))
コード例 #8
0
def token():
    # get the userid from the incoming request
    identity = request.values.get('identity', None)
    # Create access token with credentials
    token = AccessToken(TWILIO_ACCOUNT_SID, TWILIO_API_KEY,
                        TWILIO_API_SECRET, identity=identity)
    # Create a Sync grant and add to token
    sync_grant = SyncGrant(service_sid=TWILIO_SYNC_SERVICE_SID)
    token.add_grant(sync_grant)
    # Return token info as JSON
    return jsonify(identity=identity, token=token.to_jwt().decode('utf-8'))
コード例 #9
0
def generate_token():
    # get credentials from environment variables
    account_sid = os.getenv('TWILIO_ACCOUNT_SID')
    api_key = os.getenv('TWILIO_API_KEY')
    api_secret = os.getenv('TWILIO_API_SECRET')
    sync_service_sid = os.getenv('TWILIO_SYNC_SERVICE_SID', 'default')
    username = request.args.get('username', fake.user_name())

    # create access token with credentials
    token = AccessToken(account_sid, api_key, api_secret, identity=username)
    # create a Sync grant and add to token
    sync_grant = SyncGrant(sync_service_sid)
    token.add_grant(sync_grant)
    return jsonify(identity=username, token=token.to_jwt().decode())
コード例 #10
0
def generate_token(identity):
    account_sid = os.environ['TWILIO_ACCOUNT_SID']
    api_key = os.environ['TWILIO_API_KEY']
    api_secret = os.environ['TWILIO_API_SECRET']
    sync_service_sid = os.environ.get('TWILIO_SYNC_SERVICE_SID', 'default')

    new_token = AccessToken(account_sid,
                            api_key,
                            api_secret,
                            identity=identity)

    sync_grant = SyncGrant(service_sid=sync_service_sid)
    new_token.add_grant(sync_grant)

    return jsonify(identity=identity, token=new_token.to_jwt().decode('utf-8'))
コード例 #11
0
    def test_sync_grant(self):
        scat = AccessToken(ACCOUNT_SID, SIGNING_KEY_SID, 'secret')
        scat.identity = "bender"
        scat.add_grant(
            SyncGrant(service_sid='IS123', endpoint_id='blahblahendpoint'))

        token = scat.to_jwt()
        assert_is_not_none(token)
        decoded_token = AccessToken.from_jwt(token, 'secret')
        self._validate_claims(decoded_token.payload)
        assert_equal(2, len(decoded_token.payload['grants']))
        assert_equal("bender", decoded_token.payload['grants']['identity'])
        assert_equal(
            {
                'service_sid': 'IS123',
                'endpoint_id': 'blahblahendpoint'
            }, decoded_token.payload['grants']['data_sync'])
コード例 #12
0
def get_chat_token(username):
    logger.info('Generating Chat token for {}'.format(username))

    # This call is simply to ensure the service exists, though it is not needed to generate token
    service = get_service()

    token = AccessToken(settings.TWILIO_ACCOUNT_SID,
                        settings.TWILIO_API_KEY,
                        settings.TWILIO_API_SECRET,
                        identity=username)

    sync_grant = SyncGrant(service_sid='default')
    token.add_grant(sync_grant)

    chat_grant = ChatGrant(service_sid=service.sid)
    token.add_grant(chat_grant)

    return token.to_jwt()
コード例 #13
0
ファイル: app.py プロジェクト: eltoto1219/comp426final
def generateToken(identity):
    account_sid = os.environ["TWILIO_ACCOUNT_SID"]
    api_key = os.environ["TWILIO_API_KEY"]
    api_secret = os.environ["TWILIO_API_SECRET"]
    sync_service_sid = os.environ.get("TWILIO_SYNC_SERVICE_SID", "default")
    chat_service_sid = os.environ.get("TWILIO_CHAT_SERVICE_SID", None)
    token = AccessToken(account_sid, api_key, api_secret, identity=identity)
    # we sync here, according to tutorial. not too sure what it does
    if sync_service_sid:
        sync_grant = SyncGrant(service_sid=sync_service_sid)
        token.add_grant(sync_grant)

    # Create a Video grant and add to token
    # video_grant = VideoGrant()
    # token.add_grant(video_grant)

    # Create an Chat grant and add to token, we basically add chat services for the user
    if chat_service_sid:
        chat_grant = ChatGrant(service_sid=chat_service_sid)
        token.add_grant(chat_grant)

    # Return token info as JSON
    return jsonify(identity=identity, token=token.to_jwt())