first
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
tags
|
||||
__pycache__
|
||||
*.swo
|
||||
*.swp
|
||||
venv/
|
||||
.env
|
||||
59
app.py
Normal file
59
app.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import os
|
||||
|
||||
from dotenv import load_dotenv, find_dotenv
|
||||
from faker import Faker
|
||||
from flask import Flask, jsonify, request
|
||||
from flask_cors import CORS
|
||||
from twilio.rest import Client
|
||||
from twilio.jwt.access_token import AccessToken
|
||||
from twilio.jwt.access_token.grants import SyncGrant
|
||||
|
||||
app = Flask(__name__)
|
||||
CORS(app)
|
||||
dotenv_path = os.path.join(os.path.dirname(__file__), '.env')
|
||||
load_dotenv(dotenv_path, override=True)
|
||||
fake = Faker()
|
||||
|
||||
@app.route('/token', methods=['GET'])
|
||||
def randomToken():
|
||||
return generateToken(fake.user_name())
|
||||
|
||||
|
||||
@app.route('/token', methods=['POST'])
|
||||
def createToken():
|
||||
# Get the request json or form data
|
||||
content = request.get_json() or request.form
|
||||
# get the identity from the request, or make one up
|
||||
identity = content.get('identity', fake.user_name())
|
||||
return generateToken(identity)
|
||||
|
||||
|
||||
@app.route('/token/<identity>', methods=['POST', 'GET'])
|
||||
def token(identity):
|
||||
return generateToken(identity)
|
||||
|
||||
|
||||
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['TWILIO_SYNC_SERVICE_SID']
|
||||
|
||||
token = AccessToken(account_sid, api_key, api_secret)
|
||||
token.identity = identity
|
||||
|
||||
sync_grant = SyncGrant(service_sid=sync_service_sid)
|
||||
token.add_grant(sync_grant)
|
||||
token = token.to_jwt().decode('utf-8')
|
||||
return jsonify(identity=identity, token=token)
|
||||
|
||||
|
||||
|
||||
def provision_sync_default_service():
|
||||
client = Client(os.environ['TWILIO_API_KEY'], os.environ['TWILIO_API_SECRET'], os.environ['TWILIO_ACCOUNT_SID'])
|
||||
client.sync.services('default').fetch()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
provision_sync_default_service()
|
||||
app.run(debug=True, host='0.0.0.0', port=5001)
|
||||
20
requirements.txt
Normal file
20
requirements.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
certifi==2020.12.5
|
||||
chardet==3.0.4
|
||||
click==7.1.2
|
||||
Faker==3.0.1
|
||||
Flask==1.1.2
|
||||
Flask-Cors==3.0.9
|
||||
idna==2.10
|
||||
itsdangerous==1.1.0
|
||||
Jinja2==2.11.2
|
||||
MarkupSafe==1.1.1
|
||||
PyJWT==1.7.1
|
||||
python-dateutil==2.8.1
|
||||
python-dotenv==0.15.0
|
||||
pytz==2020.4
|
||||
requests==2.25.0
|
||||
six==1.15.0
|
||||
text-unidecode==1.3
|
||||
twilio==6.48.0
|
||||
urllib3==1.26.2
|
||||
Werkzeug==1.0.1
|
||||
Reference in New Issue
Block a user