Created
November 22, 2019 17:34
-
-
Save v1b3m/24d72170349255fc6a70448275bc3185 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, make_response, jsonify | |
from flask_restful import Resource, Api | |
from json import dumps | |
from authy.api import AuthyApiClient | |
from dotenv import load_dotenv | |
import os | |
from validators.helpers import valid_user, valid_token, requires_token | |
from twilio.rest import Client | |
app = Flask(__name__) | |
api = Api(app) | |
load_dotenv() | |
API_KEY = os.getenv('AUTHY_KEY') | |
account_sid = os.getenv('account_sid') | |
auth_token = os.getenv('auth_token') | |
client = Client(account_sid, auth_token) | |
service = client.verify.services.create('Yonja') | |
print(service.sid) | |
authy_api = AuthyApiClient(API_KEY) | |
class Register(Resource): | |
@valid_user | |
def post(self): | |
try: | |
data = request.get_json() | |
phone = data['phoneNumber'] | |
email = data['email'] | |
user = authy_api.users.create( | |
email, | |
phone, | |
country_code=256 | |
) | |
if user.ok(): | |
return { | |
"data": { | |
"authy_id": user.id | |
}, | |
"errors": None, | |
"message": "Succesfully registered user", | |
}, 201 | |
else: | |
print(user.errors()) | |
return { | |
"data": None, | |
"errors": user.errors(), | |
"message": "An error has occured", | |
}, 400 | |
except Exception as e: | |
return { | |
"data": None, | |
"errors": str(e), | |
"message": "An error has occured", | |
"status": 400 | |
}, 400 | |
class SendToken(Resource): | |
@valid_token | |
def post(self): | |
try: | |
verification = client.verify \ | |
.services(service.sid) \ | |
.verifications \ | |
.create(to='+256703755919', channel='sms', send_digits=4) | |
# data = request.get_json() | |
# authy_id = data['authy_id'] | |
# sms = authy_api.users.request_sms(authy_id) | |
# if sms.ok(): | |
print(verification) | |
return { | |
"data": { | |
"authy_id": verification.status | |
}, | |
"errors": None, | |
"message": "Succesfully sent sms token", | |
}, 201 | |
# else: | |
# return { | |
# "data": None, | |
# "errors": "An error has occured", | |
# "message": "SMS token not sent", | |
# }, 400 | |
except Exception as e: | |
return { | |
"data": None, | |
"errors": str(e), | |
"message": "An error has occured", | |
"status": 400 | |
}, 400 | |
class VerifyToken(Resource): | |
@requires_token | |
@valid_token | |
def post(self): | |
try: | |
verification_check = client.verify \ | |
.services(service.sid) \ | |
.verification_checks \ | |
.create(to='+256703755919', code='123456') | |
print(verification_check.status) | |
# data = request.get_json() | |
# authy_id = data['authy_id'] | |
# token = data['token'] | |
# verification = authy_api.tokens.verify(authy_id, token) | |
# if verification.ok(): | |
return { | |
"data": { | |
"authy_id": authy_id, | |
"token": token | |
}, | |
"errors": None, | |
"message": "Succesfully verified sms token", | |
}, 200 | |
# else: | |
# return { | |
# "data": None, | |
# "errors": "An error has occured", | |
# "message": "SMS token not verified", | |
# }, 400 | |
except Exception as e: | |
return { | |
"data": None, | |
"errors": str(e), | |
"message": "An error has occured", | |
"status": 400 | |
}, 400 | |
api.add_resource(Register, '/register') | |
api.add_resource(SendToken, '/send_token') | |
api.add_resource(VerifyToken, '/verify') | |
if __name__ == '__main__': | |
app.run(port='5002') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment